From 3183eca725beda56bbdebd63d383b68c1def32c2 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Wed, 21 Sep 2022 21:12:52 +0900 Subject: [PATCH] Pin to taskbar on Windows fix https://github.com/brave/brave-browser/issues/24054 * Pin to taskbar from first run or default browser dialog when user want to do it. * Pin to taskbar from settings. Use upstream's api for pin state checking pin to taskbar With this, this pin to shortcut feature is available also on Win7/8. --- app/brave_generated_resources.grd | 5 + app/brave_settings_strings.grdp | 13 + browser/brave_shell_integration_win.cc | 264 ++++-------------- browser/brave_shell_integration_win.h | 11 +- browser/resources/settings/BUILD.gn | 12 + .../getting_started_page/getting_started.html | 3 + .../getting_started_page/getting_started.js | 4 + .../settings/pin_shortcut_page/BUILD.gn | 24 ++ .../pin_shortcut_page/pin_shortcut_page.html | 27 ++ .../pin_shortcut_page/pin_shortcut_page.js | 48 ++++ .../pin_shortcut_page_browser_proxy.js | 28 ++ browser/resources/settings/settings.gni | 6 + browser/resources/settings/sources.gni | 7 + browser/ui/BUILD.gn | 9 +- browser/ui/color/brave_color_id.h | 4 +- browser/ui/color/brave_color_mixer.cc | 6 + .../brave_default_browser_dialog_view.cc | 128 ++++++--- .../views/brave_default_browser_dialog_view.h | 22 +- browser/ui/views/brave_first_run_dialog.cc | 90 ++++-- browser/ui/views/brave_first_run_dialog.h | 10 + browser/ui/webui/brave_settings_ui.cc | 8 + .../brave_settings_default_browser_handler.cc | 28 -- .../brave_settings_default_browser_handler.h | 27 -- ...ave_settings_localized_strings_provider.cc | 6 + .../ui/webui/settings/pin_shortcut_handler.cc | 72 +++++ .../ui/webui/settings/pin_shortcut_handler.h | 38 +++ .../settings_default_browser_handler.h | 19 -- .../browser/ui/webui/settings/settings_ui.cc | 3 - 28 files changed, 567 insertions(+), 355 deletions(-) create mode 100644 browser/resources/settings/pin_shortcut_page/BUILD.gn create mode 100644 browser/resources/settings/pin_shortcut_page/pin_shortcut_page.html create mode 100644 browser/resources/settings/pin_shortcut_page/pin_shortcut_page.js create mode 100644 browser/resources/settings/pin_shortcut_page/pin_shortcut_page_browser_proxy.js delete mode 100644 browser/ui/webui/settings/brave_settings_default_browser_handler.cc delete mode 100644 browser/ui/webui/settings/brave_settings_default_browser_handler.h create mode 100644 browser/ui/webui/settings/pin_shortcut_handler.cc create mode 100644 browser/ui/webui/settings/pin_shortcut_handler.h delete mode 100644 chromium_src/chrome/browser/ui/webui/settings/settings_default_browser_handler.h diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index 668e7de6473..1e2c776196c 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -928,6 +928,11 @@ Or change later at $2brave://settings/ext Maybe later + + + Pin to taskbar + + Microsoft Edge Legacy diff --git a/app/brave_settings_strings.grdp b/app/brave_settings_strings.grdp index a2ba34f2c1e..2efb92d6d1e 100644 --- a/app/brave_settings_strings.grdp +++ b/app/brave_settings_strings.grdp @@ -140,6 +140,19 @@ Customize the background image and widgets that appear on the new tab page + + + + Pin to taskbar + + + Pin + + + Brave is already pinned + + + Shields diff --git a/browser/brave_shell_integration_win.cc b/browser/brave_shell_integration_win.cc index 33334896185..168380ba9ca 100644 --- a/browser/brave_shell_integration_win.cc +++ b/browser/brave_shell_integration_win.cc @@ -5,9 +5,6 @@ #include "brave/browser/brave_shell_integration_win.h" -#include -#include - #include #include #include @@ -21,7 +18,6 @@ #include "base/strings/string_util.h" #include "base/task/thread_pool.h" #include "base/win/shortcut.h" -#include "base/win/windows_version.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_attributes_entry.h" @@ -30,6 +26,7 @@ #include "chrome/browser/shell_integration_win.h" #include "chrome/installer/util/install_util.h" #include "chrome/installer/util/shell_util.h" +#include "chrome/installer/util/taskbar_util.h" #include "content/public/browser/browser_thread.h" #include "third_party/abseil-cpp/absl/types/optional.h" @@ -106,120 +103,13 @@ absl::optional GetShortcutPath( return absl::optional(shortcut_path); } -// NOTE: Below Pin/IsPin method is copied lastest chromium. -// Delete and use upstreams one when it's available from our trunk. - -// ScopedPIDLFromPath class, and the idea of using IPinnedList3::Modify, -// are thanks to Gee Law -class ScopedPIDLFromPath { - public: - explicit ScopedPIDLFromPath(PCWSTR path) - : p_id_list_(ILCreateFromPath(path)) {} - ~ScopedPIDLFromPath() { - if (p_id_list_) - ILFree(p_id_list_); - } - PIDLIST_ABSOLUTE Get() const { return p_id_list_; } - - private: - PIDLIST_ABSOLUTE const p_id_list_; -}; - -enum class PinnedListModifyCaller { kExplorer = 4 }; - -constexpr GUID CLSID_TaskbandPin = { - 0x90aa3a4e, - 0x1cba, - 0x4233, - {0xb8, 0xbb, 0x53, 0x57, 0x73, 0xd4, 0x84, 0x49}}; - -// Undocumented COM interface for manipulating taskbar pinned list. -class __declspec(uuid("0DD79AE2-D156-45D4-9EEB-3B549769E940")) IPinnedList3 - : public IUnknown { - public: - virtual HRESULT STDMETHODCALLTYPE EnumObjects() = 0; - virtual HRESULT STDMETHODCALLTYPE GetPinnableInfo() = 0; - virtual HRESULT STDMETHODCALLTYPE IsPinnable() = 0; - virtual HRESULT STDMETHODCALLTYPE Resolve() = 0; - virtual HRESULT STDMETHODCALLTYPE LegacyModify() = 0; - virtual HRESULT STDMETHODCALLTYPE GetChangeCount() = 0; - virtual HRESULT STDMETHODCALLTYPE IsPinned(PCIDLIST_ABSOLUTE) = 0; - virtual HRESULT STDMETHODCALLTYPE GetPinnedItem() = 0; - virtual HRESULT STDMETHODCALLTYPE GetAppIDForPinnedItem() = 0; - virtual HRESULT STDMETHODCALLTYPE ItemChangeNotify() = 0; - virtual HRESULT STDMETHODCALLTYPE UpdateForRemovedItemsAsNecessary() = 0; - virtual HRESULT STDMETHODCALLTYPE PinShellLink() = 0; - virtual HRESULT STDMETHODCALLTYPE GetPinnedItemForAppID() = 0; - virtual HRESULT STDMETHODCALLTYPE Modify(PCIDLIST_ABSOLUTE unpin, - PCIDLIST_ABSOLUTE pin, - PinnedListModifyCaller caller) = 0; -}; - -// Returns the taskbar pinned list if successful, an empty ComPtr otherwise. -Microsoft::WRL::ComPtr GetTaskbarPinnedList() { - if (base::win::GetVersion() < base::win::Version::WIN10_RS5) - return nullptr; - - Microsoft::WRL::ComPtr pinned_list; - if (FAILED(CoCreateInstance(CLSID_TaskbandPin, nullptr, CLSCTX_INPROC_SERVER, - IID_PPV_ARGS(&pinned_list)))) { - return nullptr; - } - - return pinned_list; -} - -void PinShortcutWin10(const base::FilePath& shortcut) { - Microsoft::WRL::ComPtr pinned_list = GetTaskbarPinnedList(); - if (!pinned_list) - return; - - ScopedPIDLFromPath item_id_list(shortcut.value().data()); - pinned_list->Modify(nullptr, item_id_list.Get(), - PinnedListModifyCaller::kExplorer); -} - -absl::optional IsShortcutPinnedWin10(const base::FilePath& shortcut) { - Microsoft::WRL::ComPtr pinned_list = GetTaskbarPinnedList(); - if (!pinned_list.Get()) - return absl::nullopt; - - ScopedPIDLFromPath item_id_list(shortcut.value().data()); - HRESULT hr = pinned_list->IsPinned(item_id_list.Get()); - // S_OK means `shortcut` is pinned, S_FALSE mean it's not pinned. - return SUCCEEDED(hr) ? absl::optional(hr == S_OK) : absl::nullopt; -} - -bool IsShortcutPinned(const ShellUtil::ShortcutProperties& properties) { - // Generate the shortcut to check pin state. - absl::optional shortcut_path = - GetShortcutPath(ExtractShortcutNameFromProperties(properties)); - if (!shortcut_path) { - LOG(ERROR) << __func__ << " failed to get shortcut path"; - return false; - } - - if (!CreateShortcut(properties, shortcut_path->file_path())) { - LOG(ERROR) << __func__ << " Failed to create shortcut"; - return false; - } - - // Check pin state with newly created shortcut. - auto pinned = IsShortcutPinnedWin10(shortcut_path->file_path()); - if (!pinned) { - LOG(ERROR) << __func__ << " Can't use pin method."; - return false; - } - - return pinned.value(); -} - // All args could be empty when we want to pin default profile's shortcut. -void PinToTaskbarImpl(const base::FilePath& profile_path, +bool PinToTaskbarImpl(const base::FilePath& profile_path, const std::u16string& profile_name, const std::wstring& aumid) { base::FilePath chrome_exe; - base::PathService::Get(base::FILE_EXE, &chrome_exe); + if (!base::PathService::Get(base::FILE_EXE, &chrome_exe)) + return false; ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); ShellUtil::AddDefaultShortcutProperties(chrome_exe, &properties); @@ -243,131 +133,89 @@ void PinToTaskbarImpl(const base::FilePath& profile_path, GetShortcutPath(ExtractShortcutNameFromProperties(properties)); if (!shortcut_path) { LOG(ERROR) << __func__ << " failed to get shortcut path"; - return; + return false; } if (!CreateShortcut(properties, shortcut_path->file_path())) { LOG(ERROR) << __func__ << " Failed to create shortcut"; - return; + return false; } - // Check pin state with newly created shortcut. - auto pinned = IsShortcutPinnedWin10(shortcut_path->file_path()); - if (!pinned) { - LOG(ERROR) << __func__ << " Can't use pin method."; - return; - } - - // Don't try to pin again when it's already pinned. - if (pinned.value()) - return; - - PinShortcutWin10(shortcut_path->file_path()); + return PinShortcutToTaskbar(shortcut_path->file_path()); } -bool HasTaskbarAnyPinnedBraveShortcuts( - const std::vector>& - profile_attrs) { - base::FilePath chrome_exe; - base::PathService::Get(base::FILE_EXE, &chrome_exe); - - ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); - ShellUtil::AddDefaultShortcutProperties(chrome_exe, &properties); - for (const auto& attr : profile_attrs) { - const auto profile_path = std::get<0>(attr); - const auto profile_name = std::get<1>(attr); - const auto profile_aumid = std::get<2>(attr); - if (!profile_path.empty()) { - properties.set_arguments( - profiles::internal::CreateProfileShortcutFlags(profile_path)); - properties.set_shortcut_name( - profiles::internal::GetShortcutFilenameForProfile(profile_name)); - properties.set_app_id(profile_aumid); - } - - if (IsShortcutPinned(properties)) - return true; - } - - return false; -} - -void DoPinToTaskbar(Profile* profile) { +void DoPinToTaskbar(const base::FilePath& profile_path, + base::OnceCallback callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - base::FilePath profile_path; std::u16string profile_name; std::wstring aumid; - if (profile) { + if (!profile_path.empty()) { ProfileManager* profile_manager = g_browser_process->profile_manager(); ProfileAttributesEntry* entry = profile_manager->GetProfileAttributesStorage() - .GetProfileAttributesWithPath(profile->GetPath()); - profile_path = profile->GetPath(); + .GetProfileAttributesWithPath(profile_path); profile_name = entry->GetName(); aumid = shell_integration::win::GetAppUserModelIdForBrowser(profile_path); } base::ThreadPool::CreateCOMSTATaskRunner({base::MayBlock()}) - ->PostTask(FROM_HERE, base::BindOnce(&PinToTaskbarImpl, profile_path, - profile_name, aumid)); -} - -bool CanPinToTaskbar() { - base::FilePath chrome_exe; - if (!base::PathService::Get(base::FILE_EXE, &chrome_exe)) - return false; - - // TODO(simonhong): Support win7/8 - if (base::win::GetVersion() < base::win::Version::WIN10_RS5) - return false; - - return true; + ->PostTaskAndReplyWithResult( + FROM_HERE, + base::BindOnce(&PinToTaskbarImpl, profile_path, profile_name, aumid), + std::move(callback)); } } // namespace namespace shell_integration::win { -void PinToTaskbar(Profile* profile) { - // Disable pin-to-taskabar uitll we have checkbox to ask the user to use it. - return; - +void PinToTaskbar(Profile* profile, + base::OnceCallback result_callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - if (!CanPinToTaskbar()) + if (!CanPinShortcutToTaskbar()) { + std::move(result_callback).Run(false); return; - - // At the very early stage, |g_browser_process| or its profile_manager - // are not initialzied yet. In that case, skip checking existing pin state. - std::vector> - profile_attrs; - // Gather data that is available on UI thread and pass it. - if (g_browser_process && g_browser_process->profile_manager()) { - for (const auto* entry : g_browser_process->profile_manager() - ->GetProfileAttributesStorage() - .GetAllProfilesAttributes()) { - profile_attrs.push_back( - std::make_tuple(entry->GetPath(), entry->GetName(), - win::GetAppUserModelIdForBrowser(entry->GetPath()))); - } } - base::ThreadPool::CreateCOMSTATaskRunner({base::MayBlock()}) - ->PostTaskAndReplyWithResult( - FROM_HERE, - base::BindOnce(&HasTaskbarAnyPinnedBraveShortcuts, - std::move(profile_attrs)), - base::BindOnce( - [](Profile* profile, bool has_pin) { - if (has_pin) { - VLOG(2) << " Taskbar has already pinned brave shortcuts"; - return; - } - DoPinToTaskbar(profile); - }, - profile)); - return; + base::FilePath profile_path; + if (profile) + profile_path = profile->GetPath(); + + // TODO(simonhong): handle connection error state if caller wants. + GetIsPinnedToTaskbarState( + base::DoNothing(), + base::BindOnce( + [](const base::FilePath& profile_path, + base::OnceCallback result_callback, bool succeeded, + bool is_pinned_to_taskbar) { + if (succeeded && is_pinned_to_taskbar) { + // Early return. Already pinned. + std::move(result_callback).Run(true); + return; + } + DoPinToTaskbar(profile_path, std::move(result_callback)); + }, + std::move(profile_path), std::move(result_callback))); +} + +void IsShortcutPinned(base::OnceCallback result_callback) { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + + if (!CanPinShortcutToTaskbar()) { + std::move(result_callback).Run(false); + return; + } + + GetIsPinnedToTaskbarState( + base::DoNothing(), + base::BindOnce( + [](base::OnceCallback result_callback, bool succeeded, + bool is_pinned_to_taskbar) { + std::move(result_callback).Run(succeeded && is_pinned_to_taskbar); + }, + std::move(result_callback))); } } // namespace shell_integration::win diff --git a/browser/brave_shell_integration_win.h b/browser/brave_shell_integration_win.h index cb08d5afe38..363f021bfde 100644 --- a/browser/brave_shell_integration_win.h +++ b/browser/brave_shell_integration_win.h @@ -6,12 +6,21 @@ #ifndef BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_WIN_H_ #define BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_WIN_H_ +#include "base/callback.h" +#include "base/callback_helpers.h" + class Profile; namespace shell_integration::win { // Pin profile-specific shortcut when |profile| is non-null. -void PinToTaskbar(Profile* profile = nullptr); +void PinToTaskbar( + Profile* profile = nullptr, + base::OnceCallback result_callback = base::DoNothing()); + +// Returns true when taskbar has any shortcuts(default or profile-specific +// ones). +void IsShortcutPinned(base::OnceCallback result_callback); } // namespace shell_integration::win diff --git a/browser/resources/settings/BUILD.gn b/browser/resources/settings/BUILD.gn index 5b796a7c8f9..34156f33279 100644 --- a/browser/resources/settings/BUILD.gn +++ b/browser/resources/settings/BUILD.gn @@ -179,6 +179,13 @@ preprocess_if_expr("preprocess_generated") { "getting_started_page/getting_started.js", "social_blocking_page/social_blocking_page.m.js", ] + + if (is_win) { + in_files += [ + "pin_shortcut_page/pin_shortcut_page.js", + "pin_shortcut_page/pin_shortcut_page_browser_proxy.m.js", + ] + } } group("web_modules") { @@ -201,8 +208,13 @@ group("web_modules") { "brave_wallet_page:web_modules", "default_brave_shields_page:web_modules", "getting_started_page:web_modules", + "pin_shortcut_page:web_modules", "social_blocking_page:web_modules", ] + + if (is_win) { + public_deps += [ "pin_shortcut_page:web_modules" ] + } } polymer_modulizer("icons") { diff --git a/browser/resources/settings/getting_started_page/getting_started.html b/browser/resources/settings/getting_started_page/getting_started.html index a7d18c5b706..e7e97afa88b 100644 --- a/browser/resources/settings/getting_started_page/getting_started.html +++ b/browser/resources/settings/getting_started_page/getting_started.html @@ -6,6 +6,9 @@
+ + +
$i18n{onStartup}
diff --git a/browser/resources/settings/getting_started_page/getting_started.js b/browser/resources/settings/getting_started_page/getting_started.js index a302e54090e..004bb6bba2d 100644 --- a/browser/resources/settings/getting_started_page/getting_started.js +++ b/browser/resources/settings/getting_started_page/getting_started.js @@ -10,6 +10,10 @@ import '../settings_page/settings_animated_pages.js' import '../default_browser_page/default_browser_page.js' import '../on_startup_page/on_startup_page.js' +// +import '../pin_shortcut_page/pin_shortcut_page.js' +// + Polymer({ is: 'brave-settings-getting-started', diff --git a/browser/resources/settings/pin_shortcut_page/BUILD.gn b/browser/resources/settings/pin_shortcut_page/BUILD.gn new file mode 100644 index 00000000000..34effbdaae0 --- /dev/null +++ b/browser/resources/settings/pin_shortcut_page/BUILD.gn @@ -0,0 +1,24 @@ +# Copyright (c) 2022 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# you can obtain one at http://mozilla.org/MPL/2.0/. + +import("//tools/polymer/html_to_js.gni") +import("//ui/webui/resources/tools/js_modulizer.gni") +import("../settings.gni") + +group("web_modules") { + public_deps = [ + ":modules", + ":templatize", + ] +} + +js_modulizer("modules") { + input_files = [ "pin_shortcut_page_browser_proxy.js" ] + namespace_rewrites = settings_namespace_rewrites +} + +html_to_js("templatize") { + js_files = [ "pin_shortcut_page.js" ] +} diff --git a/browser/resources/settings/pin_shortcut_page/pin_shortcut_page.html b/browser/resources/settings/pin_shortcut_page/pin_shortcut_page.html new file mode 100644 index 00000000000..711641eebed --- /dev/null +++ b/browser/resources/settings/pin_shortcut_page/pin_shortcut_page.html @@ -0,0 +1,27 @@ + + + + + + + diff --git a/browser/resources/settings/pin_shortcut_page/pin_shortcut_page.js b/browser/resources/settings/pin_shortcut_page/pin_shortcut_page.js new file mode 100644 index 00000000000..e76471f9712 --- /dev/null +++ b/browser/resources/settings/pin_shortcut_page/pin_shortcut_page.js @@ -0,0 +1,48 @@ +/* Copyright (c) 2022 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + + import '../settings_shared.css.js'; + import '../settings_vars.css.js'; + + import {html, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; + import {WebUIListenerMixin} from 'chrome://resources/js/web_ui_listener_mixin.js'; + import {SettingsPinShortcutPageBrowserProxyImpl} from './pin_shortcut_page_browser_proxy.m.js'; + + const SettingsPinShortcutPageBase = WebUIListenerMixin(PolymerElement) + + class SettingsPinShortcutPage extends SettingsPinShortcutPageBase { + static get is() { + return 'settings-pin-shortcut-page' + } + + static get template() { + return html`{__html_template__}` + } + + static get properties() { + return { + pinned_: { + readOnly: false, + type: Boolean + } + } + } + + browserProxy_ = SettingsPinShortcutPageBrowserProxyImpl.getInstance() + + ready() { + super.ready() + this.pinned_ = false + + this.browserProxy_.checkShortcutPinState() + this.addWebUIListener('shortcut-pin-state-changed', pinned => this.set('pinned_', pinned)) + } + + onPinShortcutTap_() { + this.browserProxy_.pinShortcut() + } + } + + customElements.define(SettingsPinShortcutPage.is, SettingsPinShortcutPage); diff --git a/browser/resources/settings/pin_shortcut_page/pin_shortcut_page_browser_proxy.js b/browser/resources/settings/pin_shortcut_page/pin_shortcut_page_browser_proxy.js new file mode 100644 index 00000000000..8a5f274aa1f --- /dev/null +++ b/browser/resources/settings/pin_shortcut_page/pin_shortcut_page_browser_proxy.js @@ -0,0 +1,28 @@ +// Copyright (c) 2022 The Brave Authors. All rights reserved. +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// you can obtain one at http://mozilla.org/MPL/2.0/. + +import {addSingletonGetter, sendWithPromise} from 'chrome://resources/js/cr.m.js'; + +/** @interface */ +export class SettingsPinShortcutPageBrowserProxy { + checkShortcutPinState() {} + pinShortcut() {} +} + +/** + * @implements {settings.SettingsPinShortcutPageBrowserProxy} + */ +export class SettingsPinShortcutPageBrowserProxyImpl { + /** @override */ + checkShortcutPinState() { + chrome.send('checkShortcutPinState'); + } + + pinShortcut() { + chrome.send('pinShortcut'); + } +} + +addSingletonGetter(SettingsPinShortcutPageBrowserProxyImpl) diff --git a/browser/resources/settings/settings.gni b/browser/resources/settings/settings.gni index a4fdfad7ef4..5b37adbf47e 100644 --- a/browser/resources/settings/settings.gni +++ b/browser/resources/settings/settings.gni @@ -15,5 +15,11 @@ settings_namespace_rewrites = [ "settings.Router|Router", ] +if (is_win) { + settings_namespace_rewrites += [ + "settings.PinShortcutPageBrowserProxyImpl|PinShortcutPageBrowserProxyImpl", + ] +} + settings_auto_imports = [ "chrome/browser/resources/settings/router.html|Router,Route" ] diff --git a/browser/resources/settings/sources.gni b/browser/resources/settings/sources.gni index 992e435c03f..117930dd780 100644 --- a/browser/resources/settings/sources.gni +++ b/browser/resources/settings/sources.gni @@ -86,6 +86,13 @@ brave_deps_chrome_browser_resources_settings_in_files = [ "social_blocking_page/social_blocking_page.m.js", ] +if (is_win) { + brave_deps_chrome_browser_resources_settings_in_files += [ + "pin_shortcut_page/pin_shortcut_page.js", + "pin_shortcut_page/pin_shortcut_page_browser_proxy.m.js", + ] +} + brave_deps_chrome_browser_resources_settings_extra_deps = [ "//brave/browser/resources/settings:preprocess", "//brave/browser/resources/settings:preprocess_generated", diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 3096104658a..e5a6bcd933e 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -145,8 +145,6 @@ source_set("ui") { "webui/settings/brave_privacy_handler.h", "webui/settings/brave_search_engines_handler.cc", "webui/settings/brave_search_engines_handler.h", - "webui/settings/brave_settings_default_browser_handler.cc", - "webui/settings/brave_settings_default_browser_handler.h", "webui/settings/brave_settings_localized_strings_provider.cc", "webui/settings/brave_settings_localized_strings_provider.h", "webui/settings/brave_sync_handler.cc", @@ -163,6 +161,13 @@ source_set("ui") { "webui/speedreader/speedreader_panel_ui.h", ] + if (is_win) { + sources += [ + "webui/settings/pin_shortcut_handler.cc", + "webui/settings/pin_shortcut_handler.h", + ] + } + if (is_mac) { sources += [ "webui/settings/brave_import_data_handler_mac.mm" ] } else { diff --git a/browser/ui/color/brave_color_id.h b/browser/ui/color/brave_color_id.h index c46f7898b16..fdb3c2af6b3 100644 --- a/browser/ui/color/brave_color_id.h +++ b/browser/ui/color/brave_color_id.h @@ -18,7 +18,9 @@ E_CPONLY(kColorIconBase) \ E_CPONLY(kColorMenuItemSubText) \ E_CPONLY(kColorBookmarkBarInstructionsText) \ - E_CPONLY(kColorLocationBarFocusRing) + E_CPONLY(kColorLocationBarFocusRing) \ + E_CPONLY(kColorDialogDontAskAgainButton) \ + E_CPONLY(kColorDialogDontAskAgainButtonHovered) \ #define BRAVE_SEARCH_CONVERSION_COLOR_IDS \ E_CPONLY(kColorSearchConversionBannerTypeBackgroundBorder) \ diff --git a/browser/ui/color/brave_color_mixer.cc b/browser/ui/color/brave_color_mixer.cc index 3270626204c..180cc8e9b97 100644 --- a/browser/ui/color/brave_color_mixer.cc +++ b/browser/ui/color/brave_color_mixer.cc @@ -280,6 +280,9 @@ void AddChromeLightThemeColorMixer(ui::ColorProvider* provider, mixer[kColorToolbarContentAreaSeparator] = {ui::kColorFrameActive}; mixer[kColorToolbarTopSeparatorFrameActive] = {kColorToolbar}; mixer[kColorToolbarTopSeparatorFrameInactive] = {kColorToolbar}; + mixer[kColorDialogDontAskAgainButton] = {SkColorSetRGB(0x86, 0x8E, 0x96)}; + mixer[kColorDialogDontAskAgainButtonHovered] = { + SkColorSetRGB(0x49, 0x50, 0x57)}; mixer[ui::kColorFrameActive] = {kLightFrame}; mixer[ui::kColorFrameInactive] = { color_utils::HSLShift(kLightFrame, {-1, -1, 0.6})}; @@ -323,6 +326,9 @@ void AddChromeDarkThemeColorMixer(ui::ColorProvider* provider, mixer[kColorToolbarContentAreaSeparator] = {kColorToolbar}; mixer[kColorToolbarTopSeparatorFrameActive] = {kColorToolbar}; mixer[kColorToolbarTopSeparatorFrameInactive] = {kColorToolbar}; + mixer[kColorDialogDontAskAgainButton] = {SkColorSetRGB(0x84, 0x88, 0x9C)}; + mixer[kColorDialogDontAskAgainButtonHovered] = { + SkColorSetRGB(0xC2, 0xC4, 0xCF)}; mixer[ui::kColorFrameActive] = {kDarkFrame}; mixer[ui::kColorFrameInactive] = { color_utils::HSLShift(kDarkFrame, {-1, -1, 0.6})}; diff --git a/browser/ui/views/brave_default_browser_dialog_view.cc b/browser/ui/views/brave_default_browser_dialog_view.cc index 2e389b70c3d..8a3600f16f5 100644 --- a/browser/ui/views/brave_default_browser_dialog_view.cc +++ b/browser/ui/views/brave_default_browser_dialog_view.cc @@ -11,17 +11,20 @@ #include "base/memory/scoped_refptr.h" #include "brave/browser/brave_shell_integration.h" #include "brave/browser/ui/browser_dialogs.h" +#include "brave/browser/ui/color/brave_color_id.h" #include "brave/components/constants/pref_names.h" #include "brave/components/l10n/common/locale_util.h" #include "brave/grit/brave_generated_resources.h" -#include "build/build_config.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_window.h" #include "components/constrained_window/constrained_window_views.h" #include "components/prefs/pref_service.h" +#include "ui/base/metadata/metadata_impl_macros.h" +#include "ui/color/color_provider.h" #include "ui/views/bubble/bubble_frame_view.h" #include "ui/views/controls/button/checkbox.h" +#include "ui/views/controls/button/label_button.h" #include "ui/views/controls/label.h" #include "ui/views/layout/box_layout.h" #include "ui/views/layout/layout_provider.h" @@ -44,27 +47,73 @@ namespace { constexpr int kPadding = 24; -class NoSnappedBubbleFrameView : public views::BubbleFrameView { +class DontAskAgainButton : public views::LabelButton { public: - using BubbleFrameView::BubbleFrameView; - ~NoSnappedBubbleFrameView() override = default; + METADATA_HEADER(DontAskAgainButton); - NoSnappedBubbleFrameView(const NoSnappedBubbleFrameView&) = delete; - NoSnappedBubbleFrameView& operator=(const NoSnappedBubbleFrameView&) = delete; + explicit DontAskAgainButton(PressedCallback callback) + : LabelButton(std::move(callback)) { + SetFontList(); + SetText(brave_l10n::GetLocalizedResourceUTF16String( + IDS_BRAVE_DEFAULT_BROWSER_DIALOG_DONT_ASK)); + } + DontAskAgainButton(const DontAskAgainButton&) = delete; + DontAskAgainButton& operator=(const DontAskAgainButton&) = delete; + ~DontAskAgainButton() override = default; private: - // views::BubbleFrarmeView overrides: - // BubbleFrameView::GetFrameWidthForClientWidth() uses snapped dialog width - // if dialog uses buttons. This width doesn't align with our design. - int GetFrameWidthForClientWidth(int client_width) const override { - // This doesn't use title bar. So, just using |client_width| is fine. - return client_width; + void SetFontList() { + gfx::FontList font_list; + constexpr int kFontSize = 13; + font_list.Derive(kFontSize - font_list.GetFontSize(), + font_list.GetFontStyle(), gfx::Font::Weight::NORMAL); + label()->SetFontList(font_list); + } + + // views::LabelButton overrides: + void OnThemeChanged() override { + LabelButton::OnThemeChanged(); + + auto* cp = GetColorProvider(); + SetTextColor(views::Button::STATE_NORMAL, + cp->GetColor(kColorDialogDontAskAgainButton)); + SetTextColor(views::Button::STATE_HOVERED, + cp->GetColor(kColorDialogDontAskAgainButtonHovered)); } }; +BEGIN_METADATA(DontAskAgainButton, views::LabelButton) +END_METADATA + +class CustomCheckbox : public views::Checkbox { + public: + METADATA_HEADER(CustomCheckbox); + + explicit CustomCheckbox(const std::u16string& label) : Checkbox(label) { + SetFontList(); + } + ~CustomCheckbox() override = default; + CustomCheckbox(const CustomCheckbox&) = delete; + CustomCheckbox& operator=(const CustomCheckbox&) = delete; + + private: + void SetFontList() { + gfx::FontList font_list; + constexpr int kFontSize = 14; + font_list.Derive(kFontSize - font_list.GetFontSize(), + font_list.GetFontStyle(), gfx::Font::Weight::NORMAL); + label()->SetFontList(font_list); + } +}; + +BEGIN_METADATA(CustomCheckbox, views::Checkbox) +END_METADATA + } // namespace BraveDefaultBrowserDialogView::BraveDefaultBrowserDialogView() { + set_should_ignore_snapping(true); + SetButtonLabel(ui::DIALOG_BUTTON_OK, brave_l10n::GetLocalizedResourceUTF16String( IDS_BRAVE_DEFAULT_BROWSER_DIALOG_OK_BUTTON_LABEL)); @@ -78,7 +127,6 @@ BraveDefaultBrowserDialogView::BraveDefaultBrowserDialogView() { SetCancelCallback( base::BindOnce(&BraveDefaultBrowserDialogView::OnCancelButtonClicked, base::Unretained(this))); - CreateChildViews(); } @@ -119,30 +167,19 @@ void BraveDefaultBrowserDialogView::CreateChildViews() { contents_label_->SetMultiLine(true); contents_label_->SetMaximumWidth(350); - dont_ask_again_checkbox_ = AddChildView(std::make_unique( +#if BUILDFLAG(IS_WIN) + pin_shortcut_checkbox_ = AddChildView(std::make_unique( + brave_l10n::GetLocalizedResourceUTF16String( + IDS_FIRSTRUN_DLG_PIN_SHORTCUT_TEXT))); + SetExtraView(std::make_unique( + views::Button::PressedCallback(base::BindRepeating( + &BraveDefaultBrowserDialogView::OnDontAskAgainButtonPressed, + base::Unretained(this))))); +#else + dont_ask_again_checkbox_ = AddChildView(std::make_unique( brave_l10n::GetLocalizedResourceUTF16String( IDS_BRAVE_DEFAULT_BROWSER_DIALOG_DONT_ASK))); -} - -std::unique_ptr -BraveDefaultBrowserDialogView::CreateNonClientFrameView(views::Widget* widget) { - if (!use_custom_frame()) - return DialogDelegateView::CreateNonClientFrameView(widget); - - views::LayoutProvider* provider = views::LayoutProvider::Get(); - auto frame = std::make_unique( - provider->GetInsetsMetric(views::INSETS_DIALOG_TITLE), gfx::Insets()); - - const views::BubbleBorder::Shadow kShadow = - views::BubbleBorder::DIALOG_SHADOW; - std::unique_ptr border = - std::make_unique(views::BubbleBorder::FLOAT, - kShadow); - if (GetParams().round_corners) - border->SetCornerRadius(GetCornerRadius()); - frame->SetFootnoteView(DisownFootnoteView()); - frame->SetBubbleBorder(std::move(border)); - return frame; +#endif } ui::ModalType BraveDefaultBrowserDialogView::GetModalType() const { @@ -158,8 +195,10 @@ void BraveDefaultBrowserDialogView::OnWidgetInitialized() { } void BraveDefaultBrowserDialogView::OnCancelButtonClicked() { +#if !BUILDFLAG(IS_WIN) g_browser_process->local_state()->SetBoolean( kDefaultBrowserPromptEnabled, !dont_ask_again_checkbox_->GetChecked()); +#endif } void BraveDefaultBrowserDialogView::OnAcceptButtonClicked() { @@ -168,14 +207,27 @@ void BraveDefaultBrowserDialogView::OnAcceptButtonClicked() { // and it will be automatically freed once all its tasks have finished. base::MakeRefCounted() #if BUILDFLAG(IS_WIN) - ->StartSetAsDefault( - base::BindOnce([](shell_integration::DefaultWebClientState state) { + ->StartSetAsDefault(base::BindOnce( + [](bool pin_to_taskbar, + shell_integration::DefaultWebClientState state) { if (state == shell_integration::DefaultWebClientState::IS_DEFAULT) { // Try to pin to taskbar when Brave is set as a default browser. shell_integration::win::PinToTaskbar(); } - })); + }, + pin_shortcut_checkbox_->GetChecked())); #else ->StartSetAsDefault(base::NullCallback()); #endif } + +#if BUILDFLAG(IS_WIN) +void BraveDefaultBrowserDialogView::OnDontAskAgainButtonPressed() { + g_browser_process->local_state()->SetBoolean(kDefaultBrowserPromptEnabled, + false); + CancelDialog(); +} +#endif + +BEGIN_METADATA(BraveDefaultBrowserDialogView, views::DialogDelegateView) +END_METADATA diff --git a/browser/ui/views/brave_default_browser_dialog_view.h b/browser/ui/views/brave_default_browser_dialog_view.h index 951fdeb826f..760c9d10430 100644 --- a/browser/ui/views/brave_default_browser_dialog_view.h +++ b/browser/ui/views/brave_default_browser_dialog_view.h @@ -8,6 +8,9 @@ #include +#include "base/memory/raw_ptr.h" +#include "build/build_config.h" +#include "ui/base/metadata/metadata_header_macros.h" #include "ui/views/window/dialog_delegate.h" namespace views { @@ -17,6 +20,8 @@ class Label; class BraveDefaultBrowserDialogView : public views::DialogDelegateView { public: + METADATA_HEADER(BraveDefaultBrowserDialogView); + BraveDefaultBrowserDialogView(); ~BraveDefaultBrowserDialogView() override; @@ -27,8 +32,6 @@ class BraveDefaultBrowserDialogView : public views::DialogDelegateView { // views::DialogDelegateView overrides: ui::ModalType GetModalType() const override; bool ShouldShowCloseButton() const override; - std::unique_ptr CreateNonClientFrameView( - views::Widget* widget) override; void OnWidgetInitialized() override; private: @@ -36,9 +39,18 @@ class BraveDefaultBrowserDialogView : public views::DialogDelegateView { void OnAcceptButtonClicked(); void CreateChildViews(); - views::Label* header_label_ = nullptr; - views::Label* contents_label_ = nullptr; - views::Checkbox* dont_ask_again_checkbox_ = nullptr; +#if BUILDFLAG(IS_WIN) + void OnDontAskAgainButtonPressed(); +#endif + + raw_ptr header_label_ = nullptr; + raw_ptr contents_label_ = nullptr; + +#if BUILDFLAG(IS_WIN) + raw_ptr pin_shortcut_checkbox_ = nullptr; +#else + raw_ptr dont_ask_again_checkbox_ = nullptr; +#endif }; #endif // BRAVE_BROWSER_UI_VIEWS_BRAVE_DEFAULT_BROWSER_DIALOG_VIEW_H_ diff --git a/browser/ui/views/brave_first_run_dialog.cc b/browser/ui/views/brave_first_run_dialog.cc index d761cd9cebe..5cdfca20b6b 100644 --- a/browser/ui/views/brave_first_run_dialog.cc +++ b/browser/ui/views/brave_first_run_dialog.cc @@ -11,16 +11,16 @@ #include "base/bind.h" #include "base/memory/scoped_refptr.h" #include "base/run_loop.h" +#include "brave/components/l10n/common/locale_util.h" #include "brave/grit/brave_generated_resources.h" -#include "build/build_config.h" #include "chrome/browser/first_run/first_run.h" #include "chrome/browser/first_run/first_run_dialog.h" #include "chrome/browser/ui/ui_features.h" #include "chrome/grit/chromium_strings.h" -#include "ui/base/l10n/l10n_util.h" #include "ui/base/metadata/metadata_impl_macros.h" #include "ui/gfx/font.h" #include "ui/gfx/geometry/insets.h" +#include "ui/views/controls/button/checkbox.h" #include "ui/views/controls/label.h" #include "ui/views/layout/box_layout.h" #include "ui/views/window/dialog_delegate.h" @@ -32,8 +32,6 @@ #include "chrome/browser/shell_integration.h" #endif -namespace first_run { - namespace { void ShowBraveFirstRunDialogViews(Profile* profile) { @@ -42,8 +40,38 @@ void ShowBraveFirstRunDialogViews(Profile* profile) { run_loop.Run(); } +#if BUILDFLAG(IS_WIN) +class PinShortcutCheckbox : public views::Checkbox { + public: + METADATA_HEADER(PinShortcutCheckbox); + + PinShortcutCheckbox() { + SetFontList(); + SetText(brave_l10n::GetLocalizedResourceUTF16String( + IDS_FIRSTRUN_DLG_PIN_SHORTCUT_TEXT)); + } + ~PinShortcutCheckbox() override = default; + PinShortcutCheckbox(const PinShortcutCheckbox&) = delete; + PinShortcutCheckbox& operator=(const PinShortcutCheckbox&) = delete; + + private: + void SetFontList() { + gfx::FontList font_list; + constexpr int kFontSize = 14; + font_list.Derive(kFontSize - font_list.GetFontSize(), + font_list.GetFontStyle(), gfx::Font::Weight::NORMAL); + label()->SetFontList(font_list); + } +}; + +BEGIN_METADATA(PinShortcutCheckbox, views::Checkbox) +END_METADATA +#endif // BUILDFLAG(IS_WIN) + } // namespace +namespace first_run { + void ShowFirstRunDialog(Profile* profile) { #if BUILDFLAG(IS_MAC) if (base::FeatureList::IsEnabled(features::kViewsFirstRunDialog)) @@ -71,19 +99,11 @@ BraveFirstRunDialog::BraveFirstRunDialog(base::RepeatingClosure quit_runloop) SetTitle(IDS_FIRST_RUN_DIALOG_WINDOW_TITLE); #endif SetButtonLabel(ui::DIALOG_BUTTON_OK, - l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_OK_BUTTON_LABEL)); - SetButtonLabel( - ui::DIALOG_BUTTON_CANCEL, - l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_CANCEL_BUTTON_LABEL)); - constexpr int kChildSpacing = 16; - constexpr int kPadding = 24; - constexpr int kTopPadding = 20; - constexpr int kBottomPadding = 55; - - SetLayoutManager(std::make_unique( - views::BoxLayout::Orientation::kVertical, - gfx::Insets::TLBR(kTopPadding, kPadding, kBottomPadding, kPadding), - kChildSpacing)); + brave_l10n::GetLocalizedResourceUTF16String( + IDS_FIRSTRUN_DLG_OK_BUTTON_LABEL)); + SetButtonLabel(ui::DIALOG_BUTTON_CANCEL, + brave_l10n::GetLocalizedResourceUTF16String( + IDS_FIRSTRUN_DLG_CANCEL_BUTTON_LABEL)); constexpr int kHeaderFontSize = 16; int size_diff = @@ -93,7 +113,8 @@ BraveFirstRunDialog::BraveFirstRunDialog(base::RepeatingClosure quit_runloop) .DeriveWithSizeDelta(size_diff) .DeriveWithWeight(gfx::Font::Weight::SEMIBOLD)}; auto* header_label = AddChildView(std::make_unique( - l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_HEADER_TEXT), header_font)); + brave_l10n::GetLocalizedResourceUTF16String(IDS_FIRSTRUN_DLG_HEADER_TEXT), + header_font)); header_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); constexpr int kContentFontSize = 15; @@ -104,12 +125,32 @@ BraveFirstRunDialog::BraveFirstRunDialog(base::RepeatingClosure quit_runloop) .DeriveWithSizeDelta(size_diff) .DeriveWithWeight(gfx::Font::Weight::NORMAL)}; auto* contents_label = AddChildView(std::make_unique( - l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_CONTENTS_TEXT), + brave_l10n::GetLocalizedResourceUTF16String( + IDS_FIRSTRUN_DLG_CONTENTS_TEXT), contents_font)); contents_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); contents_label->SetMultiLine(true); constexpr int kMaxWidth = 350; contents_label->SetMaximumWidth(kMaxWidth); + +#if BUILDFLAG(IS_WIN) + pin_shortcut_checkbox_ = + AddChildView(std::make_unique()); +#endif + + constexpr int kChildSpacing = 16; + constexpr int kPadding = 24; + constexpr int kTopPadding = 20; + int kBottomPadding = 55; + +#if BUILDFLAG(IS_WIN) + kBottomPadding -= pin_shortcut_checkbox_->GetPreferredSize().height(); +#endif + + SetLayoutManager(std::make_unique( + views::BoxLayout::Orientation::kVertical, + gfx::Insets::TLBR(kTopPadding, kPadding, kBottomPadding, kPadding), + kChildSpacing)); } BraveFirstRunDialog::~BraveFirstRunDialog() = default; @@ -124,13 +165,16 @@ bool BraveFirstRunDialog::Accept() { #if BUILDFLAG(IS_WIN) base::MakeRefCounted() - ->StartSetAsDefault( - base::BindOnce([](shell_integration::DefaultWebClientState state) { - if (state == shell_integration::DefaultWebClientState::IS_DEFAULT) { + ->StartSetAsDefault(base::BindOnce( + [](bool pin_to_shortcut, + shell_integration::DefaultWebClientState state) { + if (pin_to_shortcut && + state == shell_integration::DefaultWebClientState::IS_DEFAULT) { // Try to pin to taskbar when Brave is set as a default browser. shell_integration::win::PinToTaskbar(); } - })); + }, + pin_shortcut_checkbox_->GetChecked())); #else shell_integration::SetAsDefaultBrowser(); #endif diff --git a/browser/ui/views/brave_first_run_dialog.h b/browser/ui/views/brave_first_run_dialog.h index 8786c0728b8..e5d60b411b3 100644 --- a/browser/ui/views/brave_first_run_dialog.h +++ b/browser/ui/views/brave_first_run_dialog.h @@ -7,9 +7,15 @@ #define BRAVE_BROWSER_UI_VIEWS_BRAVE_FIRST_RUN_DIALOG_H_ #include "base/callback.h" +#include "base/memory/raw_ptr.h" +#include "build/build_config.h" #include "ui/base/metadata/metadata_header_macros.h" #include "ui/views/window/dialog_delegate.h" +namespace views { +class Checkbox; +} // namespace views + class BraveFirstRunDialog : public views::DialogDelegateView { public: METADATA_HEADER(BraveFirstRunDialog); @@ -33,6 +39,10 @@ class BraveFirstRunDialog : public views::DialogDelegateView { void WindowClosing() override; base::RepeatingClosure quit_runloop_; + +#if BUILDFLAG(IS_WIN) + raw_ptr pin_shortcut_checkbox_ = nullptr; +#endif }; #endif // BRAVE_BROWSER_UI_VIEWS_BRAVE_FIRST_RUN_DIALOG_H_ diff --git a/browser/ui/webui/brave_settings_ui.cc b/browser/ui/webui/brave_settings_ui.cc index 4afc74bd11f..9c5a9ba6af6 100644 --- a/browser/ui/webui/brave_settings_ui.cc +++ b/browser/ui/webui/brave_settings_ui.cc @@ -29,12 +29,17 @@ #include "brave/components/speedreader/common/buildflags.h" #include "brave/components/tor/buildflags/buildflags.h" #include "brave/components/version_info/version_info.h" +#include "build/build_config.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/webui/settings/metrics_reporting_handler.h" #include "components/sync/base/command_line_switches.h" #include "content/public/browser/web_ui_data_source.h" #include "content/public/common/content_features.h" +#if BUILDFLAG(IS_WIN) +#include "brave/browser/ui/webui/settings/pin_shortcut_handler.h" +#endif + #if BUILDFLAG(ENABLE_SPEEDREADER) #include "brave/components/speedreader/common/features.h" #endif @@ -64,6 +69,9 @@ BraveSettingsUI::BraveSettingsUI(content::WebUI* web_ui, #if BUILDFLAG(ENABLE_TOR) web_ui->AddMessageHandler(std::make_unique()); #endif +#if BUILDFLAG(IS_WIN) + web_ui->AddMessageHandler(std::make_unique()); +#endif } BraveSettingsUI::~BraveSettingsUI() = default; diff --git a/browser/ui/webui/settings/brave_settings_default_browser_handler.cc b/browser/ui/webui/settings/brave_settings_default_browser_handler.cc deleted file mode 100644 index c520b6d7a5b..00000000000 --- a/browser/ui/webui/settings/brave_settings_default_browser_handler.cc +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright (c) 2022 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "brave/browser/ui/webui/settings/brave_settings_default_browser_handler.h" - -#include "build/build_config.h" - -#if BUILDFLAG(IS_WIN) -#include "brave/browser/brave_shell_integration_win.h" -#endif - -namespace settings { - -BraveDefaultBrowserHandler::~BraveDefaultBrowserHandler() = default; - -void BraveDefaultBrowserHandler::SetAsDefaultBrowser( - const base::Value::List& args) { - DefaultBrowserHandler::SetAsDefaultBrowser(args); - -#if BUILDFLAG(IS_WIN) - // Trying to pin when user ask this as a default browser. - shell_integration::win::PinToTaskbar(Profile::FromWebUI(web_ui())); -#endif -} - -} // namespace settings diff --git a/browser/ui/webui/settings/brave_settings_default_browser_handler.h b/browser/ui/webui/settings/brave_settings_default_browser_handler.h deleted file mode 100644 index a879d14ba0e..00000000000 --- a/browser/ui/webui/settings/brave_settings_default_browser_handler.h +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) 2022 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_SETTINGS_DEFAULT_BROWSER_HANDLER_H_ -#define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_SETTINGS_DEFAULT_BROWSER_HANDLER_H_ - -#include "chrome/browser/ui/webui/settings/settings_default_browser_handler.h" - -namespace settings { - -class BraveDefaultBrowserHandler : public DefaultBrowserHandler { - public: - using DefaultBrowserHandler::DefaultBrowserHandler; - BraveDefaultBrowserHandler(const BraveDefaultBrowserHandler&) = delete; - BraveDefaultBrowserHandler& operator=(const BraveDefaultBrowserHandler&) = - delete; - ~BraveDefaultBrowserHandler() override; - - // DefaultBrowserHandler overrides: - void SetAsDefaultBrowser(const base::Value::List& args) override; -}; - -} // namespace settings - -#endif // BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_SETTINGS_DEFAULT_BROWSER_HANDLER_H_ diff --git a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc index 4550ef73659..daf70eaefaf 100644 --- a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc +++ b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc @@ -270,6 +270,12 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, {"braveNewTabNewTabPageShows", IDS_SETTINGS_NEW_TAB_NEW_TAB_PAGE_SHOWS}, {"braveNewTabNewTabCustomizeWidgets", IDS_SETTINGS_NEW_TAB_NEW_TAB_CUSTOMIZE_WIDGETS}, + // Pin shortcut page +#if BUILDFLAG(IS_WIN) + {"canPinShortcut", IDS_SETTINGS_CAN_PIN_SHORTCUT}, + {"pinShortcut", IDS_SETTINGS_PIN_SHORTCUT}, + {"shortcutPinned", IDS_SETTINGS_SHORTCUT_PINNED}, +#endif // Rewards page {"braveRewards", IDS_SETTINGS_BRAVE_REWARDS_TITLE}, {"braveRewardsDisabledLabel", IDS_SETTINGS_BRAVE_REWARDS_DISABLED_LABEL}, diff --git a/browser/ui/webui/settings/pin_shortcut_handler.cc b/browser/ui/webui/settings/pin_shortcut_handler.cc new file mode 100644 index 00000000000..53790284613 --- /dev/null +++ b/browser/ui/webui/settings/pin_shortcut_handler.cc @@ -0,0 +1,72 @@ +/* Copyright (c) 2022 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/ui/webui/settings/pin_shortcut_handler.h" + +#include "base/bind.h" +#include "build/build_config.h" +#include "chrome/browser/profiles/profile.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/web_ui.h" + +#if BUILDFLAG(IS_WIN) +#include "brave/browser/brave_shell_integration_win.h" +#endif + +PinShortcutHandler::PinShortcutHandler() = default; + +PinShortcutHandler::~PinShortcutHandler() = default; + +void PinShortcutHandler::RegisterMessages() { + web_ui()->RegisterMessageCallback( + "checkShortcutPinState", + base::BindRepeating(&PinShortcutHandler::HandleCheckShortcutPinState, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "pinShortcut", base::BindRepeating(&PinShortcutHandler::HandlePinShortcut, + base::Unretained(this))); +} + +void PinShortcutHandler::HandlePinShortcut(const base::Value::List& args) { + AllowJavascript(); + +#if BUILDFLAG(IS_WIN) + shell_integration::win::PinToTaskbar( + Profile::FromWebUI(web_ui()), + base::BindOnce(&PinShortcutHandler::OnPinShortcut, + weak_factory_.GetWeakPtr())); +#endif +} + +void PinShortcutHandler::HandleCheckShortcutPinState( + const base::Value::List& args) { + AllowJavascript(); + +#if BUILDFLAG(IS_WIN) + shell_integration::win::IsShortcutPinned( + base::BindOnce(&PinShortcutHandler::OnCheckShortcutPinState, + weak_factory_.GetWeakPtr())); +#endif +} + +#if BUILDFLAG(IS_WIN) +void PinShortcutHandler::OnPinShortcut(bool pinned) { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + + NotifyShortcutPinStateChangeToPage(pinned); +} + +void PinShortcutHandler::OnCheckShortcutPinState(bool pinned) { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + + NotifyShortcutPinStateChangeToPage(pinned); +} +#endif // BUILDFLAG(IS_WIN) + +void PinShortcutHandler::NotifyShortcutPinStateChangeToPage(bool pinned) { + if (IsJavascriptAllowed()) { + FireWebUIListener("shortcut-pin-state-changed", base::Value(pinned)); + } +} diff --git a/browser/ui/webui/settings/pin_shortcut_handler.h b/browser/ui/webui/settings/pin_shortcut_handler.h new file mode 100644 index 00000000000..2401d5fa63a --- /dev/null +++ b/browser/ui/webui/settings/pin_shortcut_handler.h @@ -0,0 +1,38 @@ +/* Copyright (c) 2022 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_UI_WEBUI_SETTINGS_PIN_SHORTCUT_HANDLER_H_ +#define BRAVE_BROWSER_UI_WEBUI_SETTINGS_PIN_SHORTCUT_HANDLER_H_ + +#include "base/memory/weak_ptr.h" +#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h" + +class PinShortcutHandler : public settings::SettingsPageUIHandler { + public: + PinShortcutHandler(); + ~PinShortcutHandler() override; + + PinShortcutHandler(const PinShortcutHandler&) = delete; + PinShortcutHandler& operator=(const PinShortcutHandler&) = delete; + + private: + // SettingsPageUIHandler overrides: + void RegisterMessages() override; + void OnJavascriptAllowed() override {} + void OnJavascriptDisallowed() override {} + + void HandleCheckShortcutPinState(const base::Value::List& args); + void HandlePinShortcut(const base::Value::List& args); + void NotifyShortcutPinStateChangeToPage(bool pinned); + +#if BUILDFLAG(IS_WIN) + void OnPinShortcut(bool pinned); + void OnCheckShortcutPinState(bool pinned); + + base::WeakPtrFactory weak_factory_{this}; +#endif +}; + +#endif // BRAVE_BROWSER_UI_WEBUI_SETTINGS_PIN_SHORTCUT_HANDLER_H_ diff --git a/chromium_src/chrome/browser/ui/webui/settings/settings_default_browser_handler.h b/chromium_src/chrome/browser/ui/webui/settings/settings_default_browser_handler.h deleted file mode 100644 index 3cf386e22d7..00000000000 --- a/chromium_src/chrome/browser/ui/webui/settings/settings_default_browser_handler.h +++ /dev/null @@ -1,19 +0,0 @@ -/* Copyright (c) 2022 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_WEBUI_SETTINGS_SETTINGS_DEFAULT_BROWSER_HANDLER_H_ -#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_WEBUI_SETTINGS_SETTINGS_DEFAULT_BROWSER_HANDLER_H_ - -#include "chrome/browser/shell_integration.h" - -#define SetAsDefaultBrowser \ - UnUsed() {} \ - friend class BraveDefaultBrowserHandler; \ - virtual void SetAsDefaultBrowser - -#include "src/chrome/browser/ui/webui/settings/settings_default_browser_handler.h" -#undef SetAsDefaultBrowser - -#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_WEBUI_SETTINGS_SETTINGS_DEFAULT_BROWSER_HANDLER_H_ diff --git a/chromium_src/chrome/browser/ui/webui/settings/settings_ui.cc b/chromium_src/chrome/browser/ui/webui/settings/settings_ui.cc index e71b31ab3f7..6b91001e5e7 100644 --- a/chromium_src/chrome/browser/ui/webui/settings/settings_ui.cc +++ b/chromium_src/chrome/browser/ui/webui/settings/settings_ui.cc @@ -5,12 +5,9 @@ #include "brave/browser/ui/webui/settings/brave_import_data_handler.h" #include "brave/browser/ui/webui/settings/brave_search_engines_handler.h" -#include "brave/browser/ui/webui/settings/brave_settings_default_browser_handler.h" #define ImportDataHandler BraveImportDataHandler #define SearchEnginesHandler BraveSearchEnginesHandler -#define DefaultBrowserHandler BraveDefaultBrowserHandler #include "src/chrome/browser/ui/webui/settings/settings_ui.cc" -#undef DefaultBrowserHandler #undef ImportDataHandler #undef SearchEnginesHandler