1 // Copyright 2013 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/install_time_util.h"
7 #include <Foundation/Foundation.h>
9 #include "base/mac/foundation_util.h"
13 NSString* const kInstallationTimeKey = @"omaha.InstallationTime";
15 // Returns the installation time of the application: this is the time the
16 // application was first installed, not the time the last version was installed.
17 // If the installation time is unknown, returns a base::Time corresponding to
18 // |kUnknownInstallDate|.
19 // To be noted: this value is reset if the application is uninstalled.
20 base::Time GetNSUserDefaultsInstallationTime() {
21 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
22 CFAbsoluteTime time = [defaults doubleForKey:kInstallationTimeKey];
25 return base::Time::FromCFAbsoluteTime(time);
30 namespace install_time_util {
32 // 2 is used because 0 is a magic value for Time, and 1 was the pre-M29 value
33 // which was migrated to a specific date (crbug.com/270124).
34 const int64 kUnknownInstallDate = 2;
36 base::Time ComputeInstallationTime(bool is_first_run) {
37 return ComputeInstallationTimeInternal(is_first_run,
38 GetNSUserDefaultsInstallationTime());
41 base::Time ComputeInstallationTimeInternal(
43 base::Time ns_user_defaults_install_time) {
45 return base::Time::Now();
47 if (ns_user_defaults_install_time.is_null())
48 return base::Time::FromTimeT(kUnknownInstallDate);
50 return ns_user_defaults_install_time;
53 } // namespace install_time_util