OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 #include "chrome/common/quarantine_mac.h" | |
6 | |
7 #include <ApplicationServices/ApplicationServices.h> | |
8 #import <Foundation/Foundation.h> | |
9 | |
10 #include "base/file_path.h" | |
11 #include "base/logging.h" | |
12 #include "base/mac_util.h" | |
13 #include "googleurl/src/gurl.h" | |
14 | |
15 namespace quarantine_mac { | |
16 | |
17 // The OS will automatically quarantine files due to the | |
18 // LSFileQuarantineEnabled entry in our Info.plist, but it knows relatively | |
19 // little about the files. We add more information about the download to | |
20 // improve the UI shown by the OS when the users tries to open the file. | |
21 void AddQuarantineMetadataToFile(const FilePath& file, const GURL& source, | |
22 const GURL& referrer) { | |
23 FSRef file_ref; | |
24 if (!mac_util::FSRefFromPath(file.value(), &file_ref)) | |
25 return; | |
26 | |
27 NSMutableDictionary* quarantine_properties = nil; | |
28 CFTypeRef quarantine_properties_base = NULL; | |
29 if (::LSCopyItemAttribute(&file_ref, kLSRolesAll, kLSItemQuarantineProperties, | |
30 &quarantine_properties_base) == noErr) { | |
31 if (::CFGetTypeID(quarantine_properties_base) == | |
32 ::CFDictionaryGetTypeID()) { | |
33 // Quarantine properties will already exist if LSFileQuarantineEnabled | |
34 // is on and the file doesn't match an exclusion. | |
35 quarantine_properties = | |
36 [[(NSDictionary*)quarantine_properties_base mutableCopy] autorelease]; | |
37 } else { | |
38 LOG(WARNING) << "kLSItemQuarantineProperties is not a dictionary on file " | |
39 << file.value(); | |
40 } | |
41 ::CFRelease(quarantine_properties_base); | |
42 } | |
43 | |
44 if (!quarantine_properties) { | |
45 // If there are no quarantine properties, then the file isn't quarantined | |
46 // (e.g., because the user has set up exclusions for certain file types). | |
47 // We don't want to add any metadata, because that will cause the file to | |
48 // be quarantined against the user's wishes. | |
49 return; | |
50 } | |
51 | |
52 // kLSQuarantineAgentNameKey, kLSQuarantineAgentBundleIdentifierKey, and | |
53 // kLSQuarantineTimeStampKey are set for us (see LSQuarantine.h), so we only | |
54 // need to set the values that the OS can't infer. | |
55 | |
56 if (![quarantine_properties valueForKey:(NSString*)kLSQuarantineTypeKey]) { | |
57 CFStringRef type = (source.SchemeIs("http") || source.SchemeIs("https")) | |
58 ? kLSQuarantineTypeWebDownload | |
59 : kLSQuarantineTypeOtherDownload; | |
60 [quarantine_properties setValue:(NSString*)type | |
61 forKey:(NSString*)kLSQuarantineTypeKey]; | |
62 } | |
63 | |
64 if (![quarantine_properties | |
65 valueForKey:(NSString*)kLSQuarantineOriginURLKey] && | |
66 referrer.is_valid()) { | |
67 NSString* referrer_url = | |
68 [NSString stringWithUTF8String:referrer.spec().c_str()]; | |
69 [quarantine_properties setValue:referrer_url | |
70 forKey:(NSString*)kLSQuarantineOriginURLKey]; | |
71 } | |
72 | |
73 if (![quarantine_properties valueForKey:(NSString*)kLSQuarantineDataURLKey] && | |
74 source.is_valid()) { | |
75 NSString* origin_url = | |
76 [NSString stringWithUTF8String:source.spec().c_str()]; | |
77 [quarantine_properties setValue:origin_url | |
78 forKey:(NSString*)kLSQuarantineDataURLKey]; | |
79 } | |
80 | |
81 OSStatus os_error = ::LSSetItemAttribute(&file_ref, kLSRolesAll, | |
82 kLSItemQuarantineProperties, | |
83 quarantine_properties); | |
84 if (os_error != noErr) { | |
85 LOG(WARNING) << "Unable to set quarantine attributes on file " | |
86 << file.value(); | |
87 } | |
88 } | |
89 | |
90 } // namespace quarantine_mac | |
OLD | NEW |