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/values.h" | |
16 #include "chrome/common/chrome_constants.h" | |
17 #include "chrome/common/pref_names.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" | |
21 | |
22 namespace chrome { | |
23 | |
24 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(PrefService* pref_service, | |
25 const base::FilePath& profile_path) | |
26 : pref_service_(pref_service), | |
27 profile_path_(profile_path), | |
28 host_zoom_map_(NULL) { | |
29 DCHECK(pref_service_); | |
30 } | |
31 | |
32 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() { | |
33 zoom_subscription_.reset(); | |
34 pref_change_registrar_.RemoveAll(); | |
35 } | |
36 | |
37 void ChromeZoomLevelPrefs::InitPrefsAndCopyToHostZoomMap( | |
38 const base::FilePath& partition_path, | |
39 content::HostZoomMap* host_zoom_map) { | |
40 DCHECK(!partition_path.empty()); | |
41 DCHECK((partition_path == profile_path_) || | |
42 profile_path_.IsParent(partition_path)); | |
43 DCHECK(host_zoom_map); | |
44 host_zoom_map_ = host_zoom_map; | |
45 | |
46 pref_change_registrar_.Init(pref_service_); | |
47 | |
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 size_t int_key = | |
53 BASE_HASH_NAMESPACE::hash<base::FilePath>()(partition_relative_path); | |
54 std::stringstream key_stream; | |
55 key_stream << int_key; | |
Bernhard Bauer
2014/10/01 15:09:14
I don't think we use streams.
Could you use base:
wjmaclean
2014/10/01 20:33:00
Done.
| |
56 partition_key_ = key_stream.str(); | |
57 default_zoom_level_path_ = prefs::kDefaultZoomLevel; | |
58 default_zoom_level_path_ += "." + partition_key_; | |
59 | |
60 // Initialize the default zoom level. | |
61 double default_zoom_level = 0.0; | |
62 const base::DictionaryValue* default_zoom_level_dictionary = | |
63 pref_service_->GetDictionary(prefs::kDefaultZoomLevel); | |
64 if (!default_zoom_level_dictionary || | |
65 !default_zoom_level_dictionary->GetDouble(partition_key_, | |
66 &default_zoom_level)) { | |
67 // We will only get here if the default zoom level for this partition | |
68 // has never been set before. | |
69 SetDefaultZoomLevel(default_zoom_level); | |
70 } | |
71 host_zoom_map_->SetDefaultZoomLevel(default_zoom_level); | |
72 | |
73 // Only sign up for notifications if the default zoom level for this | |
74 // partition has been registered. | |
75 if (pref_service_->FindPreference(default_zoom_level_path_.c_str())) { | |
76 pref_change_registrar_.Add( | |
77 default_zoom_level_path_.c_str(), | |
78 base::Bind(&ChromeZoomLevelPrefs::OnDefaultZoomLevelChanged, | |
79 base::Unretained(this))); | |
80 } | |
81 | |
82 // Initialize the HostZoomMap with per-host zoom levels from the persisted | |
83 // zoom-level preference values. | |
84 const base::DictionaryValue* host_zoom_dictionaries = | |
85 pref_service_->GetDictionary(prefs::kPerHostZoomLevels); | |
86 const base::DictionaryValue* host_zoom_dictionary; | |
87 if (host_zoom_dictionaries && | |
88 host_zoom_dictionaries->GetDictionary(partition_key_, | |
89 &host_zoom_dictionary)) { | |
90 std::vector<std::string> keys_to_remove; | |
91 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); !i.IsAtEnd(); | |
92 i.Advance()) { | |
93 const std::string& host(i.key()); | |
94 double zoom_level = 0; | |
95 | |
96 bool success = i.value().GetAsDouble(&zoom_level); | |
97 DCHECK(success); | |
98 | |
99 if (host.empty() || | |
100 content::ZoomValuesEqual(zoom_level, | |
101 host_zoom_map_->GetDefaultZoomLevel())) { | |
102 keys_to_remove.push_back(host); | |
103 continue; | |
104 } | |
105 | |
106 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); | |
107 } | |
108 | |
109 // Sanitize prefs to remove entries that match the default zoom level and/or | |
110 // have an empty host. | |
111 { | |
112 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); | |
113 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
114 base::DictionaryValue* host_zoom_dictionary; | |
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); | |
119 } | |
120 | |
121 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); | |
122 it != keys_to_remove.end(); | |
123 ++it) { | |
124 host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); | |
125 } | |
126 } | |
127 } | |
128 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( | |
129 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); | |
130 } | |
131 | |
132 void ChromeZoomLevelPrefs::OnDefaultZoomLevelChanged(const std::string& key) { | |
133 // Push changes to the default zoom level to the HostZoomMap; all | |
134 // other consumers will get it from there. | |
135 if (key == default_zoom_level_path_) { | |
136 host_zoom_map_->SetDefaultZoomLevel( | |
137 pref_service_->GetDouble(default_zoom_level_path_.c_str())); | |
138 } | |
139 } | |
140 | |
141 void ChromeZoomLevelPrefs::SetDefaultZoomLevel(double level) { | |
142 if (pref_service_->FindPreference(default_zoom_level_path_.c_str())) { | |
143 pref_service_->SetDouble(default_zoom_level_path_.c_str(), level); | |
144 } else { | |
145 DictionaryPrefUpdate update(pref_service_, prefs::kDefaultZoomLevel); | |
146 update->SetDouble(partition_key_, level); | |
147 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so | |
148 // set this manually. | |
149 host_zoom_map_->SetDefaultZoomLevel(level); | |
150 } | |
151 } | |
152 | |
153 void ChromeZoomLevelPrefs::OnZoomLevelChanged( | |
154 const content::HostZoomMap::ZoomLevelChange& change) { | |
155 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | |
156 return; | |
157 double level = change.zoom_level; | |
158 // Note: the following will create the dictionary if it doesn't already exist. | |
159 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); | |
160 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
161 DCHECK(host_zoom_dictionaries); | |
162 | |
163 base::DictionaryValue* host_zoom_dictionary; | |
164 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | |
165 &host_zoom_dictionary)) { | |
166 host_zoom_dictionary = new base::DictionaryValue(); | |
167 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
168 } | |
169 | |
170 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | |
171 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL); | |
172 else | |
173 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | |
174 } | |
175 | |
176 } // namespace chrome | |
OLD | NEW |