| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" | 5 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/ui/zoom/chrome_zoom_prefs_helper.h" |
| 12 #include "chrome/common/chrome_constants.h" | 13 #include "chrome/common/chrome_constants.h" |
| 13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 14 #include "components/prefs/json_pref_store.h" | |
| 15 #include "components/prefs/pref_filter.h" | |
| 16 #include "components/prefs/pref_registry_simple.h" | |
| 17 #include "components/prefs/pref_service_factory.h" | |
| 18 #include "components/prefs/scoped_user_pref_update.h" | 15 #include "components/prefs/scoped_user_pref_update.h" |
| 19 #include "components/zoom/zoom_event_manager.h" | 16 #include "components/zoom/zoom_event_manager.h" |
| 20 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/host_zoom_map.h" | 18 #include "content/public/browser/host_zoom_map.h" |
| 22 #include "content/public/common/page_zoom.h" | 19 #include "content/public/common/page_zoom.h" |
| 23 | 20 |
| 24 namespace { | |
| 25 | |
| 26 std::string GetHash(const base::FilePath& relative_path) { | |
| 27 size_t int_key = BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path); | |
| 28 return base::SizeTToString(int_key); | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs( | 21 ChromeZoomLevelPrefs::ChromeZoomLevelPrefs( |
| 34 PrefService* pref_service, | 22 PrefService* pref_service, |
| 35 const base::FilePath& profile_path, | 23 const base::FilePath& profile_path, |
| 36 const base::FilePath& partition_path, | 24 const base::FilePath& partition_path, |
| 37 base::WeakPtr<zoom::ZoomEventManager> zoom_event_manager) | 25 base::WeakPtr<zoom::ZoomEventManager> zoom_event_manager) |
| 38 : pref_service_(pref_service), | 26 : partitioned_prefs_(pref_service, profile_path, partition_path), |
| 39 zoom_event_manager_(zoom_event_manager), | 27 zoom_event_manager_(zoom_event_manager), |
| 40 host_zoom_map_(nullptr) { | 28 host_zoom_map_(nullptr) { |
| 41 DCHECK(pref_service_); | 29 // If this is the default partition, emit the zoom scope setting. |
| 42 | 30 if (profile_path == partition_path) |
| 43 DCHECK(!partition_path.empty()); | 31 UMA_HISTOGRAM_BOOLEAN("Settings.ZoomScopeIsPerOrigin", |
| 44 DCHECK((partition_path == profile_path) || | 32 GetZoomScopeIsPerOriginPref()); |
| 45 profile_path.IsParent(partition_path)); | |
| 46 // Create a partition_key string with no '.'s in it. For the default | |
| 47 // StoragePartition, this string will always be "0". | |
| 48 base::FilePath partition_relative_path; | |
| 49 profile_path.AppendRelativePath(partition_path, &partition_relative_path); | |
| 50 partition_key_ = GetHash(partition_relative_path); | |
| 51 | |
| 52 } | 33 } |
| 53 | 34 |
| 54 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() { | 35 ChromeZoomLevelPrefs::~ChromeZoomLevelPrefs() { |
| 55 } | 36 } |
| 56 | 37 |
| 57 std::string ChromeZoomLevelPrefs::GetHashForTesting( | 38 std::string ChromeZoomLevelPrefs::GetHashForTesting( |
| 58 const base::FilePath& relative_path) { | 39 const base::FilePath& relative_path) { |
| 59 return GetHash(relative_path); | 40 return ChromeZoomPrefsHelper::GetHashForTesting(relative_path); |
| 60 } | 41 } |
| 61 | 42 |
| 62 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { | 43 void ChromeZoomLevelPrefs::SetDefaultZoomLevelPref(double level) { |
| 63 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) | 44 if (content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel())) |
| 64 return; | 45 return; |
| 65 | 46 |
| 66 DictionaryPrefUpdate update(pref_service_, prefs::kPartitionDefaultZoomLevel); | 47 partitioned_prefs_.SetDouble(prefs::kPartitionDefaultZoomLevel, level); |
| 67 update->SetDouble(partition_key_, level); | |
| 68 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so | 48 // For unregistered paths, OnDefaultZoomLevelChanged won't be called, so |
| 69 // set this manually. | 49 // set this manually. |
| 70 host_zoom_map_->SetDefaultZoomLevel(level); | 50 host_zoom_map_->SetDefaultZoomLevel(level); |
| 71 default_zoom_changed_callbacks_.Notify(); | 51 default_zoom_changed_callbacks_.Notify(); |
| 72 if (zoom_event_manager_) | 52 if (zoom_event_manager_) |
| 73 zoom_event_manager_->OnDefaultZoomLevelChanged(); | 53 zoom_event_manager_->OnDefaultZoomLevelChanged(); |
| 74 } | 54 } |
| 75 | 55 |
| 76 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const { | 56 double ChromeZoomLevelPrefs::GetDefaultZoomLevelPref() const { |
| 77 double default_zoom_level = 0.0; | 57 double default_zoom_level = 0.0; |
| 78 | 58 |
| 79 const base::DictionaryValue* default_zoom_level_dictionary = | |
| 80 pref_service_->GetDictionary(prefs::kPartitionDefaultZoomLevel); | |
| 81 // If no default has been previously set, the default returned is the | 59 // If no default has been previously set, the default returned is the |
| 82 // value used to initialize default_zoom_level in this function. | 60 // value used to initialize default_zoom_level in this function. |
| 83 default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level); | 61 partitioned_prefs_.GetDouble(prefs::kPartitionDefaultZoomLevel, |
| 62 &default_zoom_level); |
| 84 return default_zoom_level; | 63 return default_zoom_level; |
| 85 } | 64 } |
| 86 | 65 |
| 87 std::unique_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> | 66 std::unique_ptr<ChromeZoomLevelPrefs::DefaultZoomLevelSubscription> |
| 88 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( | 67 ChromeZoomLevelPrefs::RegisterDefaultZoomLevelCallback( |
| 89 const base::Closure& callback) { | 68 const base::Closure& callback) { |
| 90 return default_zoom_changed_callbacks_.Add(callback); | 69 return default_zoom_changed_callbacks_.Add(callback); |
| 91 } | 70 } |
| 92 | 71 |
| 72 void ChromeZoomLevelPrefs::SetZoomScopeIsPerOriginPref(bool is_per_origin) { |
| 73 if (is_per_origin == GetZoomScopeIsPerOriginPref()) |
| 74 return; |
| 75 |
| 76 partitioned_prefs_.SetBoolean(prefs::kPartitionZoomScopeIsPerOrigin, |
| 77 is_per_origin); |
| 78 default_scope_changed_callbacks_.Notify(); |
| 79 } |
| 80 |
| 81 bool ChromeZoomLevelPrefs::GetZoomScopeIsPerOriginPref() const { |
| 82 bool is_per_origin = kZoomScopeSettingDefault; |
| 83 |
| 84 // If no value has been previously set, |is_per_origin| will be untouched |
| 85 // from the default value we've set above. |
| 86 partitioned_prefs_.GetBoolean(prefs::kPartitionZoomScopeIsPerOrigin, |
| 87 &is_per_origin); |
| 88 return is_per_origin; |
| 89 } |
| 90 |
| 91 std::unique_ptr<ChromeZoomLevelPrefs::DefaultZoomScopeSubscription> |
| 92 ChromeZoomLevelPrefs::RegisterDefaultZoomScopeCallback( |
| 93 const base::Closure& callback) { |
| 94 return default_scope_changed_callbacks_.Add(callback); |
| 95 } |
| 96 |
| 93 void ChromeZoomLevelPrefs::OnZoomLevelChanged( | 97 void ChromeZoomLevelPrefs::OnZoomLevelChanged( |
| 94 const content::HostZoomMap::ZoomLevelChange& change) { | 98 const content::HostZoomMap::ZoomLevelChange& change) { |
| 95 // If there's a manager to aggregate ZoomLevelChanged events, pass this event | 99 // If there's a manager to aggregate ZoomLevelChanged events, pass this event |
| 96 // along. Since we already hold a subscription to our associated HostZoomMap, | 100 // along. Since we already hold a subscription to our associated HostZoomMap, |
| 97 // we don't need to create a separate subscription for this. | 101 // we don't need to create a separate subscription for this. |
| 98 if (zoom_event_manager_) | 102 if (zoom_event_manager_) |
| 99 zoom_event_manager_->OnZoomLevelChanged(change); | 103 zoom_event_manager_->OnZoomLevelChanged(change); |
| 100 | 104 |
| 101 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) | 105 if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) |
| 102 return; | 106 return; |
| 103 double level = change.zoom_level; | 107 double level = change.zoom_level; |
| 104 DictionaryPrefUpdate update(pref_service_, | 108 std::unique_ptr<DictionaryPrefUpdate> update( |
| 105 prefs::kPartitionPerHostZoomLevels); | 109 partitioned_prefs_.GetUpdate(prefs::kPartitionPerHostZoomLevels)); |
| 106 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | |
| 107 DCHECK(host_zoom_dictionaries); | |
| 108 | 110 |
| 109 bool modification_is_removal = | 111 bool modification_is_removal = |
| 110 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()); | 112 content::ZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()); |
| 111 | 113 |
| 112 base::DictionaryValue* host_zoom_dictionary = nullptr; | 114 base::DictionaryValue* host_zoom_dictionary( |
| 113 if (!host_zoom_dictionaries->GetDictionary(partition_key_, | 115 partitioned_prefs_.GetMutableDictionary(update.get())); |
| 114 &host_zoom_dictionary)) { | |
| 115 host_zoom_dictionary = new base::DictionaryValue(); | |
| 116 host_zoom_dictionaries->Set(partition_key_, host_zoom_dictionary); | |
| 117 } | |
| 118 | 116 |
| 119 if (modification_is_removal) | 117 if (modification_is_removal) |
| 120 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr); | 118 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, nullptr); |
| 121 else | 119 else |
| 122 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); | 120 host_zoom_dictionary->SetDoubleWithoutPathExpansion(change.host, level); |
| 123 } | 121 } |
| 124 | 122 |
| 125 // TODO(wjmaclean): Remove the dictionary_path once the migration code is | 123 // TODO(wjmaclean): Remove the dictionary_path once the migration code is |
| 126 // removed. crbug.com/420643 | 124 // removed. crbug.com/420643 |
| 127 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels( | 125 void ChromeZoomLevelPrefs::ExtractPerHostZoomLevels( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 // discarded in the migration process. Note: since the structure of partition | 157 // discarded in the migration process. Note: since the structure of partition |
| 160 // per-host zoom level dictionaries is different from the legacy profile | 158 // per-host zoom level dictionaries is different from the legacy profile |
| 161 // per-host zoom level dictionaries, the following code will fail if run | 159 // per-host zoom level dictionaries, the following code will fail if run |
| 162 // on the legacy dictionaries. | 160 // on the legacy dictionaries. |
| 163 if (!sanitize_partition_host_zoom_levels) | 161 if (!sanitize_partition_host_zoom_levels) |
| 164 return; | 162 return; |
| 165 | 163 |
| 166 // Sanitize prefs to remove entries that match the default zoom level and/or | 164 // Sanitize prefs to remove entries that match the default zoom level and/or |
| 167 // have an empty host. | 165 // have an empty host. |
| 168 { | 166 { |
| 169 DictionaryPrefUpdate update(pref_service_, | 167 std::unique_ptr<DictionaryPrefUpdate> update( |
| 170 prefs::kPartitionPerHostZoomLevels); | 168 partitioned_prefs_.GetUpdate(prefs::kPartitionPerHostZoomLevels)); |
| 171 base::DictionaryValue* host_zoom_dictionaries = update.Get(); | 169 base::DictionaryValue* host_zoom_dictionary( |
| 172 base::DictionaryValue* host_zoom_dictionary = nullptr; | 170 partitioned_prefs_.GetMutableDictionary(update.get())); |
| 173 host_zoom_dictionaries->GetDictionary(partition_key_, | |
| 174 &host_zoom_dictionary); | |
| 175 for (const std::string& s : keys_to_remove) | 171 for (const std::string& s : keys_to_remove) |
| 176 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr); | 172 host_zoom_dictionary->RemoveWithoutPathExpansion(s, nullptr); |
| 177 } | 173 } |
| 178 } | 174 } |
| 179 | 175 |
| 180 void ChromeZoomLevelPrefs::InitHostZoomMap( | 176 void ChromeZoomLevelPrefs::InitHostZoomMap( |
| 181 content::HostZoomMap* host_zoom_map) { | 177 content::HostZoomMap* host_zoom_map) { |
| 182 // This init function must be called only once. | 178 // This init function must be called only once. |
| 183 DCHECK(!host_zoom_map_); | 179 DCHECK(!host_zoom_map_); |
| 184 DCHECK(host_zoom_map); | 180 DCHECK(host_zoom_map); |
| 185 host_zoom_map_ = host_zoom_map; | 181 host_zoom_map_ = host_zoom_map; |
| 186 | 182 |
| 187 // Initialize the default zoom level. | 183 // Initialize the default zoom level. |
| 188 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref()); | 184 host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref()); |
| 189 | 185 |
| 190 // Initialize the HostZoomMap with per-host zoom levels from the persisted | 186 // Initialize the HostZoomMap with per-host zoom levels from the persisted |
| 191 // zoom-level preference values. | 187 // zoom-level preference values. |
| 192 const base::DictionaryValue* host_zoom_dictionaries = | |
| 193 pref_service_->GetDictionary(prefs::kPartitionPerHostZoomLevels); | |
| 194 const base::DictionaryValue* host_zoom_dictionary = nullptr; | 188 const base::DictionaryValue* host_zoom_dictionary = nullptr; |
| 195 if (host_zoom_dictionaries->GetDictionary(partition_key_, | 189 if (partitioned_prefs_.GetDictionary(prefs::kPartitionPerHostZoomLevels, |
| 196 &host_zoom_dictionary)) { | 190 &host_zoom_dictionary)) { |
| 197 // Since we're calling this before setting up zoom_subscription_ below we | 191 // Since we're calling this before setting up zoom_subscription_ below we |
| 198 // don't need to worry that host_zoom_dictionary is indirectly affected | 192 // don't need to worry that host_zoom_dictionary is indirectly affected |
| 199 // by calls to HostZoomMap::SetZoomLevelForHost(). | 193 // by calls to HostZoomMap::SetZoomLevelForHost(). |
| 200 ExtractPerHostZoomLevels(host_zoom_dictionary, | 194 ExtractPerHostZoomLevels(host_zoom_dictionary, |
| 201 true /* sanitize_partition_host_zoom_levels */); | 195 true /* sanitize_partition_host_zoom_levels */); |
| 202 } | 196 } |
| 203 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( | 197 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind( |
| 204 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); | 198 &ChromeZoomLevelPrefs::OnZoomLevelChanged, base::Unretained(this))); |
| 205 } | 199 } |
| OLD | NEW |