toolkit/components/jsdownloads/src/DownloadPlatform.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "DownloadPlatform.h"
     6 #include "nsAutoPtr.h"
     7 #include "nsString.h"
     8 #include "nsIURI.h"
     9 #include "nsIFile.h"
    10 #include "nsDirectoryServiceDefs.h"
    12 #include "mozilla/Preferences.h"
    14 #define PREF_BDM_ADDTORECENTDOCS "browser.download.manager.addToRecentDocs"
    16 #ifdef XP_WIN
    17 #include <shlobj.h>
    18 #include <urlmon.h>
    19 #include "nsILocalFileWin.h"
    20 #endif
    22 #ifdef XP_MACOSX
    23 #include <CoreFoundation/CoreFoundation.h>
    24 #endif
    26 #ifdef MOZ_WIDGET_ANDROID
    27 #include "AndroidBridge.h"
    28 #endif
    30 #ifdef MOZ_WIDGET_GTK
    31 #include <gtk/gtk.h>
    32 #endif
    34 using namespace mozilla;
    36 DownloadPlatform *DownloadPlatform::gDownloadPlatformService = nullptr;
    38 NS_IMPL_ISUPPORTS(DownloadPlatform, mozIDownloadPlatform);
    40 DownloadPlatform* DownloadPlatform::GetDownloadPlatform()
    41 {
    42   if (!gDownloadPlatformService) {
    43     gDownloadPlatformService = new DownloadPlatform();
    44   }
    46   NS_ADDREF(gDownloadPlatformService);
    48 #if defined(MOZ_WIDGET_GTK)
    49   g_type_init();
    50 #endif
    52   return gDownloadPlatformService;
    53 }
    55 #ifdef MOZ_ENABLE_GIO
    56 static void gio_set_metadata_done(GObject *source_obj, GAsyncResult *res, gpointer user_data)
    57 {
    58   GError *err = nullptr;
    59   g_file_set_attributes_finish(G_FILE(source_obj), res, nullptr, &err);
    60   if (err) {
    61 #ifdef DEBUG
    62     NS_DebugBreak(NS_DEBUG_WARNING, "Set file metadata failed: ", err->message, __FILE__, __LINE__);
    63 #endif
    64     g_error_free(err);
    65   }
    66 }
    67 #endif
    69 nsresult DownloadPlatform::DownloadDone(nsIURI* aSource, nsIFile* aTarget,
    70                                         const nsACString& aContentType, bool aIsPrivate)
    71 {
    72 #if defined(XP_WIN) || defined(XP_MACOSX) || defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK)
    73   nsAutoString path;
    74   if (aTarget && NS_SUCCEEDED(aTarget->GetPath(path))) {
    75 #if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
    76     // On Windows and Gtk, add the download to the system's "recent documents"
    77     // list, with a pref to disable.
    78     {
    79       bool addToRecentDocs = Preferences::GetBool(PREF_BDM_ADDTORECENTDOCS);
    80       if (addToRecentDocs && !aIsPrivate) {
    81 #ifdef XP_WIN
    82         ::SHAddToRecentDocs(SHARD_PATHW, path.get());
    83 #elif defined(MOZ_WIDGET_GTK)
    84         GtkRecentManager* manager = gtk_recent_manager_get_default();
    86         gchar* uri = g_filename_to_uri(NS_ConvertUTF16toUTF8(path).get(),
    87                                        nullptr, nullptr);
    88         if (uri) {
    89           gtk_recent_manager_add_item(manager, uri);
    90           g_free(uri);
    91         }
    92 #endif
    93       }
    94 #ifdef MOZ_ENABLE_GIO
    95       // Use GIO to store the source URI for later display in the file manager.
    96       GFile* gio_file = g_file_new_for_path(NS_ConvertUTF16toUTF8(path).get());
    97       nsCString source_uri;
    98       aSource->GetSpec(source_uri);
    99       GFileInfo *file_info = g_file_info_new();
   100       g_file_info_set_attribute_string(file_info, "metadata::download-uri", source_uri.get());
   101       g_file_set_attributes_async(gio_file,
   102                                   file_info,
   103                                   G_FILE_QUERY_INFO_NONE,
   104                                   G_PRIORITY_DEFAULT,
   105                                   nullptr, gio_set_metadata_done, nullptr);
   106       g_object_unref(file_info);
   107       g_object_unref(gio_file);
   108 #endif
   109     }
   110 #endif
   111 #ifdef XP_MACOSX
   112     // On OS X, make the downloads stack bounce.
   113     CFStringRef observedObject = ::CFStringCreateWithCString(kCFAllocatorDefault,
   114                                              NS_ConvertUTF16toUTF8(path).get(),
   115                                              kCFStringEncodingUTF8);
   116     CFNotificationCenterRef center = ::CFNotificationCenterGetDistributedCenter();
   117     ::CFNotificationCenterPostNotification(center, CFSTR("com.apple.DownloadFileFinished"),
   118                                            observedObject, nullptr, TRUE);
   119     ::CFRelease(observedObject);
   120 #endif
   121 #ifdef MOZ_WIDGET_ANDROID
   122     if (!aContentType.IsEmpty()) {
   123       mozilla::widget::android::GeckoAppShell::ScanMedia(path, NS_ConvertUTF8toUTF16(aContentType));
   124     }
   125 #endif
   126   }
   128 #ifdef XP_WIN
   129   // Adjust file attributes so that by default, new files are indexed by
   130   // desktop search services. Skip off those that land in the temp folder.
   131   nsCOMPtr<nsIFile> tempDir, fileDir;
   132   nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tempDir));
   133   NS_ENSURE_SUCCESS(rv, rv);
   134   aTarget->GetParent(getter_AddRefs(fileDir));
   136   bool isTemp = false;
   137   if (fileDir) {
   138     fileDir->Equals(tempDir, &isTemp);
   139   }
   141   nsCOMPtr<nsILocalFileWin> localFileWin(do_QueryInterface(aTarget));
   142   if (!isTemp && localFileWin) {
   143     localFileWin->SetFileAttributesWin(nsILocalFileWin::WFA_SEARCH_INDEXED);
   144   }
   145 #endif
   147 #endif
   149   return NS_OK;
   150 }
   152 nsresult DownloadPlatform::MapUrlToZone(const nsAString& aURL,
   153                                         uint32_t* aZone)
   154 {
   155 #ifdef XP_WIN
   156   nsRefPtr<IInternetSecurityManager> inetSecMgr;
   157   if (FAILED(CoCreateInstance(CLSID_InternetSecurityManager, NULL,
   158                               CLSCTX_ALL, IID_IInternetSecurityManager,
   159                               getter_AddRefs(inetSecMgr)))) {
   160     return NS_ERROR_UNEXPECTED;
   161   }
   163   DWORD zone;
   164   if (inetSecMgr->MapUrlToZone(PromiseFlatString(aURL).get(),
   165                                &zone, 0) != S_OK) {
   166     return NS_ERROR_UNEXPECTED;
   167   } else {
   168     *aZone = zone;
   169   }
   171   return NS_OK;
   172 #else
   173   return NS_ERROR_NOT_IMPLEMENTED;
   174 #endif
   175 }

mercurial