| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 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/ui/zoom/chrome_zoom_level_prefs.h" |
| 6 |
| 7 #include <sstream> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/prefs/json_pref_store.h" |
| 11 #include "base/prefs/pref_filter.h" |
| 12 #include "base/prefs/pref_registry_simple.h" |
| 13 #include "base/prefs/pref_service_factory.h" |
| 14 #include "base/prefs/scoped_user_pref_update.h" |
| 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/values.h" |
| 17 #include "chrome/common/chrome_constants.h" |
| 18 #include "chrome/common/pref_names.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/host_zoom_map.h" |
| 21 #include "content/public/common/page_zoom.h" |
| 22 |
| 23 namespace chrome { |
| 24 |
| 25 std::string ChromeZoomLevelPrefs::GetHash(const base::FilePath& relative_path) { |
| 26 size_t int_key = |
| 27 #if defined(COMPILER_MSVC) |
| 28 BASE_HASH_NAMESPACE::hash_value(relative_path); |
| 29 #else |
| 30 BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path); |
| 31 #endif // COMPILER |
| 32 return base::SizeTToString(int_key); |
| 33 } |
| 34 |
| 35 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(PrefService* pref_service, |
| 36 const base::FilePath& profile_path) |
| 37 : pref_service_(pref_service), |
| 38 profile_path_(profile_path), |
| 39 host_zoom_map_(NULL) { |
| 40 DCHECK(pref_service_); |
| 41 } |
| 42 |
| 43 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() { |
| 44 } |
| 45 |
| 46 void ChromeZoomLevelPrefs::InitPrefsAndCopyToHostZoomMap( |
| 47 const base::FilePath& partition_path, |
| 48 content::HostZoomMap* host_zoom_map) { |
| 49 DCHECK(!partition_path.empty()); |
| 50 DCHECK((partition_path == profile_path_) || |
| 51 profile_path_.IsParent(partition_path)); |
| 52 // This init function must be called only once. |
| 53 DCHECK(!host_zoom_map_); |
| 54 DCHECK(host_zoom_map); |
| 55 host_zoom_map_ = host_zoom_map; |
| 56 |
| 57 // Create a partition_key string with no '.'s in it. For the default |
| 58 // StoragePartition, this string will always be "0". |
| 59 base::FilePath partition_relative_path; |
| 60 profile_path_.AppendRelativePath(partition_path, &partition_relative_path); |
| 61 partition_key_ = GetHash(partition_relative_path); |
| 62 |
| 63 // Initialize the default zoom level. |
| 64 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref()); |
| 65 |
| 66 // Initialize the HostZoomMap with per-host zoom levels from the persisted |
| 67 // zoom-level preference values. |
| 68 const base::DictionaryValue* host_zoom_dictionaries = |
| 69 pref_service_->GetDictionary(prefs::kPerHostZoomLevels); |
| 70 const base::DictionaryValue* host_zoom_dictionary; |
| 71 if (host_zoom_dictionaries && |
| 72 host_zoom_dictionaries->GetDictionary(partition_key_, |
| 73 &host_zoom_dictionary)) { |
| 74 std::vector<std::string> keys_to_remove; |
| 75 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); !i.IsAtEnd(); |
| 76 i.Advance()) { |
| 77 const std::string& host(i.key()); |
| 78 double zoom_level = 0; |
| 79 |
| 80 bool success = i.value().GetAsDouble(&zoom_level); |
| 81 DCHECK(success); |
| 82 |
| 83 if (host.empty() || |
| 84 content::ZoomValuesEqual(zoom_level, |
| 85 host_zoom_map_->GetDefaultZoomLevel())) { |
| 86 keys_to_remove.push_back(host); |
| 87 continue; |
| 88 } |
| 89 |
| 90 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); |
| 91 } |
| 92 |
| 93 // Sanitize prefs to remove entries that match the default zoom level and/or |
| 94 // have an empty host. |
| 95 { |
| 96 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); |
| 97 base::DictionaryValue* host_zoom_dictionaries = update.Get(); |
| 98 base::DictionaryValue* host_zoom_dictionary; |
| 99 if (!host_zoom_dictionaries->GetDictionary(partition_key_, |
| 100 &host_zoom_dictionary)) { |
| 101 host_zoom_dictionary = new base::DictionaryValue(); |
| 102 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); |
| 103 } |
| 104 |
| 105 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); |
| 106 it != keys_to_remove.end(); |
| 107 ++it) { |
| 108 host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); |
| 109 } |
| 110 } |
| 111 } |
| 112 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( |
| 113 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); |
| 114 } |
| 115 |
| 116 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { |
| 117 if (host_zoom_map_->GetDefaultZoomLevel() == level) |
| 118 return; |
| 119 |
| 120 DictionaryPrefUpdate update(pref_service_, prefs::kDefaultZoomLevel); |
| 121 update->SetDouble(partition_key_, level); |
| 122 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so |
| 123 // set this manually. |
| 124 host_zoom_map_->SetDefaultZoomLevel(level); |
| 125 default_zoom_changed_callbacks_.Notify(); |
| 126 } |
| 127 |
| 128 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() { |
| 129 double default_zoom_level = 0.0; |
| 130 |
| 131 const base::DictionaryValue* default_zoom_level_dictionary = |
| 132 pref_service_->GetDictionary(prefs::kDefaultZoomLevel); |
| 133 if (!default_zoom_level_dictionary->GetDouble(partition_key_, |
| 134 &default_zoom_level)) { |
| 135 // We will only get here if the default zoom level for this partition |
| 136 // has never been set before. |
| 137 SetDefaultZoomLevelPref(default_zoom_level); |
| 138 } |
| 139 return default_zoom_level; |
| 140 } |
| 141 |
| 142 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> |
| 143 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( |
| 144 const base::Closure& callback) { |
| 145 return default_zoom_changed_callbacks_.Add(callback); |
| 146 } |
| 147 |
| 148 void ChromeZoomLevelPrefs::OnZoomLevelChanged( |
| 149 const content::HostZoomMap::ZoomLevelChange& change) { |
| 150 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) |
| 151 return; |
| 152 double level = change.zoom_level; |
| 153 // Note: the following will create the dictionary if it doesn't already exist. |
| 154 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); |
| 155 base::DictionaryValue* host_zoom_dictionaries = update.Get(); |
| 156 DCHECK(host_zoom_dictionaries); |
| 157 |
| 158 base::DictionaryValue* host_zoom_dictionary; |
| 159 if (!host_zoom_dictionaries->GetDictionary(partition_key_, |
| 160 &host_zoom_dictionary)) { |
| 161 host_zoom_dictionary = new base::DictionaryValue(); |
| 162 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); |
| 163 } |
| 164 |
| 165 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) |
| 166 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL); |
| 167 else |
| 168 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); |
| 169 } |
| 170 |
| 171 } // namespace chrome |
| OLD | NEW |