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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/drag_util.h"
7 #include "base/files/file_path.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "content/public/browser/plugin_service.h"
10 #include "content/public/common/url_constants.h"
11 #include "content/public/common/webplugininfo.h"
12 #include "ipc/ipc_message.h"
13 #include "net/base/filename_util.h"
14 #include "net/base/mime_util.h"
15 #import "third_party/mozilla/NSPasteboard+Utils.h"
16 #import "ui/base/dragdrop/cocoa_dnd_util.h"
19 using content::PluginService;
25 BOOL IsSupportedFileURL(Profile* profile, const GURL& url) {
26 base::FilePath full_path;
27 net::FileURLToFilePath(url, &full_path);
29 std::string mime_type;
30 net::GetMimeTypeFromFile(full_path, &mime_type);
32 // This logic mirrors |BufferedResourceHandler::ShouldDownload()|.
33 // TODO(asvitkine): Refactor this out to a common location instead of
35 if (net::IsSupportedMimeType(mime_type))
38 // Check whether there is a plugin that supports the mime type. (e.g. PDF)
39 // TODO(bauerb): This possibly uses stale information, but it's guaranteed not
41 bool allow_wildcard = false;
42 content::WebPluginInfo plugin;
43 return PluginService::GetInstance()->GetPluginInfo(
45 MSG_ROUTING_NONE, // routing ID
46 profile->GetResourceContext(),
47 url, GURL(), mime_type, allow_wildcard,
53 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) {
54 if ([[info draggingPasteboard] containsURLData]) {
56 ui::PopulateURLAndTitleFromPasteboard(&url,
58 [info draggingPasteboard],
61 if (url.SchemeIs(content::kFileScheme))
67 BOOL IsUnsupportedDropData(Profile* profile, id<NSDraggingInfo> info) {
68 GURL url = GetFileURLFromDropData(info);
69 if (!url.is_empty()) {
70 // If dragging a file, only allow dropping supported file types (that the
71 // web view can display).
72 return !IsSupportedFileURL(profile, url);
77 } // namespace drag_util