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..62fe0103b9effe8af296cb542fe2eacfc808a454 |
| --- /dev/null |
| +++ b/chrome/browser/ui/zoom/zoom_level_prefs_store_impl.cc |
| @@ -0,0 +1,123 @@ |
| +// 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 browser { |
|
wjmaclean
2014/08/14 19:42:17
Ooops, this needs to be chrome too ...
|
| + |
| +ZoomLevelPrefsStoreImpl::ZoomLevelPrefsStoreImpl() |
| + : host_zoom_map_(NULL) {} |
| + |
| +void ZoomLevelPrefsStoreImpl::InitHostZoomMap( |
| + 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->RegisterDictionaryPref( |
| + prefs::kPerHostZoomLevels, |
| + user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| + |
| + PrefServiceSyncableFactory factory; |
| + 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::OnPrefValueChanged(const std::string& key) { |
| +} |
| + |
| +void ZoomLevelPrefsStoreImpl::OnInitializationCompleted(bool succeeded) { |
| + if (!succeeded) { |
| + return; |
| + } |
| + 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))); |
| +} |
| + |
| +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); |
| +} |
| + |
| +ZoomLevelPrefsStoreImpl::~ZoomLevelPrefsStoreImpl() { |
| + zoom_subscription_.reset(); |
| + if (zoom_level_prefs_) |
| + zoom_level_prefs_->RemoveObserver(this); |
| +} |
| + |
| +} // namespace browser |