1 // Copyright 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.
5 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.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"
26 const base::FilePath& relative_path) {
28 BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path);
29 return base::SizeTToString(int_key);
36 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(PrefService* pref_service,
37 const base::FilePath& profile_path)
38 : pref_service_(pref_service),
39 profile_path_(profile_path),
40 host_zoom_map_(nullptr) {
41 DCHECK(pref_service_);
44 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() {
47 void ChromeZoomLevelPrefs::InitPrefsAndCopyToHostZoomMap(
48 const base::FilePath& partition_path,
49 content::HostZoomMap* host_zoom_map) {
50 DCHECK(!partition_path.empty());
51 DCHECK((partition_path == profile_path_) ||
52 profile_path_.IsParent(partition_path));
53 // This init function must be called only once.
54 DCHECK(!host_zoom_map_);
55 DCHECK(host_zoom_map);
56 host_zoom_map_ = host_zoom_map;
58 // Create a partition_key string with no '.'s in it. For the default
59 // StoragePartition, this string will always be "0".
60 base::FilePath partition_relative_path;
61 profile_path_.AppendRelativePath(partition_path, &partition_relative_path);
62 partition_key_ = GetHash(partition_relative_path);
64 // Initialize the default zoom level.
65 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
67 // Initialize the HostZoomMap with per-host zoom levels from the persisted
68 // zoom-level preference values.
69 const base::DictionaryValue* host_zoom_dictionaries =
70 pref_service_->GetDictionary(prefs::kPartitionPerHostZoomLevels);
71 const base::DictionaryValue* host_zoom_dictionary = nullptr;
72 if (host_zoom_dictionaries->GetDictionary(partition_key_,
73 &host_zoom_dictionary)) {
74 // Since we're calling this before setting up zoom_subscription_ below we
75 // don't need to worry that host_zoom_dictionary is indirectly affected
76 // by calls to HostZoomMap::SetZoomLevelForHost().
77 ExtractPerHostZoomLevels(host_zoom_dictionary,
78 true /* sanitize_partition_host_zoom_levels */);
80 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
81 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this)));
84 std::string ChromeZoomLevelPrefs::GetHashForTesting(
85 const base::FilePath& relative_path) {
86 return GetHash(relative_path);
89 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) {
90 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()))
93 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel);
94 update->SetDouble(partition_key_, level);
95 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so
97 host_zoom_map_->SetDefaultZoomLevel(level);
98 default_zoom_changed_callbacks_.Notify();
101 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const {
102 double default_zoom_level = 0.0;
104 const base::DictionaryValue* default_zoom_level_dictionary =
105 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel);
106 // If no default has been previously set, the default returned is the
107 // value used to initialize default_zoom_level in this function.
108 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level);
109 return default_zoom_level;
112 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription>
113 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback(
114 const base::Closure& callback) {
115 return default_zoom_changed_callbacks_.Add(callback);
118 void ChromeZoomLevelPrefs::OnZoomLevelChanged(
119 const content::HostZoomMap::ZoomLevelChange& change) {
120 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST)
122 double level = change.zoom_level;
123 DictionaryPrefUpdate update(pref_service_,
124 prefs::kPartitionPerHostZoomLevels);
125 base::DictionaryValue* host_zoom_dictionaries = update.Get();
126 DCHECK(host_zoom_dictionaries);
128 bool modification_is_removal =
129 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel());
131 base::DictionaryValue* host_zoom_dictionary = nullptr;
132 if (!host_zoom_dictionaries->GetDictionary(partition_key_,
133 &host_zoom_dictionary)) {
134 host_zoom_dictionary = new base::DictionaryValue();
135 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary);
138 if (modification_is_removal)
139 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr);
141 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level);
144 // TODO(wjmaclean): Remove the dictionary_path once the migration code is
145 // removed. crbug.com/420643
146 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels(
147 const base::DictionaryValue* host_zoom_dictionary,
148 bool sanitize_partition_host_zoom_levels) {
149 std::vector<std::string> keys_to_remove;
150 scoped_ptr<base::DictionaryValue> host_zoom_dictionary_copy(
151 host_zoom_dictionary->DeepCopyWithoutEmptyChildren());
152 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary_copy);
155 const std::string& host(i.key());
156 double zoom_level = 0;
158 bool has_valid_zoom_level = i.value().GetAsDouble(&zoom_level);
160 // Filter out A) the empty host, B) zoom levels equal to the default; and
161 // remember them, so that we can later erase them from Prefs.
162 // Values of type A and B could have been stored due to crbug.com/364399.
163 // Values of type B could further have been stored before the default zoom
164 // level was set to its current value. In either case, SetZoomLevelForHost
165 // will ignore type B values, thus, to have consistency with HostZoomMap's
166 // internal state, these values must also be removed from Prefs.
167 if (host.empty() || !has_valid_zoom_level ||
168 content::ZoomValuesEqual(zoom_level,
169 host_zoom_map_->GetDefaultZoomLevel())) {
170 keys_to_remove.push_back(host);
174 host_zoom_map_->SetZoomLevelForHost(host, zoom_level);
177 // We don't bother sanitizing non-partition dictionaries as they will be
178 // discarded in the migration process. Note: since the structure of partition
179 // per-host zoom level dictionaries is different from the legacy profile
180 // per-host zoom level dictionaries, the following code will fail if run
181 // on the legacy dictionaries.
182 if (!sanitize_partition_host_zoom_levels)
185 // Sanitize prefs to remove entries that match the default zoom level and/or
186 // have an empty host.
188 DictionaryPrefUpdate update(pref_service_,
189 prefs::kPartitionPerHostZoomLevels);
190 base::DictionaryValue* host_zoom_dictionaries = update.Get();
191 base::DictionaryValue* host_zoom_dictionary = nullptr;
192 host_zoom_dictionaries->GetDictionary(partition_key_,
193 &host_zoom_dictionary);
194 for (const std::string& s : keys_to_remove)
195 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr);
199 } // namespace chrome