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