From f0bd6571402664098b00f5d9f8bee6f7b6a95597 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Mon, 12 Nov 2018 19:51:22 +0900 Subject: [PATCH] Consider brave theme type to devtools' theme When devtool ui is loaded, kDevToolsPreferences pref values are fetched. In that dictionary prefs, uiTheme represents theme type(light/dark) of devtool. To apply brave theme type to devtools, uiTheme value is replaced with brave native theme type. If user sets devtools' theme type explicitely, that value is used instead of brave theme type. Otherwise, brave theme type is applied. --- browser/devtools/BUILD.gn | 29 +++++++++ .../devtools/brave_devtools_ui_bindings.cc | 31 +++++++++ browser/devtools/brave_devtools_ui_bindings.h | 23 +++++++ .../brave_devtools_ui_bindings_browsertest.cc | 65 +++++++++++++++++++ .../chrome/browser/ui/webui/devtools_ui.h | 9 +++ .../chrome-browser-devtools-BUILD.gn.patch | 12 ++++ ...wser-devtools-devtools_ui_bindings.h.patch | 14 ++++ test/BUILD.gn | 1 + 8 files changed, 184 insertions(+) create mode 100644 browser/devtools/BUILD.gn create mode 100644 browser/devtools/brave_devtools_ui_bindings.cc create mode 100644 browser/devtools/brave_devtools_ui_bindings.h create mode 100644 browser/devtools/brave_devtools_ui_bindings_browsertest.cc create mode 100644 chromium_src/chrome/browser/ui/webui/devtools_ui.h create mode 100644 patches/chrome-browser-devtools-BUILD.gn.patch create mode 100644 patches/chrome-browser-devtools-devtools_ui_bindings.h.patch diff --git a/browser/devtools/BUILD.gn b/browser/devtools/BUILD.gn new file mode 100644 index 000000000000..795ff324cda6 --- /dev/null +++ b/browser/devtools/BUILD.gn @@ -0,0 +1,29 @@ +source_set("devtools") { + deps = [ + "//base", + "//content/public/browser", + "//net", + "//services/viz/privileged/interfaces/compositing", + "//third_party/blink/public:buildflags", + "//ui/events:dom_keycode_converter", + ] + + if (!is_android) { + sources = [ + "brave_devtools_ui_bindings.cc", + "brave_devtools_ui_bindings.h", + ] + + deps += [ + "//chrome:extra_resources", + "//chrome:resources", + "//chrome:strings", + "//chrome/app/theme:theme_resources", + "//chrome/common", + "//components/viz/host", + "//skia", + "//third_party/icu", + "//third_party/leveldatabase", + ] + } +} diff --git a/browser/devtools/brave_devtools_ui_bindings.cc b/browser/devtools/brave_devtools_ui_bindings.cc new file mode 100644 index 000000000000..308a9b130c79 --- /dev/null +++ b/browser/devtools/brave_devtools_ui_bindings.cc @@ -0,0 +1,31 @@ +/* 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/devtools/brave_devtools_ui_bindings.h" + +#include "base/values.h" +#include "brave/browser/themes/brave_theme_service.h" +#include "chrome/common/pref_names.h" +#include "components/prefs/pref_service.h" + +namespace { +std::string GetDevToolsUIThemeValue(Profile* profile) { + BraveThemeType theme_type = + BraveThemeService::GetActiveBraveThemeType(profile); + // In devtools' theme, default is translated to light. + return theme_type == BRAVE_THEME_TYPE_DARK ? "\"dark\"" : "\"default\""; +} +} + +void BraveDevToolsUIBindings::GetPreferences(const DispatchCallback& callback) { + const base::DictionaryValue* prefs = + profile_->GetPrefs()->GetDictionary(prefs::kDevToolsPreferences); + + if (prefs->FindKey("uiTheme")) + return DevToolsUIBindings::GetPreferences(callback); + + base::Value new_prefs(prefs->Clone()); + new_prefs.SetKey("uiTheme", base::Value(GetDevToolsUIThemeValue(profile()))); + callback.Run(&new_prefs); +} diff --git a/browser/devtools/brave_devtools_ui_bindings.h b/browser/devtools/brave_devtools_ui_bindings.h new file mode 100644 index 000000000000..48edfb7c7817 --- /dev/null +++ b/browser/devtools/brave_devtools_ui_bindings.h @@ -0,0 +1,23 @@ +/* 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_DEVTOOLS_BRAVE_DEVTOOLS_UI_BINDINGS_H_ +#define BRAVE_BROWSER_DEVTOOLS_BRAVE_DEVTOOLS_UI_BINDINGS_H_ + +#include "chrome/browser/devtools/devtools_ui_bindings.h" + +class BraveDevToolsUIBindings : public DevToolsUIBindings { + public: + using DevToolsUIBindings::DevToolsUIBindings; + + private: + FRIEND_TEST_ALL_PREFIXES(BraveDevToolsUIBindingsBrowserTest, ThemeTest); + + // DevToolsUIBindings overrides: + void GetPreferences(const DispatchCallback& callback) override; + + DISALLOW_COPY_AND_ASSIGN(BraveDevToolsUIBindings); +}; + +#endif // BRAVE_BROWSER_DEVTOOLS_BRAVE_DEVTOOLS_UI_BINDINGS_H_ diff --git a/browser/devtools/brave_devtools_ui_bindings_browsertest.cc b/browser/devtools/brave_devtools_ui_bindings_browsertest.cc new file mode 100644 index 000000000000..5bbe038ffbcb --- /dev/null +++ b/browser/devtools/brave_devtools_ui_bindings_browsertest.cc @@ -0,0 +1,65 @@ +/* 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/devtools/brave_devtools_ui_bindings.h" + +#include + +#include "base/bind.h" +#include "base/values.h" +#include "brave/browser/themes/brave_theme_service.h" +#include "brave/common/pref_names.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "content/public/browser/web_contents.h" +#include "components/prefs/pref_service.h" + +using BTS = BraveThemeService; + +namespace { +void SetBraveThemeType(Profile* profile, BraveThemeType type) { + profile->GetPrefs()->SetInteger(kBraveThemeType, type); +} +} // namespace + +class BraveDevToolsUIBindingsBrowserTest : public InProcessBrowserTest { + public: + void GetPreferenceCallback(const base::Value* value) { + ui_theme_ = value->FindKey("uiTheme")->GetString(); + } + + std::string ui_theme_; +}; + +IN_PROC_BROWSER_TEST_F(BraveDevToolsUIBindingsBrowserTest, ThemeTest) { + auto* profile = browser()->profile(); + auto* tab_strip_model = browser()->tab_strip_model(); + content::WebContents* web_contents = tab_strip_model->GetActiveWebContents(); + DCHECK(web_contents); + + SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_DARK); + auto* devtools_ui_bindings = new BraveDevToolsUIBindings(web_contents); + DCHECK(devtools_ui_bindings); + devtools_ui_bindings->GetPreferences( + base::Bind(&BraveDevToolsUIBindingsBrowserTest::GetPreferenceCallback, + base::Unretained(this))); + // Check current devtools' theme is same as native theme when user doesn't + // change devtools' theme explicitely. + EXPECT_EQ(ui_theme_, "\"dark\""); + + SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_LIGHT); + devtools_ui_bindings->GetPreferences( + base::Bind(&BraveDevToolsUIBindingsBrowserTest::GetPreferenceCallback, + base::Unretained(this))); + // In devtools, default is used as light. + EXPECT_EQ(ui_theme_, "\"default\""); + + // When user sets devtools' theme explicitely, respect user's setting. + devtools_ui_bindings->SetPreference("uiTheme", "\"dark\""); + devtools_ui_bindings->GetPreferences( + base::Bind(&BraveDevToolsUIBindingsBrowserTest::GetPreferenceCallback, + base::Unretained(this))); + EXPECT_EQ(ui_theme_, "\"dark\""); +} diff --git a/chromium_src/chrome/browser/ui/webui/devtools_ui.h b/chromium_src/chrome/browser/ui/webui/devtools_ui.h new file mode 100644 index 000000000000..98bba1b67b3e --- /dev/null +++ b/chromium_src/chrome/browser/ui/webui/devtools_ui.h @@ -0,0 +1,9 @@ +/* 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/devtools/brave_devtools_ui_bindings.h" + +#define DevToolsUIBindings BraveDevToolsUIBindings +#include "../../../../../../chrome/browser/ui/webui/devtools_ui.h" +#undef DevToolsUIBindings diff --git a/patches/chrome-browser-devtools-BUILD.gn.patch b/patches/chrome-browser-devtools-BUILD.gn.patch new file mode 100644 index 000000000000..4ec15272199c --- /dev/null +++ b/patches/chrome-browser-devtools-BUILD.gn.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/browser/devtools/BUILD.gn b/chrome/browser/devtools/BUILD.gn +index 8e4c3ab3e21896576f4816c9c985a5a0f56e2c08..fc9662f85c93ab16685e7205034a539bf3ad7435 100644 +--- a/chrome/browser/devtools/BUILD.gn ++++ b/chrome/browser/devtools/BUILD.gn +@@ -100,6 +100,7 @@ static_library("devtools") { + "//third_party/blink/public:buildflags", + "//ui/events:dom_keycode_converter", + ] ++ if (brave_chromium_build) { deps += [ "//brave/browser/devtools" ] } + + if (!is_android) { + deps += [ diff --git a/patches/chrome-browser-devtools-devtools_ui_bindings.h.patch b/patches/chrome-browser-devtools-devtools_ui_bindings.h.patch new file mode 100644 index 000000000000..dc071d1aa6b3 --- /dev/null +++ b/patches/chrome-browser-devtools-devtools_ui_bindings.h.patch @@ -0,0 +1,14 @@ +diff --git a/chrome/browser/devtools/devtools_ui_bindings.h b/chrome/browser/devtools/devtools_ui_bindings.h +index 35ea228f2f3a0e469d8cd50f07b6a1a1f754fb5d..13a9d3c5d59458d17eee66600e1974da19dbb1be 100644 +--- a/chrome/browser/devtools/devtools_ui_bindings.h ++++ b/chrome/browser/devtools/devtools_ui_bindings.h +@@ -90,6 +90,9 @@ class DevToolsUIBindings : public DevToolsEmbedderMessageDispatcher::Delegate, + bool IsAttachedTo(content::DevToolsAgentHost* agent_host); + + private: ++ friend class BraveDevToolsUIBindings; ++ FRIEND_TEST_ALL_PREFIXES(BraveDevToolsUIBindingsBrowserTest, ThemeTest); ++ + void HandleMessageFromDevToolsFrontend(const std::string& message); + + // content::DevToolsAgentHostClient implementation. diff --git a/test/BUILD.gn b/test/BUILD.gn index 9e40c0da656d..6078d7910681 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -220,6 +220,7 @@ test("brave_browser_tests") { "//brave/browser/brave_features_browsertest.cc", "//brave/browser/brave_profile_prefs_browsertest.cc", "//brave/browser/brave_resources_browsertest.cc", + "//brave/browser/devtools/brave_devtools_ui_bindings_browsertest.cc", "//brave/browser/extensions/brave_tor_client_updater_browsertest.cc", "//brave/browser/extensions/api/brave_shields_api_browsertest.cc", "//brave/browser/renderer_context_menu/brave_mock_render_view_context_menu.cc",