[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 | |
| 5 | #include "chrome/browser/host_zoom_map.h" |
| 6 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame^] | 7 | #include "base/string_piece.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 8 | #include "base/utf_string_conversions.h" |
| 9 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 10 | #include "chrome/browser/pref_service.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 11 | #include "chrome/browser/profile.h" |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 12 | #include "chrome/browser/scoped_pref_update.h" |
| 13 | #include "chrome/common/notification_details.h" |
[email protected] | 4db6ae4 | 2010-06-09 15:58:47 | [diff] [blame] | 14 | #include "chrome/common/notification_service.h" |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 15 | #include "chrome/common/notification_source.h" |
| 16 | #include "chrome/common/notification_type.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 17 | #include "chrome/common/pref_names.h" |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 18 | #include "googleurl/src/gurl.h" |
| 19 | #include "net/base/net_util.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 20 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 21 | HostZoomMap::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] | c18d4f48 | 2010-05-18 21:19:50 | [diff] [blame] | 27 | // 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] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void HostZoomMap::Load() { |
| 35 | if (!profile_) |
| 36 | return; |
| 37 | |
| 38 | AutoLock auto_lock(lock_); |
| 39 | host_zoom_levels_.clear(); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 40 | 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] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 46 | const std::string& host(*i); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 47 | int zoom_level = 0; |
| 48 | bool success = host_zoom_dictionary->GetIntegerWithoutPathExpansion( |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 49 | host, &zoom_level); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 50 | DCHECK(success); |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 51 | host_zoom_levels_[host] = zoom_level; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // static |
| 57 | void HostZoomMap::RegisterUserPrefs(PrefService* prefs) { |
| 58 | prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels); |
| 59 | } |
| 60 | |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 61 | int HostZoomMap::GetZoomLevel(const GURL& url) const { |
| 62 | std::string host(net::GetHostOrSpecFromURL(url)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 63 | 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] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 68 | void HostZoomMap::SetZoomLevel(const GURL& url, int level) { |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 69 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 70 | if (!profile_) |
| 71 | return; |
| 72 | |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 73 | std::string host(net::GetHostOrSpecFromURL(url)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 74 | |
| 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] | 4db6ae4 | 2010-06-09 15:58:47 | [diff] [blame] | 83 | Details<std::string> details(&host); |
| 84 | NotificationService::current()->Notify(NotificationType::ZOOM_LEVEL_CHANGED, |
| 85 | Source<Profile>(profile_), |
| 86 | details); |
| 87 | |
[email protected] | c18d4f48 | 2010-05-18 21:19:50 | [diff] [blame] | 88 | // 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] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 93 | updating_preferences_ = true; |
| 94 | { |
| 95 | ScopedPrefUpdate update(profile_->GetPrefs(), prefs::kPerHostZoomLevels); |
| 96 | DictionaryValue* host_zoom_dictionary = |
| 97 | profile_->GetPrefs()->GetMutableDictionary(prefs::kPerHostZoomLevels); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 98 | if (level == 0) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 99 | host_zoom_dictionary->RemoveWithoutPathExpansion(host, NULL); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 100 | } else { |
| 101 | host_zoom_dictionary->SetWithoutPathExpansion( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 102 | host, Value::CreateIntegerValue(level)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 103 | } |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 104 | } |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 105 | updating_preferences_ = false; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 108 | void HostZoomMap::ResetToDefaults() { |
| 109 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 110 | if (!profile_) |
| 111 | return; |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 112 | |
[email protected] | 629a5cd1 | 2010-01-30 02:42:39 | [diff] [blame] | 113 | { |
| 114 | AutoLock auto_lock(lock_); |
| 115 | host_zoom_levels_.clear(); |
| 116 | } |
| 117 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 118 | updating_preferences_ = true; |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 119 | profile_->GetPrefs()->ClearPref(prefs::kPerHostZoomLevels); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 120 | updating_preferences_ = false; |
| 121 | } |
| 122 | |
| 123 | void HostZoomMap::Shutdown() { |
| 124 | if (!profile_) |
| 125 | return; |
| 126 | |
| 127 | registrar_.Remove(this, |
| 128 | NotificationType::PROFILE_DESTROYED, |
| 129 | Source<Profile>(profile_)); |
[email protected] | c18d4f48 | 2010-05-18 21:19:50 | [diff] [blame] | 130 | if (!profile_->IsOffTheRecord()) |
| 131 | profile_->GetPrefs()->RemovePrefObserver(prefs::kPerHostZoomLevels, this); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 132 | profile_ = NULL; |
| 133 | } |
| 134 | |
| 135 | void 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] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 152 | std::string* name = Details<std::string>(details).ptr(); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 153 | if (prefs::kPerHostZoomLevels == *name) { |
| 154 | Load(); |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | NOTREACHED() << "Unexpected preference observed."; |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 162 | HostZoomMap::~HostZoomMap() { |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 163 | Shutdown(); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 164 | } |