diff options
author | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-11 01:55:44 +0000 |
---|---|---|
committer | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-11 01:55:44 +0000 |
commit | a551ebd84a64c2723cae1b1c48eef602f9c3b44b (patch) | |
tree | d3e2fed57729cbbc06a74258426f65e9809583e0 | |
parent | 285a0674eadfe577c5f23484ac024b2371b9eac5 (diff) | |
download | chromium_src-a551ebd84a64c2723cae1b1c48eef602f9c3b44b.zip chromium_src-a551ebd84a64c2723cae1b1c48eef602f9c3b44b.tar.gz chromium_src-a551ebd84a64c2723cae1b1c48eef602f9c3b44b.tar.bz2 |
NTP: Sync shown sections across instances
This adds a pref observer for prefs::kNTPShownSections and calls a js function when these changes. This is so that instances of the NTP do not get out of sync with each other.
BUG=None
TEST=Open two or more NTPs. Hide and show different sections and the sections should be hidden and removed on all instances.
Review URL: http://codereview.chromium.org/2792004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49487 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/dom_ui/new_tab_ui.cc | 3 | ||||
-rw-r--r-- | chrome/browser/dom_ui/shown_sections_handler.cc | 34 | ||||
-rw-r--r-- | chrome/browser/dom_ui/shown_sections_handler.h | 15 | ||||
-rw-r--r-- | chrome/browser/resources/new_new_tab.html | 1 | ||||
-rw-r--r-- | chrome/browser/resources/new_new_tab.js | 21 |
5 files changed, 61 insertions, 13 deletions
diff --git a/chrome/browser/dom_ui/new_tab_ui.cc b/chrome/browser/dom_ui/new_tab_ui.cc index 097c8b7..8bcb449 100644 --- a/chrome/browser/dom_ui/new_tab_ui.cc +++ b/chrome/browser/dom_ui/new_tab_ui.cc @@ -458,7 +458,8 @@ NewTabUI::NewTabUI(TabContents* contents) } if (!GetProfile()->IsOffTheRecord()) { - AddMessageHandler((new ShownSectionsHandler())->Attach(this)); + PrefService* pref_service = GetProfile()->GetPrefs(); + AddMessageHandler((new ShownSectionsHandler(pref_service))->Attach(this)); AddMessageHandler((new MostVisitedHandler())->Attach(this)); AddMessageHandler((new RecentlyClosedTabsHandler())->Attach(this)); AddMessageHandler((new MetricsHandler())->Attach(this)); diff --git a/chrome/browser/dom_ui/shown_sections_handler.cc b/chrome/browser/dom_ui/shown_sections_handler.cc index ecc8f1b..aad15014 100644 --- a/chrome/browser/dom_ui/shown_sections_handler.cc +++ b/chrome/browser/dom_ui/shown_sections_handler.cc @@ -13,6 +13,8 @@ #include "chrome/browser/profile.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/notification_details.h" +#include "chrome/common/notification_type.h" #include "chrome/common/pref_names.h" namespace { @@ -39,6 +41,15 @@ void NotifySectionDisabled(int new_mode, int old_mode, Profile *profile) { } // namespace +ShownSectionsHandler::ShownSectionsHandler(PrefService* pref_service) + : pref_service_(pref_service) { + pref_service_->AddPrefObserver(prefs::kNTPShownSections, this); +} + +ShownSectionsHandler::~ShownSectionsHandler() { + pref_service_->RemovePrefObserver(prefs::kNTPShownSections, this); +} + void ShownSectionsHandler::RegisterMessages() { dom_ui_->RegisterMessageCallback("getShownSections", NewCallback(this, &ShownSectionsHandler::HandleGetShownSections)); @@ -46,10 +57,21 @@ void ShownSectionsHandler::RegisterMessages() { NewCallback(this, &ShownSectionsHandler::HandleSetShownSections)); } +void ShownSectionsHandler::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + DCHECK(NotificationType::PREF_CHANGED == type); + std::wstring* pref_name = Details<std::wstring>(details).ptr(); + DCHECK(*pref_name == prefs::kNTPShownSections); + + int sections = pref_service_->GetInteger(prefs::kNTPShownSections); + FundamentalValue sections_value(sections); + dom_ui_->CallJavascriptFunction(L"setShownSections", sections_value); +} + void ShownSectionsHandler::HandleGetShownSections(const Value* value) { - PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); - SetFirstAppLauncherRunPref(pref_service); - int sections = pref_service->GetInteger(prefs::kNTPShownSections); + SetFirstAppLauncherRunPref(pref_service_); + int sections = pref_service_->GetInteger(prefs::kNTPShownSections); FundamentalValue sections_value(sections); dom_ui_->CallJavascriptFunction(L"onShownSections", sections_value); } @@ -72,13 +94,11 @@ void ShownSectionsHandler::HandleSetShownSections(const Value* value) { DCHECK(r) << "Missing value in setShownSections from the NTP Most Visited."; int mode = StringToInt(mode_string); - int old_mode = dom_ui_->GetProfile()->GetPrefs()->GetInteger( - prefs::kNTPShownSections); + int old_mode = pref_service_->GetInteger(prefs::kNTPShownSections); if (old_mode != mode) { NotifySectionDisabled(mode, old_mode, dom_ui_->GetProfile()); - dom_ui_->GetProfile()->GetPrefs()->SetInteger( - prefs::kNTPShownSections, mode); + pref_service_->SetInteger(prefs::kNTPShownSections, mode); } } diff --git a/chrome/browser/dom_ui/shown_sections_handler.h b/chrome/browser/dom_ui/shown_sections_handler.h index 32cc70c..804c368 100644 --- a/chrome/browser/dom_ui/shown_sections_handler.h +++ b/chrome/browser/dom_ui/shown_sections_handler.h @@ -6,6 +6,7 @@ #define CHROME_BROWSER_DOM_UI_SHOWN_SECTIONS_HANDLER_H_ #include "chrome/browser/dom_ui/dom_ui.h" +#include "chrome/common/notification_observer.h" class DOMUI; class Value; @@ -21,14 +22,20 @@ enum Section { DEBUG = 32 }; -class ShownSectionsHandler : public DOMMessageHandler { +class ShownSectionsHandler : public DOMMessageHandler, + public NotificationObserver { public: - ShownSectionsHandler() {} - virtual ~ShownSectionsHandler() {} + explicit ShownSectionsHandler(PrefService* pref_service); + virtual ~ShownSectionsHandler(); // DOMMessageHandler implementation. virtual void RegisterMessages(); + // NotificationObserver implementation. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + // Callback for "getShownSections" message. void HandleGetShownSections(const Value* value); @@ -45,6 +52,8 @@ class ShownSectionsHandler : public DOMMessageHandler { int new_pref_version); private: + PrefService* pref_service_; + DISALLOW_COPY_AND_ASSIGN(ShownSectionsHandler); }; diff --git a/chrome/browser/resources/new_new_tab.html b/chrome/browser/resources/new_new_tab.html index 3be5df2..b29a3fa 100644 --- a/chrome/browser/resources/new_new_tab.html +++ b/chrome/browser/resources/new_new_tab.html @@ -52,6 +52,7 @@ registerCallback('syncMessageChanged'); registerCallback('tips'); registerCallback('onHomePageSet'); registerCallback('getAppsCallback'); +registerCallback('setShownSections'); </script> <!-- template data placeholder --> diff --git a/chrome/browser/resources/new_new_tab.js b/chrome/browser/resources/new_new_tab.js index 16d47b7..8fc13a7 100644 --- a/chrome/browser/resources/new_new_tab.js +++ b/chrome/browser/resources/new_new_tab.js @@ -239,7 +239,9 @@ function getSectionElement(section) { function showSection(section) { if (!(section & shownSections)) { shownSections |= section; - getSectionElement(section).classList.remove('hidden'); + var el = getSectionElement(section); + if (el) + el.classList.remove('hidden'); switch (section) { case Section.THUMB: @@ -267,7 +269,22 @@ function hideSection(section) { break; } - getSectionElement(section).classList.add('hidden'); + var el = getSectionElement(section); + if (el) + el.classList.add('hidden'); + } +} + +/** + * Callback when the shown sections changes in another NTP. + * @param {number} newShownSections Bitmask of the shown sections. + */ +function setShownSections(newShownSections) { + for (var key in Section) { + if (newShownSections & Section[key]) + showSection(Section[key]); + else + hideSection(Section[key]); } } |