Home | History | Annotate | Download | only in cocoa
      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.
      4 
      5 #import "chrome/browser/ui/cocoa/drag_util.h"
      6 
      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/webplugininfo.h"
     11 #include "ipc/ipc_message.h"
     12 #include "net/base/filename_util.h"
     13 #include "net/base/mime_util.h"
     14 #import "third_party/mozilla/NSPasteboard+Utils.h"
     15 #import "ui/base/dragdrop/cocoa_dnd_util.h"
     16 #include "url/gurl.h"
     17 #include "url/url_constants.h"
     18 
     19 using content::PluginService;
     20 
     21 namespace drag_util {
     22 
     23 namespace {
     24 
     25 BOOL IsSupportedFileURL(Profile* profile, const GURL& url) {
     26   base::FilePath full_path;
     27   net::FileURLToFilePath(url, &full_path);
     28 
     29   std::string mime_type;
     30   net::GetMimeTypeFromFile(full_path, &mime_type);
     31 
     32   // This logic mirrors |BufferedResourceHandler::ShouldDownload()|.
     33   // TODO(asvitkine): Refactor this out to a common location instead of
     34   //                  duplicating code.
     35   if (net::IsSupportedMimeType(mime_type))
     36     return YES;
     37 
     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
     40   // to do disk access.
     41   bool allow_wildcard = false;
     42   content::WebPluginInfo plugin;
     43   return PluginService::GetInstance()->GetPluginInfo(
     44       -1,                // process ID
     45       MSG_ROUTING_NONE,  // routing ID
     46       profile->GetResourceContext(),
     47       url, GURL(), mime_type, allow_wildcard,
     48       NULL, &plugin, NULL);
     49 }
     50 
     51 }  // namespace
     52 
     53 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) {
     54   if ([[info draggingPasteboard] containsURLData]) {
     55     GURL url;
     56     ui::PopulateURLAndTitleFromPasteboard(&url,
     57                                           NULL,
     58                                           [info draggingPasteboard],
     59                                           YES);
     60 
     61     if (url.SchemeIs(url::kFileScheme))
     62       return url;
     63   }
     64   return GURL();
     65 }
     66 
     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);
     73   }
     74   return NO;
     75 }
     76 
     77 }  // namespace drag_util
     78