-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Tab Page WebUI: Partially refactor all observing and messaging to…
… MessageHandler Following best practices from chromium webui documentation, move towards using DataSource keys for the initial page data, and FireWebUIListener for updating data. We're still manually setting data on the render_host in the WebUIController, but this refactor allows us to use FireWebUIListener now, and move towards using DataSource for that data in the future. MessageHandler allows us to use an established JS lifecycle to ensure that data is only sent to the correct contexts at the correct time. Use FireWebUIListener for 'stats-updated' instead of expecting a global 'statsUpdated' function to exist (which was causing some JS errors).
- Loading branch information
Showing
9 changed files
with
212 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2019 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/brave_new_tab_message_handler.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/values.h" | ||
#include "brave/browser/ui/webui/brave_new_tab_ui.h" | ||
#include "brave/browser/search_engines/search_engine_provider_util.h" | ||
#include "brave/common/pref_names.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "components/prefs/pref_service.h" | ||
|
||
BraveNewTabMessageHandler::BraveNewTabMessageHandler(BraveNewTabUI* web_ui) | ||
: new_tab_web_ui_(web_ui) { | ||
} | ||
|
||
BraveNewTabMessageHandler::~BraveNewTabMessageHandler() {} | ||
|
||
void BraveNewTabMessageHandler::OnJavascriptAllowed() { | ||
// Observe relevant preferences | ||
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | ||
pref_change_registrar_.Init(prefs); | ||
// Stats | ||
pref_change_registrar_.Add(kAdsBlocked, | ||
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged, | ||
base::Unretained(this))); | ||
pref_change_registrar_.Add(kTrackersBlocked, | ||
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged, | ||
base::Unretained(this))); | ||
pref_change_registrar_.Add(kHttpsUpgrades, | ||
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged, | ||
base::Unretained(this))); | ||
// Private New Tab Page preferences | ||
pref_change_registrar_.Add(kUseAlternativeSearchEngineProvider, | ||
base::Bind(&BraveNewTabMessageHandler::OnPrivatePropertiesChanged, | ||
base::Unretained(this))); | ||
pref_change_registrar_.Add(kAlternativeSearchEngineProviderInTor, | ||
base::Bind(&BraveNewTabMessageHandler::OnPrivatePropertiesChanged, | ||
base::Unretained(this))); | ||
} | ||
|
||
void BraveNewTabMessageHandler::OnJavascriptDisallowed() { | ||
pref_change_registrar_.RemoveAll(); | ||
} | ||
|
||
void BraveNewTabMessageHandler::RegisterMessages() { | ||
web_ui()->RegisterMessageCallback( | ||
"newTabPageInitialized", | ||
base::BindRepeating( | ||
&BraveNewTabMessageHandler::HandleInitialized, | ||
base::Unretained(this))); | ||
web_ui()->RegisterMessageCallback( | ||
"toggleAlternativePrivateSearchEngine", | ||
base::BindRepeating( | ||
&BraveNewTabMessageHandler::HandleToggleAlternativeSearchEngineProvider, | ||
base::Unretained(this))); | ||
} | ||
|
||
void BraveNewTabMessageHandler::HandleInitialized(const base::ListValue* args) { | ||
AllowJavascript(); | ||
} | ||
|
||
void BraveNewTabMessageHandler::HandleToggleAlternativeSearchEngineProvider( | ||
const base::ListValue* args) { | ||
brave::ToggleUseAlternativeSearchEngineProvider( | ||
Profile::FromWebUI(web_ui())); | ||
} | ||
|
||
void BraveNewTabMessageHandler::OnPrivatePropertiesChanged() { | ||
new_tab_web_ui_->OnPrivatePropertiesChanged(); | ||
} | ||
|
||
void BraveNewTabMessageHandler::OnStatsChanged() { | ||
new_tab_web_ui_->OnStatsChanged(); | ||
FireWebUIListener("stats-updated"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2019 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_BRAVE_NEW_TAB_MESSAGE_HANDLER_H_ | ||
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_MESSAGE_HANDLER_H_ | ||
|
||
#include "components/prefs/pref_change_registrar.h" | ||
#include "content/public/browser/web_ui_message_handler.h" | ||
|
||
class Profile; | ||
class BraveNewTabUI; | ||
|
||
// Handles messages to and from the New Tab Page javascript | ||
class BraveNewTabMessageHandler : public content::WebUIMessageHandler { | ||
public: | ||
explicit BraveNewTabMessageHandler(BraveNewTabUI* web_ui); | ||
~BraveNewTabMessageHandler() override; | ||
|
||
private: | ||
// WebUIMessageHandler implementation. | ||
void RegisterMessages() override; | ||
void OnJavascriptAllowed() override; | ||
void OnJavascriptDisallowed() override; | ||
|
||
void HandleInitialized(const base::ListValue* args); | ||
void HandleToggleAlternativeSearchEngineProvider( | ||
const base::ListValue* args); | ||
|
||
void OnStatsChanged(); | ||
void OnPrivatePropertiesChanged(); | ||
|
||
PrefChangeRegistrar pref_change_registrar_; | ||
BraveNewTabUI* new_tab_web_ui_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(BraveNewTabMessageHandler); | ||
}; | ||
|
||
#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_MESSAGE_HANDLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.