Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(843)

Side by Side Diff: chrome/browser/ui/zoom/chrome_zoom_level_prefs.cc

Issue 541103002: Introduce ChromeZoomLevelPref, make zoom level prefs independent of profile prefs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address final prefs-related comments. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_(nullptr) {
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::kPartitionPerHostZoomLevels);
70 const base::DictionaryValue* host_zoom_dictionary = nullptr;
71 if (host_zoom_dictionaries->GetDictionary(partition_key_,
72 &host_zoom_dictionary)) {
73 // Since we're calling this before setting up zoom_subscription_ below we
74 // don't need to worry that host_zoom_dictionary is indirectly affected
75 // by calls to HostZoomMap::SetZoomLevelForHost().
76 ExtractPerHostZoomLevels(host_zoom_dictionary);
77 }
78 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
79 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this)));
80 }
81
82 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) {
83 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()))
84 return;
85
86 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel);
87 update->SetDouble(partition_key_, level);
88 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so
89 // set this manually.
90 host_zoom_map_->SetDefaultZoomLevel(level);
91 default_zoom_changed_callbacks_.Notify();
92 }
93
94 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const {
95 double default_zoom_level = 0.0;
96
97 const base::DictionaryValue* default_zoom_level_dictionary =
98 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel);
99 // If no default has been previously set, the default returned is the
100 // value used to initialize default_zoom_level in this function.
101 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level);
102 return default_zoom_level;
103 }
104
105 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription>
106 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback(
107 const base::Closure& callback) {
108 return default_zoom_changed_callbacks_.Add(callback);
109 }
110
111 void ChromeZoomLevelPrefs::OnZoomLevelChanged(
112 const content::HostZoomMap::ZoomLevelChange& change) {
113 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST)
114 return;
115 double level = change.zoom_level;
116 DictionaryPrefUpdate update(pref_service_,
117 prefs::kPartitionPerHostZoomLevels);
118 base::DictionaryValue* host_zoom_dictionaries = update.Get();
119 DCHECK(host_zoom_dictionaries);
120
121 bool modification_is_removal =
122 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel());
123
124 base::DictionaryValue* host_zoom_dictionary = nullptr;
125 if (!host_zoom_dictionaries->GetDictionary(partition_key_,
126 &host_zoom_dictionary)) {
127 host_zoom_dictionary = new base::DictionaryValue();
128 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary);
129 }
130
131 if (modification_is_removal)
132 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr);
133 else
134 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level);
135 }
136
137 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels(
138 const base::DictionaryValue* host_zoom_dictionary) {
139 std::vector<std::string> keys_to_remove;
sky 2014/10/10 19:43:59 Is there a particular reason you build up a vector
wjmaclean 2014/10/10 20:23:02 In the original code (some of this existed in prof
140 scoped_ptr<base::DictionaryValue> host_zoom_dictionary_copy(
141 host_zoom_dictionary->DeepCopyWithoutEmptyChildren());
142 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary_copy);
143 !i.IsAtEnd();
144 i.Advance()) {
145 const std::string& host(i.key());
146 double zoom_level = 0;
147
148 bool has_valid_zoom_level = i.value().GetAsDouble(&zoom_level);
149
150 // Filter out A) the empty host, B) zoom levels equal to the default; and
151 // remember them, so that we can later erase them from Prefs.
152 // Values of type A and B could have been stored due to crbug.com/364399.
153 // Values of type B could further have been stored before the default zoom
154 // level was set to its current value. In either case, SetZoomLevelForHost
155 // will ignore type B values, thus, to have consistency with HostZoomMap's
156 // internal state, these values must also be removed from Prefs.
157 if (host.empty() || !has_valid_zoom_level ||
158 content::ZoomValuesEqual(zoom_level,
159 host_zoom_map_->GetDefaultZoomLevel())) {
160 keys_to_remove.push_back(host);
161 continue;
162 }
163
164 host_zoom_map_->SetZoomLevelForHost(host, zoom_level);
165 }
166
167 // Sanitize prefs to remove entries that match the default zoom level and/or
168 // have an empty host.
169 {
170 DictionaryPrefUpdate update(pref_service_,
171 prefs::kPartitionPerHostZoomLevels);
172 base::DictionaryValue* host_zoom_dictionaries = update.Get();
173 base::DictionaryValue* host_zoom_dictionary = nullptr;
174 host_zoom_dictionaries->GetDictionary(partition_key_,
175 &host_zoom_dictionary);
176 for (const std::string& s : keys_to_remove)
177 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr);
178 }
179 }
180
181 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698