OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" | 5 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/ui/zoom/chrome_zoom_prefs_helper.h" |
12 #include "chrome/common/chrome_constants.h" | 12 #include "chrome/common/chrome_constants.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "components/prefs/json_pref_store.h" | |
15 #include "components/prefs/pref_filter.h" | |
16 #include "components/prefs/pref_registry_simple.h" | |
17 #include "components/prefs/pref_service_factory.h" | |
18 #include "components/prefs/scoped_user_pref_update.h" | 14 #include "components/prefs/scoped_user_pref_update.h" |
19 #include "components/zoom/zoom_event_manager.h" | 15 #include "components/zoom/zoom_event_manager.h" |
20 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/host_zoom_map.h" | 17 #include "content/public/browser/host_zoom_map.h" |
22 #include "content/public/common/page_zoom.h" | 18 #include "content/public/common/page_zoom.h" |
23 | 19 |
24 namespace { | |
25 | |
26 std::string GetHash(const base::FilePath& relative_path) { | |
27 size_t int_key = BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path); | |
28 return base::SizeTToString(int_key); | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs( | 20 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs( |
34 PrefService* pref_service, | 21 PrefService* pref_service, |
35 const base::FilePath& profile_path, | 22 const base::FilePath& profile_path, |
36 const base::FilePath& partition_path, | 23 const base::FilePath& partition_path, |
37 base::WeakPtr<zoom::ZoomEventManager> zoom_event_manager) | 24 base::WeakPtr<zoom::ZoomEventManager> zoom_event_manager) |
38 : pref_service_(pref_service), | 25 : partitioned_prefs_(pref_service, profile_path, partition_path), |
39 zoom_event_manager_(zoom_event_manager), | 26 zoom_event_manager_(zoom_event_manager), |
40 host_zoom_map_(nullptr) { | 27 host_zoom_map_(nullptr) {} |
41 DCHECK(pref_service_); | |
42 | |
43 DCHECK(!partition_path.empty()); | |
44 DCHECK((partition_path == profile_path) || | |
45 profile_path.IsParent(partition_path)); | |
46 // Create a partition_key string with no '.'s in it. For the default | |
47 // StoragePartition, this string will always be "0". | |
48 base::FilePath partition_relative_path; | |
49 profile_path.AppendRelativePath(partition_path, &partition_relative_path); | |
50 partition_key_ = GetHash(partition_relative_path); | |
51 | |
52 } | |
53 | 28 |
54 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() { | 29 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() { |
55 } | 30 } |
56 | 31 |
57 std::string ChromeZoomLevelPrefs::GetHashForTesting( | 32 std::string ChromeZoomLevelPrefs::GetHashForTesting( |
58 const base::FilePath& relative_path) { | 33 const base::FilePath& relative_path) { |
59 return GetHash(relative_path); | 34 return ChromeZoomPrefsHelper::GetHashForTesting(relative_path); |
60 } | 35 } |
61 | 36 |
62 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { | 37 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { |
63 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | 38 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) |
64 return; | 39 return; |
65 | 40 |
66 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel); | 41 partitioned_prefs_.SetDouble(prefs::kPartitionDefaultZoomLevel, level); |
67 update->SetDouble(partition_key_, level); | |
68 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so | 42 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so |
69 // set this manually. | 43 // set this manually. |
70 host_zoom_map_->SetDefaultZoomLevel(level); | 44 host_zoom_map_->SetDefaultZoomLevel(level); |
71 default_zoom_changed_callbacks_.Notify(); | 45 default_zoom_changed_callbacks_.Notify(); |
72 if (zoom_event_manager_) | 46 if (zoom_event_manager_) |
73 zoom_event_manager_->OnDefaultZoomLevelChanged(); | 47 zoom_event_manager_->OnDefaultZoomLevelChanged(); |
74 } | 48 } |
75 | 49 |
76 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const { | 50 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const { |
77 double default_zoom_level = 0.0; | 51 double default_zoom_level = 0.0; |
78 | 52 |
79 const base::DictionaryValue* default_zoom_level_dictionary = | |
80 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel); | |
81 // If no default has been previously set, the default returned is the | 53 // If no default has been previously set, the default returned is the |
82 // value used to initialize default_zoom_level in this function. | 54 // value used to initialize default_zoom_level in this function. |
83 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level); | 55 partitioned_prefs_.GetDouble(prefs::kPartitionDefaultZoomLevel, |
| 56 &default_zoom_level); |
84 return default_zoom_level; | 57 return default_zoom_level; |
85 } | 58 } |
86 | 59 |
87 std::unique_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> | 60 std::unique_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> |
88 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( | 61 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( |
89 const base::Closure& callback) { | 62 const base::Closure& callback) { |
90 return default_zoom_changed_callbacks_.Add(callback); | 63 return default_zoom_changed_callbacks_.Add(callback); |
91 } | 64 } |
92 | 65 |
| 66 void ChromeZoomLevelPrefs::SetZoomScopeIsPerOriginPref(bool is_per_origin) { |
| 67 if (is_per_origin == GetZoomScopeIsPerOriginPref()) |
| 68 return; |
| 69 |
| 70 partitioned_prefs_.SetBoolean(prefs::kPartitionZoomScopeIsPerOrigin, |
| 71 is_per_origin); |
| 72 default_scope_changed_callbacks_.Notify(); |
| 73 } |
| 74 |
| 75 bool ChromeZoomLevelPrefs::GetZoomScopeIsPerOriginPref() const { |
| 76 bool is_per_origin = kZoomScopeSettingDefault; |
| 77 |
| 78 // If no value has been previously set, |is_per_origin| will be untouched |
| 79 // from the default value we've set above. |
| 80 partitioned_prefs_.GetBoolean(prefs::kPartitionZoomScopeIsPerOrigin, |
| 81 &is_per_origin); |
| 82 return is_per_origin; |
| 83 } |
| 84 |
| 85 std::unique_ptr<ChromeZoomLevelPrefs::DefaultZoomScopeSubscription> |
| 86 ChromeZoomLevelPrefs::RegisterDefaultZoomScopeCallback( |
| 87 const base::Closure& callback) { |
| 88 return default_scope_changed_callbacks_.Add(callback); |
| 89 } |
| 90 |
93 void ChromeZoomLevelPrefs::OnZoomLevelChanged( | 91 void ChromeZoomLevelPrefs::OnZoomLevelChanged( |
94 const content::HostZoomMap::ZoomLevelChange& change) { | 92 const content::HostZoomMap::ZoomLevelChange& change) { |
95 // If there's a manager to aggregate ZoomLevelChanged events, pass this event | 93 // If there's a manager to aggregate ZoomLevelChanged events, pass this event |
96 // along. Since we already hold a subscription to our associated HostZoomMap, | 94 // along. Since we already hold a subscription to our associated HostZoomMap, |
97 // we don't need to create a separate subscription for this. | 95 // we don't need to create a separate subscription for this. |
98 if (zoom_event_manager_) | 96 if (zoom_event_manager_) |
99 zoom_event_manager_->OnZoomLevelChanged(change); | 97 zoom_event_manager_->OnZoomLevelChanged(change); |
100 | 98 |
101 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | 99 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) |
102 return; | 100 return; |
103 double level = change.zoom_level; | 101 double level = change.zoom_level; |
104 DictionaryPrefUpdate update(pref_service_, | 102 std::unique_ptr<DictionaryPrefUpdate> update( |
105 prefs::kPartitionPerHostZoomLevels); | 103 partitioned_prefs_.GetUpdate(prefs::kPartitionPerHostZoomLevels)); |
106 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
107 DCHECK(host_zoom_dictionaries); | |
108 | 104 |
109 bool modification_is_removal = | 105 bool modification_is_removal = |
110 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()); | 106 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()); |
111 | 107 |
112 base::DictionaryValue* host_zoom_dictionary = nullptr; | 108 base::DictionaryValue* host_zoom_dictionary( |
113 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | 109 partitioned_prefs_.GetMutableDictionary(update.get())); |
114 &host_zoom_dictionary)) { | |
115 host_zoom_dictionary = new base::DictionaryValue(); | |
116 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
117 } | |
118 | 110 |
119 if (modification_is_removal) | 111 if (modification_is_removal) |
120 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr); | 112 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr); |
121 else | 113 else |
122 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | 114 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); |
123 } | 115 } |
124 | 116 |
125 // TODO(wjmaclean): Remove the dictionary_path once the migration code is | 117 // TODO(wjmaclean): Remove the dictionary_path once the migration code is |
126 // removed. crbug.com/420643 | 118 // removed. crbug.com/420643 |
127 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels( | 119 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 // discarded in the migration process. Note: since the structure of partition | 151 // discarded in the migration process. Note: since the structure of partition |
160 // per-host zoom level dictionaries is different from the legacy profile | 152 // per-host zoom level dictionaries is different from the legacy profile |
161 // per-host zoom level dictionaries, the following code will fail if run | 153 // per-host zoom level dictionaries, the following code will fail if run |
162 // on the legacy dictionaries. | 154 // on the legacy dictionaries. |
163 if (!sanitize_partition_host_zoom_levels) | 155 if (!sanitize_partition_host_zoom_levels) |
164 return; | 156 return; |
165 | 157 |
166 // Sanitize prefs to remove entries that match the default zoom level and/or | 158 // Sanitize prefs to remove entries that match the default zoom level and/or |
167 // have an empty host. | 159 // have an empty host. |
168 { | 160 { |
169 DictionaryPrefUpdate update(pref_service_, | 161 std::unique_ptr<DictionaryPrefUpdate> update( |
170 prefs::kPartitionPerHostZoomLevels); | 162 partitioned_prefs_.GetUpdate(prefs::kPartitionPerHostZoomLevels)); |
171 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | 163 base::DictionaryValue* host_zoom_dictionary( |
172 base::DictionaryValue* host_zoom_dictionary = nullptr; | 164 partitioned_prefs_.GetMutableDictionary(update.get())); |
173 host_zoom_dictionaries->GetDictionary(partition_key_, | |
174 &host_zoom_dictionary); | |
175 for (const std::string& s : keys_to_remove) | 165 for (const std::string& s : keys_to_remove) |
176 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr); | 166 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr); |
177 } | 167 } |
178 } | 168 } |
179 | 169 |
180 void ChromeZoomLevelPrefs::InitHostZoomMap( | 170 void ChromeZoomLevelPrefs::InitHostZoomMap( |
181 content::HostZoomMap* host_zoom_map) { | 171 content::HostZoomMap* host_zoom_map) { |
182 // This init function must be called only once. | 172 // This init function must be called only once. |
183 DCHECK(!host_zoom_map_); | 173 DCHECK(!host_zoom_map_); |
184 DCHECK(host_zoom_map); | 174 DCHECK(host_zoom_map); |
185 host_zoom_map_ = host_zoom_map; | 175 host_zoom_map_ = host_zoom_map; |
186 | 176 |
187 // Initialize the default zoom level. | 177 // Initialize the default zoom level. |
188 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref()); | 178 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref()); |
189 | 179 |
190 // Initialize the HostZoomMap with per-host zoom levels from the persisted | 180 // Initialize the HostZoomMap with per-host zoom levels from the persisted |
191 // zoom-level preference values. | 181 // zoom-level preference values. |
192 const base::DictionaryValue* host_zoom_dictionaries = | |
193 pref_service_->GetDictionary(prefs::kPartitionPerHostZoomLevels); | |
194 const base::DictionaryValue* host_zoom_dictionary = nullptr; | 182 const base::DictionaryValue* host_zoom_dictionary = nullptr; |
195 if (host_zoom_dictionaries->GetDictionary(partition_key_, | 183 if (partitioned_prefs_.GetDictionary(prefs::kPartitionPerHostZoomLevels, |
196 &host_zoom_dictionary)) { | 184 &host_zoom_dictionary)) { |
197 // Since we're calling this before setting up zoom_subscription_ below we | 185 // Since we're calling this before setting up zoom_subscription_ below we |
198 // don't need to worry that host_zoom_dictionary is indirectly affected | 186 // don't need to worry that host_zoom_dictionary is indirectly affected |
199 // by calls to HostZoomMap::SetZoomLevelForHost(). | 187 // by calls to HostZoomMap::SetZoomLevelForHost(). |
200 ExtractPerHostZoomLevels(host_zoom_dictionary, | 188 ExtractPerHostZoomLevels(host_zoom_dictionary, |
201 true /* sanitize_partition_host_zoom_levels */); | 189 true /* sanitize_partition_host_zoom_levels */); |
202 } | 190 } |
203 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( | 191 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( |
204 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); | 192 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); |
205 } | 193 } |
OLD | NEW |