blob: c646b0db967fd618f56b1e47e7ef6d71e9f8fecd [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
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/chrome_thread.h"
[email protected]052313b2010-02-19 09:43:089#include "chrome/browser/pref_service.h"
[email protected]40bd6582009-12-04 23:49:5110#include "chrome/browser/profile.h"
[email protected]3ef4bc632010-04-09 04:25:3111#include "chrome/browser/scoped_pref_update.h"
12#include "chrome/common/notification_details.h"
13#include "chrome/common/notification_source.h"
14#include "chrome/common/notification_type.h"
[email protected]40bd6582009-12-04 23:49:5115#include "chrome/common/pref_names.h"
[email protected]9d797f32010-04-23 07:17:5416#include "googleurl/src/gurl.h"
17#include "net/base/net_util.h"
[email protected]40bd6582009-12-04 23:49:5118
[email protected]3ef4bc632010-04-09 04:25:3119HostZoomMap::HostZoomMap(Profile* profile)
20 : profile_(profile),
21 updating_preferences_(false) {
22 Load();
23 registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
24 Source<Profile>(profile));
25 profile_->GetPrefs()->AddPrefObserver(prefs::kPerHostZoomLevels, this);
26}
27
28void HostZoomMap::Load() {
29 if (!profile_)
30 return;
31
32 AutoLock auto_lock(lock_);
33 host_zoom_levels_.clear();
[email protected]40bd6582009-12-04 23:49:5134 const DictionaryValue* host_zoom_dictionary =
35 profile_->GetPrefs()->GetDictionary(prefs::kPerHostZoomLevels);
36 // Careful: The returned value could be NULL if the pref has never been set.
37 if (host_zoom_dictionary != NULL) {
38 for (DictionaryValue::key_iterator i(host_zoom_dictionary->begin_keys());
39 i != host_zoom_dictionary->end_keys(); ++i) {
40 std::wstring wide_host(*i);
41 int zoom_level = 0;
42 bool success = host_zoom_dictionary->GetIntegerWithoutPathExpansion(
43 wide_host, &zoom_level);
44 DCHECK(success);
45 host_zoom_levels_[WideToUTF8(wide_host)] = zoom_level;
46 }
47 }
48}
49
50// static
51void HostZoomMap::RegisterUserPrefs(PrefService* prefs) {
52 prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels);
53}
54
[email protected]9d797f32010-04-23 07:17:5455int HostZoomMap::GetZoomLevel(const GURL& url) const {
56 std::string host(net::GetHostOrSpecFromURL(url));
[email protected]40bd6582009-12-04 23:49:5157 AutoLock auto_lock(lock_);
58 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
59 return (i == host_zoom_levels_.end()) ? 0 : i->second;
60}
61
[email protected]9d797f32010-04-23 07:17:5462void HostZoomMap::SetZoomLevel(const GURL& url, int level) {
[email protected]40bd6582009-12-04 23:49:5163 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3164 if (!profile_)
65 return;
66
[email protected]9d797f32010-04-23 07:17:5467 std::string host(net::GetHostOrSpecFromURL(url));
[email protected]40bd6582009-12-04 23:49:5168
69 {
70 AutoLock auto_lock(lock_);
71 if (level == 0)
72 host_zoom_levels_.erase(host);
73 else
74 host_zoom_levels_[host] = level;
75 }
76
[email protected]3ef4bc632010-04-09 04:25:3177 updating_preferences_ = true;
78 {
79 ScopedPrefUpdate update(profile_->GetPrefs(), prefs::kPerHostZoomLevels);
80 DictionaryValue* host_zoom_dictionary =
81 profile_->GetPrefs()->GetMutableDictionary(prefs::kPerHostZoomLevels);
82 std::wstring wide_host(UTF8ToWide(host));
83 if (level == 0) {
84 host_zoom_dictionary->RemoveWithoutPathExpansion(wide_host, NULL);
85 } else {
86 host_zoom_dictionary->SetWithoutPathExpansion(
87 wide_host,
88 Value::CreateIntegerValue(level));
89 }
[email protected]40bd6582009-12-04 23:49:5190 }
[email protected]3ef4bc632010-04-09 04:25:3191 updating_preferences_ = false;
[email protected]40bd6582009-12-04 23:49:5192}
93
[email protected]779c3362010-01-30 01:09:3394void HostZoomMap::ResetToDefaults() {
95 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3196 if (!profile_)
97 return;
[email protected]779c3362010-01-30 01:09:3398
[email protected]629a5cd12010-01-30 02:42:3999 {
100 AutoLock auto_lock(lock_);
101 host_zoom_levels_.clear();
102 }
103
[email protected]3ef4bc632010-04-09 04:25:31104 updating_preferences_ = true;
[email protected]779c3362010-01-30 01:09:33105 profile_->GetPrefs()->ClearPref(prefs::kPerHostZoomLevels);
[email protected]3ef4bc632010-04-09 04:25:31106 updating_preferences_ = false;
107}
108
109void HostZoomMap::Shutdown() {
110 if (!profile_)
111 return;
112
113 registrar_.Remove(this,
114 NotificationType::PROFILE_DESTROYED,
115 Source<Profile>(profile_));
116 profile_->GetPrefs()->RemovePrefObserver(prefs::kPerHostZoomLevels, this);
117 profile_ = NULL;
118}
119
120void HostZoomMap::Observe(
121 NotificationType type,
122 const NotificationSource& source,
123 const NotificationDetails& details) {
124 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
125
126 // If the profile is going away, we need to stop using it.
127 if (type == NotificationType::PROFILE_DESTROYED) {
128 Shutdown();
129 return;
130 }
131
132 if (type == NotificationType::PREF_CHANGED) {
133 // If we are updating our own preference, don't reload.
134 if (updating_preferences_)
135 return;
136
137 std::wstring* name = Details<std::wstring>(details).ptr();
138 if (prefs::kPerHostZoomLevels == *name) {
139 Load();
140 return;
141 }
142 }
143
144 NOTREACHED() << "Unexpected preference observed.";
[email protected]779c3362010-01-30 01:09:33145}
146
[email protected]40bd6582009-12-04 23:49:51147HostZoomMap::~HostZoomMap() {
[email protected]3ef4bc632010-04-09 04:25:31148 Shutdown();
[email protected]40bd6582009-12-04 23:49:51149}