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/files/file_util.h"
9 #include "base/path_service.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/sys_info.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/extensions/features/feature_channel.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/web_resource/web_resource_pref_names.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "ui/base/l10n/l10n_util.h"
22 using content::BrowserThread
;
26 // Saves boolean "Local State" preference and forces its persistence to disk.
27 void SaveBoolPreferenceForced(const char* pref_name
, bool value
) {
28 PrefService
* prefs
= g_browser_process
->local_state();
29 prefs
->SetBoolean(pref_name
, value
);
30 prefs
->CommitPendingWrite();
33 // Saves integer "Local State" preference and forces its persistence to disk.
34 void SaveIntegerPreferenceForced(const char* pref_name
, int value
) {
35 PrefService
* prefs
= g_browser_process
->local_state();
36 prefs
->SetInteger(pref_name
, value
);
37 prefs
->CommitPendingWrite();
40 // Saves string "Local State" preference and forces its persistence to disk.
41 void SaveStringPreferenceForced(const char* pref_name
,
42 const std::string
& value
) {
43 PrefService
* prefs
= g_browser_process
->local_state();
44 prefs
->SetString(pref_name
, value
);
45 prefs
->CommitPendingWrite();
53 void StartupUtils::RegisterPrefs(PrefRegistrySimple
* registry
) {
54 registry
->RegisterBooleanPref(prefs::kOobeComplete
, false);
55 registry
->RegisterStringPref(prefs::kOobeScreenPending
, "");
56 registry
->RegisterIntegerPref(prefs::kDeviceRegistered
, -1);
57 registry
->RegisterBooleanPref(prefs::kEnrollmentRecoveryRequired
, false);
58 registry
->RegisterStringPref(prefs::kInitialLocale
, "en-US");
59 registry
->RegisterBooleanPref(prefs::kNewOobe
, false);
60 registry
->RegisterBooleanPref(prefs::kWebviewSigninEnabled
, false);
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::IsWebviewSigninAllowed() {
180 return extensions::GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV
;
184 bool StartupUtils::IsWebviewSigninEnabled() {
185 return IsWebviewSigninAllowed() &&
186 g_browser_process
->local_state()->GetBoolean(
187 prefs::kWebviewSigninEnabled
);
191 bool StartupUtils::EnableWebviewSignin(bool is_enabled
) {
192 if (!IsWebviewSigninAllowed())
195 g_browser_process
->local_state()->SetBoolean(prefs::kWebviewSigninEnabled
,
201 void StartupUtils::SetInitialLocale(const std::string
& locale
) {
202 if (l10n_util::IsValidLocaleSyntax(locale
))
203 SaveStringPreferenceForced(prefs::kInitialLocale
, locale
);
209 bool StartupUtils::IsNewOobeAllowed() {
210 return extensions::GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV
;
214 bool StartupUtils::IsNewOobeActivated() {
215 return g_browser_process
->local_state()->GetBoolean(prefs::kNewOobe
) &&
219 } // namespace chromeos