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

Side by Side Diff: content/browser/file_metadata_mac.mm

Issue 10821069: Moving file_metadata files under content/browser/download (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Git-based patch to overcome the 'svn mv' problem Created 8 years, 4 months 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 | Annotate | Revision Log
« no previous file with comments | « content/browser/file_metadata_mac.h ('k') | content/content_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "content/browser/file_metadata_mac.h"
6
7 #include <ApplicationServices/ApplicationServices.h>
8 #include <Foundation/Foundation.h>
9
10 #include "base/file_path.h"
11 #include "base/logging.h"
12 #include "base/mac/mac_logging.h"
13 #include "base/mac/mac_util.h"
14 #include "base/mac/scoped_cftyperef.h"
15 #include "googleurl/src/gurl.h"
16
17 namespace file_metadata {
18
19 // As of Mac OS X 10.4 ("Tiger"), files can be tagged with metadata describing
20 // various attributes. Metadata is integrated with the system's Spotlight
21 // feature and is searchable. Ordinarily, metadata can only be set by
22 // Spotlight importers, which requires that the importer own the target file.
23 // However, there's an attribute intended to describe the origin of a
24 // file, that can store the source URL and referrer of a downloaded file.
25 // It's stored as a "com.apple.metadata:kMDItemWhereFroms" extended attribute,
26 // structured as a binary1-format plist containing a list of sources. This
27 // attribute can only be populated by the downloader, not a Spotlight importer.
28 // Safari on 10.4 and later populates this attribute.
29 //
30 // With this metadata set, you can locate downloads by performing a Spotlight
31 // search for their source or referrer URLs, either from within the Spotlight
32 // UI or from the command line:
33 // mdfind 'kMDItemWhereFroms == "http://releases.mozilla.org/*"'
34 //
35 // There is no documented API to set metadata on a file directly as of the
36 // 10.5 SDK. The MDSetItemAttribute function does exist to perform this task,
37 // but it's undocumented.
38 void AddOriginMetadataToFile(const FilePath& file, const GURL& source,
39 const GURL& referrer) {
40 // There's no declaration for MDItemSetAttribute in any known public SDK.
41 // It exists in the 10.4 and 10.5 runtimes. To play it safe, do the lookup
42 // at runtime instead of declaring it ourselves and linking against what's
43 // provided. This has two benefits:
44 // - If Apple relents and declares the function in a future SDK (it's
45 // happened before), our build won't break.
46 // - If Apple removes or renames the function in a future runtime, the
47 // loader won't refuse to let the application launch. Instead, we'll
48 // silently fail to set any metadata.
49 typedef OSStatus (*MDItemSetAttribute_type)(MDItemRef, CFStringRef,
50 CFTypeRef);
51 static MDItemSetAttribute_type md_item_set_attribute_func = NULL;
52
53 static bool did_symbol_lookup = false;
54 if (!did_symbol_lookup) {
55 did_symbol_lookup = true;
56 CFBundleRef metadata_bundle =
57 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Metadata"));
58 if (!metadata_bundle)
59 return;
60
61 md_item_set_attribute_func = (MDItemSetAttribute_type)
62 CFBundleGetFunctionPointerForName(metadata_bundle,
63 CFSTR("MDItemSetAttribute"));
64 }
65 if (!md_item_set_attribute_func)
66 return;
67
68 NSString* file_path =
69 [NSString stringWithUTF8String:file.value().c_str()];
70 if (!file_path)
71 return;
72
73 base::mac::ScopedCFTypeRef<MDItemRef> md_item(
74 MDItemCreate(NULL, base::mac::NSToCFCast(file_path)));
75 if (!md_item)
76 return;
77
78 // We won't put any more than 2 items into the attribute.
79 NSMutableArray* list = [NSMutableArray arrayWithCapacity:2];
80
81 // Follow Safari's lead: the first item in the list is the source URL of
82 // the downloaded file. If the referrer is known, store that, too.
83 NSString* origin_url = [NSString stringWithUTF8String:source.spec().c_str()];
84 if (origin_url)
85 [list addObject:origin_url];
86 NSString* referrer_url =
87 [NSString stringWithUTF8String:referrer.spec().c_str()];
88 if (referrer_url)
89 [list addObject:referrer_url];
90
91 md_item_set_attribute_func(md_item, kMDItemWhereFroms,
92 base::mac::NSToCFCast(list));
93 }
94
95 // The OS will automatically quarantine files due to the
96 // LSFileQuarantineEnabled entry in our Info.plist, but it knows relatively
97 // little about the files. We add more information about the download to
98 // improve the UI shown by the OS when the users tries to open the file.
99 void AddQuarantineMetadataToFile(const FilePath& file, const GURL& source,
100 const GURL& referrer) {
101 FSRef file_ref;
102 if (!base::mac::FSRefFromPath(file.value(), &file_ref))
103 return;
104
105 NSMutableDictionary* quarantine_properties = nil;
106 CFTypeRef quarantine_properties_base = NULL;
107 if (LSCopyItemAttribute(&file_ref, kLSRolesAll, kLSItemQuarantineProperties,
108 &quarantine_properties_base) == noErr) {
109 if (CFGetTypeID(quarantine_properties_base) ==
110 CFDictionaryGetTypeID()) {
111 // Quarantine properties will already exist if LSFileQuarantineEnabled
112 // is on and the file doesn't match an exclusion.
113 quarantine_properties =
114 [[(NSDictionary*)quarantine_properties_base mutableCopy] autorelease];
115 } else {
116 LOG(WARNING) << "kLSItemQuarantineProperties is not a dictionary on file "
117 << file.value();
118 }
119 CFRelease(quarantine_properties_base);
120 }
121
122 if (!quarantine_properties) {
123 // If there are no quarantine properties, then the file isn't quarantined
124 // (e.g., because the user has set up exclusions for certain file types).
125 // We don't want to add any metadata, because that will cause the file to
126 // be quarantined against the user's wishes.
127 return;
128 }
129
130 // kLSQuarantineAgentNameKey, kLSQuarantineAgentBundleIdentifierKey, and
131 // kLSQuarantineTimeStampKey are set for us (see LSQuarantine.h), so we only
132 // need to set the values that the OS can't infer.
133
134 if (![quarantine_properties valueForKey:(NSString*)kLSQuarantineTypeKey]) {
135 CFStringRef type = (source.SchemeIs("http") || source.SchemeIs("https"))
136 ? kLSQuarantineTypeWebDownload
137 : kLSQuarantineTypeOtherDownload;
138 [quarantine_properties setValue:(NSString*)type
139 forKey:(NSString*)kLSQuarantineTypeKey];
140 }
141
142 if (![quarantine_properties
143 valueForKey:(NSString*)kLSQuarantineOriginURLKey] &&
144 referrer.is_valid()) {
145 NSString* referrer_url =
146 [NSString stringWithUTF8String:referrer.spec().c_str()];
147 [quarantine_properties setValue:referrer_url
148 forKey:(NSString*)kLSQuarantineOriginURLKey];
149 }
150
151 if (![quarantine_properties valueForKey:(NSString*)kLSQuarantineDataURLKey] &&
152 source.is_valid()) {
153 NSString* origin_url =
154 [NSString stringWithUTF8String:source.spec().c_str()];
155 [quarantine_properties setValue:origin_url
156 forKey:(NSString*)kLSQuarantineDataURLKey];
157 }
158
159 OSStatus os_error = LSSetItemAttribute(&file_ref, kLSRolesAll,
160 kLSItemQuarantineProperties,
161 quarantine_properties);
162 if (os_error != noErr) {
163 OSSTATUS_LOG(WARNING, os_error)
164 << "Unable to set quarantine attributes on file " << file.value();
165 }
166 }
167
168 } // namespace file_metadata
OLDNEW
« no previous file with comments | « content/browser/file_metadata_mac.h ('k') | content/content_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698