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"
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"
26 const base::FilePath
& relative_path
) {
28 #if defined(COMPILER_MSVC)
29 BASE_HASH_NAMESPACE::hash_value(relative_path
);
31 BASE_HASH_NAMESPACE::hash
<base::FilePath
>()(relative_path
);
33 return base::SizeTToString(int_key
);
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_
);
48 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() {
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
;
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
);
68 // Initialize the default zoom level.
69 host_zoom_map_
->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
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
);
83 zoom_subscription_
= host_zoom_map_
->AddZoomLevelChangedCallback(base::Bind(
84 &ChromeZoomLevelPrefs::OnZoomLevelChanged
, base::Unretained(this)));
87 std::string
ChromeZoomLevelPrefs::GetHashForTesting(
88 const base::FilePath
& relative_path
) {
89 return GetHash(relative_path
);
92 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level
) {
93 if (content::ZoomValuesEqual(level
, host_zoom_map_
->GetDefaultZoomLevel()))
96 DictionaryPrefUpdate
update(pref_service_
, prefs::kPartitionDefaultZoomLevel
);
97 update
->SetDouble(partition_key_
, level
);
98 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so
100 host_zoom_map_
->SetDefaultZoomLevel(level
);
101 default_zoom_changed_callbacks_
.Notify();
104 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const {
105 double default_zoom_level
= 0.0;
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
;
115 scoped_ptr
<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription
>
116 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback(
117 const base::Closure
& callback
) {
118 return default_zoom_changed_callbacks_
.Add(callback
);
121 void ChromeZoomLevelPrefs::OnZoomLevelChanged(
122 const content::HostZoomMap::ZoomLevelChange
& change
) {
123 if (change
.mode
!= content::HostZoomMap::ZOOM_CHANGED_FOR_HOST
)
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
);
131 bool modification_is_removal
=
132 content::ZoomValuesEqual(level
, host_zoom_map_
->GetDefaultZoomLevel());
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
);
141 if (modification_is_removal
)
142 host_zoom_dictionary
->RemoveWithoutPathExpansion(change
.host
, nullptr);
144 host_zoom_dictionary
->SetDoubleWithoutPathExpansion(change
.host
, level
);
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
);
155 const std::string
& host(i
.key());
156 double zoom_level
= 0;
158 bool has_valid_zoom_level
= i
.value().GetAsDouble(&zoom_level
);
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
);
174 host_zoom_map_
->SetZoomLevelForHost(host
, zoom_level
);
177 // Sanitize prefs to remove entries that match the default zoom level and/or
178 // have an empty host.
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);
191 } // namespace chrome