Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: chrome/browser/ui/zoom/chrome_zoom_level_prefs.cc

Issue 541103002: Introduce ChromeZoomLevelPref, make zoom level prefs independent of profile prefs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 std::string ChromeZoomLevelPrefs::GetHash(const base::FilePath& relative_path) {
26 size_t int_key =
27 #if defined(COMPILER_MSVC)
28 BASE_HASH_NAMESPACE::hash_value(relative_path);
29 #else
30 BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path);
31 #endif // COMPILER
32 return base::SizeTToString(int_key);
33 }
34
35 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs(PrefService* pref_service,
36 const base::FilePath& profile_path)
37 : pref_service_(pref_service),
38 profile_path_(profile_path),
39 host_zoom_map_(NULL) {
40 DCHECK(pref_service_);
41 }
42
43 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() {
44 }
45
46 void ChromeZoomLevelPrefs::InitPrefsAndCopyToHostZoomMap(
47 const base::FilePath& partition_path,
48 content::HostZoomMap* host_zoom_map) {
49 DCHECK(!partition_path.empty());
50 DCHECK((partition_path == profile_path_) ||
51 profile_path_.IsParent(partition_path));
52 // This init function must be called only once.
53 DCHECK(!host_zoom_map_);
54 DCHECK(host_zoom_map);
55 host_zoom_map_ = host_zoom_map;
56
57 // Create a partition_key string with no '.'s in it. For the default
58 // StoragePartition, this string will always be "0".
59 base::FilePath partition_relative_path;
60 profile_path_.AppendRelativePath(partition_path, &partition_relative_path);
61 partition_key_ = GetHash(partition_relative_path);
62
63 // Initialize the default zoom level.
64 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
65
66 // Initialize the HostZoomMap with per-host zoom levels from the persisted
67 // zoom-level preference values.
68 const base::DictionaryValue* host_zoom_dictionaries =
69 pref_service_->GetDictionary(prefs::kPartitionPerHostZoomLevels);
70 const base::DictionaryValue* host_zoom_dictionary = NULL;
71 if (host_zoom_dictionaries->GetDictionary(partition_key_,
72 &host_zoom_dictionary)) {
73 std::vector<std::string> keys_to_remove;
74 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); !i.IsAtEnd();
75 i.Advance()) {
76 const std::string& host(i.key());
77 double zoom_level = 0;
78
79 bool has_valid_zoom_level = i.value().GetAsDouble(&zoom_level);
80
81 if (host.empty() || !has_valid_zoom_level ||
82 content::ZoomValuesEqual(zoom_level,
83 host_zoom_map_->GetDefaultZoomLevel())) {
84 keys_to_remove.push_back(host);
85 continue;
86 }
87
88 host_zoom_map_->SetZoomLevelForHost(host, zoom_level);
89 }
90
91 // Sanitize prefs to remove entries that match the default zoom level and/or
92 // have an empty host.
93 {
94 DictionaryPrefUpdate update(pref_service_,
95 prefs::kPartitionPerHostZoomLevels);
96 base::DictionaryValue* host_zoom_dictionaries = update.Get();
97 base::DictionaryValue* host_zoom_dictionary = NULL;
98 host_zoom_dictionaries->GetDictionary(partition_key_,
99 &host_zoom_dictionary);
100 for (std::string s : keys_to_remove)
battre 2014/10/08 11:27:10 for (const std::string& s : keys_to_remove)
wjmaclean 2014/10/10 14:40:29 Done.
101 host_zoom_dictionary->RemoveWithoutPathExpansion(s, NULL);
102 }
103 }
104 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
105 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this)));
106 }
107
108 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) {
109 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()))
110 return;
111
112 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel);
113 update->SetDouble(partition_key_, level);
114 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so
115 // set this manually.
116 host_zoom_map_->SetDefaultZoomLevel(level);
117 default_zoom_changed_callbacks_.Notify();
118 }
119
120 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const {
121 double default_zoom_level = 0.0;
122
123 const base::DictionaryValue* default_zoom_level_dictionary =
124 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel);
125 // If no default has been previously set, the default returned is the
126 // value used to initialize default_zoom_level in this function.
127 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level);
128 return default_zoom_level;
129 }
130
131 scoped_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription>
132 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback(
133 const base::Closure& callback) {
134 return default_zoom_changed_callbacks_.Add(callback);
135 }
136
137 void ChromeZoomLevelPrefs::OnZoomLevelChanged(
138 const content::HostZoomMap::ZoomLevelChange& change) {
139 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST)
140 return;
141 double level = change.zoom_level;
142 DictionaryPrefUpdate update(pref_service_,
143 prefs::kPartitionPerHostZoomLevels);
144 base::DictionaryValue* host_zoom_dictionaries = update.Get();
145 DCHECK(host_zoom_dictionaries);
146
147 bool modification_is_removal =
wjmaclean 2014/10/06 19:24:07 I'd like to use this value as a key to when to exp
148 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel());
149
150 base::DictionaryValue* host_zoom_dictionary = NULL;
151 if (!host_zoom_dictionaries->GetDictionary(partition_key_,
152 &host_zoom_dictionary)) {
153 host_zoom_dictionary = new base::DictionaryValue();
154 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary);
155 }
156
157 if (modification_is_removal)
158 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL);
159 else
160 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level);
161 }
162
163 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698