Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / startup_utils.cc
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/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/pref_names.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "ui/base/l10n/l10n_util.h"
19
20 using content::BrowserThread;
21
22 namespace {
23
24 // Saves boolean "Local State" preference and forces its persistence to disk.
25 void SaveBoolPreferenceForced(const char* pref_name, bool value) {
26   PrefService* prefs = g_browser_process->local_state();
27   prefs->SetBoolean(pref_name, value);
28   prefs->CommitPendingWrite();
29 }
30
31 // Saves integer "Local State" preference and forces its persistence to disk.
32 void SaveIntegerPreferenceForced(const char* pref_name, int value) {
33   PrefService* prefs = g_browser_process->local_state();
34   prefs->SetInteger(pref_name, value);
35   prefs->CommitPendingWrite();
36 }
37
38 // Saves string "Local State" preference and forces its persistence to disk.
39 void SaveStringPreferenceForced(const char* pref_name,
40                                 const std::string& value) {
41   PrefService* prefs = g_browser_process->local_state();
42   prefs->SetString(pref_name, value);
43   prefs->CommitPendingWrite();
44 }
45
46 }  // namespace
47
48 namespace chromeos {
49
50 // static
51 void StartupUtils::RegisterPrefs(PrefRegistrySimple* registry) {
52   registry->RegisterBooleanPref(prefs::kOobeComplete, false);
53   registry->RegisterStringPref(prefs::kOobeScreenPending, "");
54   registry->RegisterIntegerPref(prefs::kDeviceRegistered, -1);
55   registry->RegisterStringPref(prefs::kInitialLocale, "en-US");
56 }
57
58 // static
59 bool StartupUtils::IsEulaAccepted() {
60   return g_browser_process->local_state()->GetBoolean(prefs::kEulaAccepted);
61 }
62
63 // static
64 bool StartupUtils::IsOobeCompleted() {
65   return g_browser_process->local_state()->GetBoolean(prefs::kOobeComplete);
66 }
67
68 // static
69 void StartupUtils::MarkEulaAccepted() {
70   SaveBoolPreferenceForced(prefs::kEulaAccepted, true);
71 }
72
73 // static
74 void StartupUtils::MarkOobeCompleted() {
75   // Forcing the second pref will force this one as well. Even if this one
76   // doesn't end up synced it is only going to eat up a couple of bytes with no
77   // side-effects.
78   g_browser_process->local_state()->ClearPref(prefs::kOobeScreenPending);
79   SaveBoolPreferenceForced(prefs::kOobeComplete, true);
80 }
81
82 void StartupUtils::SaveOobePendingScreen(const std::string& screen) {
83   SaveStringPreferenceForced(prefs::kOobeScreenPending, screen);
84 }
85
86 // Returns the path to flag file indicating that both parts of OOBE were
87 // completed.
88 // On chrome device, returns /home/chronos/.oobe_completed.
89 // On Linux desktop, returns {DIR_USER_DATA}/.oobe_completed.
90 static base::FilePath GetOobeCompleteFlagPath() {
91   // The constant is defined here so it won't be referenced directly.
92   const char kOobeCompleteFlagFilePath[] = "/home/chronos/.oobe_completed";
93
94   if (base::SysInfo::IsRunningOnChromeOS()) {
95     return base::FilePath(kOobeCompleteFlagFilePath);
96   } else {
97     base::FilePath user_data_dir;
98     PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
99     return user_data_dir.AppendASCII(".oobe_completed");
100   }
101 }
102
103 static void CreateOobeCompleteFlagFile() {
104   // Create flag file for boot-time init scripts.
105   base::FilePath oobe_complete_path = GetOobeCompleteFlagPath();
106   if (!base::PathExists(oobe_complete_path)) {
107     FILE* oobe_flag_file = base::OpenFile(oobe_complete_path, "w+b");
108     if (oobe_flag_file == NULL)
109       DLOG(WARNING) << oobe_complete_path.value() << " doesn't exist.";
110     else
111       base::CloseFile(oobe_flag_file);
112   }
113 }
114
115 // static
116 bool StartupUtils::IsDeviceRegistered() {
117   int value =
118       g_browser_process->local_state()->GetInteger(prefs::kDeviceRegistered);
119   if (value > 0) {
120     // Recreate flag file in case it was lost.
121     BrowserThread::PostTask(
122         BrowserThread::FILE,
123         FROM_HERE,
124         base::Bind(&CreateOobeCompleteFlagFile));
125     return true;
126   } else if (value == 0) {
127     return false;
128   } else {
129     // Pref is not set. For compatibility check flag file. It causes blocking
130     // IO on UI thread. But it's required for update from old versions.
131     base::ThreadRestrictions::ScopedAllowIO allow_io;
132     base::FilePath oobe_complete_flag_file_path = GetOobeCompleteFlagPath();
133     bool file_exists = base::PathExists(oobe_complete_flag_file_path);
134     SaveIntegerPreferenceForced(prefs::kDeviceRegistered, file_exists ? 1 : 0);
135     return file_exists;
136   }
137 }
138
139 // static
140 void StartupUtils::MarkDeviceRegistered(const base::Closure& done_callback) {
141   SaveIntegerPreferenceForced(prefs::kDeviceRegistered, 1);
142   if (done_callback.is_null()) {
143     BrowserThread::PostTask(
144         BrowserThread::FILE,
145         FROM_HERE,
146         base::Bind(&CreateOobeCompleteFlagFile));
147   } else {
148     BrowserThread::PostTaskAndReply(
149         BrowserThread::FILE,
150         FROM_HERE,
151         base::Bind(&CreateOobeCompleteFlagFile),
152         done_callback);
153   }
154 }
155
156 // static
157 std::string StartupUtils::GetInitialLocale() {
158   std::string locale =
159       g_browser_process->local_state()->GetString(prefs::kInitialLocale);
160   if (!l10n_util::IsValidLocaleSyntax(locale))
161     locale = "en-US";
162   return locale;
163 }
164
165 // static
166 void StartupUtils::SetInitialLocale(const std::string& locale) {
167   if (l10n_util::IsValidLocaleSyntax(locale))
168     SaveStringPreferenceForced(prefs::kInitialLocale, locale);
169   else
170     NOTREACHED();
171 }
172
173 }  // namespace chromeos