diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-23 18:21:58 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-23 18:21:58 +0000 |
commit | 162e3df58563d1dae809980b084bfa706e6832a9 (patch) | |
tree | 2c2dae6c629f4ae067ad3cf8b0be2a7015619068 /content/browser | |
parent | 05699652b60d05243cf4a3c6f2e2fb6cf88e7a8f (diff) | |
download | chromium_src-162e3df58563d1dae809980b084bfa706e6832a9.zip chromium_src-162e3df58563d1dae809980b084bfa706e6832a9.tar.gz chromium_src-162e3df58563d1dae809980b084bfa706e6832a9.tar.bz2 |
Take out pref watching from HostZoomMap, in preparation of removing Profile from it.
I've moved watching changes to the default zoom level to the Profile class. The other value being watched was for the per-hostname level, which is only needed if it's being synced. However this pref stopped being synced in r51849, so the removal is roughly a revert of 44056.
Review URL: http://codereview.chromium.org/7058003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86303 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser')
-rw-r--r-- | content/browser/host_zoom_map.cc | 84 | ||||
-rw-r--r-- | content/browser/host_zoom_map.h | 18 | ||||
-rw-r--r-- | content/browser/host_zoom_map_unittest.cc | 120 |
3 files changed, 10 insertions, 212 deletions
diff --git a/content/browser/host_zoom_map.cc b/content/browser/host_zoom_map.cc index 441473c..dc7b227 100644 --- a/content/browser/host_zoom_map.cc +++ b/content/browser/host_zoom_map.cc @@ -15,10 +15,7 @@ #include "content/browser/browser_thread.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host.h" -#include "content/common/notification_details.h" #include "content/common/notification_service.h" -#include "content/common/notification_source.h" -#include "content/common/notification_type.h" #include "googleurl/src/gurl.h" #include "net/base/net_util.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" @@ -26,30 +23,11 @@ using WebKit::WebView; HostZoomMap::HostZoomMap(Profile* profile) - : profile_(profile), - updating_preferences_(false) { - Load(); - default_zoom_level_ = - profile_->GetPrefs()->GetDouble(prefs::kDefaultZoomLevel); - registrar_.Add(this, NotificationType::PROFILE_DESTROYED, - Source<Profile>(profile)); - // Don't observe pref changes (e.g. from sync) in Incognito; once we create - // the incognito window it should have no further connection to the main - // profile/prefs. - if (!profile_->IsOffTheRecord()) { - pref_change_registrar_.Init(profile_->GetPrefs()); - pref_change_registrar_.Add(prefs::kPerHostZoomLevels, this); - pref_change_registrar_.Add(prefs::kDefaultZoomLevel, this); - } + : profile_(profile), default_zoom_level_(0.0) { registrar_.Add( this, NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, NotificationService::AllSources()); -} - -void HostZoomMap::Load() { - if (!profile_) - return; base::AutoLock auto_lock(lock_); host_zoom_levels_.clear(); @@ -70,15 +48,6 @@ void HostZoomMap::Load() { } } -// static -void HostZoomMap::RegisterUserPrefs(PrefService* prefs) { - prefs->RegisterDoublePref(prefs::kDefaultZoomLevel, - 0.0, - PrefService::UNSYNCABLE_PREF); - prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels, - PrefService::UNSYNCABLE_PREF); -} - double HostZoomMap::GetZoomLevel(const GURL& url) const { std::string host(net::GetHostOrSpecFromURL(url)); base::AutoLock auto_lock(lock_); @@ -88,8 +57,6 @@ double HostZoomMap::GetZoomLevel(const GURL& url) const { void HostZoomMap::SetZoomLevel(const GURL& url, double level) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (!profile_) - return; std::string host(net::GetHostOrSpecFromURL(url)); @@ -110,19 +77,14 @@ void HostZoomMap::SetZoomLevel(const GURL& url, double level) { if (profile_->IsOffTheRecord()) return; - updating_preferences_ = true; - { - DictionaryPrefUpdate update(profile_->GetPrefs(), - prefs::kPerHostZoomLevels); - DictionaryValue* host_zoom_dictionary = update.Get(); - if (level == default_zoom_level_) { - host_zoom_dictionary->RemoveWithoutPathExpansion(host, NULL); - } else { - host_zoom_dictionary->SetWithoutPathExpansion( - host, Value::CreateDoubleValue(level)); - } + DictionaryPrefUpdate update(profile_->GetPrefs(), prefs::kPerHostZoomLevels); + DictionaryValue* host_zoom_dictionary = update.Get(); + if (level == default_zoom_level_) { + host_zoom_dictionary->RemoveWithoutPathExpansion(host, NULL); + } else { + host_zoom_dictionary->SetWithoutPathExpansion( + host, Value::CreateDoubleValue(level)); } - updating_preferences_ = false; } double HostZoomMap::GetTemporaryZoomLevel(int render_process_id, @@ -141,8 +103,6 @@ void HostZoomMap::SetTemporaryZoomLevel(int render_process_id, int render_view_id, double level) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (!profile_) - return; { base::AutoLock auto_lock(lock_); @@ -173,16 +133,6 @@ void HostZoomMap::SetTemporaryZoomLevel(int render_process_id, NotificationService::NoDetails()); } -void HostZoomMap::Shutdown() { - if (!profile_) - return; - - registrar_.RemoveAll(); - if (!profile_->IsOffTheRecord()) - pref_change_registrar_.RemoveAll(); - profile_ = NULL; -} - void HostZoomMap::Observe( NotificationType type, const NotificationSource& source, @@ -190,10 +140,6 @@ void HostZoomMap::Observe( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); switch (type.value) { - case NotificationType::PROFILE_DESTROYED: - // If the profile is going away, we need to stop using it. - Shutdown(); - break; case NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { base::AutoLock auto_lock(lock_); int render_view_id = Source<RenderViewHost>(source)->routing_id(); @@ -208,24 +154,10 @@ void HostZoomMap::Observe( } break; } - case NotificationType::PREF_CHANGED: { - // If we are updating our own preference, don't reload. - if (!updating_preferences_) { - std::string* name = Details<std::string>(details).ptr(); - if (prefs::kPerHostZoomLevels == *name) - Load(); - else if (prefs::kDefaultZoomLevel == *name) { - default_zoom_level_ = - profile_->GetPrefs()->GetDouble(prefs::kDefaultZoomLevel); - } - } - break; - } default: NOTREACHED() << "Unexpected preference observed."; } } HostZoomMap::~HostZoomMap() { - Shutdown(); } diff --git a/content/browser/host_zoom_map.h b/content/browser/host_zoom_map.h index ebe3582..7631dd8 100644 --- a/content/browser/host_zoom_map.h +++ b/content/browser/host_zoom_map.h @@ -16,13 +16,11 @@ #include "base/basictypes.h" #include "base/memory/ref_counted.h" #include "base/synchronization/lock.h" -#include "chrome/browser/prefs/pref_change_registrar.h" #include "content/browser/browser_thread.h" #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" class GURL; -class PrefService; class Profile; // HostZoomMap needs to be deleted on the UI thread because it listens @@ -34,8 +32,6 @@ class HostZoomMap : public: explicit HostZoomMap(Profile* profile); - static void RegisterUserPrefs(PrefService* prefs); - // Returns the zoom level for a given url. The zoom level is determined by // the host portion of the URL, or (in the absence of a host) the complete // spec of the URL. In most cases, there is no custom zoom level, and this @@ -73,6 +69,8 @@ class HostZoomMap : const NotificationSource& source, const NotificationDetails& details); + void set_default_zoom_level(double level) { default_zoom_level_ = level; } + private: friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; friend class DeleteTask<HostZoomMap>; @@ -81,13 +79,6 @@ class HostZoomMap : ~HostZoomMap(); - // Reads the zoom levels from the preferences service. - void Load(); - - // Removes dependencies on the profile so we can live longer than - // the profile without crashing. - void Shutdown(); - // The profile we're associated with. Profile* profile_; @@ -109,12 +100,7 @@ class HostZoomMap : // |temporary_zoom_levels_| to guarantee thread safety. mutable base::Lock lock_; - // Whether we are currently updating preferences, this is used to ignore - // notifications from the preference service that we triggered ourself. - bool updating_preferences_; - NotificationRegistrar registrar_; - PrefChangeRegistrar pref_change_registrar_; DISALLOW_COPY_AND_ASSIGN(HostZoomMap); }; diff --git a/content/browser/host_zoom_map_unittest.cc b/content/browser/host_zoom_map_unittest.cc deleted file mode 100644 index c676a53..0000000 --- a/content/browser/host_zoom_map_unittest.cc +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/memory/ref_counted.h" -#include "base/message_loop.h" -#include "base/utf_string_conversions.h" -#include "base/values.h" -#include "chrome/browser/prefs/pref_service.h" -#include "chrome/browser/prefs/scoped_user_pref_update.h" -#include "chrome/common/pref_names.h" -#include "chrome/test/testing_profile.h" -#include "content/browser/browser_thread.h" -#include "content/browser/host_zoom_map.h" -#include "content/common/notification_details.h" -#include "content/common/notification_observer_mock.h" -#include "content/common/notification_source.h" -#include "content/common/notification_type.h" -#include "googleurl/src/gurl.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -using testing::_; -using testing::Pointee; -using testing::Property; - -class HostZoomMapTest : public testing::Test { - public: - static const double kZoomLevel; - static const double kDefaultZoomLevel; - HostZoomMapTest() - : ui_thread_(BrowserThread::UI, &message_loop_), - prefs_(profile_.GetPrefs()), - per_host_zoom_levels_pref_(prefs::kPerHostZoomLevels), - url_("http://example.com/test"), - host_("example.com") {} - - protected: - void SetPrefObserverExpectation() { - EXPECT_CALL( - pref_observer_, - Observe(NotificationType(NotificationType::PREF_CHANGED), - _, - Property(&Details<std::string>::ptr, - Pointee(per_host_zoom_levels_pref_)))); - } - - MessageLoopForUI message_loop_; - BrowserThread ui_thread_; - TestingProfile profile_; - PrefService* prefs_; - std::string per_host_zoom_levels_pref_; // For the observe matcher. - GURL url_; - std::string host_; - NotificationObserverMock pref_observer_; -}; -const double HostZoomMapTest::kZoomLevel = 4; -const double HostZoomMapTest::kDefaultZoomLevel = -2; - -TEST_F(HostZoomMapTest, LoadNoPrefs) { - scoped_refptr<HostZoomMap> map(new HostZoomMap(&profile_)); - EXPECT_EQ(0, map->GetZoomLevel(url_)); -} - -TEST_F(HostZoomMapTest, Load) { - { - DictionaryPrefUpdate update(prefs_, prefs::kPerHostZoomLevels); - DictionaryValue* dict = update.Get(); - dict->SetWithoutPathExpansion(host_, Value::CreateDoubleValue(kZoomLevel)); - } - scoped_refptr<HostZoomMap> map(new HostZoomMap(&profile_)); - EXPECT_EQ(kZoomLevel, map->GetZoomLevel(url_)); -} - -TEST_F(HostZoomMapTest, SetZoomLevel) { - scoped_refptr<HostZoomMap> map(new HostZoomMap(&profile_)); - PrefChangeRegistrar registrar; - registrar.Init(prefs_); - registrar.Add(prefs::kPerHostZoomLevels, &pref_observer_); - SetPrefObserverExpectation(); - map->SetZoomLevel(url_, kZoomLevel); - EXPECT_EQ(kZoomLevel, map->GetZoomLevel(url_)); - const DictionaryValue* dict = - prefs_->GetDictionary(prefs::kPerHostZoomLevels); - double zoom_level = 0; - EXPECT_TRUE(dict->GetDoubleWithoutPathExpansion(host_, &zoom_level)); - EXPECT_EQ(kZoomLevel, zoom_level); - - SetPrefObserverExpectation(); - map->SetZoomLevel(url_, 0); - EXPECT_EQ(0, map->GetZoomLevel(url_)); - EXPECT_FALSE(dict->HasKey(host_)); -} - -TEST_F(HostZoomMapTest, ReloadOnPrefChange) { - scoped_refptr<HostZoomMap> map(new HostZoomMap(&profile_)); - map->SetZoomLevel(url_, kZoomLevel); - - DictionaryValue dict; - dict.SetWithoutPathExpansion(host_, Value::CreateDoubleValue(0)); - prefs_->Set(prefs::kPerHostZoomLevels, dict); - EXPECT_EQ(0, map->GetZoomLevel(url_)); -} - -TEST_F(HostZoomMapTest, NoHost) { - scoped_refptr<HostZoomMap> map(new HostZoomMap(&profile_)); - GURL file_url1_("file:///tmp/test.html"); - GURL file_url2_("file:///tmp/other.html"); - map->SetZoomLevel(file_url1_, kZoomLevel); - - EXPECT_EQ(kZoomLevel, map->GetZoomLevel(file_url1_)); - EXPECT_EQ(0, map->GetZoomLevel(file_url2_)); -} - -TEST_F(HostZoomMapTest, ChangeDefaultZoomLevel) { - FundamentalValue zoom_level(kDefaultZoomLevel); - prefs_->Set(prefs::kDefaultZoomLevel, zoom_level); - scoped_refptr<HostZoomMap> map(new HostZoomMap(&profile_)); - EXPECT_EQ(kDefaultZoomLevel, map->GetZoomLevel(url_)); -} |