mirror of
https://github.com/morgan9e/helium
synced 2026-04-15 00:44:06 +09:00
helium/core/onboarding: format and add "acceptLatestSchema" call
This commit is contained in:
@@ -160,36 +160,36 @@
|
||||
+
|
||||
+#include "chrome/browser/profiles/profile.h"
|
||||
+#include "chrome/browser/ui/webui/onboarding/onboarding_handler.h"
|
||||
+#include "chrome/browser/ui/webui/settings/brave_import_data_handler.h"
|
||||
+#include "chrome/browser/ui/webui/settings/search_engines_handler.h"
|
||||
+#include "chrome/browser/ui/webui/settings/settings_default_browser_handler.h"
|
||||
+#include "chrome/browser/ui/webui/settings/brave_import_data_handler.h"
|
||||
+#include "chrome/common/webui_url_constants.h"
|
||||
+#include "content/public/browser/web_ui_controller.h"
|
||||
+#include "content/public/browser/web_ui_data_source.h"
|
||||
+#include "components/helium_onboarding/resources/grit/helium_onboarding_generated.h"
|
||||
+#include "components/helium_onboarding/resources/grit/helium_onboarding_generated_map.h"
|
||||
+#include "content/public/browser/web_ui_controller.h"
|
||||
+#include "content/public/browser/web_ui_data_source.h"
|
||||
+#include "ui/webui/webui_util.h"
|
||||
+
|
||||
+HeliumOnboardingPage::HeliumOnboardingPage(content::WebUI* web_ui) : WebUIController(web_ui) {
|
||||
+ Profile* profile = Profile::FromWebUI(web_ui);
|
||||
+ content::WebUIDataSource* source =
|
||||
+ content::WebUIDataSource::CreateAndAdd(profile, chrome::kHeliumSetupHost);
|
||||
+HeliumOnboardingPage::HeliumOnboardingPage(content::WebUI* web_ui)
|
||||
+ : WebUIController(web_ui) {
|
||||
+ Profile* profile = Profile::FromWebUI(web_ui);
|
||||
+ content::WebUIDataSource* source =
|
||||
+ content::WebUIDataSource::CreateAndAdd(profile, chrome::kHeliumSetupHost);
|
||||
+
|
||||
+ webui::SetupWebUIDataSource(
|
||||
+ source, kHeliumOnboardingGenerated,
|
||||
+ IDR_HELIUM_ONBOARDING_INDEX_HTML);
|
||||
+ webui::SetupWebUIDataSource(source, kHeliumOnboardingGenerated,
|
||||
+ IDR_HELIUM_ONBOARDING_INDEX_HTML);
|
||||
+
|
||||
+ source->UseStringsJs();
|
||||
+ source->DisableTrustedTypesCSP();
|
||||
+ source->UseStringsJs();
|
||||
+ source->DisableTrustedTypesCSP();
|
||||
+
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<OnboardingMessageHandler>(profile));
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<settings::SearchEnginesHandler>(profile));
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<settings::DefaultBrowserHandler>());
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<settings::BraveImportDataHandler>());
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<OnboardingMessageHandler>(profile));
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<settings::SearchEnginesHandler>(profile));
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<settings::DefaultBrowserHandler>());
|
||||
+ web_ui->AddMessageHandler(
|
||||
+ std::make_unique<settings::BraveImportDataHandler>());
|
||||
+}
|
||||
+
|
||||
+HeliumOnboardingPage::~HeliumOnboardingPage() = default;
|
||||
@@ -204,8 +204,8 @@
|
||||
+#define CHROME_BROWSER_UI_WEBUI_ONBOARDING_ONBOARDING_PAGE_UI_H_
|
||||
+
|
||||
+#include "chrome/common/webui_url_constants.h"
|
||||
+#include "content/public/browser/webui_config.h"
|
||||
+#include "content/public/browser/web_ui_controller.h"
|
||||
+#include "content/public/browser/webui_config.h"
|
||||
+
|
||||
+class HeliumOnboardingPage;
|
||||
+
|
||||
@@ -213,23 +213,23 @@
|
||||
+ : public content::DefaultWebUIConfig<HeliumOnboardingPage> {
|
||||
+ public:
|
||||
+ HeliumOnboardingPageConfig()
|
||||
+ : DefaultWebUIConfig(content::kChromeUIScheme,
|
||||
+ chrome::kHeliumSetupHost) {}
|
||||
+ : DefaultWebUIConfig(content::kChromeUIScheme, chrome::kHeliumSetupHost) {
|
||||
+ }
|
||||
+};
|
||||
+
|
||||
+class HeliumOnboardingPage : public content::WebUIController {
|
||||
+public:
|
||||
+ explicit HeliumOnboardingPage(content::WebUI* web_ui);
|
||||
+ public:
|
||||
+ explicit HeliumOnboardingPage(content::WebUI* web_ui);
|
||||
+
|
||||
+ HeliumOnboardingPage(const HeliumOnboardingPage&) = delete;
|
||||
+ HeliumOnboardingPage& operator=(const HeliumOnboardingPage&) = delete;
|
||||
+ ~HeliumOnboardingPage() override;
|
||||
+ HeliumOnboardingPage(const HeliumOnboardingPage&) = delete;
|
||||
+ HeliumOnboardingPage& operator=(const HeliumOnboardingPage&) = delete;
|
||||
+ ~HeliumOnboardingPage() override;
|
||||
+};
|
||||
+
|
||||
+#endif // CHROME_BROWSER_UI_WEBUI_ONBOARDING_ONBOARDING_PAGE_UI_H_
|
||||
+#endif // CHROME_BROWSER_UI_WEBUI_ONBOARDING_ONBOARDING_PAGE_UI_H_
|
||||
--- /dev/null
|
||||
+++ b/chrome/browser/ui/webui/onboarding/onboarding_handler.cc
|
||||
@@ -0,0 +1,190 @@
|
||||
@@ -0,0 +1,188 @@
|
||||
+// Copyright 2025 The Helium Authors
|
||||
+// You can use, redistribute, and/or modify this source code under
|
||||
+// the terms of the GPL-3.0 license that can be found in the LICENSE file.
|
||||
@@ -248,6 +248,7 @@
|
||||
+#include "chrome/browser/themes/theme_service_factory.h"
|
||||
+#include "chrome/browser/ui/color/chrome_color_id.h"
|
||||
+#include "components/helium_services/pref_names.h"
|
||||
+#include "components/helium_services/schema.h"
|
||||
+#include "components/prefs/pref_change_registrar.h"
|
||||
+#include "components/prefs/pref_service.h"
|
||||
+#include "content/public/browser/web_contents.h"
|
||||
@@ -255,174 +256,171 @@
|
||||
+#include "ui/gfx/color_utils.h"
|
||||
+
|
||||
+OnboardingMessageHandler::OnboardingMessageHandler(Profile* profile)
|
||||
+ : profile_(profile),
|
||||
+ pref_service_(profile->GetPrefs()) {
|
||||
+ pref_change_registrar_.Init(pref_service_);
|
||||
+ : profile_(profile), pref_service_(profile->GetPrefs()) {
|
||||
+ pref_change_registrar_.Init(pref_service_);
|
||||
+
|
||||
+ for (const auto [ key, _ ] : GetPreferencesDict()) {
|
||||
+ pref_change_registrar_.Add(
|
||||
+ prefs::kHeliumPrefPrefix + key,
|
||||
+ base::BindRepeating(
|
||||
+ &OnboardingMessageHandler::OnPreferencesChanged,
|
||||
+ base::Unretained(this)
|
||||
+ )
|
||||
+ );
|
||||
+ }
|
||||
+ for (const auto [key, _] : GetPreferencesDict()) {
|
||||
+ pref_change_registrar_.Add(
|
||||
+ prefs::kHeliumPrefPrefix + key,
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::OnPreferencesChanged,
|
||||
+ base::Unretained(this)));
|
||||
+ }
|
||||
+
|
||||
+ ThemeServiceFactory::GetForProfile(profile_)->AddObserver(this);
|
||||
+ ThemeServiceFactory::GetForProfile(profile_)->AddObserver(this);
|
||||
+}
|
||||
+
|
||||
+OnboardingMessageHandler::~OnboardingMessageHandler() {
|
||||
+ ThemeServiceFactory::GetForProfile(profile_)->RemoveObserver(this);
|
||||
+ ThemeServiceFactory::GetForProfile(profile_)->RemoveObserver(this);
|
||||
+}
|
||||
+
|
||||
+base::Value::Dict OnboardingMessageHandler::GetPreferencesDict() {
|
||||
+ std::vector<PrefService::PreferenceValueAndStore> values =
|
||||
+ pref_service_->GetPreferencesValueAndStore();
|
||||
+ base::Value::Dict output;
|
||||
+ std::vector<PrefService::PreferenceValueAndStore> values =
|
||||
+ pref_service_->GetPreferencesValueAndStore();
|
||||
+ base::Value::Dict output;
|
||||
+
|
||||
+ for (const auto& [name, value, _] : values) {
|
||||
+ if (!base::StartsWith(name, prefs::kHeliumPrefPrefix,
|
||||
+ base::CompareCase::SENSITIVE)) {
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ output.Set(
|
||||
+ name.substr(sizeof(prefs::kHeliumPrefPrefix) - 1),
|
||||
+ value.Clone()
|
||||
+ );
|
||||
+ for (const auto& [name, value, _] : values) {
|
||||
+ if (!base::StartsWith(name, prefs::kHeliumPrefPrefix,
|
||||
+ base::CompareCase::SENSITIVE)) {
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ return output;
|
||||
+ output.Set(name.substr(sizeof(prefs::kHeliumPrefPrefix) - 1),
|
||||
+ value.Clone());
|
||||
+ }
|
||||
+
|
||||
+ return output;
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::OnThemeChanged() {
|
||||
+ AllowJavascript();
|
||||
+ FireWebUIListener("ntp-background-changed", GetNTPBackgroundColor());
|
||||
+ AllowJavascript();
|
||||
+ FireWebUIListener("ntp-background-changed", GetNTPBackgroundColor());
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::OnPreferencesChanged() {
|
||||
+ AllowJavascript();
|
||||
+ FireWebUIListener("helium-prefs-changed", GetPreferencesDict());
|
||||
+ AllowJavascript();
|
||||
+ FireWebUIListener("helium-prefs-changed", GetPreferencesDict());
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::RegisterMessages() {
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "getPrefs",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleGetPreferences,
|
||||
+ base::Unretained(this)));
|
||||
+
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "setPref",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleSetPreference,
|
||||
+ base::Unretained(this)));
|
||||
+
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "getProfileName",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleGetProfileName,
|
||||
+ base::Unretained(this)));
|
||||
+
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "setProfileName",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleSetProfileName,
|
||||
+ base::Unretained(this)));
|
||||
+
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "getBackgroundColor",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleGetBackgroundColor,
|
||||
+ base::Unretained(this)));
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "getPrefs",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleGetPreferences,
|
||||
+ base::Unretained(this)));
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "setPref",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleSetPreference,
|
||||
+ base::Unretained(this)));
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "acceptLatestSchema",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleAcceptSchema,
|
||||
+ base::Unretained(this)));
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "getProfileName",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleGetProfileName,
|
||||
+ base::Unretained(this)));
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "setProfileName",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleSetProfileName,
|
||||
+ base::Unretained(this)));
|
||||
+ web_ui()->RegisterMessageCallback(
|
||||
+ "getBackgroundColor",
|
||||
+ base::BindRepeating(&OnboardingMessageHandler::HandleGetBackgroundColor,
|
||||
+ base::Unretained(this)));
|
||||
+}
|
||||
+
|
||||
+base::Value OnboardingMessageHandler::GetNTPBackgroundColor() {
|
||||
+ return base::Value(
|
||||
+ color_utils::SkColorToRgbaString(
|
||||
+ web_ui()
|
||||
+ ->GetWebContents()
|
||||
+ ->GetColorProvider().GetColor(kColorNewTabPageBackground)
|
||||
+ )
|
||||
+ );
|
||||
+ return base::Value(color_utils::SkColorToRgbaString(
|
||||
+ web_ui()->GetWebContents()->GetColorProvider().GetColor(
|
||||
+ kColorNewTabPageBackground)));
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::HandleGetBackgroundColor(const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+void OnboardingMessageHandler::HandleGetBackgroundColor(
|
||||
+ const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+
|
||||
+ AllowJavascript();
|
||||
+ ResolveJavascriptCallback(
|
||||
+ base::Value(callback_id), GetNTPBackgroundColor());
|
||||
+ AllowJavascript();
|
||||
+ ResolveJavascriptCallback(base::Value(callback_id), GetNTPBackgroundColor());
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::HandleGetPreferences(const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+void OnboardingMessageHandler::HandleGetPreferences(
|
||||
+ const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+
|
||||
+ AllowJavascript();
|
||||
+ ResolveJavascriptCallback(
|
||||
+ base::Value(callback_id), base::Value(GetPreferencesDict()));
|
||||
+ AllowJavascript();
|
||||
+ ResolveJavascriptCallback(base::Value(callback_id),
|
||||
+ base::Value(GetPreferencesDict()));
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::HandleGetProfileName(const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+void OnboardingMessageHandler::HandleGetProfileName(
|
||||
+ const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+
|
||||
+ ProfileAttributesEntry* entry =
|
||||
+ g_browser_process->profile_manager()
|
||||
+ ->GetProfileAttributesStorage()
|
||||
+ .GetProfileAttributesWithPath(profile_->GetPath());
|
||||
+ ProfileAttributesEntry* entry =
|
||||
+ g_browser_process->profile_manager()
|
||||
+ ->GetProfileAttributesStorage()
|
||||
+ .GetProfileAttributesWithPath(profile_->GetPath());
|
||||
+
|
||||
+ if (!entry) {
|
||||
+ return RejectJavascriptCallback(callback_id, "entry not found");
|
||||
+ }
|
||||
+ if (!entry) {
|
||||
+ return RejectJavascriptCallback(callback_id, "entry not found");
|
||||
+ }
|
||||
+
|
||||
+ AllowJavascript();
|
||||
+ ResolveJavascriptCallback(
|
||||
+ base::Value(callback_id),
|
||||
+ base::Value(entry->GetLocalProfileName()));
|
||||
+ AllowJavascript();
|
||||
+ ResolveJavascriptCallback(base::Value(callback_id),
|
||||
+ base::Value(entry->GetLocalProfileName()));
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::HandleSetProfileName(const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ std::u16string new_name = base::UTF8ToUTF16(args[0].GetString());
|
||||
+ base::TrimWhitespace(new_name, base::TRIM_ALL, &new_name);
|
||||
+void OnboardingMessageHandler::HandleSetProfileName(
|
||||
+ const base::Value::List& args) {
|
||||
+ CHECK_EQ(1U, args.size());
|
||||
+ std::u16string new_name = base::UTF8ToUTF16(args[0].GetString());
|
||||
+ base::TrimWhitespace(new_name, base::TRIM_ALL, &new_name);
|
||||
+
|
||||
+ if (!new_name.empty()) {
|
||||
+ profiles::UpdateProfileName(profile_, new_name);
|
||||
+ }
|
||||
+ if (!new_name.empty()) {
|
||||
+ profiles::UpdateProfileName(profile_, new_name);
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::HandleSetPreference(const base::Value::List& args) {
|
||||
+ if (args.size() == 0) {
|
||||
+ return;
|
||||
+ }
|
||||
+void OnboardingMessageHandler::HandleSetPreference(
|
||||
+ const base::Value::List& args) {
|
||||
+ if (args.size() == 0) {
|
||||
+ return;
|
||||
+ }
|
||||
+
|
||||
+ AllowJavascript();
|
||||
+ AllowJavascript();
|
||||
+
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+ if (args.size() != 3) {
|
||||
+ return RejectJavascriptCallback(callback_id, "invalid arguments");
|
||||
+ }
|
||||
+ const auto& callback_id = args[0].GetString();
|
||||
+ if (args.size() != 3) {
|
||||
+ return RejectJavascriptCallback(callback_id, "invalid arguments");
|
||||
+ }
|
||||
+
|
||||
+ if (!args[1].is_string()) {
|
||||
+ return RejectJavascriptCallback(callback_id, "pref is not a string");
|
||||
+ }
|
||||
+ if (!args[1].is_string()) {
|
||||
+ return RejectJavascriptCallback(callback_id, "pref is not a string");
|
||||
+ }
|
||||
+
|
||||
+ const auto& pref_name = prefs::kHeliumPrefPrefix + args[1].GetString();
|
||||
+ auto* pref = pref_service_->FindPreference(pref_name);
|
||||
+ if (!pref) {
|
||||
+ return RejectJavascriptCallback(callback_id, pref_name + " does not exist");
|
||||
+ }
|
||||
+ const auto& pref_name = prefs::kHeliumPrefPrefix + args[1].GetString();
|
||||
+ auto* pref = pref_service_->FindPreference(pref_name);
|
||||
+ if (!pref) {
|
||||
+ return RejectJavascriptCallback(callback_id, pref_name + " does not exist");
|
||||
+ }
|
||||
+
|
||||
+ if (args[2].type() != pref->GetType()) {
|
||||
+ return RejectJavascriptCallback(callback_id, "invalid value type");
|
||||
+ }
|
||||
+ if (args[2].type() != pref->GetType()) {
|
||||
+ return RejectJavascriptCallback(callback_id, "invalid value type");
|
||||
+ }
|
||||
+
|
||||
+ pref_service_->Set(pref_name, args[2].Clone());
|
||||
+ pref_service_->Set(pref_name, args[2].Clone());
|
||||
+
|
||||
+ ResolveJavascriptCallback(callback_id, {});
|
||||
+ ResolveJavascriptCallback(callback_id, {});
|
||||
+}
|
||||
+
|
||||
+void OnboardingMessageHandler::HandleAcceptSchema(const base::Value::List&) {
|
||||
+ helium::AcceptCurrentSchema(*pref_service_);
|
||||
+}
|
||||
--- /dev/null
|
||||
+++ b/chrome/browser/ui/webui/onboarding/onboarding_handler.h
|
||||
@@ -0,0 +1,51 @@
|
||||
@@ -0,0 +1,52 @@
|
||||
+// Copyright 2025 The Helium Authors
|
||||
+// You can use, redistribute, and/or modify this source code under
|
||||
+// the terms of the GPL-3.0 license that can be found in the LICENSE file.
|
||||
@@ -437,10 +435,10 @@
|
||||
+#include "base/memory/weak_ptr.h"
|
||||
+#include "base/values.h"
|
||||
+#include "chrome/browser/profiles/profile.h"
|
||||
+#include "components/prefs/pref_service.h"
|
||||
+#include "components/prefs/pref_change_registrar.h"
|
||||
+#include "content/public/browser/web_ui_message_handler.h"
|
||||
+#include "chrome/browser/themes/theme_service_observer.h"
|
||||
+#include "components/prefs/pref_change_registrar.h"
|
||||
+#include "components/prefs/pref_service.h"
|
||||
+#include "content/public/browser/web_ui_message_handler.h"
|
||||
+
|
||||
+class OnboardingMessageHandler : public content::WebUIMessageHandler,
|
||||
+ public ThemeServiceObserver {
|
||||
@@ -455,6 +453,7 @@
|
||||
+ private:
|
||||
+ base::Value::Dict GetPreferencesDict();
|
||||
+ void HandleGetPreferences(const base::Value::List& args);
|
||||
+ void HandleAcceptSchema(const base::Value::List& args);
|
||||
+ void HandleSetPreference(const base::Value::List& args);
|
||||
+ void HandleGetProfileName(const base::Value::List& args);
|
||||
+ void HandleSetProfileName(const base::Value::List& args);
|
||||
@@ -473,7 +472,7 @@
|
||||
+ base::WeakPtrFactory<OnboardingMessageHandler> weak_ptr_factory_{this};
|
||||
+};
|
||||
+
|
||||
+#endif // CHROME_BROWSER_UI_WEBUI_ONBOARDING_ONBOARDING_HANDLER_H_
|
||||
+#endif // CHROME_BROWSER_UI_WEBUI_ONBOARDING_ONBOARDING_HANDLER_H_
|
||||
--- a/components/helium_services/helium_services_helpers.cc
|
||||
+++ b/components/helium_services/helium_services_helpers.cc
|
||||
@@ -53,12 +53,18 @@ GURL GetDummyURL() {
|
||||
|
||||
Reference in New Issue
Block a user