blob: a9e9ef45b21863a9475e9937076a552dc256eb05 [file] [log] [blame]
[email protected]779c3362010-01-30 01:09:331// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]40bd6582009-12-04 23:49:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/host_zoom_map.h"
6
[email protected]864b1362010-08-19 03:49:387#include "base/string_piece.h"
[email protected]40bd6582009-12-04 23:49:518#include "base/utf_string_conversions.h"
9#include "chrome/browser/chrome_thread.h"
[email protected]052313b2010-02-19 09:43:0810#include "chrome/browser/pref_service.h"
[email protected]40bd6582009-12-04 23:49:5111#include "chrome/browser/profile.h"
[email protected]3ef4bc632010-04-09 04:25:3112#include "chrome/browser/scoped_pref_update.h"
13#include "chrome/common/notification_details.h"
[email protected]4db6ae42010-06-09 15:58:4714#include "chrome/common/notification_service.h"
[email protected]3ef4bc632010-04-09 04:25:3115#include "chrome/common/notification_source.h"
16#include "chrome/common/notification_type.h"
[email protected]40bd6582009-12-04 23:49:5117#include "chrome/common/pref_names.h"
[email protected]9d797f32010-04-23 07:17:5418#include "googleurl/src/gurl.h"
19#include "net/base/net_util.h"
[email protected]40bd6582009-12-04 23:49:5120
[email protected]3ef4bc632010-04-09 04:25:3121HostZoomMap::HostZoomMap(Profile* profile)
22 : profile_(profile),
23 updating_preferences_(false) {
24 Load();
25 registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
26 Source<Profile>(profile));
[email protected]c18d4f482010-05-18 21:19:5027 // Don't observe pref changes (e.g. from sync) in Incognito; once we create
28 // the incognito window it should have no further connection to the main
29 // profile/prefs.
30 if (!profile_->IsOffTheRecord())
31 profile_->GetPrefs()->AddPrefObserver(prefs::kPerHostZoomLevels, this);
[email protected]3ef4bc632010-04-09 04:25:3132}
33
34void HostZoomMap::Load() {
35 if (!profile_)
36 return;
37
38 AutoLock auto_lock(lock_);
39 host_zoom_levels_.clear();
[email protected]40bd6582009-12-04 23:49:5140 const DictionaryValue* host_zoom_dictionary =
41 profile_->GetPrefs()->GetDictionary(prefs::kPerHostZoomLevels);
42 // Careful: The returned value could be NULL if the pref has never been set.
43 if (host_zoom_dictionary != NULL) {
44 for (DictionaryValue::key_iterator i(host_zoom_dictionary->begin_keys());
45 i != host_zoom_dictionary->end_keys(); ++i) {
[email protected]e7b418b2010-07-30 19:47:4746 const std::string& host(*i);
[email protected]40bd6582009-12-04 23:49:5147 int zoom_level = 0;
48 bool success = host_zoom_dictionary->GetIntegerWithoutPathExpansion(
[email protected]e7b418b2010-07-30 19:47:4749 host, &zoom_level);
[email protected]40bd6582009-12-04 23:49:5150 DCHECK(success);
[email protected]e7b418b2010-07-30 19:47:4751 host_zoom_levels_[host] = zoom_level;
[email protected]40bd6582009-12-04 23:49:5152 }
53 }
54}
55
56// static
57void HostZoomMap::RegisterUserPrefs(PrefService* prefs) {
58 prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels);
59}
60
[email protected]9d797f32010-04-23 07:17:5461int HostZoomMap::GetZoomLevel(const GURL& url) const {
62 std::string host(net::GetHostOrSpecFromURL(url));
[email protected]40bd6582009-12-04 23:49:5163 AutoLock auto_lock(lock_);
64 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
65 return (i == host_zoom_levels_.end()) ? 0 : i->second;
66}
67
[email protected]9d797f32010-04-23 07:17:5468void HostZoomMap::SetZoomLevel(const GURL& url, int level) {
[email protected]40bd6582009-12-04 23:49:5169 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3170 if (!profile_)
71 return;
72
[email protected]9d797f32010-04-23 07:17:5473 std::string host(net::GetHostOrSpecFromURL(url));
[email protected]40bd6582009-12-04 23:49:5174
75 {
76 AutoLock auto_lock(lock_);
77 if (level == 0)
78 host_zoom_levels_.erase(host);
79 else
80 host_zoom_levels_[host] = level;
81 }
82
[email protected]4db6ae42010-06-09 15:58:4783 Details<std::string> details(&host);
84 NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED,
85 Source<Profile>(profile_),
86 details);
87
[email protected]c18d4f482010-05-18 21:19:5088 // If we're in incognito mode, don't persist changes to the prefs. We'll keep
89 // them in memory only so they will be forgotten on exiting incognito.
90 if (profile_->IsOffTheRecord())
91 return;
92
[email protected]3ef4bc632010-04-09 04:25:3193 updating_preferences_ = true;
94 {
95 ScopedPrefUpdate update(profile_->GetPrefs(), prefs::kPerHostZoomLevels);
96 DictionaryValue* host_zoom_dictionary =
97 profile_->GetPrefs()->GetMutableDictionary(prefs::kPerHostZoomLevels);
[email protected]3ef4bc632010-04-09 04:25:3198 if (level == 0) {
[email protected]57ecc4b2010-08-11 03:02:5199 host_zoom_dictionary->RemoveWithoutPathExpansion(host, NULL);
[email protected]3ef4bc632010-04-09 04:25:31100 } else {
101 host_zoom_dictionary->SetWithoutPathExpansion(
[email protected]57ecc4b2010-08-11 03:02:51102 host, Value::CreateIntegerValue(level));
[email protected]3ef4bc632010-04-09 04:25:31103 }
[email protected]40bd6582009-12-04 23:49:51104 }
[email protected]3ef4bc632010-04-09 04:25:31105 updating_preferences_ = false;
[email protected]40bd6582009-12-04 23:49:51106}
107
[email protected]779c3362010-01-30 01:09:33108void HostZoomMap::ResetToDefaults() {
109 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]3ef4bc632010-04-09 04:25:31110 if (!profile_)
111 return;
[email protected]779c3362010-01-30 01:09:33112
[email protected]629a5cd12010-01-30 02:42:39113 {
114 AutoLock auto_lock(lock_);
115 host_zoom_levels_.clear();
116 }
117
[email protected]3ef4bc632010-04-09 04:25:31118 updating_preferences_ = true;
[email protected]779c3362010-01-30 01:09:33119 profile_->GetPrefs()->ClearPref(prefs::kPerHostZoomLevels);
[email protected]3ef4bc632010-04-09 04:25:31120 updating_preferences_ = false;
121}
122
123void HostZoomMap::Shutdown() {
124 if (!profile_)
125 return;
126
127 registrar_.Remove(this,
128 NotificationType::PROFILE_DESTROYED,
129 Source<Profile>(profile_));
[email protected]c18d4f482010-05-18 21:19:50130 if (!profile_->IsOffTheRecord())
131 profile_->GetPrefs()->RemovePrefObserver(prefs::kPerHostZoomLevels, this);
[email protected]3ef4bc632010-04-09 04:25:31132 profile_ = NULL;
133}
134
135void HostZoomMap::Observe(
136 NotificationType type,
137 const NotificationSource& source,
138 const NotificationDetails& details) {
139 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
140
141 // If the profile is going away, we need to stop using it.
142 if (type == NotificationType::PROFILE_DESTROYED) {
143 Shutdown();
144 return;
145 }
146
147 if (type == NotificationType::PREF_CHANGED) {
148 // If we are updating our own preference, don't reload.
149 if (updating_preferences_)
150 return;
151
[email protected]57ecc4b2010-08-11 03:02:51152 std::string* name = Details<std::string>(details).ptr();
[email protected]3ef4bc632010-04-09 04:25:31153 if (prefs::kPerHostZoomLevels == *name) {
154 Load();
155 return;
156 }
157 }
158
159 NOTREACHED() << "Unexpected preference observed.";
[email protected]779c3362010-01-30 01:09:33160}
161
[email protected]40bd6582009-12-04 23:49:51162HostZoomMap::~HostZoomMap() {
[email protected]3ef4bc632010-04-09 04:25:31163 Shutdown();
[email protected]40bd6582009-12-04 23:49:51164}