Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / first_run / first_run.mm
blob0f420bde7382bc932770578890c703c47806d67e
1 // Copyright 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 "ios/chrome/browser/first_run/first_run.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "components/pref_registry/pref_registry_syncable.h"
11 #include "ios/chrome/browser/chrome_paths.h"
13 namespace {
15 // The absence of kSentinelFile file will tell us it is a first run.
16 const base::FilePath::CharType kSentinelFile[] = FILE_PATH_LITERAL("First Run");
18 // RLZ ping delay pref name.
19 const char kPingDelayPrefName[] = "distribution.ping_delay";
21 }  // namespace
23 FirstRun::FirstRunState FirstRun::first_run_ = FIRST_RUN_UNKNOWN;
25 // static
26 bool FirstRun::GetFirstRunSentinelFilePath(base::FilePath* path) {
27   base::FilePath first_run_sentinel;
28   if (!PathService::Get(ios::DIR_USER_DATA, &first_run_sentinel))
29     return false;
30   *path = first_run_sentinel.Append(kSentinelFile);
31   return true;
34 // static
35 bool FirstRun::IsChromeFirstRun() {
36   if (first_run_ != FIRST_RUN_UNKNOWN)
37     return first_run_ == FIRST_RUN_TRUE;
39   base::FilePath first_run_sentinel;
40   if (!GetFirstRunSentinelFilePath(&first_run_sentinel) ||
41       base::PathExists(first_run_sentinel)) {
42     first_run_ = FIRST_RUN_FALSE;
43     return false;
44   }
45   first_run_ = FIRST_RUN_TRUE;
46   return true;
49 // static
50 bool FirstRun::RemoveSentinel() {
51   base::FilePath first_run_sentinel;
52   if (!GetFirstRunSentinelFilePath(&first_run_sentinel))
53     return false;
54   return base::DeleteFile(first_run_sentinel, false);
57 // static
58 bool FirstRun::CreateSentinel() {
59   base::FilePath first_run_sentinel;
60   if (!GetFirstRunSentinelFilePath(&first_run_sentinel) ||
61       base::PathExists(first_run_sentinel))
62     return false;
63   return base::WriteFile(first_run_sentinel, "", 0) != -1;
66 // static
67 const char* FirstRun::GetPingDelayPrefName() {
68   return kPingDelayPrefName;
71 // static
72 void FirstRun::RegisterProfilePrefs(
73     user_prefs::PrefRegistrySyncable* registry) {
74   registry->RegisterIntegerPref(GetPingDelayPrefName(), 0);