Chromium Code Reviews| Index: chrome/browser/ui/zoom/zoom_level_prefs_store_impl.cc |
| diff --git a/chrome/browser/ui/zoom/zoom_level_prefs_store_impl.cc b/chrome/browser/ui/zoom/zoom_level_prefs_store_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a60cb26e2666743956551214b869293b2d3e740b |
| --- /dev/null |
| +++ b/chrome/browser/ui/zoom/zoom_level_prefs_store_impl.cc |
| @@ -0,0 +1,149 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/zoom/zoom_level_prefs_store_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/prefs/json_pref_store.h" |
| +#include "base/prefs/pref_filter.h" |
| +#include "base/prefs/scoped_user_pref_update.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/prefs/pref_service_syncable.h" |
| +#include "chrome/browser/prefs/pref_service_syncable_factory.h" |
| +#include "chrome/common/chrome_constants.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "components/pref_registry/pref_registry_syncable.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/host_zoom_map.h" |
| +#include "content/public/common/page_zoom.h" |
| + |
| +namespace chrome { |
| + |
| +ZoomLevelPrefsStoreImpl::ZoomLevelPrefsStoreImpl() |
| + : host_zoom_map_(NULL) {} |
| + |
| +ZoomLevelPrefsStoreImpl::~ZoomLevelPrefsStoreImpl() { |
| + 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
|
| + zoom_level_prefs_->RemoveObserver(this); |
| +} |
| + |
| +void ZoomLevelPrefsStoreImpl::OnPrefValueChanged(const std::string& key) { |
| + // Push changes to the default zoom level to the HostZoomMap; all |
| + // other consumers will get it from there. |
| + if (key == prefs::kDefaultZoomLevel) { |
| + host_zoom_map_->SetDefaultZoomLevel( |
| + pref_service_->GetDouble(prefs::kDefaultZoomLevel)); |
| + } |
| +} |
| + |
| +void ZoomLevelPrefsStoreImpl::OnInitializationCompleted(bool succeeded) { |
| + if (!succeeded) |
| + return; |
| + |
| + // Initialize the default zoom in the HostZoomMap from the zoom preferences. |
| + double default_zoom_level = |
| + pref_service_->GetDouble(prefs::kDefaultZoomLevel); |
| + host_zoom_map_->SetDefaultZoomLevel(default_zoom_level); |
| + |
| + // Initialize the HostZoomMap with per-host zoom levels from the persisted |
| + // zoom-level preference values. |
| + const base::DictionaryValue* host_zoom_dictionary = |
| + pref_service_->GetDictionary(prefs::kPerHostZoomLevels); |
| + if (host_zoom_dictionary != NULL) { |
| + std::vector<std::string> keys_to_remove; |
| + for (base::DictionaryValue::Iterator i(*host_zoom_dictionary); |
| + !i.IsAtEnd(); |
| + i.Advance()) { |
| + const std::string& host(i.key()); |
| + double zoom_level = 0; |
| + |
| + bool success = i.value().GetAsDouble(&zoom_level); |
| + DCHECK(success); |
| + |
| + if (host.empty() || |
| + content::ZoomValuesEqual( |
| + zoom_level, host_zoom_map_->GetDefaultZoomLevel())) { |
| + keys_to_remove.push_back(host); |
| + continue; |
| + } |
| + |
| + host_zoom_map_->SetZoomLevelForHost(host, zoom_level); |
| + } |
| + |
| + // Sanitize prefs to remove entries that match the default zoom level and/or |
| + // have an empty host. |
| + DictionaryPrefUpdate update(pref_service_.get(), prefs::kPerHostZoomLevels); |
| + base::DictionaryValue* host_zoom_dictionary = update.Get(); |
| + for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); |
| + it != keys_to_remove.end(); ++it) { |
| + host_zoom_dictionary->RemoveWithoutPathExpansion(*it, NULL); |
| + } |
| + } |
| + zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( |
| + base::Bind(&ZoomLevelPrefsStoreImpl::OnZoomLevelChanged, |
| + base::Unretained(this))); |
| + |
| + if (!callback_.is_null()) { |
| + callback_.Run(); |
| + // We'll never need to call this again. |
| + callback_.Reset(); |
| + } |
| +} |
| + |
| +void ZoomLevelPrefsStoreImpl::InitPrefsAndCopyToHostZoomMap( |
| + const base::FilePath& file_path, |
| + content::HostZoomMap* host_zoom_map) { |
| + DCHECK(!file_path.empty()); |
| + DCHECK(host_zoom_map); |
| + host_zoom_map_ = host_zoom_map; |
| + |
| + base::FilePath file_name( |
| + file_path.Append(chrome::kZoomLevelPreferencesFilename)); |
| + zoom_level_prefs_ = new JsonPrefStore( |
| + file_name, |
| + JsonPrefStore::GetTaskRunnerForFile( |
| + file_name, content::BrowserThread::GetBlockingPool()), |
| + scoped_ptr<PrefFilter>()); |
| + scoped_refptr<user_prefs::PrefRegistrySyncable> registry = |
| + new user_prefs::PrefRegistrySyncable(); |
| + registry->RegisterDoublePref( |
| + prefs::kDefaultZoomLevel, |
| + 0.0, |
| + user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| + registry->RegisterDictionaryPref( |
| + prefs::kPerHostZoomLevels, |
| + user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| + |
| + 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
|
| + factory.set_user_prefs(zoom_level_prefs_); |
| + factory.set_async(true); |
| + |
| + zoom_level_prefs_->AddObserver(this); |
| + |
| + pref_service_ = factory.CreateSyncable(registry.get()); |
| +} |
| + |
| +PrefService* ZoomLevelPrefsStoreImpl::GetPrefs() { |
| + return pref_service_.get(); |
| +} |
| + |
| +void ZoomLevelPrefsStoreImpl::SetInitCallback(const base::Closure& callback) { |
| + callback_ = callback; |
| +} |
| + |
| +void ZoomLevelPrefsStoreImpl::OnZoomLevelChanged( |
| + const content::HostZoomMap::ZoomLevelChange& change) { |
| + if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) |
| + return; |
| + double level = change.zoom_level; |
| + DictionaryPrefUpdate update(pref_service_.get(), prefs::kPerHostZoomLevels); |
| + base::DictionaryValue* host_zoom_dictionary = update.Get(); |
| + if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) |
| + host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL); |
| + else |
| + host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); |
| +} |
| + |
| +} // namespace chrome |