1 // Copyright 2015 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 "ios/chrome/browser/prefs/ios_chrome_pref_service_factory.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/prefs/json_pref_store.h"
12 #include "base/prefs/persistent_pref_store.h"
13 #include "base/prefs/pref_filter.h"
14 #include "base/prefs/pref_service.h"
15 #include "components/proxy_config/proxy_config_pref_names.h"
16 #include "components/search_engines/default_search_pref_migration.h"
17 #include "components/syncable_prefs/pref_service_syncable.h"
18 #include "components/syncable_prefs/pref_service_syncable_factory.h"
19 #include "ios/chrome/browser/application_context.h"
20 #include "ios/chrome/browser/prefs/ios_chrome_pref_model_associator_client.h"
24 const char kPreferencesFilename
[] = "Preferences";
26 // Record PersistentPrefStore's reading errors distribution.
27 void HandleReadError(PersistentPrefStore::PrefReadError error
) {
28 // Sample the histogram also for the successful case in order to get a
29 // baseline on the success rate in addition to the error distribution.
30 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error
,
31 PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM
);
34 void PrepareFactory(syncable_prefs::PrefServiceSyncableFactory
* factory
,
35 policy::PolicyService
* policy_service
,
36 const base::FilePath
& pref_filename
,
37 base::SequencedTaskRunner
* pref_io_task_runner
,
39 factory
->set_user_prefs(make_scoped_refptr(new JsonPrefStore(
40 pref_filename
, pref_io_task_runner
, scoped_ptr
<PrefFilter
>())));
42 #if defined(ENABLE_CONFIGURATION_POLICY)
43 policy::BrowserPolicyConnector
* policy_connector
=
44 GetApplicationContext()->GetBrowserPolicyConnector();
45 factory
->SetManagedPolicies(policy_service
, policy_connector
);
46 factory
->SetRecommendedPolicies(policy_service
, policy_connector
);
47 #endif // ENABLE_CONFIGURATION_POLICY
49 factory
->set_async(async
);
50 factory
->set_read_error_callback(base::Bind(&HandleReadError
));
51 factory
->SetPrefModelAssociatorClient(
52 IOSChromePrefModelAssociatorClient::GetInstance());
57 scoped_ptr
<PrefService
> CreateLocalState(
58 const base::FilePath
& pref_filename
,
59 base::SequencedTaskRunner
* pref_io_task_runner
,
60 policy::PolicyService
* policy_service
,
61 const scoped_refptr
<PrefRegistry
>& pref_registry
,
63 syncable_prefs::PrefServiceSyncableFactory factory
;
64 PrepareFactory(&factory
, policy_service
, pref_filename
, pref_io_task_runner
,
66 return factory
.Create(pref_registry
.get());
69 scoped_ptr
<syncable_prefs::PrefServiceSyncable
> CreateBrowserStatePrefs(
70 const base::FilePath
& browser_state_path
,
71 base::SequencedTaskRunner
* pref_io_task_runner
,
72 TrackedPreferenceValidationDelegate
* validation_delegate
,
73 policy::PolicyService
* policy_service
,
74 const scoped_refptr
<user_prefs::PrefRegistrySyncable
>& pref_registry
,
76 // chrome_prefs::CreateProfilePrefs uses ProfilePrefStoreManager to create
77 // the preference store however since Chrome on iOS does not need to track
78 // preference modifications (as applications are sand-boxed), it can use a
79 // simple JsonPrefStore to store them (which is what PrefStoreManager uses
80 // on platforms that do not track preference modifications).
81 syncable_prefs::PrefServiceSyncableFactory factory
;
82 PrepareFactory(&factory
, policy_service
,
83 browser_state_path
.Append(kPreferencesFilename
),
84 pref_io_task_runner
, async
);
85 scoped_ptr
<syncable_prefs::PrefServiceSyncable
> pref_service
=
86 factory
.CreateSyncable(pref_registry
.get());
87 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service
.get());
88 return pref_service
.Pass();
91 scoped_ptr
<syncable_prefs::PrefServiceSyncable
>
92 CreateIncognitoBrowserStatePrefs(
93 syncable_prefs::PrefServiceSyncable
* pref_service
) {
94 // List of keys that cannot be changed in the user prefs file by the incognito
95 // browser state. All preferences that store information about the browsing
96 // history or behaviour of the user should have this property.
97 std::vector
<const char*> overlay_pref_names
;
98 overlay_pref_names
.push_back(proxy_config::prefs::kProxy
);
99 return make_scoped_ptr(pref_service
->CreateIncognitoPrefService(
100 nullptr, // incognito_extension_pref_store
101 overlay_pref_names
));