Home | History | Annotate | Download | only in login
      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->RegisterIntegerPref(prefs::kDeviceRegistered, -1);
     54   registry->RegisterStringPref(prefs::kInitialLocale, "en-US");
     55 }
     56 
     57 // static
     58 bool StartupUtils::IsEulaAccepted() {
     59   return g_browser_process->local_state()->GetBoolean(prefs::kEulaAccepted);
     60 }
     61 
     62 // static
     63 bool StartupUtils::IsOobeCompleted() {
     64   return g_browser_process->local_state()->GetBoolean(prefs::kOobeComplete);
     65 }
     66 
     67 // static
     68 void StartupUtils::MarkEulaAccepted() {
     69   SaveBoolPreferenceForced(prefs::kEulaAccepted, true);
     70 }
     71 
     72 // static
     73 void StartupUtils::MarkOobeCompleted() {
     74   SaveBoolPreferenceForced(prefs::kOobeComplete, true);
     75 }
     76 
     77 // Returns the path to flag file indicating that both parts of OOBE were
     78 // completed.
     79 // On chrome device, returns /home/chronos/.oobe_completed.
     80 // On Linux desktop, returns {DIR_USER_DATA}/.oobe_completed.
     81 static base::FilePath GetOobeCompleteFlagPath() {
     82   // The constant is defined here so it won't be referenced directly.
     83   const char kOobeCompleteFlagFilePath[] = "/home/chronos/.oobe_completed";
     84 
     85   if (base::SysInfo::IsRunningOnChromeOS()) {
     86     return base::FilePath(kOobeCompleteFlagFilePath);
     87   } else {
     88     base::FilePath user_data_dir;
     89     PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
     90     return user_data_dir.AppendASCII(".oobe_completed");
     91   }
     92 }
     93 
     94 static void CreateOobeCompleteFlagFile() {
     95   // Create flag file for boot-time init scripts.
     96   base::FilePath oobe_complete_path = GetOobeCompleteFlagPath();
     97   if (!base::PathExists(oobe_complete_path)) {
     98     FILE* oobe_flag_file = base::OpenFile(oobe_complete_path, "w+b");
     99     if (oobe_flag_file == NULL)
    100       DLOG(WARNING) << oobe_complete_path.value() << " doesn't exist.";
    101     else
    102       base::CloseFile(oobe_flag_file);
    103   }
    104 }
    105 
    106 // static
    107 bool StartupUtils::IsDeviceRegistered() {
    108   int value =
    109       g_browser_process->local_state()->GetInteger(prefs::kDeviceRegistered);
    110   if (value > 0) {
    111     // Recreate flag file in case it was lost.
    112     BrowserThread::PostTask(
    113         BrowserThread::FILE,
    114         FROM_HERE,
    115         base::Bind(&CreateOobeCompleteFlagFile));
    116     return true;
    117   } else if (value == 0) {
    118     return false;
    119   } else {
    120     // Pref is not set. For compatibility check flag file. It causes blocking
    121     // IO on UI thread. But it's required for update from old versions.
    122     base::ThreadRestrictions::ScopedAllowIO allow_io;
    123     base::FilePath oobe_complete_flag_file_path = GetOobeCompleteFlagPath();
    124     bool file_exists = base::PathExists(oobe_complete_flag_file_path);
    125     SaveIntegerPreferenceForced(prefs::kDeviceRegistered, file_exists ? 1 : 0);
    126     return file_exists;
    127   }
    128 }
    129 
    130 // static
    131 void StartupUtils::MarkDeviceRegistered() {
    132   SaveIntegerPreferenceForced(prefs::kDeviceRegistered, 1);
    133   BrowserThread::PostTask(
    134       BrowserThread::FILE,
    135       FROM_HERE,
    136       base::Bind(&CreateOobeCompleteFlagFile));
    137 }
    138 
    139 // static
    140 std::string StartupUtils::GetInitialLocale() {
    141   std::string locale =
    142       g_browser_process->local_state()->GetString(prefs::kInitialLocale);
    143   if (!l10n_util::IsValidLocaleSyntax(locale))
    144     locale = "en-US";
    145   return locale;
    146 }
    147 
    148 // static
    149 void StartupUtils::SetInitialLocale(const std::string& locale) {
    150   if (l10n_util::IsValidLocaleSyntax(locale))
    151     SaveStringPreferenceForced(prefs::kInitialLocale, locale);
    152   else
    153     NOTREACHED();
    154 }
    155 
    156 }  // namespace chromeos
    157