Skip to content

Commit

Permalink
Merge pull request #39 from brave/web-ui-cleanup
Browse files Browse the repository at this point in the history
Update new-tab stats as stats change and cleanup
  • Loading branch information
bbondy authored Feb 24, 2018
2 parents 7ee845b + b78ea74 commit 94fb5d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
31 changes: 15 additions & 16 deletions browser/ui/webui/basic_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "brave/common/webui_url_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_ui_data_source.h"
Expand Down Expand Up @@ -77,15 +78,18 @@ BasicUI::BasicUI(content::WebUI* web_ui,
CreateBasicUIHTMLSource(profile, name,
js_file, js_resource_id, html_resource_id));
PrefService* prefs = profile->GetPrefs();
prefs->AddPrefObserverAllPrefs(this);
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(kAdsBlocked,
base::Bind(&BasicUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kTrackersBlocked,
base::Bind(&BasicUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kHttpsUpgrades,
base::Bind(&BasicUI::OnPreferenceChanged, base::Unretained(this)));
}

BasicUI::~BasicUI() {
Profile* profile = Profile::FromWebUI(web_ui());
PrefService* prefs = profile->GetPrefs();
if (prefs) {
prefs->RemovePrefObserverAllPrefs(this);
}
pref_change_registrar_.reset();
}

void BasicUI::RenderFrameCreated(content::RenderFrameHost* render_frame_host) {
Expand All @@ -99,16 +103,11 @@ void BasicUI::RenderFrameCreated(content::RenderFrameHost* render_frame_host) {
}
}

void BasicUI::OnPreferenceChanged(PrefService* service,
const std::string& pref_name) {
if (pref_name == kAdsBlocked ||
pref_name == kTrackersBlocked ||
pref_name == kHttpsUpgrades) {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
Profile* profile = Profile::FromWebUI(web_ui());
CustomizeNewTabWebUIProperties(web_ui(), profile, render_view_host_);
web_ui()->CallJavascriptFunctionUnsafe("brave_new_tab.statsUpdated");
}
void BasicUI::OnPreferenceChanged() {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
Profile* profile = Profile::FromWebUI(web_ui());
CustomizeNewTabWebUIProperties(web_ui(), profile, render_view_host_);
web_ui()->CallJavascriptFunctionUnsafe("brave_new_tab.statsUpdated");
}
}

12 changes: 6 additions & 6 deletions browser/ui/webui/basic_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/web_ui_controller.h"
#include "components/prefs/pref_observer.h"

class PrefService;

class PrefChangeRegistrar;

namespace content {
class RenderViewHost;
} // content


class BasicUI : public content::WebUIController, public PrefObserver {
class BasicUI : public content::WebUIController {
public:
explicit BasicUI(content::WebUI* web_ui, const std::string& host,
const std::string& js_file, int js_resource_id, int html_resource_id);
~BasicUI() override;

private:
void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
void OnPreferenceChanged();

// PrefObserver implementation.
void OnPreferenceChanged(PrefService* service,
const std::string& pref_name) override;
content::RenderViewHost* render_view_host_;
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;

DISALLOW_COPY_AND_ASSIGN(BasicUI);
};
Expand Down

0 comments on commit 94fb5d2

Please sign in to comment.