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 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(PrefService* pref_service, | |
26 const base::FilePath& profile_path) | |
27 : pref_service_(pref_service), | |
28 profile_path_(profile_path), | |
29 host_zoom_map_(NULL) { | |
30 DCHECK(pref_service_); | |
31 } | |
32 | |
33 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() { | |
34 zoom_subscription_.reset(); | |
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 // 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 size_t int_key = | |
51 #if defined(COMPILER_MSVC) | |
52 BASE_HASH_NAMESPACE::hash_value(partition_relative_path); | |
53 #else | |
54 BASE_HASH_NAMESPACE::hash<base::FilePath>()(partition_relative_path); | |
55 #endif // COMPILER | |
56 partition_key_ = base::IntToString(int_key); | |
57 LOG(WARNING) << "wjm: Hash = " << partition_key_; | |
Alexei Svitkine (slow)
2014/10/02 20:40:20
You probably want to remove this.
wjmaclean
2014/10/03 18:53:23
He he ... you inadvertently got an experimental ve
| |
58 | |
59 // Initialize the default zoom level. | |
60 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref()); | |
61 | |
62 // Initialize the HostZoomMap with per-host zoom levels from the persisted | |
63 // zoom-level preference values. | |
64 const base::DictionaryValue* host_zoom_dictionaries = | |
65 pref_service_->GetDictionary(prefs::kPerHostZoomLevels); | |
66 const base::DictionaryValue* host_zoom_dictionary; | |
67 if (host_zoom_dictionaries && | |
68 host_zoom_dictionaries->GetDictionary(partition_key_, | |
69 &host_zoom_dictionary)) { | |
70 std::vector<std::string> keys_to_remove; | |
71 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); !i.IsAtEnd(); | |
72 i.Advance()) { | |
73 const std::string& host(i.key()); | |
74 double zoom_level = 0; | |
75 | |
76 bool success = i.value().GetAsDouble(&zoom_level); | |
77 DCHECK(success); | |
78 | |
79 if (host.empty() || | |
80 content::ZoomValuesEqual(zoom_level, | |
81 host_zoom_map_->GetDefaultZoomLevel())) { | |
82 keys_to_remove.push_back(host); | |
83 continue; | |
84 } | |
85 | |
86 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); | |
87 } | |
88 | |
89 // Sanitize prefs to remove entries that match the default zoom level and/or | |
90 // have an empty host. | |
91 { | |
92 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); | |
93 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
94 base::DictionaryValue* host_zoom_dictionary; | |
95 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | |
96 &host_zoom_dictionary)) { | |
97 host_zoom_dictionary = new base::DictionaryValue(); | |
98 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
99 } | |
100 | |
101 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); | |
102 it != keys_to_remove.end(); | |
103 ++it) { | |
104 host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); | |
105 } | |
106 } | |
107 } | |
108 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( | |
109 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); | |
110 } | |
111 | |
112 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { | |
113 if (host_zoom_map_->GetDefaultZoomLevel() == level) | |
114 return; | |
115 | |
116 DictionaryPrefUpdate update(pref_service_, prefs::kDefaultZoomLevel); | |
117 update->SetDouble(partition_key_, level); | |
118 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so | |
119 // set this manually. | |
120 host_zoom_map_->SetDefaultZoomLevel(level); | |
121 default_zoom_changed_callbacks_.Notify(); | |
122 } | |
123 | |
124 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() { | |
125 double default_zoom_level = 0.0; | |
126 | |
127 const base::DictionaryValue* default_zoom_level_dictionary = | |
128 pref_service_->GetDictionary(prefs::kDefaultZoomLevel); | |
129 if (!default_zoom_level_dictionary || | |
130 !default_zoom_level_dictionary->GetDouble(partition_key_, | |
131 &default_zoom_level)) { | |
132 // We will only get here if the default zoom level for this partition | |
133 // has never been set before. | |
134 SetDefaultZoomLevelPref(default_zoom_level); | |
135 } | |
136 return default_zoom_level; | |
137 } | |
138 | |
139 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> | |
140 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( | |
141 const base::Closure& callback) { | |
142 return default_zoom_changed_callbacks_.Add(callback); | |
143 } | |
144 | |
145 void ChromeZoomLevelPrefs::OnZoomLevelChanged( | |
146 const content::HostZoomMap::ZoomLevelChange& change) { | |
147 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | |
148 return; | |
149 double level = change.zoom_level; | |
150 // Note: the following will create the dictionary if it doesn't already exist. | |
151 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); | |
152 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
153 DCHECK(host_zoom_dictionaries); | |
154 | |
155 base::DictionaryValue* host_zoom_dictionary; | |
156 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | |
157 &host_zoom_dictionary)) { | |
158 host_zoom_dictionary = new base::DictionaryValue(); | |
159 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
160 } | |
161 | |
162 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | |
163 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL); | |
164 else | |
165 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | |
166 } | |
167 | |
168 } // namespace chrome | |
OLD | NEW |