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

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: Remove obsolete access of ZoomController from WebUILoginView. 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 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 {
24
25 std::string GetHash(
26 const base::FilePath& relative_path) {
27 size_t int_key =
28 #if defined(COMPILER_MSVC)
29 BASE_HASH_NAMESPACE::hash_value(relative_path);
30 #else
31 BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path);
32 #endif // COMPILER
33 return base::SizeTToString(int_key);
34 }
35
36 } // namespace
37
38 namespace chrome {
39
40 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(PrefService* pref_service,
41 const base::FilePath& profile_path)
42 : pref_service_(pref_service),
43 profile_path_(profile_path),
44 host_zoom_map_(nullptr) {
45 DCHECK(pref_service_);
46 }
47
48 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() {
49 }
50
51 void ChromeZoomLevelPrefs::InitPrefsAndCopyToHostZoomMap(
52 const base::FilePath& partition_path,
53 content::HostZoomMap* host_zoom_map) {
54 DCHECK(!partition_path.empty());
55 DCHECK((partition_path == profile_path_) ||
56 profile_path_.IsParent(partition_path));
57 // This init function must be called only once.
58 DCHECK(!host_zoom_map_);
59 DCHECK(host_zoom_map);
60 host_zoom_map_ = host_zoom_map;
61
62 // Create a partition_key string with no '.'s in it. For the default
63 // StoragePartition, this string will always be "0".
64 base::FilePath partition_relative_path;
65 profile_path_.AppendRelativePath(partition_path, &partition_relative_path);
66 partition_key_ = GetHash(partition_relative_path);
67
68 // Initialize the default zoom level.
69 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
70
71 // Initialize the HostZoomMap with per-host zoom levels from the persisted
72 // zoom-level preference values.
73 const base::DictionaryValue* host_zoom_dictionaries =
74 pref_service_->GetDictionary(prefs::kPartitionPerHostZoomLevels);
75 const base::DictionaryValue* host_zoom_dictionary = nullptr;
76 if (host_zoom_dictionaries->GetDictionary(partition_key_,
77 &host_zoom_dictionary)) {
78 // Since we're calling this before setting up zoom_subscription_ below we
79 // don't need to worry that host_zoom_dictionary is indirectly affected
80 // by calls to HostZoomMap::SetZoomLevelForHost().
81 ExtractPerHostZoomLevels(host_zoom_dictionary);
82 }
83 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
84 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this)));
85 }
86
87 std::string ChromeZoomLevelPrefs::GetHashForTesting(
88 const base::FilePath& relative_path) {
89 return GetHash(relative_path);
90 }
91
92 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) {
93 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()))
94 return;
95
96 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel);
97 update->SetDouble(partition_key_, level);
98 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so
99 // set this manually.
100 host_zoom_map_->SetDefaultZoomLevel(level);
101 default_zoom_changed_callbacks_.Notify();
102 }
103
104 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const {
105 double default_zoom_level = 0.0;
106
107 const base::DictionaryValue* default_zoom_level_dictionary =
108 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel);
109 // If no default has been previously set, the default returned is the
110 // value used to initialize default_zoom_level in this function.
111 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level);
112 return default_zoom_level;
113 }
114
115 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription>
116 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback(
117 const base::Closure& callback) {
118 return default_zoom_changed_callbacks_.Add(callback);
119 }
120
121 void ChromeZoomLevelPrefs::OnZoomLevelChanged(
122 const content::HostZoomMap::ZoomLevelChange& change) {
123 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST)
124 return;
125 double level = change.zoom_level;
126 DictionaryPrefUpdate update(pref_service_,
127 prefs::kPartitionPerHostZoomLevels);
128 base::DictionaryValue* host_zoom_dictionaries = update.Get();
129 DCHECK(host_zoom_dictionaries);
130
131 bool modification_is_removal =
132 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel());
133
134 base::DictionaryValue* host_zoom_dictionary = nullptr;
135 if (!host_zoom_dictionaries->GetDictionary(partition_key_,
136 &host_zoom_dictionary)) {
137 host_zoom_dictionary = new base::DictionaryValue();
138 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary);
139 }
140
141 if (modification_is_removal)
142 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr);
143 else
144 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level);
145 }
146
147 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels(
148 const base::DictionaryValue* host_zoom_dictionary) {
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);
153 !i.IsAtEnd();
154 i.Advance()) {
155 const std::string& host(i.key());
156 double zoom_level = 0;
157
158 bool has_valid_zoom_level = i.value().GetAsDouble(&zoom_level);
159
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);
171 continue;
172 }
173
174 host_zoom_map_->SetZoomLevelForHost(host, zoom_level);
175 }
176
177 // Sanitize prefs to remove entries that match the default zoom level and/or
178 // have an empty host.
179 {
180 DictionaryPrefUpdate update(pref_service_,
181 prefs::kPartitionPerHostZoomLevels);
182 base::DictionaryValue* host_zoom_dictionaries = update.Get();
183 base::DictionaryValue* host_zoom_dictionary = nullptr;
184 host_zoom_dictionaries->GetDictionary(partition_key_,
185 &host_zoom_dictionary);
186 for (const std::string& s : keys_to_remove)
187 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr);
188 }
189 }
190
191 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/ui/zoom/chrome_zoom_level_prefs.h ('k') | chrome/browser/ui/zoom/zoom_controller_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698