summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prefs/chrome_pref_service_factory.cc
blob: a074a342930d0583f64aa4a2af936ea3ba6423d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/prefs/chrome_pref_service_factory.h"

#include "base/bind.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/metrics/histogram.h"
#include "base/prefs/default_pref_store.h"
#include "base/prefs/json_pref_store.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/prefs/command_line_pref_store.h"
#include "chrome/browser/prefs/pref_model_associator.h"
#include "chrome/browser/prefs/pref_notifier_impl.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/pref_service_syncable_builder.h"
#include "chrome/browser/prefs/pref_value_store.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/profile_error_dialog.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"

using content::BrowserContext;
using content::BrowserThread;

namespace {

// Shows notifications which correspond to PersistentPrefStore's reading errors.
void HandleReadError(PersistentPrefStore::PrefReadError error) {
  if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) {
    // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for
    // an example problem that this can cause.
    // Do some diagnosis and try to avoid losing data.
    int message_id = 0;
    if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) {
      message_id = IDS_PREFERENCES_CORRUPT_ERROR;
    } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
      message_id = IDS_PREFERENCES_UNREADABLE_ERROR;
    }

    if (message_id) {
      BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                              base::Bind(&ShowProfileErrorDialog, message_id));
    }
    UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error,
                              PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM);
  }
}

void PrepareBuilder(
    PrefServiceSyncableBuilder* builder,
    const FilePath& pref_filename,
    base::SequencedTaskRunner* pref_io_task_runner,
    policy::PolicyService* policy_service,
    PrefStore* extension_prefs,
    bool async) {
#if defined(OS_LINUX)
  // We'd like to see what fraction of our users have the preferences
  // stored on a network file system, as we've had no end of troubles
  // with NFS/AFS.
  // TODO(evanm): remove this once we've collected state.
  file_util::FileSystemType fstype;
  if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) {
    UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType",
                              static_cast<int>(fstype),
                              file_util::FILE_SYSTEM_TYPE_COUNT);
  }
#endif

#if defined(ENABLE_CONFIGURATION_POLICY)
  using policy::ConfigurationPolicyPrefStore;
  builder->WithManagedPrefs(
      ConfigurationPolicyPrefStore::CreateMandatoryPolicyPrefStore(
          policy_service));
  builder->WithRecommendedPrefs(
      ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(
          policy_service));
#endif  // ENABLE_CONFIGURATION_POLICY

  builder->WithAsync(async);
  builder->WithExtensionPrefs(extension_prefs);
  builder->WithCommandLinePrefs(
      new CommandLinePrefStore(CommandLine::ForCurrentProcess()));
  builder->WithReadErrorCallback(base::Bind(&HandleReadError));
  builder->WithUserPrefs(new JsonPrefStore(pref_filename, pref_io_task_runner));
}

}  // namespace

// TODO(joi): Find a better home for this.
PrefServiceBase* PrefServiceBase::FromBrowserContext(BrowserContext* context) {
  return static_cast<Profile*>(context)->GetPrefs();
}

namespace chrome_prefs {

PrefServiceSimple* CreateLocalState(
    const FilePath& pref_filename,
    base::SequencedTaskRunner* pref_io_task_runner,
    policy::PolicyService* policy_service,
    PrefStore* extension_prefs,
    bool async) {
  PrefServiceSyncableBuilder builder;
  PrepareBuilder(&builder,
                 pref_filename,
                 pref_io_task_runner,
                 policy_service,
                 extension_prefs,
                 async);
  return builder.CreateSimple();
}

PrefServiceSyncable* CreateProfilePrefs(
    const FilePath& pref_filename,
    base::SequencedTaskRunner* pref_io_task_runner,
    policy::PolicyService* policy_service,
    PrefStore* extension_prefs,
    bool async) {
  PrefServiceSyncableBuilder builder;
  PrepareBuilder(&builder,
                 pref_filename,
                 pref_io_task_runner,
                 policy_service,
                 extension_prefs,
                 async);
  return builder.CreateSyncable();
}

}  // namespace chrome_prefs