Roll src/third_party/WebKit 3aea697:d9c6159 (svn 201973:201974)
[chromium-blink-merge.git] / chromecast / browser / pref_service_helper.cc
blobc3cf47251dbb541d47b17fc97684cf5238540a41
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 "chromecast/browser/pref_service_helper.h"
7 #include <string>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/prefs/json_pref_store.h"
14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/prefs/pref_service_factory.h"
16 #include "base/prefs/pref_store.h"
17 #include "chromecast/base/cast_paths.h"
18 #include "chromecast/base/pref_names.h"
19 #include "content/public/browser/browser_thread.h"
21 namespace chromecast {
22 namespace shell {
24 namespace {
26 void UserPrefsLoadError(PersistentPrefStore::PrefReadError* error_val,
27 PersistentPrefStore::PrefReadError error) {
28 DCHECK(error_val);
29 *error_val = error;
32 base::FilePath GetConfigPath() {
33 base::FilePath config_path;
34 CHECK(PathService::Get(FILE_CAST_CONFIG, &config_path));
35 return config_path;
38 } // namespace
40 // static
41 scoped_ptr<PrefService> PrefServiceHelper::CreatePrefService(
42 PrefRegistrySimple* registry) {
43 const base::FilePath config_path(GetConfigPath());
44 VLOG(1) << "Loading config from " << config_path.value();
46 registry->RegisterBooleanPref(prefs::kEnableRemoteDebugging, false);
47 registry->RegisterBooleanPref(prefs::kMetricsIsNewClientID, false);
48 // Opt-in stats default to true to handle two different cases:
49 // 1) Any crashes or UMA logs are recorded prior to setup completing
50 // successfully (even though we can't send them yet). Unless the user
51 // ends up actually opting out, we don't want to lose this data once
52 // we get network connectivity and are able to send it. If the user
53 // opts out, nothing further will be sent (honoring the user's setting).
54 // 2) Dogfood users (see dogfood agreement).
55 registry->RegisterBooleanPref(prefs::kOptInStats, true);
57 RegisterPlatformPrefs(registry);
59 base::PrefServiceFactory prefServiceFactory;
60 scoped_refptr<base::SequencedTaskRunner> task_runner =
61 JsonPrefStore::GetTaskRunnerForFile(
62 config_path,
63 content::BrowserThread::GetBlockingPool());
64 prefServiceFactory.SetUserPrefsFile(config_path, task_runner.get());
65 prefServiceFactory.set_async(false);
67 PersistentPrefStore::PrefReadError prefs_read_error =
68 PersistentPrefStore::PREF_READ_ERROR_NONE;
69 prefServiceFactory.set_read_error_callback(
70 base::Bind(&UserPrefsLoadError, &prefs_read_error));
72 scoped_ptr<PrefService> pref_service(prefServiceFactory.Create(registry));
73 if (prefs_read_error != PersistentPrefStore::PREF_READ_ERROR_NONE) {
74 LOG(ERROR) << "Cannot initialize chromecast config: "
75 << config_path.value()
76 << ", pref_error=" << prefs_read_error;
79 OnPrefsLoaded(pref_service.get());
80 return pref_service.Pass();
83 } // namespace shell
84 } // namespace chromecast