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/zoom_level_prefs_store_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/prefs/json_pref_store.h" | |
10 #include "base/prefs/pref_filter.h" | |
11 #include "base/prefs/scoped_user_pref_update.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/prefs/pref_service_syncable.h" | |
14 #include "chrome/browser/prefs/pref_service_syncable_factory.h" | |
15 #include "chrome/common/chrome_constants.h" | |
16 #include "chrome/common/pref_names.h" | |
17 #include "components/pref_registry/pref_registry_syncable.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 ZoomLevelPrefsStoreImpl::ZoomLevelPrefsStoreImpl() | |
25 : host_zoom_map_(NULL) {} | |
26 | |
27 void ZoomLevelPrefsStoreImpl::SetInitCallback(const base::Closure& callback) { | |
sky
2014/09/05 19:28:07
Make order match header.
wjmaclean
2014/09/05 21:01:47
Done.
| |
28 callback_ = callback; | |
29 } | |
30 | |
31 void ZoomLevelPrefsStoreImpl::InitPrefsAndCopyToHostZoomMap( | |
32 const base::FilePath& file_path, | |
33 content::HostZoomMap* host_zoom_map) { | |
34 DCHECK(!file_path.empty()); | |
35 DCHECK(host_zoom_map); | |
36 host_zoom_map_ = host_zoom_map; | |
37 | |
38 base::FilePath file_name( | |
39 file_path.Append(chrome::kZoomLevelPreferencesFilename)); | |
40 zoom_level_prefs_ = new JsonPrefStore( | |
41 file_name, | |
42 JsonPrefStore::GetTaskRunnerForFile( | |
43 file_name, content::BrowserThread::GetBlockingPool()), | |
44 scoped_ptr<PrefFilter>()); | |
45 scoped_refptr<user_prefs::PrefRegistrySyncable> registry = | |
46 new user_prefs::PrefRegistrySyncable(); | |
47 registry->RegisterDoublePref( | |
48 prefs::kDefaultZoomLevel, | |
49 0.0, | |
sky
2014/09/05 19:28:07
Why is the default 0 and not 1?
wjmaclean
2014/09/05 21:01:47
Zoom levels are logarithmic, so level = 0 => zoom
| |
50 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
51 registry->RegisterDictionaryPref( | |
52 prefs::kPerHostZoomLevels, | |
53 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
54 | |
55 PrefServiceSyncableFactory factory; | |
56 factory.set_user_prefs(zoom_level_prefs_); | |
57 factory.set_async(true); | |
58 | |
59 zoom_level_prefs_->AddObserver(this); | |
60 | |
61 pref_service_ = factory.CreateSyncable(registry.get()); | |
62 } | |
63 | |
64 PrefService* ZoomLevelPrefsStoreImpl::GetPrefs() { | |
65 return pref_service_.get(); | |
66 } | |
67 | |
68 void ZoomLevelPrefsStoreImpl::OnPrefValueChanged(const std::string& key) { | |
69 // Push changes to the default zoom level to the HostZoomMap; all | |
70 // other consumers will get it from there. | |
71 if (key == prefs::kDefaultZoomLevel) | |
sky
2014/09/05 19:28:07
nit: I would use {} as it's multiple lines.
wjmaclean
2014/09/05 21:01:47
Done. Thanks for catching that!
| |
72 host_zoom_map_->SetDefaultZoomLevel( | |
73 pref_service_->GetDouble(prefs::kDefaultZoomLevel)); | |
74 } | |
75 | |
76 void ZoomLevelPrefsStoreImpl::OnInitializationCompleted(bool succeeded) { | |
77 if (!succeeded) { | |
sky
2014/09/05 19:28:07
nit: no {}
wjmaclean
2014/09/05 21:01:47
Done. Ditto!
| |
78 return; | |
79 } | |
80 double default_zoom_level = | |
sky
2014/09/05 19:28:07
Add a comment as to what this does.
wjmaclean
2014/09/05 21:01:47
Done.
| |
81 pref_service_->GetDouble(prefs::kDefaultZoomLevel); | |
82 host_zoom_map_->SetDefaultZoomLevel(default_zoom_level); | |
83 const base::DictionaryValue* host_zoom_dictionary = | |
84 pref_service_->GetDictionary(prefs::kPerHostZoomLevels); | |
85 if (host_zoom_dictionary != NULL) { | |
86 std::vector<std::string> keys_to_remove; | |
87 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); | |
88 !i.IsAtEnd(); | |
89 i.Advance()) { | |
90 const std::string& host(i.key()); | |
91 double zoom_level = 0; | |
92 | |
93 bool success = i.value().GetAsDouble(&zoom_level); | |
94 DCHECK(success); | |
95 | |
96 if (host.empty() || | |
97 content::ZoomValuesEqual( | |
98 zoom_level, host_zoom_map_->GetDefaultZoomLevel())) { | |
99 keys_to_remove.push_back(host); | |
100 continue; | |
101 } | |
102 | |
103 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); | |
104 } | |
105 | |
106 // Sanitize prefs to remove entries that match the default zoom level and/or | |
107 // have an empty host. | |
108 DictionaryPrefUpdate update(pref_service_.get(), prefs::kPerHostZoomLevels); | |
109 base::DictionaryValue* host_zoom_dictionary = update.Get(); | |
110 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); | |
111 it != keys_to_remove.end(); ++it) { | |
112 host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); | |
113 } | |
114 } | |
115 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( | |
116 base::Bind(&ZoomLevelPrefsStoreImpl::OnZoomLevelChanged, | |
117 base::Unretained(this))); | |
118 | |
119 if (!callback_.is_null()) { | |
120 callback_.Run(); | |
121 // We'll never need to call this again. | |
122 callback_.Reset(); | |
123 } | |
124 } | |
125 | |
126 void ZoomLevelPrefsStoreImpl::OnZoomLevelChanged( | |
127 const content::HostZoomMap::ZoomLevelChange& change) { | |
128 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | |
129 return; | |
130 double level = change.zoom_level; | |
131 DictionaryPrefUpdate update(pref_service_.get(), prefs::kPerHostZoomLevels); | |
132 base::DictionaryValue* host_zoom_dictionary = update.Get(); | |
133 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | |
134 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL); | |
135 else | |
136 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | |
137 } | |
138 | |
139 ZoomLevelPrefsStoreImpl::~ZoomLevelPrefsStoreImpl() { | |
140 zoom_subscription_.reset(); | |
141 if (zoom_level_prefs_.get()) | |
sky
2014/09/05 19:28:07
Why the conditional here?
wjmaclean
2014/09/05 21:01:47
Done. Looking at this again, this is leftover from
| |
142 zoom_level_prefs_->RemoveObserver(this); | |
143 } | |
144 | |
145 } // namespace chrome | |
OLD | NEW |