[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 1 | // Copyright (c) 2010 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/test/testing_pref_service.h" |
| 6 | |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 7 | #include "chrome/browser/prefs/command_line_pref_store.h" |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 8 | #include "chrome/browser/prefs/testing_pref_store.h" |
| 9 | #include "chrome/browser/prefs/pref_notifier.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 10 | #include "chrome/browser/prefs/pref_value_store.h" |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 11 | #include "chrome/browser/policy/configuration_policy_pref_store.h" |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 12 | |
| 13 | // TODO(pamg): Instantiate no PrefStores by default. Allow callers to specify |
| 14 | // which they want, and expand usage of this class to more unit tests. |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 15 | TestingPrefService::TestingPrefService() |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 16 | : PrefService( |
| 17 | managed_platform_prefs_ = new TestingPrefStore(), |
| 18 | device_management_prefs_ = new TestingPrefStore(), |
| 19 | NULL, |
| 20 | NULL, |
| 21 | user_prefs_ = new TestingPrefStore(), |
| 22 | NULL, |
| 23 | NULL) { |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | TestingPrefService::TestingPrefService( |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 27 | policy::ConfigurationPolicyProvider* managed_platform_provider, |
| 28 | policy::ConfigurationPolicyProvider* device_management_provider, |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 29 | CommandLine* command_line) |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 30 | : PrefService( |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 31 | managed_platform_prefs_ = CreatePolicyPrefStoreFromProvider( |
| 32 | managed_platform_provider), |
| 33 | device_management_prefs_ = |
| 34 | CreatePolicyPrefStoreFromProvider(device_management_provider), |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 35 | NULL, |
| 36 | CreateCommandLinePrefStore(command_line), |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 37 | user_prefs_ = new TestingPrefStore(), |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 38 | NULL, |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 39 | NULL) { |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 42 | PrefStore* TestingPrefService::CreatePolicyPrefStoreFromProvider( |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 43 | policy::ConfigurationPolicyProvider* provider) { |
| 44 | if (provider) |
| 45 | return new policy::ConfigurationPolicyPrefStore(provider); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 46 | return new TestingPrefStore(); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | PrefStore* TestingPrefService::CreateCommandLinePrefStore( |
| 50 | CommandLine* command_line) { |
| 51 | if (command_line) |
| 52 | return new CommandLinePrefStore(command_line); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 53 | return new TestingPrefStore(); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 56 | const Value* TestingPrefService::GetManagedPref(const char* path) { |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 57 | return GetPref(managed_platform_prefs_, path); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 60 | void TestingPrefService::SetManagedPref(const char* path, Value* value) { |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 61 | SetPref(managed_platform_prefs_, path, value); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 64 | void TestingPrefService::RemoveManagedPref(const char* path) { |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 65 | RemovePref(managed_platform_prefs_, path); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | b6fd1fa7 | 2010-10-05 23:49:31 | [diff] [blame] | 68 | void TestingPrefService::SetManagedPrefWithoutNotification(const char* path, |
| 69 | Value* value) { |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 70 | managed_platform_prefs_->prefs()->Set(path, value); |
[email protected] | b6fd1fa7 | 2010-10-05 23:49:31 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void TestingPrefService::RemoveManagedPrefWithoutNotification( |
| 74 | const char* path) { |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 75 | managed_platform_prefs_->prefs()->Remove(path, NULL); |
[email protected] | b6fd1fa7 | 2010-10-05 23:49:31 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 78 | const Value* TestingPrefService::GetUserPref(const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 79 | return GetPref(user_prefs_, path); |
| 80 | } |
| 81 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 82 | void TestingPrefService::SetUserPref(const char* path, Value* value) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 83 | SetPref(user_prefs_, path, value); |
| 84 | } |
| 85 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 86 | void TestingPrefService::RemoveUserPref(const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 87 | RemovePref(user_prefs_, path); |
| 88 | } |
| 89 | |
| 90 | const Value* TestingPrefService::GetPref(PrefStore* pref_store, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 91 | const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 92 | Value* result; |
| 93 | return pref_store->prefs()->Get(path, &result) ? result : NULL; |
| 94 | } |
| 95 | |
| 96 | void TestingPrefService::SetPref(PrefStore* pref_store, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 97 | const char* path, |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 98 | Value* value) { |
| 99 | pref_store->prefs()->Set(path, value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 100 | pref_notifier()->OnPreferenceChanged(path); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void TestingPrefService::RemovePref(PrefStore* pref_store, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 104 | const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 105 | pref_store->prefs()->Remove(path, NULL); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame^] | 106 | pref_notifier()->OnPreferenceChanged(path); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 107 | } |