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_(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 ExtractPerHostZoomLevels(host_zoom_dictionary); | |
battre
2014/10/10 15:41:08
Here is a time dependency that should be documente
wjmaclean
2014/10/10 17:14:10
I've added a comment here, but the important chang
| |
74 } | |
75 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( | |
76 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); | |
77 } | |
78 | |
79 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { | |
80 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | |
81 return; | |
82 | |
83 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel); | |
84 update->SetDouble(partition_key_, level); | |
85 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so | |
86 // set this manually. | |
87 host_zoom_map_->SetDefaultZoomLevel(level); | |
88 default_zoom_changed_callbacks_.Notify(); | |
89 } | |
90 | |
91 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const { | |
92 double default_zoom_level = 0.0; | |
93 | |
94 const base::DictionaryValue* default_zoom_level_dictionary = | |
95 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel); | |
96 // If no default has been previously set, the default returned is the | |
97 // value used to initialize default_zoom_level in this function. | |
98 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level); | |
99 return default_zoom_level; | |
100 } | |
101 | |
102 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> | |
103 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( | |
104 const base::Closure& callback) { | |
105 return default_zoom_changed_callbacks_.Add(callback); | |
106 } | |
107 | |
108 void ChromeZoomLevelPrefs::OnZoomLevelChanged( | |
109 const content::HostZoomMap::ZoomLevelChange& change) { | |
110 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | |
111 return; | |
112 double level = change.zoom_level; | |
113 DictionaryPrefUpdate update(pref_service_, | |
114 prefs::kPartitionPerHostZoomLevels); | |
115 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
116 DCHECK(host_zoom_dictionaries); | |
117 | |
118 bool modification_is_removal = | |
119 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()); | |
120 | |
121 base::DictionaryValue* host_zoom_dictionary = nullptr; | |
122 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | |
123 &host_zoom_dictionary)) { | |
124 host_zoom_dictionary = new base::DictionaryValue(); | |
125 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
126 } | |
127 | |
128 if (modification_is_removal) | |
129 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr); | |
130 else | |
131 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | |
132 } | |
133 | |
134 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels( | |
135 const base::DictionaryValue* host_zoom_dictionary) { | |
136 std::vector<std::string> keys_to_remove; | |
137 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); !i.IsAtEnd(); | |
138 i.Advance()) { | |
139 const std::string& host(i.key()); | |
140 double zoom_level = 0; | |
141 | |
142 bool has_valid_zoom_level = i.value().GetAsDouble(&zoom_level); | |
143 | |
144 // Filter out A) the empty host, B) zoom levels equal to the default; and | |
145 // remember them, so that we can later erase them from Prefs. | |
146 // Values of type A and B could have been stored due to crbug.com/364399. | |
147 // Values of type B could further have been stored before the default zoom | |
148 // level was set to its current value. In either case, SetZoomLevelForHost | |
149 // will ignore type B values, thus, to have consistency with HostZoomMap's | |
150 // internal state, these values must also be removed from Prefs. | |
151 if (host.empty() || !has_valid_zoom_level || | |
152 content::ZoomValuesEqual(zoom_level, | |
153 host_zoom_map_->GetDefaultZoomLevel())) { | |
154 keys_to_remove.push_back(host); | |
155 continue; | |
156 } | |
157 | |
158 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); | |
battre
2014/10/10 15:41:08
Note that this may modify the host_zoom_dictionary
wjmaclean
2014/10/10 17:14:10
I think making a copy is the safest way to avoid t
| |
159 } | |
160 | |
161 // Sanitize prefs to remove entries that match the default zoom level and/or | |
162 // have an empty host. | |
163 { | |
164 DictionaryPrefUpdate update(pref_service_, | |
165 prefs::kPartitionPerHostZoomLevels); | |
166 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
167 base::DictionaryValue* host_zoom_dictionary = nullptr; | |
168 host_zoom_dictionaries->GetDictionary(partition_key_, | |
169 &host_zoom_dictionary); | |
170 for (const std::string& s : keys_to_remove) | |
171 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr); | |
172 } | |
173 } | |
174 | |
175 } // namespace chrome | |
OLD | NEW |