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_client.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"
14 // Number of log files to keep in the |kPendingLogFileDirectory|. Any older file
16 const int kMaxFileNumber = 100;
21 namespace client_app {
23 void AddPendingLog(NSData* log, AppGroupApplications application) {
24 NSFileManager* file_manager = [NSFileManager defaultManager];
25 NSURL* store_url = [file_manager
26 containerURLForSecurityApplicationGroupIdentifier:ApplicationGroup()];
28 NSURL* log_dir_url = [store_url
29 URLByAppendingPathComponent:app_group::kPendingLogFileDirectory];
30 [file_manager createDirectoryAtURL:log_dir_url
31 withIntermediateDirectories:YES
35 // File name are formated using creationtimestamp_extensionname_PendingLog
36 // (e.g: 123456789_TodayExtension_PendingLog).
37 NSString* file_name = [NSString
38 stringWithFormat:@"%ld_%@%@",
39 static_cast<long>([[NSDate date] timeIntervalSince1970]),
40 ApplicationName(application),
41 app_group::kPendingLogFileSuffix];
42 NSURL* ready_log_url = [log_dir_url URLByAppendingPathComponent:file_name];
44 [file_manager createFileAtPath:[ready_log_url path]
49 void CleanOldPendingLogs() {
50 NSFileManager* file_manager = [NSFileManager defaultManager];
51 NSURL* store_url = [file_manager
52 containerURLForSecurityApplicationGroupIdentifier:ApplicationGroup()];
53 NSURL* log_dir_url = [store_url
54 URLByAppendingPathComponent:app_group::kPendingLogFileDirectory];
56 NSArray* pending_logs =
57 [file_manager contentsOfDirectoryAtPath:[log_dir_url path] error:nil];
59 if (kMaxFileNumber >= [pending_logs count])
61 // sort by creation date
62 NSMutableArray* files_and_properties =
63 [NSMutableArray arrayWithCapacity:[pending_logs count]];
64 for (NSString* file : pending_logs) {
65 if (![file hasSuffix:app_group::kPendingLogFileSuffix])
67 NSURL* file_url = [log_dir_url URLByAppendingPathComponent:file];
69 NSDictionary* properties =
70 [file_manager attributesOfItemAtPath:[file_url path] error:nil];
71 NSDate* mod_date = [properties objectForKey:NSFileModificationDate];
73 [files_and_properties addObject:@{
75 @"lastModDate" : mod_date
79 // Sort files by modification date. Older files will be first.
80 NSArray* sorted_files =
81 [files_and_properties sortedArrayUsingComparator:^(id path1, id path2) {
82 return [[path1 objectForKey:@"lastModDate"]
83 compare:[path2 objectForKey:@"lastModDate"]];
85 if (kMaxFileNumber >= [sorted_files count])
87 NSUInteger first_file_to_keep = [sorted_files count] - kMaxFileNumber;
88 for (NSUInteger file_index = 0; file_index < first_file_to_keep;
91 [[sorted_files objectAtIndex:file_index] objectForKey:@"path"];
92 NSURL* file_url = [log_dir_url URLByAppendingPathComponent:path];
93 [file_manager removeItemAtURL:file_url error:nil];
97 } // namespace client_app
98 } // namespace app_group