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(); | |
gab
2014/10/02 20:58:42
Why explicitly reset it? Doesn't letting it go out
wjmaclean
2014/10/03 18:53:22
You're right ... there may once have been a reason
| |
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_) || | |
gab
2014/10/02 20:58:42
Remove unnecessary surrounding ()
wjmaclean
2014/10/03 18:53:22
Done.
| |
42 profile_path_.IsParent(partition_path)); | |
43 DCHECK(host_zoom_map); | |
44 host_zoom_map_ = host_zoom_map; | |
gab
2014/10/02 20:58:42
DCHECK(!host_zoom_map_);
? (to make sure Init is o
wjmaclean
2014/10/03 18:53:22
Done.
| |
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 | |
58 // Initialize the default zoom level. | |
59 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref()); | |
60 | |
61 // Initialize the HostZoomMap with per-host zoom levels from the persisted | |
62 // zoom-level preference values. | |
63 const base::DictionaryValue* host_zoom_dictionaries = | |
64 pref_service_->GetDictionary(prefs::kPerHostZoomLevels); | |
gab
2014/10/02 20:58:42
I'm confused, so you migrate things out of kProfil
wjmaclean
2014/10/03 18:53:22
Yes, although it's a little more than that.
Prior
| |
65 const base::DictionaryValue* host_zoom_dictionary; | |
66 if (host_zoom_dictionaries && | |
67 host_zoom_dictionaries->GetDictionary(partition_key_, | |
68 &host_zoom_dictionary)) { | |
69 std::vector<std::string> keys_to_remove; | |
70 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); !i.IsAtEnd(); | |
71 i.Advance()) { | |
72 const std::string& host(i.key()); | |
73 double zoom_level = 0; | |
74 | |
75 bool success = i.value().GetAsDouble(&zoom_level); | |
76 DCHECK(success); | |
77 | |
78 if (host.empty() || | |
79 content::ZoomValuesEqual(zoom_level, | |
80 host_zoom_map_->GetDefaultZoomLevel())) { | |
81 keys_to_remove.push_back(host); | |
82 continue; | |
83 } | |
84 | |
85 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); | |
86 } | |
87 | |
88 // Sanitize prefs to remove entries that match the default zoom level and/or | |
89 // have an empty host. | |
90 { | |
91 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); | |
92 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
93 base::DictionaryValue* host_zoom_dictionary; | |
94 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | |
95 &host_zoom_dictionary)) { | |
96 host_zoom_dictionary = new base::DictionaryValue(); | |
97 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
98 } | |
99 | |
100 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); | |
101 it != keys_to_remove.end(); | |
102 ++it) { | |
103 host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); | |
104 } | |
105 } | |
106 } | |
107 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( | |
108 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); | |
109 } | |
110 | |
111 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { | |
112 if (host_zoom_map_->GetDefaultZoomLevel() == level) | |
113 return; | |
114 | |
115 DictionaryPrefUpdate update(pref_service_, prefs::kDefaultZoomLevel); | |
116 update->SetDouble(partition_key_, level); | |
117 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so | |
118 // set this manually. | |
119 host_zoom_map_->SetDefaultZoomLevel(level); | |
120 default_zoom_changed_callbacks_.Notify(); | |
121 } | |
122 | |
123 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() { | |
124 double default_zoom_level = 0.0; | |
125 | |
126 const base::DictionaryValue* default_zoom_level_dictionary = | |
127 pref_service_->GetDictionary(prefs::kDefaultZoomLevel); | |
128 if (!default_zoom_level_dictionary || | |
gab
2014/10/02 20:58:42
PrefService::GetDictionary() never returns NULL.
wjmaclean
2014/10/03 18:53:22
Done.
| |
129 !default_zoom_level_dictionary->GetDouble(partition_key_, | |
130 &default_zoom_level)) { | |
131 // We will only get here if the default zoom level for this partition | |
132 // has never been set before. | |
133 SetDefaultZoomLevelPref(default_zoom_level); | |
134 } | |
135 return default_zoom_level; | |
136 } | |
137 | |
138 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> | |
139 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( | |
140 const base::Closure& callback) { | |
141 return default_zoom_changed_callbacks_.Add(callback); | |
142 } | |
143 | |
144 void ChromeZoomLevelPrefs::OnZoomLevelChanged( | |
145 const content::HostZoomMap::ZoomLevelChange& change) { | |
146 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | |
147 return; | |
148 double level = change.zoom_level; | |
149 // Note: the following will create the dictionary if it doesn't already exist. | |
150 DictionaryPrefUpdate update(pref_service_, prefs::kPerHostZoomLevels); | |
151 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
152 DCHECK(host_zoom_dictionaries); | |
153 | |
154 base::DictionaryValue* host_zoom_dictionary; | |
155 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | |
156 &host_zoom_dictionary)) { | |
157 host_zoom_dictionary = new base::DictionaryValue(); | |
158 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
159 } | |
160 | |
161 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | |
162 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL); | |
163 else | |
164 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | |
165 } | |
166 | |
167 } // namespace chrome | |
OLD | NEW |