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"
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"
25 const base::FilePath
& relative_path
) {
27 BASE_HASH_NAMESPACE::hash
<base::FilePath
>()(relative_path
);
28 return base::SizeTToString(int_key
);
35 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(
36 PrefService
* pref_service
,
37 const base::FilePath
& profile_path
,
38 const base::FilePath
& partition_path
,
39 base::WeakPtr
<ui_zoom::ZoomEventManager
> zoom_event_manager
)
40 : pref_service_(pref_service
),
41 zoom_event_manager_(zoom_event_manager
),
42 host_zoom_map_(nullptr) {
43 DCHECK(pref_service_
);
45 DCHECK(!partition_path
.empty());
46 DCHECK((partition_path
== profile_path
) ||
47 profile_path
.IsParent(partition_path
));
48 // Create a partition_key string with no '.'s in it. For the default
49 // StoragePartition, this string will always be "0".
50 base::FilePath partition_relative_path
;
51 profile_path
.AppendRelativePath(partition_path
, &partition_relative_path
);
52 partition_key_
= GetHash(partition_relative_path
);
56 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() {
59 std::string
ChromeZoomLevelPrefs::GetHashForTesting(
60 const base::FilePath
& relative_path
) {
61 return GetHash(relative_path
);
64 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level
) {
65 if (content::ZoomValuesEqual(level
, host_zoom_map_
->GetDefaultZoomLevel()))
68 DictionaryPrefUpdate
update(pref_service_
, prefs::kPartitionDefaultZoomLevel
);
69 update
->SetDouble(partition_key_
, level
);
70 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so
72 host_zoom_map_
->SetDefaultZoomLevel(level
);
73 default_zoom_changed_callbacks_
.Notify();
74 if (zoom_event_manager_
)
75 zoom_event_manager_
->OnDefaultZoomLevelChanged();
78 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const {
79 double default_zoom_level
= 0.0;
81 const base::DictionaryValue
* default_zoom_level_dictionary
=
82 pref_service_
->GetDictionary(prefs::kPartitionDefaultZoomLevel
);
83 // If no default has been previously set, the default returned is the
84 // value used to initialize default_zoom_level in this function.
85 default_zoom_level_dictionary
->GetDouble(partition_key_
, &default_zoom_level
);
86 return default_zoom_level
;
89 scoped_ptr
<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription
>
90 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback(
91 const base::Closure
& callback
) {
92 return default_zoom_changed_callbacks_
.Add(callback
);
95 void ChromeZoomLevelPrefs::OnZoomLevelChanged(
96 const content::HostZoomMap::ZoomLevelChange
& change
) {
97 // If there's a manager to aggregate ZoomLevelChanged events, pass this event
98 // along. Since we already hold a subscription to our associated HostZoomMap,
99 // we don't need to create a separate subscription for this.
100 if (zoom_event_manager_
)
101 zoom_event_manager_
->OnZoomLevelChanged(change
);
103 if (change
.mode
!= content::HostZoomMap::ZOOM_CHANGED_FOR_HOST
)
105 double level
= change
.zoom_level
;
106 DictionaryPrefUpdate
update(pref_service_
,
107 prefs::kPartitionPerHostZoomLevels
);
108 base::DictionaryValue
* host_zoom_dictionaries
= update
.Get();
109 DCHECK(host_zoom_dictionaries
);
111 bool modification_is_removal
=
112 content::ZoomValuesEqual(level
, host_zoom_map_
->GetDefaultZoomLevel());
114 base::DictionaryValue
* host_zoom_dictionary
= nullptr;
115 if (!host_zoom_dictionaries
->GetDictionary(partition_key_
,
116 &host_zoom_dictionary
)) {
117 host_zoom_dictionary
= new base::DictionaryValue();
118 host_zoom_dictionaries
->Set(partition_key_
, host_zoom_dictionary
);
121 if (modification_is_removal
)
122 host_zoom_dictionary
->RemoveWithoutPathExpansion(change
.host
, nullptr);
124 host_zoom_dictionary
->SetDoubleWithoutPathExpansion(change
.host
, level
);
127 // TODO(wjmaclean): Remove the dictionary_path once the migration code is
128 // removed. crbug.com/420643
129 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels(
130 const base::DictionaryValue
* host_zoom_dictionary
,
131 bool sanitize_partition_host_zoom_levels
) {
132 std::vector
<std::string
> keys_to_remove
;
133 scoped_ptr
<base::DictionaryValue
> host_zoom_dictionary_copy(
134 host_zoom_dictionary
->DeepCopyWithoutEmptyChildren());
135 for (base::DictionaryValue::Iterator
i(*host_zoom_dictionary_copy
);
138 const std::string
& host(i
.key());
139 double zoom_level
= 0;
141 bool has_valid_zoom_level
= i
.value().GetAsDouble(&zoom_level
);
143 // Filter out A) the empty host, B) zoom levels equal to the default; and
144 // remember them, so that we can later erase them from Prefs.
145 // Values of type A and B could have been stored due to crbug.com/364399.
146 // Values of type B could further have been stored before the default zoom
147 // level was set to its current value. In either case, SetZoomLevelForHost
148 // will ignore type B values, thus, to have consistency with HostZoomMap's
149 // internal state, these values must also be removed from Prefs.
150 if (host
.empty() || !has_valid_zoom_level
||
151 content::ZoomValuesEqual(zoom_level
,
152 host_zoom_map_
->GetDefaultZoomLevel())) {
153 keys_to_remove
.push_back(host
);
157 host_zoom_map_
->SetZoomLevelForHost(host
, zoom_level
);
160 // We don't bother sanitizing non-partition dictionaries as they will be
161 // discarded in the migration process. Note: since the structure of partition
162 // per-host zoom level dictionaries is different from the legacy profile
163 // per-host zoom level dictionaries, the following code will fail if run
164 // on the legacy dictionaries.
165 if (!sanitize_partition_host_zoom_levels
)
168 // Sanitize prefs to remove entries that match the default zoom level and/or
169 // have an empty host.
171 DictionaryPrefUpdate
update(pref_service_
,
172 prefs::kPartitionPerHostZoomLevels
);
173 base::DictionaryValue
* host_zoom_dictionaries
= update
.Get();
174 base::DictionaryValue
* host_zoom_dictionary
= nullptr;
175 host_zoom_dictionaries
->GetDictionary(partition_key_
,
176 &host_zoom_dictionary
);
177 for (const std::string
& s
: keys_to_remove
)
178 host_zoom_dictionary
->RemoveWithoutPathExpansion(s
, nullptr);
182 void ChromeZoomLevelPrefs::InitHostZoomMap(
183 content::HostZoomMap
* host_zoom_map
) {
184 // This init function must be called only once.
185 DCHECK(!host_zoom_map_
);
186 DCHECK(host_zoom_map
);
187 host_zoom_map_
= host_zoom_map
;
189 // Initialize the default zoom level.
190 host_zoom_map_
->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
192 // Initialize the HostZoomMap with per-host zoom levels from the persisted
193 // zoom-level preference values.
194 const base::DictionaryValue
* host_zoom_dictionaries
=
195 pref_service_
->GetDictionary(prefs::kPartitionPerHostZoomLevels
);
196 const base::DictionaryValue
* host_zoom_dictionary
= nullptr;
197 if (host_zoom_dictionaries
->GetDictionary(partition_key_
,
198 &host_zoom_dictionary
)) {
199 // Since we're calling this before setting up zoom_subscription_ below we
200 // don't need to worry that host_zoom_dictionary is indirectly affected
201 // by calls to HostZoomMap::SetZoomLevelForHost().
202 ExtractPerHostZoomLevels(host_zoom_dictionary
,
203 true /* sanitize_partition_host_zoom_levels */);
205 zoom_subscription_
= host_zoom_map_
->AddZoomLevelChangedCallback(base::Bind(
206 &ChromeZoomLevelPrefs::OnZoomLevelChanged
, base::Unretained(this)));
209 } // namespace chrome