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(); |
| 29 zoom_level_prefs_->RemoveObserver(this); |
| 30 } |
| 31 |
| 32 void ZoomLevelPrefsStoreImpl::InitPrefsAndCopyToHostZoomMap( |
| 33 const base::FilePath& file_path, |
| 34 content::HostZoomMap* host_zoom_map) { |
| 35 DCHECK(!file_path.empty()); |
| 36 DCHECK(host_zoom_map); |
| 37 host_zoom_map_ = host_zoom_map; |
| 38 |
| 39 base::FilePath file_name( |
| 40 file_path.Append(chrome::kZoomLevelPreferencesFilename)); |
| 41 zoom_level_prefs_ = new JsonPrefStore( |
| 42 file_name, |
| 43 JsonPrefStore::GetTaskRunnerForFile( |
| 44 file_name, content::BrowserThread::GetBlockingPool()), |
| 45 scoped_ptr<PrefFilter>()); |
| 46 scoped_refptr<user_prefs::PrefRegistrySyncable> registry = |
| 47 new user_prefs::PrefRegistrySyncable(); |
| 48 registry->RegisterDoublePref( |
| 49 prefs::kDefaultZoomLevel, |
| 50 0.0, |
| 51 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 52 registry->RegisterDictionaryPref( |
| 53 prefs::kPerHostZoomLevels, |
| 54 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 55 |
| 56 PrefServiceSyncableFactory factory; |
| 57 factory.set_user_prefs(zoom_level_prefs_); |
| 58 factory.set_async(true); |
| 59 |
| 60 zoom_level_prefs_->AddObserver(this); |
| 61 |
| 62 pref_service_ = factory.CreateSyncable(registry.get()); |
| 63 } |
| 64 |
| 65 PrefService* ZoomLevelPrefsStoreImpl::GetPrefs() { |
| 66 return pref_service_.get(); |
| 67 } |
| 68 |
| 69 void ZoomLevelPrefsStoreImpl::OnPrefValueChanged(const std::string& key) { |
| 70 // Push changes to the default zoom level to the HostZoomMap; all |
| 71 // other consumers will get it from there. |
| 72 if (key == prefs::kDefaultZoomLevel) { |
| 73 host_zoom_map_->SetDefaultZoomLevel( |
| 74 pref_service_->GetDouble(prefs::kDefaultZoomLevel)); |
| 75 } |
| 76 } |
| 77 |
| 78 void ZoomLevelPrefsStoreImpl::OnInitializationCompleted(bool succeeded) { |
| 79 if (!succeeded) |
| 80 return; |
| 81 |
| 82 // Initialize the default zoom in the HostZoomMap from the zoom preferences. |
| 83 double default_zoom_level = |
| 84 pref_service_->GetDouble(prefs::kDefaultZoomLevel); |
| 85 host_zoom_map_->SetDefaultZoomLevel(default_zoom_level); |
| 86 |
| 87 // Initialize the HostZoomMap with per-host zoom levels from the persisted |
| 88 // zoom-level preference values. |
| 89 const base::DictionaryValue* host_zoom_dictionary = |
| 90 pref_service_->GetDictionary(prefs::kPerHostZoomLevels); |
| 91 if (host_zoom_dictionary != NULL) { |
| 92 std::vector<std::string> keys_to_remove; |
| 93 for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); |
| 94 !i.IsAtEnd(); |
| 95 i.Advance()) { |
| 96 const std::string& host(i.key()); |
| 97 double zoom_level = 0; |
| 98 |
| 99 bool success = i.value().GetAsDouble(&zoom_level); |
| 100 DCHECK(success); |
| 101 |
| 102 if (host.empty() || |
| 103 content::ZoomValuesEqual( |
| 104 zoom_level, host_zoom_map_->GetDefaultZoomLevel())) { |
| 105 keys_to_remove.push_back(host); |
| 106 continue; |
| 107 } |
| 108 |
| 109 host_zoom_map_->SetZoomLevelForHost(host, zoom_level); |
| 110 } |
| 111 |
| 112 // Sanitize prefs to remove entries that match the default zoom level and/or |
| 113 // have an empty host. |
| 114 DictionaryPrefUpdate update(pref_service_.get(), prefs::kPerHostZoomLevels); |
| 115 base::DictionaryValue* host_zoom_dictionary = update.Get(); |
| 116 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); |
| 117 it != keys_to_remove.end(); ++it) { |
| 118 host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); |
| 119 } |
| 120 } |
| 121 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( |
| 122 base::Bind(&ZoomLevelPrefsStoreImpl::OnZoomLevelChanged, |
| 123 base::Unretained(this))); |
| 124 |
| 125 if (!callback_.is_null()) { |
| 126 callback_.Run(); |
| 127 // We'll never need to call this again. |
| 128 callback_.Reset(); |
| 129 } |
| 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 |