[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 | |
| 7 | #include "base/utf_string_conversions.h" |
| 8 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 9 | #include "chrome/browser/pref_service.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 10 | #include "chrome/browser/profile.h" |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 11 | #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] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 15 | #include "chrome/common/pref_names.h" |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame^] | 16 | #include "googleurl/src/gurl.h" |
| 17 | #include "net/base/net_util.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 18 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 19 | HostZoomMap::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 | |
| 28 | void HostZoomMap::Load() { |
| 29 | if (!profile_) |
| 30 | return; |
| 31 | |
| 32 | AutoLock auto_lock(lock_); |
| 33 | host_zoom_levels_.clear(); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 34 | 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 |
| 51 | void HostZoomMap::RegisterUserPrefs(PrefService* prefs) { |
| 52 | prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels); |
| 53 | } |
| 54 | |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame^] | 55 | int HostZoomMap::GetZoomLevel(const GURL& url) const { |
| 56 | std::string host(net::GetHostOrSpecFromURL(url)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 57 | 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] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame^] | 62 | void HostZoomMap::SetZoomLevel(const GURL& url, int level) { |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 63 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 64 | if (!profile_) |
| 65 | return; |
| 66 | |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame^] | 67 | std::string host(net::GetHostOrSpecFromURL(url)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 68 | |
| 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] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 77 | 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] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 90 | } |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 91 | updating_preferences_ = false; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 94 | void HostZoomMap::ResetToDefaults() { |
| 95 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 96 | if (!profile_) |
| 97 | return; |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 98 | |
[email protected] | 629a5cd1 | 2010-01-30 02:42:39 | [diff] [blame] | 99 | { |
| 100 | AutoLock auto_lock(lock_); |
| 101 | host_zoom_levels_.clear(); |
| 102 | } |
| 103 | |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 104 | updating_preferences_ = true; |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 105 | profile_->GetPrefs()->ClearPref(prefs::kPerHostZoomLevels); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 106 | updating_preferences_ = false; |
| 107 | } |
| 108 | |
| 109 | void 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 | |
| 120 | void 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] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 147 | HostZoomMap::~HostZoomMap() { |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 148 | Shutdown(); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 149 | } |