OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/chromeos/login/startup_utils.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/chromeos/chromeos_version.h" | |
9 #include "base/file_util.h" | |
10 #include "base/prefs/pref_registry_simple.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "base/threading/thread_restrictions.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/common/pref_names.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "ui/base/l10n/l10n_util.h" | |
17 | |
18 using content::BrowserThread; | |
19 | |
20 namespace { | |
21 | |
22 // A string pref with initial locale set in VPD or manifest. | |
23 const char kInitialLocale[] = "intl.initial_locale"; | |
24 | |
25 // A boolean pref of the OOBE complete flag (first OOBE part before login). | |
26 const char kOobeComplete[] = "OobeComplete"; | |
27 | |
28 // A boolean pref of the device registered flag (second part after first login). | |
29 const char kDeviceRegistered[] = "DeviceRegistered"; | |
30 | |
31 // Time in seconds that we wait for the device to reboot. | |
32 // If reboot didn't happen, ask user to reboot device manually. | |
33 const int kWaitForRebootTimeSec = 3; | |
34 | |
35 // Saves boolean "Local State" preference and forces its persistence to disk. | |
36 void SaveBoolPreferenceForced(const char* pref_name, bool value) { | |
37 PrefService* prefs = g_browser_process->local_state(); | |
38 prefs->SetBoolean(pref_name, value); | |
39 prefs->CommitPendingWrite(); | |
40 } | |
41 | |
42 // Saves integer "Local State" preference and forces its persistence to disk. | |
43 void SaveIntegerPreferenceForced(const char* pref_name, int value) { | |
44 PrefService* prefs = g_browser_process->local_state(); | |
45 prefs->SetInteger(pref_name, value); | |
46 prefs->CommitPendingWrite(); | |
47 } | |
48 | |
49 // Saves string "Local State" preference and forces its persistence to disk. | |
50 void SaveStringPreferenceForced(const char* pref_name, | |
51 const std::string& value) { | |
52 PrefService* prefs = g_browser_process->local_state(); | |
53 prefs->SetString(pref_name, value); | |
54 prefs->CommitPendingWrite(); | |
55 } | |
56 | |
57 } // namespace | |
58 | |
59 namespace chromeos { | |
60 | |
61 // static | |
62 void StartupUtils::RegisterPrefs(PrefRegistrySimple* registry) { | |
63 registry->RegisterBooleanPref(kOobeComplete, false); | |
64 registry->RegisterIntegerPref(kDeviceRegistered, -1); | |
65 registry->RegisterBooleanPref(prefs::kEulaAccepted, false); | |
Nikita (slow)
2013/04/19 14:37:56
Heads up, there's https://codereview.chromium.org/
| |
66 registry->RegisterStringPref(kInitialLocale, "en-US"); | |
67 } | |
68 | |
69 // static | |
70 bool StartupUtils::IsEulaAccepted() { | |
71 return g_browser_process->local_state()->GetBoolean(prefs::kEulaAccepted); | |
72 } | |
73 | |
74 // static | |
75 bool StartupUtils::IsOobeCompleted() { | |
76 return g_browser_process->local_state()->GetBoolean(kOobeComplete); | |
77 } | |
78 | |
79 // static | |
80 void StartupUtils::MarkEulaAccepted() { | |
81 SaveBoolPreferenceForced(prefs::kEulaAccepted, true); | |
82 } | |
83 | |
84 // static | |
85 void StartupUtils::MarkOobeCompleted() { | |
86 SaveBoolPreferenceForced(kOobeComplete, true); | |
87 } | |
88 | |
89 // Returns the path to flag file indicating that both parts of OOBE were | |
90 // completed. | |
91 // On chrome device, returns /home/chronos/.oobe_completed. | |
92 // On Linux desktop, returns $HOME/.oobe_completed. | |
93 static base::FilePath GetOobeCompleteFlagPath() { | |
94 // The constant is defined here so it won't be referenced directly. | |
95 const char kOobeCompleteFlagFilePath[] = "/home/chronos/.oobe_completed"; | |
96 | |
97 if (base::chromeos::IsRunningOnChromeOS()) { | |
98 return base::FilePath(kOobeCompleteFlagFilePath); | |
99 } else { | |
100 const char* home = getenv("HOME"); | |
101 // Unlikely but if HOME is not defined, use the current directory. | |
102 if (!home) | |
103 home = ""; | |
104 return base::FilePath(home).AppendASCII(".oobe_completed"); | |
105 } | |
106 } | |
107 | |
108 static void CreateOobeCompleteFlagFile() { | |
109 // Create flag file for boot-time init scripts. | |
110 base::FilePath oobe_complete_path = GetOobeCompleteFlagPath(); | |
111 if (!file_util::PathExists(oobe_complete_path)) { | |
112 FILE* oobe_flag_file = file_util::OpenFile(oobe_complete_path, "w+b"); | |
113 if (oobe_flag_file == NULL) | |
114 DLOG(WARNING) << oobe_complete_path.value() << " doesn't exist."; | |
115 else | |
116 file_util::CloseFile(oobe_flag_file); | |
117 } | |
118 } | |
119 | |
120 // static | |
121 bool StartupUtils::IsDeviceRegistered() { | |
122 int value = g_browser_process->local_state()->GetInteger(kDeviceRegistered); | |
123 if (value > 0) { | |
124 // Recreate flag file in case it was lost. | |
125 BrowserThread::PostTask( | |
126 BrowserThread::FILE, | |
127 FROM_HERE, | |
128 base::Bind(&CreateOobeCompleteFlagFile)); | |
129 return true; | |
130 } else if (value == 0) { | |
131 return false; | |
132 } else { | |
133 // Pref is not set. For compatibility check flag file. It causes blocking | |
134 // IO on UI thread. But it's required for update from old versions. | |
135 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
136 base::FilePath oobe_complete_flag_file_path = GetOobeCompleteFlagPath(); | |
137 bool file_exists = file_util::PathExists(oobe_complete_flag_file_path); | |
138 SaveIntegerPreferenceForced(kDeviceRegistered, file_exists ? 1 : 0); | |
139 return file_exists; | |
140 } | |
141 } | |
142 | |
143 // static | |
144 void StartupUtils::MarkDeviceRegistered() { | |
145 SaveIntegerPreferenceForced(kDeviceRegistered, 1); | |
146 BrowserThread::PostTask( | |
147 BrowserThread::FILE, | |
148 FROM_HERE, | |
149 base::Bind(&CreateOobeCompleteFlagFile)); | |
150 } | |
151 | |
152 // static | |
153 std::string StartupUtils::GetInitialLocale() { | |
154 std::string locale = | |
155 g_browser_process->local_state()->GetString(kInitialLocale); | |
156 if (!l10n_util::IsValidLocaleSyntax(locale)) | |
157 locale = "en-US"; | |
158 return locale; | |
159 } | |
160 | |
161 // static | |
162 void StartupUtils::SetInitialLocale(const std::string& locale) { | |
163 if (l10n_util::IsValidLocaleSyntax(locale)) | |
164 SaveStringPreferenceForced(kInitialLocale, locale); | |
165 else | |
166 NOTREACHED(); | |
167 } | |
168 | |
169 } // namespace chromeos | |
OLD | NEW |