This source file includes following definitions.
- SaveBoolPreferenceForced
- SaveIntegerPreferenceForced
- SaveStringPreferenceForced
- RegisterPrefs
- IsEulaAccepted
- IsOobeCompleted
- MarkEulaAccepted
- MarkOobeCompleted
- GetOobeCompleteFlagPath
- CreateOobeCompleteFlagFile
- IsDeviceRegistered
- MarkDeviceRegistered
- GetInitialLocale
- SetInitialLocale
#include "chrome/browser/chromeos/login/startup_utils.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/sys_info.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/l10n/l10n_util.h"
using content::BrowserThread;
namespace {
void SaveBoolPreferenceForced(const char* pref_name, bool value) {
PrefService* prefs = g_browser_process->local_state();
prefs->SetBoolean(pref_name, value);
prefs->CommitPendingWrite();
}
void SaveIntegerPreferenceForced(const char* pref_name, int value) {
PrefService* prefs = g_browser_process->local_state();
prefs->SetInteger(pref_name, value);
prefs->CommitPendingWrite();
}
void SaveStringPreferenceForced(const char* pref_name,
const std::string& value) {
PrefService* prefs = g_browser_process->local_state();
prefs->SetString(pref_name, value);
prefs->CommitPendingWrite();
}
}
namespace chromeos {
void StartupUtils::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kOobeComplete, false);
registry->RegisterIntegerPref(prefs::kDeviceRegistered, -1);
registry->RegisterStringPref(prefs::kInitialLocale, "en-US");
}
bool StartupUtils::IsEulaAccepted() {
return g_browser_process->local_state()->GetBoolean(prefs::kEulaAccepted);
}
bool StartupUtils::IsOobeCompleted() {
return g_browser_process->local_state()->GetBoolean(prefs::kOobeComplete);
}
void StartupUtils::MarkEulaAccepted() {
SaveBoolPreferenceForced(prefs::kEulaAccepted, true);
}
void StartupUtils::MarkOobeCompleted() {
SaveBoolPreferenceForced(prefs::kOobeComplete, true);
}
static base::FilePath GetOobeCompleteFlagPath() {
const char kOobeCompleteFlagFilePath[] = "/home/chronos/.oobe_completed";
if (base::SysInfo::IsRunningOnChromeOS()) {
return base::FilePath(kOobeCompleteFlagFilePath);
} else {
base::FilePath user_data_dir;
PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
return user_data_dir.AppendASCII(".oobe_completed");
}
}
static void CreateOobeCompleteFlagFile() {
base::FilePath oobe_complete_path = GetOobeCompleteFlagPath();
if (!base::PathExists(oobe_complete_path)) {
FILE* oobe_flag_file = base::OpenFile(oobe_complete_path, "w+b");
if (oobe_flag_file == NULL)
DLOG(WARNING) << oobe_complete_path.value() << " doesn't exist.";
else
base::CloseFile(oobe_flag_file);
}
}
bool StartupUtils::IsDeviceRegistered() {
int value =
g_browser_process->local_state()->GetInteger(prefs::kDeviceRegistered);
if (value > 0) {
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&CreateOobeCompleteFlagFile));
return true;
} else if (value == 0) {
return false;
} else {
base::ThreadRestrictions::ScopedAllowIO allow_io;
base::FilePath oobe_complete_flag_file_path = GetOobeCompleteFlagPath();
bool file_exists = base::PathExists(oobe_complete_flag_file_path);
SaveIntegerPreferenceForced(prefs::kDeviceRegistered, file_exists ? 1 : 0);
return file_exists;
}
}
void StartupUtils::MarkDeviceRegistered() {
SaveIntegerPreferenceForced(prefs::kDeviceRegistered, 1);
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&CreateOobeCompleteFlagFile));
}
std::string StartupUtils::GetInitialLocale() {
std::string locale =
g_browser_process->local_state()->GetString(prefs::kInitialLocale);
if (!l10n_util::IsValidLocaleSyntax(locale))
locale = "en-US";
return locale;
}
void StartupUtils::SetInitialLocale(const std::string& locale) {
if (l10n_util::IsValidLocaleSyntax(locale))
SaveStringPreferenceForced(prefs::kInitialLocale, locale);
else
NOTREACHED();
}
}