Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: ios/chrome/browser/ui/external_file_controller.mm

Issue 2588713002: Upstream Chrome on iOS source code [4/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #import "ios/chrome/browser/ui/external_file_controller.h"
6
7 #import <WebKit/WebKit.h>
8
9 #include "base/logging.h"
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/threading/thread_restrictions.h"
13 #import "ios/chrome/browser/tabs/tab_model.h"
14 #import "ios/web/public/web_view_creation_util.h"
15
16 namespace {
17
18 // The path relative to the <Application_Home>/Documents/ directory where the
19 // files received from other applications are saved.
20 NSString* const kInboxPath = @"Inbox";
21
22 // Conversion factor to turn number of days to number of seconds.
23 const CFTimeInterval kSecondsPerDay = 60 * 60 * 24;
24
25 } // namespace
26
27 @interface ExternalFileController ()
28 // Returns the path to the Inbox directory.
29 + (NSString*)inboxDirectoryPath;
30 @end
31
32 @implementation ExternalFileController {
33 base::scoped_nsobject<WKWebView> _webView;
34 }
35
36 - (instancetype)initWithURL:(const GURL&)URL
37 browserState:(web::BrowserState*)browserState {
38 self = [super initWithURL:URL];
39 if (self) {
40 _webView.reset([web::BuildWKWebView(CGRectZero, browserState) retain]);
41 [_webView setBackgroundColor:[UIColor whiteColor]];
42 [_webView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
43 UIViewAutoresizingFlexibleHeight)];
44 [_webView setUserInteractionEnabled:YES];
45 base::scoped_nsobject<UIView> view(
46 [[UIView alloc] initWithFrame:CGRectZero]);
47 self.view = view;
48 [self.view addSubview:_webView];
49 [self.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
50 UIViewAutoresizingFlexibleHeight)];
51 NSString* filePath = [ExternalFileController pathForExternalFileURL:URL];
52 DCHECK([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
53 // TODO(cgrigoruta): Currently only PDFs are supported. Change it to support
54 // other file types as well.
55 NSURL* fileURL = [NSURL fileURLWithPath:filePath];
56 DCHECK(fileURL);
57 [_webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
58 }
59 return self;
60 }
61
62 + (NSString*)inboxDirectoryPath {
63 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
64 NSUserDomainMask, YES);
65 if ([paths count] < 1)
66 return nil;
67
68 NSString* documentsDirectoryPath = [paths objectAtIndex:0];
69 return [documentsDirectoryPath stringByAppendingPathComponent:kInboxPath];
70 }
71
72 + (NSString*)pathForExternalFileURL:(const GURL&)url {
73 NSString* fileName = base::SysUTF8ToNSString(url.ExtractFileName());
74 return [[ExternalFileController inboxDirectoryPath]
75 stringByAppendingPathComponent:[fileName
76 stringByRemovingPercentEncoding]];
77 }
78
79 + (void)removeFilesExcluding:(NSSet*)filesToKeep
80 olderThan:(NSInteger)ageInDays {
81 base::ThreadRestrictions::AssertIOAllowed();
82 NSFileManager* fileManager = [NSFileManager defaultManager];
83 NSString* inboxDirectory = [ExternalFileController inboxDirectoryPath];
84 NSArray* externalFiles =
85 [fileManager contentsOfDirectoryAtPath:inboxDirectory error:nil];
86 for (NSString* filename in externalFiles) {
87 NSString* filePath =
88 [inboxDirectory stringByAppendingPathComponent:filename];
89 if ([filesToKeep containsObject:filename])
90 continue;
91 // Checks the age of the file and do not remove files that are too recent.
92 // Under normal circumstances, e.g. when file purge is not initiated by
93 // user action, leave recently downloaded files around to avoid users
94 // using history back or recent tabs to reach an external file that was
95 // pre-maturely purged.
96 NSError* error = nil;
97 NSDictionary* attributesDictionary =
98 [fileManager attributesOfItemAtPath:filePath error:&error];
99 if (error) {
100 DLOG(ERROR) << "Failed to retrieve attributes for " << filePath << ": "
101 << base::SysNSStringToUTF8([error description]);
102 continue;
103 }
104 NSDate* date = [attributesDictionary objectForKey:NSFileCreationDate];
105 if (-[date timeIntervalSinceNow] <= (ageInDays * kSecondsPerDay))
106 continue;
107 // Removes the file.
108 [fileManager removeItemAtPath:filePath error:&error];
109 if (error) {
110 DLOG(ERROR) << "Failed to remove file " << filePath << ": "
111 << base::SysNSStringToUTF8([error description]);
112 continue;
113 }
114 }
115 }
116
117 - (void)dealloc {
118 [_webView removeFromSuperview];
119 [super dealloc];
120 }
121
122 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/external_file_controller.h ('k') | ios/chrome/browser/ui/external_file_remover.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698