12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/prefs/chrome_pref_service_factory.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <string>
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <vector>
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/compiler_specific.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/debug/trace_event.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/metrics/field_trial.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/metrics/histogram.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/default_pref_store.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/json_pref_store.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/prefs/pref_filter.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_notifier_impl.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_registry.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/prefs/pref_registry_simple.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/prefs/pref_store.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_value_store.h"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/time.h"
27c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "chrome/browser/browser_process.h"
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/prefs/browser_ui_prefs_migrator.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/prefs/command_line_pref_store.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/prefs/pref_hash_filter.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/prefs/pref_model_associator.h"
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/prefs/pref_service_syncable.h"
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/prefs/pref_service_syncable_factory.h"
34effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/prefs/profile_pref_store_manager.h"
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/file_path_verifier_win.h"
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
37010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chrome/browser/search_engines/default_search_pref_migration.h"
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/sync/glue/sync_start_util.h"
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/profile_error_dialog.h"
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/pref_names.h"
4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/chromium_strings.h"
4303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/generated_resources.h"
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/component_updater/pref_names.h"
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/default_search_manager.h"
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/search_engines_pref_names.h"
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/sync_driver/pref_names.h"
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_context.h"
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "grit/browser_resources.h"
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/internal_api/public/base/model_type.h"
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
55c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#if defined(ENABLE_CONFIGURATION_POLICY)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/policy/core/browser/browser_policy_connector.h"
57a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/browser/configuration_policy_pref_store.h"
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_types.h"
59c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#endif
60c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(ENABLE_EXTENSIONS)
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/browser/pref_names.h"
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
6558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(ENABLE_MANAGED_USERS)
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_pref_store.h"
6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_WIN)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/win/win_util.h"
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(ENABLE_RLZ)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "rlz/lib/machine_id.h"
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // defined(ENABLE_RLZ)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // defined(OS_WIN)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using content::BrowserContext;
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using content::BrowserThread;
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Whether we are in testing mode; can be enabled via
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// DisableDelaysAndDomainCheckForTesting(). Forces startup checks to occur
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// with no delay and ignores the presence of a domain when determining the
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// active SettingsEnforcement group.
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool g_disable_delays_and_domain_check_for_testing = false;
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// These preferences must be kept in sync with the TrackedPreference enum in
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// tools/metrics/histograms/histograms.xml. To add a new preference, append it
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to the array and add a corresponding value to the histogram enum. Each
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// tracked preference must be given a unique reporting ID.
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// See CleanupDeprecatedTrackedPreferences() in pref_hash_filter.cc to remove a
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// deprecated tracked preference.
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const PrefHashFilter::TrackedPreferenceMetadata kTrackedPrefs[] = {
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0, prefs::kShowHomeButton,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    1, prefs::kHomePageIsNewTabPage,
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    2, prefs::kHomePage,
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    3, prefs::kRestoreOnStartup,
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    4, prefs::kURLsToRestoreOnStartup,
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(ENABLE_EXTENSIONS)
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    5, extensions::pref_names::kExtensions,
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::NO_ENFORCEMENT,
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_SPLIT
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    6, prefs::kGoogleServicesLastUsername,
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    7, prefs::kSearchProviderOverrides,
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    8, prefs::kDefaultSearchProviderSearchURL,
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    9, prefs::kDefaultSearchProviderKeyword,
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    10, prefs::kDefaultSearchProviderName,
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(OS_ANDROID)
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  {
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    11, prefs::kPinnedTabs,
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  },
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  {
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    13, prefs::kProfileResetPromptMementoInProfilePrefs,
1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefHashFilter::ENFORCE_ON_LOAD,
1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  },
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
163010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  {
164010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    14, DefaultSearchManager::kDefaultSearchProviderDataPrefName,
165010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    PrefHashFilter::NO_ENFORCEMENT,
166010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
167010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  },
168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  {
169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Protecting kPreferenceResetTime does two things:
170cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //  1) It ensures this isn't accidently set by someone stomping the pref
171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //     file.
172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //  2) More importantly, it declares kPreferenceResetTime as a protected
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //     pref which is required for it to be visible when queried via the
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //     SegregatedPrefStore. This is because it's written directly in the
175cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //     protected JsonPrefStore by that store's PrefHashFilter if there was
176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //     a reset in FilterOnLoad and SegregatedPrefStore will not look for it
177cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //     in the protected JsonPrefStore unless it's declared as a protected
178cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    //     preference here.
179cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    15, prefs::kPreferenceResetTime,
180cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
181cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  },
1836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  {
1846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    16, prefs::kSafeBrowsingIncidentReportSent,
1856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  },
188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  {
189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    17, sync_driver::prefs::kSyncRemainingRollbackTries,
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    PrefHashFilter::ENFORCE_ON_LOAD,
191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  },
1935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  {
1945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    18, prefs::kSafeBrowsingIncidentsSent,
1955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    PrefHashFilter::ENFORCE_ON_LOAD,
1965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
1975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  },
1981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_WIN)
1991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  {
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    19, prefs::kSwReporterPromptVersion,
2011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefHashFilter::ENFORCE_ON_LOAD,
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
2031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  },
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  {
2051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    20, prefs::kSwReporterPromptReason,
2061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefHashFilter::ENFORCE_ON_LOAD,
2071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefHashFilter::TRACKING_STRATEGY_ATOMIC
2081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  },
2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// One more than the last tracked preferences ID above.
2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)const size_t kTrackedPrefsReportingIDsCount =
2145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    kTrackedPrefs[arraysize(kTrackedPrefs) - 1].reporting_id + 1;
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
216c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// Each group enforces a superset of the protection provided by the previous
217c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// one.
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)enum SettingsEnforcementGroup {
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GROUP_NO_ENFORCEMENT,
220f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Enforce protected settings on profile loads.
221c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GROUP_ENFORCE_ALWAYS,
222f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Also enforce extension default search.
223f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GROUP_ENFORCE_ALWAYS_WITH_DSE,
224010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Also enforce extension settings and default search.
225010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  GROUP_ENFORCE_ALWAYS_WITH_EXTENSIONS_AND_DSE,
226c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // The default enforcement group contains all protection features.
227c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GROUP_ENFORCE_DEFAULT
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)SettingsEnforcementGroup GetSettingsEnforcementGroup() {
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# if defined(OS_WIN)
232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!g_disable_delays_and_domain_check_for_testing) {
233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    static bool first_call = true;
234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    static const bool is_enrolled_to_domain = base::win::IsEnrolledToDomain();
235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (first_call) {
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      UMA_HISTOGRAM_BOOLEAN("Settings.TrackedPreferencesNoEnforcementOnDomain",
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            is_enrolled_to_domain);
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      first_call = false;
239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (is_enrolled_to_domain)
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return GROUP_NO_ENFORCEMENT;
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  struct {
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const char* group_name;
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SettingsEnforcementGroup group;
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } static const kEnforcementLevelMap[] = {
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    { chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement,
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GROUP_NO_ENFORCEMENT },
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    { chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways,
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GROUP_ENFORCE_ALWAYS },
253c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    { chrome_prefs::internals::
254f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          kSettingsEnforcementGroupEnforceAlwaysWithDSE,
255f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      GROUP_ENFORCE_ALWAYS_WITH_DSE },
256010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { chrome_prefs::internals::
257010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE,
258010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GROUP_ENFORCE_ALWAYS_WITH_EXTENSIONS_AND_DSE },
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Use the strongest enforcement setting in the absence of a field trial
262116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // config on Windows. Remember to update the OFFICIAL_BUILD section of
2635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // extension_startup_browsertest.cc and pref_hash_browsertest.cc when updating
2645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // the default value below.
265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // TODO(gab): Enforce this on all platforms.
266116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SettingsEnforcementGroup enforcement_group =
267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(OS_WIN)
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GROUP_ENFORCE_DEFAULT;
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#else
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GROUP_NO_ENFORCEMENT;
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool group_determined_from_trial = false;
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FieldTrial* trial =
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::FieldTrialList::Find(
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          chrome_prefs::internals::kSettingsEnforcementTrialName);
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (trial) {
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& group_name = trial->group_name();
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // ARRAYSIZE_UNSAFE must be used since the array is declared locally; it is
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // only unsafe because it could not trigger a compile error on some
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // non-array pointer types; this is fine since kEnforcementLevelMap is
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // clearly an array.
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kEnforcementLevelMap); ++i) {
283a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      if (kEnforcementLevelMap[i].group_name == group_name) {
284a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        enforcement_group = kEnforcementLevelMap[i].group;
285a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        group_determined_from_trial = true;
286a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        break;
287a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      }
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  UMA_HISTOGRAM_BOOLEAN("Settings.EnforcementGroupDeterminedFromTrial",
291a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                        group_determined_from_trial);
292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return enforcement_group;
293a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
294a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Returns the effective preference tracking configuration.
296a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)std::vector<PrefHashFilter::TrackedPreferenceMetadata>
297a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)GetTrackingConfiguration() {
298c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  const SettingsEnforcementGroup enforcement_group =
299c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      GetSettingsEnforcementGroup();
300a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<PrefHashFilter::TrackedPreferenceMetadata> result;
302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (size_t i = 0; i < arraysize(kTrackedPrefs); ++i) {
303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    PrefHashFilter::TrackedPreferenceMetadata data = kTrackedPrefs[i];
304c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
305010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (GROUP_NO_ENFORCEMENT == enforcement_group) {
306010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      // Remove enforcement for all tracked preferences.
307010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      data.enforcement_level = PrefHashFilter::NO_ENFORCEMENT;
308010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
309010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
310f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (enforcement_group >= GROUP_ENFORCE_ALWAYS_WITH_DSE &&
311f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        data.name == DefaultSearchManager::kDefaultSearchProviderDataPrefName) {
312f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      // Specifically enable default search settings enforcement.
313f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      data.enforcement_level = PrefHashFilter::ENFORCE_ON_LOAD;
314f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
315f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
316116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(ENABLE_EXTENSIONS)
317f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (enforcement_group >= GROUP_ENFORCE_ALWAYS_WITH_EXTENSIONS_AND_DSE &&
318116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        data.name == extensions::pref_names::kExtensions) {
319116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // Specifically enable extension settings enforcement.
320010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      data.enforcement_level = PrefHashFilter::ENFORCE_ON_LOAD;
321010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
322116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
323010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
324a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    result.push_back(data);
325a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
326a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return result;
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Shows notifications which correspond to PersistentPrefStore's reading errors.
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void HandleReadError(PersistentPrefStore::PrefReadError error) {
33158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Sample the histogram also for the successful case in order to get a
33258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // baseline on the success rate in addition to the error distribution.
33358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error,
33458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM);
33558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) {
33758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if !defined(OS_CHROMEOS)
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // an example problem that this can cause.
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Do some diagnosis and try to avoid losing data.
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int message_id = 0;
342cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE ||
343cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        error == PersistentPrefStore::PREF_READ_ERROR_LEVELDB_CORRUPTION) {
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_id = IDS_PREFERENCES_CORRUPT_ERROR;
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_id = IDS_PREFERENCES_UNREADABLE_ERROR;
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (message_id) {
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
351a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              base::Bind(&ShowProfileErrorDialog,
352a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                         PROFILE_ERROR_PREFERENCES,
353a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                         message_id));
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
35558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#else
35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // On ChromeOS error screen with message about broken local state
35758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // will be displayed.
3581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // A supplementary error message about broken local state - is included
3601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // in logs and user feedbacks.
3611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (error != PersistentPrefStore::PREF_READ_ERROR_NONE &&
3621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
3631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      LOG(ERROR) << "An error happened during prefs loading: " << error;
3641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
36558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
369effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochscoped_ptr<ProfilePrefStoreManager> CreateProfilePrefStoreManager(
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::FilePath& profile_path) {
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string device_id;
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_WIN) && defined(ENABLE_RLZ)
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This is used by
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // chrome/browser/extensions/api/music_manager_private/device_id_win.cc
3755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // but that API is private (http://crbug.com/276485) and other platforms are
3765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // not available synchronously.
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // As part of improving pref metrics on other platforms we may want to find
3785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ways to defer preference loading until the device ID can be used.
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  rlz_lib::GetMachineId(&device_id);
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
3816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string seed;
3826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(GOOGLE_CHROME_BUILD)
3836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  seed = ResourceBundle::GetSharedInstance().GetRawDataResource(
3846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      IDR_PREF_HASH_SEED_BIN).as_string();
3856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
386effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return make_scoped_ptr(new ProfilePrefStoreManager(
387effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      profile_path,
388effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      GetTrackingConfiguration(),
389effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      kTrackedPrefsReportingIDsCount,
3906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      seed,
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      device_id,
392effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      g_browser_process->local_state()));
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid PrepareFactory(
396f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    PrefServiceSyncableFactory* factory,
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    policy::PolicyService* policy_service,
398f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    SupervisedUserSettingsService* supervised_user_settings,
399effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    scoped_refptr<PersistentPrefStore> user_pref_store,
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const scoped_refptr<PrefStore>& extension_prefs,
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool async) {
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(ENABLE_CONFIGURATION_POLICY)
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  using policy::ConfigurationPolicyPrefStore;
404f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  factory->set_managed_prefs(
405f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      make_scoped_refptr(new ConfigurationPolicyPrefStore(
406f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          policy_service,
407f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          g_browser_process->browser_policy_connector()->GetHandlerList(),
408f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          policy::POLICY_LEVEL_MANDATORY)));
409f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  factory->set_recommended_prefs(
410f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      make_scoped_refptr(new ConfigurationPolicyPrefStore(
411f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          policy_service,
412f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          g_browser_process->browser_policy_connector()->GetHandlerList(),
413f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          policy::POLICY_LEVEL_RECOMMENDED)));
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // ENABLE_CONFIGURATION_POLICY
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
41658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(ENABLE_MANAGED_USERS)
417f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (supervised_user_settings) {
418f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    factory->set_supervised_user_prefs(
419f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        make_scoped_refptr(
420f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            new SupervisedUserPrefStore(supervised_user_settings)));
42158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
42258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
42358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
424f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  factory->set_async(async);
425f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  factory->set_extension_prefs(extension_prefs);
426f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  factory->set_command_line_prefs(
427f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      make_scoped_refptr(
428f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          new CommandLinePrefStore(CommandLine::ForCurrentProcess())));
429f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  factory->set_read_error_callback(base::Bind(&HandleReadError));
430effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  factory->set_user_prefs(user_pref_store);
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace chrome_prefs {
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace internals {
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Group modifications should be reflected in first_run_browsertest.cc and
4405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// pref_hash_browsertest.cc.
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kSettingsEnforcementTrialName[] = "SettingsEnforcement";
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kSettingsEnforcementGroupNoEnforcement[] = "no_enforcement";
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kSettingsEnforcementGroupEnforceAlways[] = "enforce_always";
444f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const char kSettingsEnforcementGroupEnforceAlwaysWithDSE[] =
445f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    "enforce_always_with_dse";
446010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)const char kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE[] =
447010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    "enforce_always_with_extensions_and_dse";
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace internals
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
451f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)scoped_ptr<PrefService> CreateLocalState(
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& pref_filename,
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::SequencedTaskRunner* pref_io_task_runner,
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    policy::PolicyService* policy_service,
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const scoped_refptr<PrefRegistry>& pref_registry,
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool async) {
457f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PrefServiceSyncableFactory factory;
458effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  PrepareFactory(
459effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      &factory,
460effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      policy_service,
461f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      NULL,  // supervised_user_settings
462effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      new JsonPrefStore(
463effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          pref_filename, pref_io_task_runner, scoped_ptr<PrefFilter>()),
464effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      NULL,  // extension_prefs
465effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      async);
466f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return factory.Create(pref_registry.get());
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
469f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)scoped_ptr<PrefServiceSyncable> CreateProfilePrefs(
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::FilePath& profile_path,
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::SequencedTaskRunner* pref_io_task_runner,
472cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    TrackedPreferenceValidationDelegate* validation_delegate,
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    policy::PolicyService* policy_service,
474f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    SupervisedUserSettingsService* supervised_user_settings,
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const scoped_refptr<PrefStore>& extension_prefs,
476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry,
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool async) {
478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TRACE_EVENT0("browser", "chrome_prefs::CreateProfilePrefs");
4795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
4805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // A StartSyncFlare used to kick sync early in case of a reset event. This is
4815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // done since sync may bring back the user's server value post-reset which
4825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // could potentially cause a "settings flash" between the factory default and
4835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // the re-instantiated server value. Starting sync ASAP minimizes the window
4845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // before the server value is re-instantiated (this window can otherwise be
4855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // as long as 10 seconds by default).
4865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const base::Closure start_sync_flare_for_prefs =
4875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      base::Bind(sync_start_util::GetFlareForSyncableService(profile_path),
4885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 syncer::PREFERENCES);
4895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
490f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PrefServiceSyncableFactory factory;
4911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<PersistentPrefStore> user_pref_store(
4921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      CreateProfilePrefStoreManager(profile_path)
4931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          ->CreateProfilePrefStore(pref_io_task_runner,
4941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   start_sync_flare_for_prefs,
4951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   validation_delegate));
4961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // BrowserUIPrefsMigrator unregisters and deletes itself after it is done.
4971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  user_pref_store->AddObserver(
4981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new BrowserUIPrefsMigrator(user_pref_store.get()));
4995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  PrepareFactory(&factory,
5005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 policy_service,
5015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 supervised_user_settings,
5021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 user_pref_store,
5035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 extension_prefs,
5045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 async);
505010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  scoped_ptr<PrefServiceSyncable> pref_service =
506010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      factory.CreateSyncable(pref_registry.get());
507010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
508010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service.get());
509010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
510010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return pref_service.Pass();
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SchedulePrefsFilePathVerification(const base::FilePath& profile_path) {
5145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_WIN)
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Only do prefs file verification on Windows.
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int kVerifyPrefsFileDelaySeconds = 60;
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  BrowserThread::GetBlockingPool()->PostDelayedTask(
518effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      FROM_HERE,
519effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      base::Bind(&VerifyPreferencesFile,
520effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
521effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                     profile_path)),
522effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      base::TimeDelta::FromSeconds(g_disable_delays_and_domain_check_for_testing
523effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                       ? 0
524effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                       : kVerifyPrefsFileDelaySeconds));
5255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
5265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
528a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void DisableDelaysAndDomainCheckForTesting() {
529a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  g_disable_delays_and_domain_check_for_testing = true;
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool InitializePrefsFromMasterPrefs(
5335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::FilePath& profile_path,
5345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::DictionaryValue& master_prefs) {
535effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return CreateProfilePrefStoreManager(profile_path)
536effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      ->InitializePrefsFromMasterPrefs(master_prefs);
5375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)base::Time GetResetTime(Profile* profile) {
540effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return ProfilePrefStoreManager::GetResetTime(profile->GetPrefs());
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
542a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void ClearResetTime(Profile* profile) {
544effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ProfilePrefStoreManager::ClearResetTime(profile->GetPrefs());
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
548effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ProfilePrefStoreManager::RegisterProfilePrefs(registry);
549a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
550a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RegisterPrefs(PrefRegistrySimple* registry) {
552effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ProfilePrefStoreManager::RegisterPrefs(registry);
5535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace chrome_prefs
556