Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / zoom / chrome_zoom_level_prefs.cc
bloba8604db75b9ad6956ad5e2eafdf22c63465b0ab9
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"
7 #include "base/bind.h"
8 #include "base/prefs/json_pref_store.h"
9 #include "base/prefs/pref_filter.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service_factory.h"
12 #include "base/prefs/scoped_user_pref_update.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/values.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/ui/zoom/zoom_event_manager.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/host_zoom_map.h"
20 #include "content/public/common/page_zoom.h"
22 namespace {
24 std::string GetHash(const base::FilePath& relative_path) {
25 size_t int_key = BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path);
26 return base::SizeTToString(int_key);
29 } // namespace
31 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(
32 PrefService* pref_service,
33 const base::FilePath& profile_path,
34 const base::FilePath& partition_path,
35 base::WeakPtr<ui_zoom::ZoomEventManager> zoom_event_manager)
36 : pref_service_(pref_service),
37 zoom_event_manager_(zoom_event_manager),
38 host_zoom_map_(nullptr) {
39 DCHECK(pref_service_);
41 DCHECK(!partition_path.empty());
42 DCHECK((partition_path == profile_path) ||
43 profile_path.IsParent(partition_path));
44 // Create a partition_key string with no '.'s in it. For the default
45 // StoragePartition, this string will always be "0".
46 base::FilePath partition_relative_path;
47 profile_path.AppendRelativePath(partition_path, &partition_relative_path);
48 partition_key_ = GetHash(partition_relative_path);
52 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() {
55 std::string ChromeZoomLevelPrefs::GetHashForTesting(
56 const base::FilePath& relative_path) {
57 return GetHash(relative_path);
60 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) {
61 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()))
62 return;
64 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel);
65 update->SetDouble(partition_key_, level);
66 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so
67 // set this manually.
68 host_zoom_map_->SetDefaultZoomLevel(level);
69 default_zoom_changed_callbacks_.Notify();
70 if (zoom_event_manager_)
71 zoom_event_manager_->OnDefaultZoomLevelChanged();
74 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const {
75 double default_zoom_level = 0.0;
77 const base::DictionaryValue* default_zoom_level_dictionary =
78 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel);
79 // If no default has been previously set, the default returned is the
80 // value used to initialize default_zoom_level in this function.
81 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level);
82 return default_zoom_level;
85 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription>
86 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback(
87 const base::Closure& callback) {
88 return default_zoom_changed_callbacks_.Add(callback);
91 void ChromeZoomLevelPrefs::OnZoomLevelChanged(
92 const content::HostZoomMap::ZoomLevelChange& change) {
93 // If there's a manager to aggregate ZoomLevelChanged events, pass this event
94 // along. Since we already hold a subscription to our associated HostZoomMap,
95 // we don't need to create a separate subscription for this.
96 if (zoom_event_manager_)
97 zoom_event_manager_->OnZoomLevelChanged(change);
99 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST)
100 return;
101 double level = change.zoom_level;
102 DictionaryPrefUpdate update(pref_service_,
103 prefs::kPartitionPerHostZoomLevels);
104 base::DictionaryValue* host_zoom_dictionaries = update.Get();
105 DCHECK(host_zoom_dictionaries);
107 bool modification_is_removal =
108 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel());
110 base::DictionaryValue* host_zoom_dictionary = nullptr;
111 if (!host_zoom_dictionaries->GetDictionary(partition_key_,
112 &host_zoom_dictionary)) {
113 host_zoom_dictionary = new base::DictionaryValue();
114 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary);
117 if (modification_is_removal)
118 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr);
119 else
120 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level);
123 // TODO(wjmaclean): Remove the dictionary_path once the migration code is
124 // removed. crbug.com/420643
125 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels(
126 const base::DictionaryValue* host_zoom_dictionary,
127 bool sanitize_partition_host_zoom_levels) {
128 std::vector<std::string> keys_to_remove;
129 scoped_ptr<base::DictionaryValue> host_zoom_dictionary_copy =
130 host_zoom_dictionary->DeepCopyWithoutEmptyChildren();
131 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary_copy);
132 !i.IsAtEnd();
133 i.Advance()) {
134 const std::string& host(i.key());
135 double zoom_level = 0;
137 bool has_valid_zoom_level = i.value().GetAsDouble(&zoom_level);
139 // Filter out A) the empty host, B) zoom levels equal to the default; and
140 // remember them, so that we can later erase them from Prefs.
141 // Values of type A and B could have been stored due to crbug.com/364399.
142 // Values of type B could further have been stored before the default zoom
143 // level was set to its current value. In either case, SetZoomLevelForHost
144 // will ignore type B values, thus, to have consistency with HostZoomMap's
145 // internal state, these values must also be removed from Prefs.
146 if (host.empty() || !has_valid_zoom_level ||
147 content::ZoomValuesEqual(zoom_level,
148 host_zoom_map_->GetDefaultZoomLevel())) {
149 keys_to_remove.push_back(host);
150 continue;
153 host_zoom_map_->SetZoomLevelForHost(host, zoom_level);
156 // We don't bother sanitizing non-partition dictionaries as they will be
157 // discarded in the migration process. Note: since the structure of partition
158 // per-host zoom level dictionaries is different from the legacy profile
159 // per-host zoom level dictionaries, the following code will fail if run
160 // on the legacy dictionaries.
161 if (!sanitize_partition_host_zoom_levels)
162 return;
164 // Sanitize prefs to remove entries that match the default zoom level and/or
165 // have an empty host.
167 DictionaryPrefUpdate update(pref_service_,
168 prefs::kPartitionPerHostZoomLevels);
169 base::DictionaryValue* host_zoom_dictionaries = update.Get();
170 base::DictionaryValue* host_zoom_dictionary = nullptr;
171 host_zoom_dictionaries->GetDictionary(partition_key_,
172 &host_zoom_dictionary);
173 for (const std::string& s : keys_to_remove)
174 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr);
178 void ChromeZoomLevelPrefs::InitHostZoomMap(
179 content::HostZoomMap* host_zoom_map) {
180 // This init function must be called only once.
181 DCHECK(!host_zoom_map_);
182 DCHECK(host_zoom_map);
183 host_zoom_map_ = host_zoom_map;
185 // Initialize the default zoom level.
186 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
188 // Initialize the HostZoomMap with per-host zoom levels from the persisted
189 // zoom-level preference values.
190 const base::DictionaryValue* host_zoom_dictionaries =
191 pref_service_->GetDictionary(prefs::kPartitionPerHostZoomLevels);
192 const base::DictionaryValue* host_zoom_dictionary = nullptr;
193 if (host_zoom_dictionaries->GetDictionary(partition_key_,
194 &host_zoom_dictionary)) {
195 // Since we're calling this before setting up zoom_subscription_ below we
196 // don't need to worry that host_zoom_dictionary is indirectly affected
197 // by calls to HostZoomMap::SetZoomLevelForHost().
198 ExtractPerHostZoomLevels(host_zoom_dictionary,
199 true /* sanitize_partition_host_zoom_levels */);
201 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
202 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this)));