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
3 // found in the LICENSE file.
5 #include "extensions/shell/browser/shell_prefs.h"
7 #include "base/prefs/json_pref_store.h"
8 #include "base/prefs/pref_filter.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/pref_service_factory.h"
12 #include "components/pref_registry/pref_registry_syncable.h"
13 #include "components/user_prefs/user_prefs.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "extensions/browser/extension_prefs.h"
18 #if defined(OS_CHROMEOS)
19 #include "chromeos/audio/audio_devices_pref_handler_impl.h"
23 using user_prefs::PrefRegistrySyncable
;
25 namespace extensions
{
28 void RegisterLocalStatePrefs(PrefRegistrySimple
* registry
) {
29 #if defined(OS_CHROMEOS)
30 chromeos::AudioDevicesPrefHandlerImpl::RegisterPrefs(registry
);
34 // Creates a JsonPrefStore from a file at |filepath| and synchronously loads
36 scoped_refptr
<JsonPrefStore
> CreateAndLoadPrefStore(const FilePath
& filepath
) {
37 scoped_refptr
<base::SequencedTaskRunner
> task_runner
=
38 JsonPrefStore::GetTaskRunnerForFile(
39 filepath
, content::BrowserThread::GetBlockingPool());
40 scoped_refptr
<JsonPrefStore
> pref_store
=
41 new JsonPrefStore(filepath
, task_runner
, scoped_ptr
<PrefFilter
>());
42 pref_store
->ReadPrefs(); // Synchronous.
48 namespace shell_prefs
{
50 scoped_ptr
<PrefService
> CreateLocalState(const FilePath
& data_dir
) {
51 FilePath filepath
= data_dir
.AppendASCII("local_state.json");
52 scoped_refptr
<JsonPrefStore
> pref_store
= CreateAndLoadPrefStore(filepath
);
54 // Local state is considered "user prefs" from the factory's perspective.
55 base::PrefServiceFactory factory
;
56 factory
.set_user_prefs(pref_store
);
58 // Local state preferences are not syncable.
59 PrefRegistrySimple
* registry
= new PrefRegistrySimple
;
60 RegisterLocalStatePrefs(registry
);
62 return factory
.Create(registry
);
65 scoped_ptr
<PrefService
> CreateUserPrefService(
66 content::BrowserContext
* browser_context
) {
67 FilePath filepath
= browser_context
->GetPath().AppendASCII("user_prefs.json");
68 scoped_refptr
<JsonPrefStore
> pref_store
= CreateAndLoadPrefStore(filepath
);
70 base::PrefServiceFactory factory
;
71 factory
.set_user_prefs(pref_store
);
73 // TODO(jamescook): If we want to support prefs that are set by extensions
74 // via ChromeSettings properties (e.g. chrome.accessibilityFeatures or
75 // chrome.proxy) then this should create an ExtensionPrefStore and attach it
76 // with PrefServiceFactory::set_extension_prefs().
77 // See https://developer.chrome.com/extensions/types#ChromeSetting
79 // Prefs should be registered before the PrefService is created.
80 PrefRegistrySyncable
* pref_registry
= new PrefRegistrySyncable
;
81 ExtensionPrefs::RegisterProfilePrefs(pref_registry
);
83 scoped_ptr
<PrefService
> pref_service
= factory
.Create(pref_registry
);
84 user_prefs::UserPrefs::Set(browser_context
, pref_service
.get());
88 } // namespace shell_prefs
90 } // namespace extensions