Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / startup_utils.cc
blobd35e06d0a9780e4883558585d0558ebf771beaa2
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"
7 #include "base/bind.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;
26 namespace {
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();
50 bool IsWebViewDisabledCmdLine() {
51 return base::CommandLine::ForCurrentProcess()->HasSwitch(
52 chromeos::switches::kDisableWebviewSigninFlow);
55 } // namespace
57 namespace chromeos {
59 // static
60 void StartupUtils::RegisterPrefs(PrefRegistrySimple* registry) {
61 registry->RegisterBooleanPref(prefs::kOobeComplete, false);
62 registry->RegisterStringPref(prefs::kOobeScreenPending, "");
63 registry->RegisterIntegerPref(prefs::kDeviceRegistered, -1);
64 registry->RegisterBooleanPref(prefs::kEnrollmentRecoveryRequired, false);
65 registry->RegisterStringPref(prefs::kInitialLocale, "en-US");
66 registry->RegisterBooleanPref(prefs::kWebviewSigninDisabled, false);
67 registry->RegisterBooleanPref(prefs::kNewLoginUIPopup, false);
70 // static
71 bool StartupUtils::IsEulaAccepted() {
72 return g_browser_process->local_state()->GetBoolean(prefs::kEulaAccepted);
75 // static
76 bool StartupUtils::IsOobeCompleted() {
77 return g_browser_process->local_state()->GetBoolean(prefs::kOobeComplete);
80 // static
81 void StartupUtils::MarkEulaAccepted() {
82 SaveBoolPreferenceForced(prefs::kEulaAccepted, true);
85 // static
86 void StartupUtils::MarkOobeCompleted() {
87 // Forcing the second pref will force this one as well. Even if this one
88 // doesn't end up synced it is only going to eat up a couple of bytes with no
89 // side-effects.
90 g_browser_process->local_state()->ClearPref(prefs::kOobeScreenPending);
91 SaveBoolPreferenceForced(prefs::kOobeComplete, true);
93 // Successful enrollment implies that recovery is not required.
94 SaveBoolPreferenceForced(prefs::kEnrollmentRecoveryRequired, false);
97 void StartupUtils::SaveOobePendingScreen(const std::string& screen) {
98 SaveStringPreferenceForced(prefs::kOobeScreenPending, screen);
101 // Returns the path to flag file indicating that both parts of OOBE were
102 // completed.
103 // On chrome device, returns /home/chronos/.oobe_completed.
104 // On Linux desktop, returns {DIR_USER_DATA}/.oobe_completed.
105 static base::FilePath GetOobeCompleteFlagPath() {
106 // The constant is defined here so it won't be referenced directly.
107 const char kOobeCompleteFlagFilePath[] = "/home/chronos/.oobe_completed";
109 if (base::SysInfo::IsRunningOnChromeOS()) {
110 return base::FilePath(kOobeCompleteFlagFilePath);
111 } else {
112 base::FilePath user_data_dir;
113 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
114 return user_data_dir.AppendASCII(".oobe_completed");
118 static void CreateOobeCompleteFlagFile() {
119 // Create flag file for boot-time init scripts.
120 base::FilePath oobe_complete_path = GetOobeCompleteFlagPath();
121 if (!base::PathExists(oobe_complete_path)) {
122 FILE* oobe_flag_file = base::OpenFile(oobe_complete_path, "w+b");
123 if (oobe_flag_file == NULL)
124 DLOG(WARNING) << oobe_complete_path.value() << " doesn't exist.";
125 else
126 base::CloseFile(oobe_flag_file);
130 // static
131 bool StartupUtils::IsDeviceRegistered() {
132 int value =
133 g_browser_process->local_state()->GetInteger(prefs::kDeviceRegistered);
134 if (value > 0) {
135 // Recreate flag file in case it was lost.
136 BrowserThread::PostTask(
137 BrowserThread::FILE,
138 FROM_HERE,
139 base::Bind(&CreateOobeCompleteFlagFile));
140 return true;
141 } else if (value == 0) {
142 return false;
143 } else {
144 // Pref is not set. For compatibility check flag file. It causes blocking
145 // IO on UI thread. But it's required for update from old versions.
146 base::ThreadRestrictions::ScopedAllowIO allow_io;
147 base::FilePath oobe_complete_flag_file_path = GetOobeCompleteFlagPath();
148 bool file_exists = base::PathExists(oobe_complete_flag_file_path);
149 SaveIntegerPreferenceForced(prefs::kDeviceRegistered, file_exists ? 1 : 0);
150 return file_exists;
154 // static
155 void StartupUtils::MarkDeviceRegistered(const base::Closure& done_callback) {
156 SaveIntegerPreferenceForced(prefs::kDeviceRegistered, 1);
157 if (done_callback.is_null()) {
158 BrowserThread::PostTask(
159 BrowserThread::FILE,
160 FROM_HERE,
161 base::Bind(&CreateOobeCompleteFlagFile));
162 } else {
163 BrowserThread::PostTaskAndReply(
164 BrowserThread::FILE,
165 FROM_HERE,
166 base::Bind(&CreateOobeCompleteFlagFile),
167 done_callback);
171 // static
172 void StartupUtils::MarkEnrollmentRecoveryRequired() {
173 SaveBoolPreferenceForced(prefs::kEnrollmentRecoveryRequired, true);
176 // static
177 std::string StartupUtils::GetInitialLocale() {
178 std::string locale =
179 g_browser_process->local_state()->GetString(prefs::kInitialLocale);
180 if (!l10n_util::IsValidLocaleSyntax(locale))
181 locale = "en-US";
182 return locale;
185 // static
186 bool StartupUtils::IsWebviewSigninEnabled() {
187 const bool is_webview_disabled_pref =
188 g_browser_process->local_state()->GetBoolean(
189 prefs::kWebviewSigninDisabled);
191 return !IsWebViewDisabledCmdLine() && !is_webview_disabled_pref;
194 // static
195 bool StartupUtils::EnableWebviewSignin(bool is_enabled) {
196 if (is_enabled && IsWebViewDisabledCmdLine())
197 return false;
199 g_browser_process->local_state()->SetBoolean(prefs::kWebviewSigninDisabled,
200 !is_enabled);
201 return true;
204 // static
205 void StartupUtils::SetInitialLocale(const std::string& locale) {
206 if (l10n_util::IsValidLocaleSyntax(locale))
207 SaveStringPreferenceForced(prefs::kInitialLocale, locale);
208 else
209 NOTREACHED();
212 } // namespace chromeos