[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 5 | #include <cmath> |
| 6 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 7 | #include "chrome/browser/host_zoom_map.h" |
| 8 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 9 | #include "base/string_piece.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | e3671727 | 2010-10-12 12:07:13 | [diff] [blame] | 11 | #include "chrome/browser/browser_thread.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 12 | #include "chrome/browser/prefs/pref_service.h" |
| 13 | #include "chrome/browser/prefs/scoped_pref_update.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 15 | #include "chrome/browser/renderer_host/render_process_host.h" |
| 16 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 17 | #include "chrome/common/notification_details.h" |
[email protected] | 4db6ae4 | 2010-06-09 15:58:47 | [diff] [blame] | 18 | #include "chrome/common/notification_service.h" |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 19 | #include "chrome/common/notification_source.h" |
| 20 | #include "chrome/common/notification_type.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 21 | #include "chrome/common/pref_names.h" |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 22 | #include "googleurl/src/gurl.h" |
| 23 | #include "net/base/net_util.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 24 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 25 | |
| 26 | using WebKit::WebView; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 27 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 28 | HostZoomMap::HostZoomMap(Profile* profile) |
| 29 | : profile_(profile), |
| 30 | updating_preferences_(false) { |
| 31 | Load(); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 32 | default_zoom_level_ = |
| 33 | profile_->GetPrefs()->GetReal(prefs::kDefaultZoomLevel); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 34 | registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 35 | Source<Profile>(profile)); |
[email protected] | c18d4f48 | 2010-05-18 21:19:50 | [diff] [blame] | 36 | // Don't observe pref changes (e.g. from sync) in Incognito; once we create |
| 37 | // the incognito window it should have no further connection to the main |
| 38 | // profile/prefs. |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 39 | if (!profile_->IsOffTheRecord()) { |
| 40 | pref_change_registrar_.Init(profile_->GetPrefs()); |
| 41 | pref_change_registrar_.Add(prefs::kPerHostZoomLevels, this); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 42 | pref_change_registrar_.Add(prefs::kDefaultZoomLevel, this); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 43 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 44 | |
| 45 | registrar_.Add( |
| 46 | this, NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 47 | NotificationService::AllSources()); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void HostZoomMap::Load() { |
| 51 | if (!profile_) |
| 52 | return; |
| 53 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 54 | base::AutoLock auto_lock(lock_); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 55 | host_zoom_levels_.clear(); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 56 | const DictionaryValue* host_zoom_dictionary = |
| 57 | profile_->GetPrefs()->GetDictionary(prefs::kPerHostZoomLevels); |
| 58 | // Careful: The returned value could be NULL if the pref has never been set. |
| 59 | if (host_zoom_dictionary != NULL) { |
| 60 | for (DictionaryValue::key_iterator i(host_zoom_dictionary->begin_keys()); |
| 61 | i != host_zoom_dictionary->end_keys(); ++i) { |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 62 | const std::string& host(*i); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 63 | double zoom_level = 0; |
| 64 | |
| 65 | bool success = host_zoom_dictionary->GetRealWithoutPathExpansion( |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 66 | host, &zoom_level); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 67 | if (!success) { |
| 68 | // The data used to be stored as ints, so try that. |
| 69 | int int_zoom_level; |
| 70 | success = host_zoom_dictionary->GetIntegerWithoutPathExpansion( |
| 71 | host, &int_zoom_level); |
| 72 | if (success) { |
| 73 | zoom_level = static_cast<double>(int_zoom_level); |
| 74 | // Since the values were once stored as non-clamped, clamp now. |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 75 | double zoom_factor = WebView::zoomLevelToZoomFactor(zoom_level); |
| 76 | if (zoom_factor < WebView::minTextSizeMultiplier) { |
| 77 | zoom_level = |
| 78 | WebView::zoomFactorToZoomLevel(WebView::minTextSizeMultiplier); |
| 79 | } else if (zoom_factor > WebView::maxTextSizeMultiplier) { |
| 80 | zoom_level = |
| 81 | WebView::zoomFactorToZoomLevel(WebView::maxTextSizeMultiplier); |
| 82 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 83 | } |
| 84 | } |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 85 | DCHECK(success); |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 86 | host_zoom_levels_[host] = zoom_level; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // static |
| 92 | void HostZoomMap::RegisterUserPrefs(PrefService* prefs) { |
| 93 | prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels); |
| 94 | } |
| 95 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 96 | double HostZoomMap::GetZoomLevel(const GURL& url) const { |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 97 | std::string host(net::GetHostOrSpecFromURL(url)); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 98 | base::AutoLock auto_lock(lock_); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 99 | HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 100 | return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 103 | void HostZoomMap::SetZoomLevel(const GURL& url, double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 104 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 105 | if (!profile_) |
| 106 | return; |
| 107 | |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 108 | std::string host(net::GetHostOrSpecFromURL(url)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 109 | |
| 110 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 111 | base::AutoLock auto_lock(lock_); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 112 | if (level == default_zoom_level_) |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 113 | host_zoom_levels_.erase(host); |
| 114 | else |
| 115 | host_zoom_levels_[host] = level; |
| 116 | } |
| 117 | |
[email protected] | 4db6ae4 | 2010-06-09 15:58:47 | [diff] [blame] | 118 | NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, |
| 119 | Source<Profile>(profile_), |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 120 | NotificationService::NoDetails()); |
[email protected] | 4db6ae4 | 2010-06-09 15:58:47 | [diff] [blame] | 121 | |
[email protected] | c18d4f48 | 2010-05-18 21:19:50 | [diff] [blame] | 122 | // If we're in incognito mode, don't persist changes to the prefs. We'll keep |
| 123 | // them in memory only so they will be forgotten on exiting incognito. |
| 124 | if (profile_->IsOffTheRecord()) |
| 125 | return; |
| 126 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 127 | updating_preferences_ = true; |
| 128 | { |
| 129 | ScopedPrefUpdate update(profile_->GetPrefs(), prefs::kPerHostZoomLevels); |
| 130 | DictionaryValue* host_zoom_dictionary = |
| 131 | profile_->GetPrefs()->GetMutableDictionary(prefs::kPerHostZoomLevels); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 132 | if (level == default_zoom_level_) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 133 | host_zoom_dictionary->RemoveWithoutPathExpansion(host, NULL); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 134 | } else { |
| 135 | host_zoom_dictionary->SetWithoutPathExpansion( |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 136 | host, Value::CreateRealValue(level)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 137 | } |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 138 | } |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 139 | updating_preferences_ = false; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 142 | double HostZoomMap::GetTemporaryZoomLevel(int render_process_id, |
| 143 | int render_view_id) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 144 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 145 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 146 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 147 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 148 | return temporary_zoom_levels_[i].zoom_level; |
| 149 | } |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void HostZoomMap::SetTemporaryZoomLevel(int render_process_id, |
| 155 | int render_view_id, |
| 156 | double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 157 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 158 | if (!profile_) |
| 159 | return; |
| 160 | |
| 161 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 162 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 163 | size_t i; |
| 164 | for (i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 165 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 166 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 167 | if (level) { |
| 168 | temporary_zoom_levels_[i].zoom_level = level; |
| 169 | } else { |
| 170 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 171 | } |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (level && i == temporary_zoom_levels_.size()) { |
| 177 | TemporaryZoomLevel temp; |
| 178 | temp.render_process_id = render_process_id; |
| 179 | temp.render_view_id = render_view_id; |
| 180 | temp.zoom_level = level; |
| 181 | temporary_zoom_levels_.push_back(temp); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, |
| 186 | Source<Profile>(profile_), |
| 187 | NotificationService::NoDetails()); |
| 188 | } |
| 189 | |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 190 | void HostZoomMap::ResetToDefaults() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 191 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 192 | if (!profile_) |
| 193 | return; |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 194 | |
[email protected] | 629a5cd1 | 2010-01-30 02:42:39 | [diff] [blame] | 195 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 196 | base::AutoLock auto_lock(lock_); |
[email protected] | 629a5cd1 | 2010-01-30 02:42:39 | [diff] [blame] | 197 | host_zoom_levels_.clear(); |
| 198 | } |
| 199 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 200 | updating_preferences_ = true; |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 201 | profile_->GetPrefs()->ClearPref(prefs::kPerHostZoomLevels); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 202 | updating_preferences_ = false; |
| 203 | } |
| 204 | |
| 205 | void HostZoomMap::Shutdown() { |
| 206 | if (!profile_) |
| 207 | return; |
| 208 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 209 | registrar_.RemoveAll(); |
[email protected] | c18d4f48 | 2010-05-18 21:19:50 | [diff] [blame] | 210 | if (!profile_->IsOffTheRecord()) |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 211 | pref_change_registrar_.RemoveAll(); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 212 | profile_ = NULL; |
| 213 | } |
| 214 | |
| 215 | void HostZoomMap::Observe( |
| 216 | NotificationType type, |
| 217 | const NotificationSource& source, |
| 218 | const NotificationDetails& details) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 219 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 220 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 221 | switch (type.value) { |
| 222 | case NotificationType::PROFILE_DESTROYED: |
| 223 | // If the profile is going away, we need to stop using it. |
| 224 | Shutdown(); |
| 225 | break; |
| 226 | case NotificationType::RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 227 | base::AutoLock auto_lock(lock_); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 228 | int render_view_id = Source<RenderViewHost>(source)->routing_id(); |
| 229 | int render_process_id = Source<RenderViewHost>(source)->process()->id(); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 230 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 231 | for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 232 | if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 233 | temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 234 | temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | break; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 239 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 240 | case NotificationType::PREF_CHANGED: { |
| 241 | // If we are updating our own preference, don't reload. |
| 242 | if (!updating_preferences_) { |
| 243 | std::string* name = Details<std::string>(details).ptr(); |
| 244 | if (prefs::kPerHostZoomLevels == *name) |
| 245 | Load(); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 246 | else if (prefs::kDefaultZoomLevel == *name) { |
| 247 | default_zoom_level_ = |
| 248 | profile_->GetPrefs()->GetReal(prefs::kDefaultZoomLevel); |
| 249 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 250 | } |
[email protected] | 2de28d9 | 2010-10-01 08:28:03 | [diff] [blame] | 251 | break; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 252 | } |
| 253 | default: |
| 254 | NOTREACHED() << "Unexpected preference observed."; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 255 | } |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 258 | HostZoomMap::~HostZoomMap() { |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 259 | Shutdown(); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 260 | } |