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 ZoomLevelPrefsStoreImpl::~ZoomLevelPrefsStoreImpl() { | |
28 zoom_subscription_.reset(); | |
Bernhard Bauer
2014/09/12 10:28:49
Why do you need this if you're being destroyed any
wjmaclean
2014/09/16 18:44:44
Because zoom_level_prefs_ gets destructed before t
| |
29 zoom_level_prefs_->RemoveObserver(this); | |
30 } | |
31 | |
32 void ZoomLevelPrefsStoreImpl::OnPrefValueChanged(const std::string& key) { | |
33 // Push changes to the default zoom level to the HostZoomMap; all | |
34 // other consumers will get it from there. | |
35 if (key == prefs::kDefaultZoomLevel) { | |
36 host_zoom_map_->SetDefaultZoomLevel( | |
37 pref_service_->GetDouble(prefs::kDefaultZoomLevel)); | |
38 } | |
39 } | |
40 | |
41 void ZoomLevelPrefsStoreImpl::OnInitializationCompleted(bool succeeded) { | |
42 if (!succeeded) | |
43 return; | |
44 | |
45 // Initialize the default zoom in the HostZoomMap from the zoom preferences. | |
46 double default_zoom_level = | |
47 pref_service_->GetDouble(prefs::kDefaultZoomLevel); | |
48 host_zoom_map_->SetDefaultZoomLevel(default_zoom_level); | |
49 | |
50 // Initialize the HostZoomMap with per-host zoom levels from the persisted | |
51 // zoom-level preference values. | |
52 const base::DictionaryValue* host_zoom_dictionary = | |
53 pref_service_->GetDictionary(prefs::kPerHostZoomLevels); | |
54 if (host_zoom_dictionary != NULL) { | |
55 std::vector<std::string> keys_to_remove; | |
56 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); | |
57 !i.IsAtEnd(); | |
58 i.Advance()) { | |
59 const std::string& host(i.key()); | |
60 double zoom_level = 0; | |
61 | |
62 bool success = i.value().GetAsDouble(&zoom_level); | |
63 DCHECK(success); | |
64 | |
65 if (host.empty() || | |
66 content::ZoomValuesEqual( | |
67 zoom_level, host_zoom_map_->GetDefaultZoomLevel())) { | |
68 keys_to_remove.push_back(host); | |
69 continue; | |
70 } | |
71 | |
72 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); | |
73 } | |
74 | |
75 // Sanitize prefs to remove entries that match the default zoom level and/or | |
76 // have an empty host. | |
77 DictionaryPrefUpdate update(pref_service_.get(), prefs::kPerHostZoomLevels); | |
78 base::DictionaryValue* host_zoom_dictionary = update.Get(); | |
79 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); | |
80 it != keys_to_remove.end(); ++it) { | |
81 host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); | |
82 } | |
83 } | |
84 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( | |
85 base::Bind(&ZoomLevelPrefsStoreImpl::OnZoomLevelChanged, | |
86 base::Unretained(this))); | |
87 | |
88 if (!callback_.is_null()) { | |
89 callback_.Run(); | |
90 // We'll never need to call this again. | |
91 callback_.Reset(); | |
92 } | |
93 } | |
94 | |
95 void ZoomLevelPrefsStoreImpl::InitPrefsAndCopyToHostZoomMap( | |
96 const base::FilePath& file_path, | |
97 content::HostZoomMap* host_zoom_map) { | |
98 DCHECK(!file_path.empty()); | |
99 DCHECK(host_zoom_map); | |
100 host_zoom_map_ = host_zoom_map; | |
101 | |
102 base::FilePath file_name( | |
103 file_path.Append(chrome::kZoomLevelPreferencesFilename)); | |
104 zoom_level_prefs_ = new JsonPrefStore( | |
105 file_name, | |
106 JsonPrefStore::GetTaskRunnerForFile( | |
107 file_name, content::BrowserThread::GetBlockingPool()), | |
108 scoped_ptr<PrefFilter>()); | |
109 scoped_refptr<user_prefs::PrefRegistrySyncable> registry = | |
110 new user_prefs::PrefRegistrySyncable(); | |
111 registry->RegisterDoublePref( | |
112 prefs::kDefaultZoomLevel, | |
113 0.0, | |
114 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
115 registry->RegisterDictionaryPref( | |
116 prefs::kPerHostZoomLevels, | |
117 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
118 | |
119 PrefServiceSyncableFactory factory; | |
Bernhard Bauer
2014/09/12 10:28:49
Do we really want a syncable prefservice?
wjmaclean
2014/09/16 18:44:44
Possibly not ... I did this originally to be consi
| |
120 factory.set_user_prefs(zoom_level_prefs_); | |
121 factory.set_async(true); | |
122 | |
123 zoom_level_prefs_->AddObserver(this); | |
124 | |
125 pref_service_ = factory.CreateSyncable(registry.get()); | |
126 } | |
127 | |
128 PrefService* ZoomLevelPrefsStoreImpl::GetPrefs() { | |
129 return pref_service_.get(); | |
130 } | |
131 | |
132 void ZoomLevelPrefsStoreImpl::SetInitCallback(const base::Closure& callback) { | |
133 callback_ = callback; | |
134 } | |
135 | |
136 void ZoomLevelPrefsStoreImpl::OnZoomLevelChanged( | |
137 const content::HostZoomMap::ZoomLevelChange& change) { | |
138 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | |
139 return; | |
140 double level = change.zoom_level; | |
141 DictionaryPrefUpdate update(pref_service_.get(), prefs::kPerHostZoomLevels); | |
142 base::DictionaryValue* host_zoom_dictionary = update.Get(); | |
143 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | |
144 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL); | |
145 else | |
146 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | |
147 } | |
148 | |
149 } // namespace chrome | |
OLD | NEW |