1 // Copyright (c) 2012 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 "chrome/browser/chromeos/login/startup_utils.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/sys_info.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/pref_names.h"
19 #include "chromeos/chromeos_switches.h"
20 #include "components/web_resource/web_resource_pref_names.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "ui/base/l10n/l10n_util.h"
24 using content::BrowserThread
;
28 // Saves boolean "Local State" preference and forces its persistence to disk.
29 void SaveBoolPreferenceForced(const char* pref_name
, bool value
) {
30 PrefService
* prefs
= g_browser_process
->local_state();
31 prefs
->SetBoolean(pref_name
, value
);
32 prefs
->CommitPendingWrite();
35 // Saves integer "Local State" preference and forces its persistence to disk.
36 void SaveIntegerPreferenceForced(const char* pref_name
, int value
) {
37 PrefService
* prefs
= g_browser_process
->local_state();
38 prefs
->SetInteger(pref_name
, value
);
39 prefs
->CommitPendingWrite();
42 // Saves string "Local State" preference and forces its persistence to disk.
43 void SaveStringPreferenceForced(const char* pref_name
,
44 const std::string
& value
) {
45 PrefService
* prefs
= g_browser_process
->local_state();
46 prefs
->SetString(pref_name
, value
);
47 prefs
->CommitPendingWrite();
55 void StartupUtils::RegisterPrefs(PrefRegistrySimple
* registry
) {
56 registry
->RegisterBooleanPref(prefs::kOobeComplete
, false);
57 registry
->RegisterStringPref(prefs::kOobeScreenPending
, "");
58 registry
->RegisterIntegerPref(prefs::kDeviceRegistered
, -1);
59 registry
->RegisterBooleanPref(prefs::kEnrollmentRecoveryRequired
, false);
60 registry
->RegisterStringPref(prefs::kInitialLocale
, "en-US");
64 bool StartupUtils::IsEulaAccepted() {
65 return g_browser_process
->local_state()->GetBoolean(prefs::kEulaAccepted
);
69 bool StartupUtils::IsOobeCompleted() {
70 return g_browser_process
->local_state()->GetBoolean(prefs::kOobeComplete
);
74 void StartupUtils::MarkEulaAccepted() {
75 SaveBoolPreferenceForced(prefs::kEulaAccepted
, true);
79 void StartupUtils::MarkOobeCompleted() {
80 // Forcing the second pref will force this one as well. Even if this one
81 // doesn't end up synced it is only going to eat up a couple of bytes with no
83 g_browser_process
->local_state()->ClearPref(prefs::kOobeScreenPending
);
84 SaveBoolPreferenceForced(prefs::kOobeComplete
, true);
86 // Successful enrollment implies that recovery is not required.
87 SaveBoolPreferenceForced(prefs::kEnrollmentRecoveryRequired
, false);
90 void StartupUtils::SaveOobePendingScreen(const std::string
& screen
) {
91 SaveStringPreferenceForced(prefs::kOobeScreenPending
, screen
);
94 // Returns the path to flag file indicating that both parts of OOBE were
96 // On chrome device, returns /home/chronos/.oobe_completed.
97 // On Linux desktop, returns {DIR_USER_DATA}/.oobe_completed.
98 static base::FilePath
GetOobeCompleteFlagPath() {
99 // The constant is defined here so it won't be referenced directly.
100 const char kOobeCompleteFlagFilePath
[] = "/home/chronos/.oobe_completed";
102 if (base::SysInfo::IsRunningOnChromeOS()) {
103 return base::FilePath(kOobeCompleteFlagFilePath
);
105 base::FilePath user_data_dir
;
106 PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
);
107 return user_data_dir
.AppendASCII(".oobe_completed");
111 static void CreateOobeCompleteFlagFile() {
112 // Create flag file for boot-time init scripts.
113 base::FilePath oobe_complete_path
= GetOobeCompleteFlagPath();
114 if (!base::PathExists(oobe_complete_path
)) {
115 FILE* oobe_flag_file
= base::OpenFile(oobe_complete_path
, "w+b");
116 if (oobe_flag_file
== NULL
)
117 DLOG(WARNING
) << oobe_complete_path
.value() << " doesn't exist.";
119 base::CloseFile(oobe_flag_file
);
124 bool StartupUtils::IsDeviceRegistered() {
126 g_browser_process
->local_state()->GetInteger(prefs::kDeviceRegistered
);
128 // Recreate flag file in case it was lost.
129 BrowserThread::PostTask(
132 base::Bind(&CreateOobeCompleteFlagFile
));
134 } else if (value
== 0) {
137 // Pref is not set. For compatibility check flag file. It causes blocking
138 // IO on UI thread. But it's required for update from old versions.
139 base::ThreadRestrictions::ScopedAllowIO allow_io
;
140 base::FilePath oobe_complete_flag_file_path
= GetOobeCompleteFlagPath();
141 bool file_exists
= base::PathExists(oobe_complete_flag_file_path
);
142 SaveIntegerPreferenceForced(prefs::kDeviceRegistered
, file_exists
? 1 : 0);
148 void StartupUtils::MarkDeviceRegistered(const base::Closure
& done_callback
) {
149 SaveIntegerPreferenceForced(prefs::kDeviceRegistered
, 1);
150 if (done_callback
.is_null()) {
151 BrowserThread::PostTask(
154 base::Bind(&CreateOobeCompleteFlagFile
));
156 BrowserThread::PostTaskAndReply(
159 base::Bind(&CreateOobeCompleteFlagFile
),
165 void StartupUtils::MarkEnrollmentRecoveryRequired() {
166 SaveBoolPreferenceForced(prefs::kEnrollmentRecoveryRequired
, true);
170 std::string
StartupUtils::GetInitialLocale() {
172 g_browser_process
->local_state()->GetString(prefs::kInitialLocale
);
173 if (!l10n_util::IsValidLocaleSyntax(locale
))
179 bool StartupUtils::IsWebviewSigninEnabled() {
184 void StartupUtils::SetInitialLocale(const std::string
& locale
) {
185 if (l10n_util::IsValidLocaleSyntax(locale
))
186 SaveStringPreferenceForced(prefs::kInitialLocale
, locale
);
191 } // namespace chromeos