OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/chrome_zoom_prefs_helper.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "components/prefs/pref_service.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 std::string GetHash(const base::FilePath& relative_path) { |
| 16 size_t int_key = BASE_HASH_NAMESPACE::hash<base::FilePath>()(relative_path); |
| 17 return base::SizeTToString(int_key); |
| 18 } |
| 19 |
| 20 } // namespace |
| 21 |
| 22 ChromeZoomPrefsHelper::ChromeZoomPrefsHelper( |
| 23 PrefService* pref_service, |
| 24 const base::FilePath& profile_path, |
| 25 const base::FilePath& partition_path) |
| 26 : pref_service_(pref_service) { |
| 27 DCHECK(pref_service_); |
| 28 |
| 29 DCHECK(!partition_path.empty()); |
| 30 DCHECK((partition_path == profile_path) || |
| 31 profile_path.IsParent(partition_path)); |
| 32 // Create a partition_key string with no '.'s in it. For the default |
| 33 // StoragePartition, this string will always be "0". |
| 34 base::FilePath partition_relative_path; |
| 35 profile_path.AppendRelativePath(partition_path, &partition_relative_path); |
| 36 partition_key_ = GetHash(partition_relative_path); |
| 37 } |
| 38 |
| 39 std::string ChromeZoomPrefsHelper::GetHashForTesting( |
| 40 const base::FilePath& relative_path) { |
| 41 return GetHash(relative_path); |
| 42 } |
| 43 |
| 44 bool ChromeZoomPrefsHelper::GetBoolean(const char* pref_name, |
| 45 bool* value) const { |
| 46 return pref_service_->GetDictionary(pref_name)->GetBoolean(partition_key_, |
| 47 value); |
| 48 } |
| 49 |
| 50 void ChromeZoomPrefsHelper::SetBoolean(const char* pref_name, bool value) { |
| 51 DictionaryPrefUpdate update(pref_service_, pref_name); |
| 52 update->SetBoolean(partition_key_, value); |
| 53 } |
| 54 |
| 55 bool ChromeZoomPrefsHelper::GetDouble(const char* pref_name, |
| 56 double* value) const { |
| 57 return pref_service_->GetDictionary(pref_name)->GetDouble(partition_key_, |
| 58 value); |
| 59 } |
| 60 |
| 61 void ChromeZoomPrefsHelper::SetDouble(const char* pref_name, double value) { |
| 62 DictionaryPrefUpdate update(pref_service_, pref_name); |
| 63 update->SetDouble(partition_key_, value); |
| 64 } |
| 65 |
| 66 bool ChromeZoomPrefsHelper::GetDictionary( |
| 67 const char* pref_name, |
| 68 const base::DictionaryValue** value) const { |
| 69 return pref_service_->GetDictionary(pref_name)->GetDictionary(partition_key_, |
| 70 value); |
| 71 } |
| 72 |
| 73 std::unique_ptr<DictionaryPrefUpdate> ChromeZoomPrefsHelper::GetUpdate( |
| 74 const char* pref_name) { |
| 75 return base::MakeUnique<DictionaryPrefUpdate>(pref_service_, pref_name); |
| 76 } |
| 77 |
| 78 base::DictionaryValue* ChromeZoomPrefsHelper::GetMutableDictionary( |
| 79 DictionaryPrefUpdate* update) const { |
| 80 base::DictionaryValue* dictionaries = update->Get(); |
| 81 DCHECK(dictionaries); |
| 82 |
| 83 base::DictionaryValue* dictionary = nullptr; |
| 84 if (!dictionaries->GetDictionary(partition_key_, &dictionary)) { |
| 85 dictionary = new base::DictionaryValue(); |
| 86 dictionaries->Set(partition_key_, base::WrapUnique(dictionary)); |
| 87 } |
| 88 |
| 89 return dictionary; |
| 90 } |
OLD | NEW |