1 // Copyright 2015 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/common/app_group/app_group_metrics_mainapp.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "ios/chrome/common/app_group/app_group_constants.h"
10 #include "ios/chrome/common/app_group/app_group_metrics.h"
16 void ProcessPendingLogs(
17 const base::mac::ScopedBlock<ProceduralBlockWithData>& callback) {
18 NSFileManager* file_manager = [NSFileManager defaultManager];
19 NSURL* store_url = [file_manager
20 containerURLForSecurityApplicationGroupIdentifier:ApplicationGroup()];
21 NSURL* log_dir_url = [store_url
22 URLByAppendingPathComponent:app_group::kPendingLogFileDirectory];
24 NSArray* pending_logs =
25 [file_manager contentsOfDirectoryAtPath:[log_dir_url path] error:nil];
28 for (NSString* pending_log : pending_logs) {
29 if ([pending_log hasSuffix:app_group::kPendingLogFileSuffix]) {
30 NSURL* file_url = [log_dir_url URLByAppendingPathComponent:pending_log];
32 NSData* log_content = [file_manager contentsAtPath:[file_url path]];
33 callback.get()(log_content);
35 [file_manager removeItemAtURL:file_url error:nil];
40 void EnableMetrics(NSString* client_id,
43 int64 enable_metrics_date) {
44 base::scoped_nsobject<NSUserDefaults> shared_defaults(
45 [[NSUserDefaults alloc] initWithSuiteName:ApplicationGroup()]);
46 [shared_defaults setObject:client_id forKey:@(kChromeAppClientID)];
49 setObject:[NSString stringWithFormat:@"%lld", enable_metrics_date]
50 forKey:@(kUserMetricsEnabledDate)];
52 [shared_defaults setObject:[NSString stringWithFormat:@"%lld", install_date]
53 forKey:@(kInstallDate)];
55 [shared_defaults setObject:brand_code forKey:@(kBrandCode)];
58 void DisableMetrics() {
59 base::scoped_nsobject<NSUserDefaults> shared_defaults(
60 [[NSUserDefaults alloc] initWithSuiteName:ApplicationGroup()]);
61 [shared_defaults removeObjectForKey:@(kChromeAppClientID)];
64 } // namespace main_app
66 } // namespace app_group