OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/drag_util.h" | 5 #import "chrome/browser/ui/cocoa/drag_util.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/file_path.h" |
8 #include "base/logging.h" | |
9 #include "base/sys_string_conversions.h" | |
10 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
11 #include "content/browser/plugin_service.h" | 9 #include "content/browser/plugin_service.h" |
12 #include "content/public/common/url_constants.h" | 10 #include "content/public/common/url_constants.h" |
13 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
14 #include "net/base/mime_util.h" | 12 #include "net/base/mime_util.h" |
15 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
16 #import "third_party/mozilla/NSPasteboard+Utils.h" | 14 #import "third_party/mozilla/NSPasteboard+Utils.h" |
17 #include "webkit/plugins/npapi/plugin_list.h" | 15 #import "ui/base/dragdrop/cocoa_dnd_util.h" |
18 | 16 |
19 namespace drag_util { | 17 namespace drag_util { |
20 | 18 |
21 BOOL PopulateURLAndTitleFromPasteBoard(GURL* url, | 19 namespace { |
22 string16* title, | |
23 NSPasteboard* pboard, | |
24 BOOL convert_filenames) { | |
25 CHECK(url); | |
26 | 20 |
27 // Bail out early if there's no URL data. | 21 BOOL IsSupportedFileURL(Profile* profile, const GURL& url) { |
28 if (![pboard containsURLData]) | |
29 return NO; | |
30 | |
31 // -getURLs:andTitles:convertingFilenames: will already validate URIs so we | |
32 // don't need to again. The arrays returned are both of NSStrings. | |
33 NSArray* url_array = nil; | |
34 NSArray* title_array = nil; | |
35 [pboard getURLs:&url_array andTitles:&title_array | |
36 convertingFilenames:convert_filenames]; | |
37 DCHECK_EQ([url_array count], [title_array count]); | |
38 // It's possible that no URLs were actually provided! | |
39 if (![url_array count]) | |
40 return NO; | |
41 NSString* url_string = [url_array objectAtIndex:0]; | |
42 if ([url_string length]) { | |
43 // Check again just to make sure to not assign NULL into a std::string, | |
44 // which throws an exception. | |
45 const char* utf8_url = [url_string UTF8String]; | |
46 if (utf8_url) { | |
47 *url = GURL(utf8_url); | |
48 // Extra paranoia check. | |
49 if (title && [title_array count]) | |
50 *title = base::SysNSStringToUTF16([title_array objectAtIndex:0]); | |
51 } | |
52 } | |
53 return YES; | |
54 } | |
55 | |
56 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) { | |
57 if ([[info draggingPasteboard] containsURLData]) { | |
58 GURL url; | |
59 PopulateURLAndTitleFromPasteBoard(&url, | |
60 NULL, | |
61 [info draggingPasteboard], | |
62 YES); | |
63 | |
64 if (url.SchemeIs(chrome::kFileScheme)) | |
65 return url; | |
66 } | |
67 return GURL(); | |
68 } | |
69 | |
70 static BOOL IsSupportedFileURL(Profile* profile, const GURL& url) { | |
71 FilePath full_path; | 22 FilePath full_path; |
72 net::FileURLToFilePath(url, &full_path); | 23 net::FileURLToFilePath(url, &full_path); |
73 | 24 |
74 std::string mime_type; | 25 std::string mime_type; |
75 net::GetMimeTypeFromFile(full_path, &mime_type); | 26 net::GetMimeTypeFromFile(full_path, &mime_type); |
76 | 27 |
77 // This logic mirrors |BufferedResourceHandler::ShouldDownload()|. | 28 // This logic mirrors |BufferedResourceHandler::ShouldDownload()|. |
78 // TODO(asvitkine): Refactor this out to a common location instead of | 29 // TODO(asvitkine): Refactor this out to a common location instead of |
79 // duplicating code. | 30 // duplicating code. |
80 if (net::IsSupportedMimeType(mime_type)) | 31 if (net::IsSupportedMimeType(mime_type)) |
81 return YES; | 32 return YES; |
82 | 33 |
83 // Check whether there is a plugin that supports the mime type. (e.g. PDF) | 34 // Check whether there is a plugin that supports the mime type. (e.g. PDF) |
84 // TODO(bauerb): This possibly uses stale information, but it's guaranteed not | 35 // TODO(bauerb): This possibly uses stale information, but it's guaranteed not |
85 // to do disk access. | 36 // to do disk access. |
86 bool allow_wildcard = false; | 37 bool allow_wildcard = false; |
87 webkit::WebPluginInfo plugin; | 38 webkit::WebPluginInfo plugin; |
88 return PluginService::GetInstance()->GetPluginInfo( | 39 return PluginService::GetInstance()->GetPluginInfo( |
89 -1, // process ID | 40 -1, // process ID |
90 MSG_ROUTING_NONE, // routing ID | 41 MSG_ROUTING_NONE, // routing ID |
91 profile->GetResourceContext(), | 42 profile->GetResourceContext(), |
92 url, GURL(), mime_type, allow_wildcard, | 43 url, GURL(), mime_type, allow_wildcard, |
93 NULL, &plugin, NULL); | 44 NULL, &plugin, NULL); |
94 } | 45 } |
95 | 46 |
| 47 } // namespace |
| 48 |
| 49 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) { |
| 50 if ([[info draggingPasteboard] containsURLData]) { |
| 51 GURL url; |
| 52 ui::PopulateURLAndTitleFromPasteboard(&url, |
| 53 NULL, |
| 54 [info draggingPasteboard], |
| 55 YES); |
| 56 |
| 57 if (url.SchemeIs(chrome::kFileScheme)) |
| 58 return url; |
| 59 } |
| 60 return GURL(); |
| 61 } |
| 62 |
96 BOOL IsUnsupportedDropData(Profile* profile, id<NSDraggingInfo> info) { | 63 BOOL IsUnsupportedDropData(Profile* profile, id<NSDraggingInfo> info) { |
97 GURL url = GetFileURLFromDropData(info); | 64 GURL url = GetFileURLFromDropData(info); |
98 if (!url.is_empty()) { | 65 if (!url.is_empty()) { |
99 // If dragging a file, only allow dropping supported file types (that the | 66 // If dragging a file, only allow dropping supported file types (that the |
100 // web view can display). | 67 // web view can display). |
101 return !IsSupportedFileURL(profile, url); | 68 return !IsSupportedFileURL(profile, url); |
102 } | 69 } |
103 return NO; | 70 return NO; |
104 } | 71 } |
105 | 72 |
106 } // namespace drag_util | 73 } // namespace drag_util |
OLD | NEW |