1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DownloadPlatform.h"
#include "nsNetUtil.h"
#include "nsString.h"
#include "nsINestedURI.h"
#include "nsIProtocolHandler.h"
#include "nsIURI.h"
#include "nsIFile.h"
#include "xpcpublic.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/Preferences.h"
#define PREF_BDM_ADDTORECENTDOCS "browser.download.manager.addToRecentDocs"
#ifdef XP_WIN
# include <shlobj.h>
# include <urlmon.h>
# include "nsILocalFileWin.h"
# include "WinTaskbar.h"
#endif
#ifdef XP_MACOSX
# include <CoreFoundation/CoreFoundation.h>
# include "../../../xpcom/io/CocoaFileUtils.h"
#endif
#ifdef MOZ_WIDGET_GTK
# include <gtk/gtk.h>
#endif
using namespace mozilla;
using dom::Promise;
DownloadPlatform* DownloadPlatform::gDownloadPlatformService = nullptr;
NS_IMPL_ISUPPORTS(DownloadPlatform, mozIDownloadPlatform);
DownloadPlatform* DownloadPlatform::GetDownloadPlatform() {
if (!gDownloadPlatformService) {
gDownloadPlatformService = new DownloadPlatform();
}
NS_ADDREF(gDownloadPlatformService);
return gDownloadPlatformService;
}
#ifdef MOZ_WIDGET_GTK
static void gio_set_metadata_done(GObject* source_obj, GAsyncResult* res,
gpointer user_data) {
GError* err = nullptr;
g_file_set_attributes_finish(G_FILE(source_obj), res, nullptr, &err);
if (err) {
# ifdef DEBUG
NS_DebugBreak(NS_DEBUG_WARNING, "Set file metadata failed: ", err->message,
__FILE__, __LINE__);
# endif
g_error_free(err);
}
}
#endif
#ifdef XP_MACOSX
// Caller is responsible for freeing any result (CF Create Rule)
CFURLRef CreateCFURLFromNSIURI(nsIURI* aURI) {
nsAutoCString spec;
if (aURI) {
aURI->GetSpec(spec);
}
CFStringRef urlStr = ::CFStringCreateWithCString(
kCFAllocatorDefault, spec.get(), kCFStringEncodingUTF8);
if (!urlStr) {
return NULL;
}
CFURLRef url = ::CFURLCreateWithString(kCFAllocatorDefault, urlStr, NULL);
::CFRelease(urlStr);
return url;
}
#endif
#ifdef XP_WIN
static void AddToRecentDocs(nsIFile* aTarget, nsAutoString& aPath) {
nsString modelId;
if (mozilla::widget::WinTaskbar::GetAppUserModelID(modelId)) {
nsCOMPtr<nsIURI> uri;
if (NS_SUCCEEDED(NS_NewFileURI(getter_AddRefs(uri), aTarget)) && uri) {
nsCString spec;
if (NS_SUCCEEDED(uri->GetSpec(spec))) {
IShellItem2* psi = nullptr;
if (SUCCEEDED(
SHCreateItemFromParsingName(NS_ConvertASCIItoUTF16(spec).get(),
nullptr, IID_PPV_ARGS(&psi)))) {
SHARDAPPIDINFO info = {psi, modelId.get()};
::SHAddToRecentDocs(SHARD_APPIDINFO, &info);
psi->Release();
return;
}
}
}
}
::SHAddToRecentDocs(SHARD_PATHW, aPath.get());
}
#endif
nsresult DownloadPlatform::DownloadDone(nsIURI* aSource, nsIURI* aReferrer,
nsIFile* aTarget,
const nsACString& aContentType,
bool aIsPrivate, JSContext* aCx,
Promise** aPromise) {
nsIGlobalObject* globalObject =
xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
if (NS_WARN_IF(!globalObject)) {
return NS_ERROR_FAILURE;
}
ErrorResult result;
RefPtr<Promise> promise = Promise::Create(globalObject, result);
if (NS_WARN_IF(result.Failed())) {
return result.StealNSResult();
}
nsresult rv = NS_OK;
bool pendingAsyncOperations = false;
#if defined(XP_WIN) || defined(XP_MACOSX) || defined(MOZ_WIDGET_ANDROID) || \
defined(MOZ_WIDGET_GTK)
nsAutoString path;
if (aTarget && NS_SUCCEEDED(aTarget->GetPath(path))) {
# if defined(XP_WIN) || defined(MOZ_WIDGET_GTK) || defined(MOZ_WIDGET_ANDROID)
// On Windows and Gtk, add the download to the system's "recent documents"
// list, with a pref to disable.
{
# ifndef MOZ_WIDGET_ANDROID
bool addToRecentDocs = Preferences::GetBool(PREF_BDM_ADDTORECENTDOCS);
if (addToRecentDocs && !aIsPrivate) {
# ifdef XP_WIN
AddToRecentDocs(aTarget, path);
# elif defined(MOZ_WIDGET_GTK)
GtkRecentManager* manager = gtk_recent_manager_get_default();
gchar* uri = g_filename_to_uri(NS_ConvertUTF16toUTF8(path).get(),
nullptr, nullptr);
if (uri) {
gtk_recent_manager_add_item(manager, uri);
g_free(uri);
}
# endif
}
# endif
# ifdef MOZ_WIDGET_GTK
// Private window should not leak URI to the system (Bug 1535950)
if (!aIsPrivate) {
// Use GIO to store the source URI for later display in the file
// manager.
GFile* gio_file =
g_file_new_for_path(NS_ConvertUTF16toUTF8(path).get());
nsCString source_uri;
nsresult rv = aSource->GetSpec(source_uri);
NS_ENSURE_SUCCESS(rv, rv);
GFileInfo* file_info = g_file_info_new();
g_file_info_set_attribute_string(file_info, "metadata::download-uri",
source_uri.get());
g_file_set_attributes_async(gio_file, file_info, G_FILE_QUERY_INFO_NONE,
G_PRIORITY_DEFAULT, nullptr,
gio_set_metadata_done, nullptr);
g_object_unref(file_info);
g_object_unref(gio_file);
}
# endif
}
# endif
# ifdef XP_MACOSX
// On OS X, make the downloads stack bounce.
CFStringRef observedObject = ::CFStringCreateWithCString(
kCFAllocatorDefault, NS_ConvertUTF16toUTF8(path).get(),
kCFStringEncodingUTF8);
CFNotificationCenterRef center =
::CFNotificationCenterGetDistributedCenter();
::CFNotificationCenterPostNotification(
center, CFSTR("com.apple.DownloadFileFinished"), observedObject,
nullptr, TRUE);
::CFRelease(observedObject);
// Add OS X origin and referrer file metadata
CFStringRef pathCFStr = NULL;
if (!path.IsEmpty()) {
pathCFStr = ::CFStringCreateWithCharacters(
kCFAllocatorDefault, (const UniChar*)path.get(), path.Length());
}
if (pathCFStr && !aIsPrivate) {
bool isFromWeb = IsURLPossiblyFromWeb(aSource);
nsCOMPtr<nsIURI> source(aSource);
nsCOMPtr<nsIURI> referrer(aReferrer);
rv = NS_DispatchBackgroundTask(
NS_NewRunnableFunction(
"DownloadPlatform::DownloadDone",
[pathCFStr, isFromWeb, source, referrer, promise]() mutable {
CFURLRef sourceCFURL = CreateCFURLFromNSIURI(source);
CFURLRef referrerCFURL = CreateCFURLFromNSIURI(referrer);
CocoaFileUtils::AddOriginMetadataToFile(pathCFStr, sourceCFURL,
referrerCFURL);
CocoaFileUtils::AddQuarantineMetadataToFile(
pathCFStr, sourceCFURL, referrerCFURL, isFromWeb);
::CFRelease(pathCFStr);
if (sourceCFURL) {
::CFRelease(sourceCFURL);
}
if (referrerCFURL) {
::CFRelease(referrerCFURL);
}
DebugOnly<nsresult> rv =
NS_DispatchToMainThread(NS_NewRunnableFunction(
"DownloadPlatform::DownloadDoneResolve",
[promise = std::move(promise)]() {
promise->MaybeResolveWithUndefined();
}));
MOZ_ASSERT(NS_SUCCEEDED(rv));
// In non-debug builds, if we've for some reason failed to
// dispatch a runnable to the main thread to resolve the
// Promise, then it's unlikely we can reject it either. At that
// point, the Promise is going to remain in pending limbo until
// its global goes away.
}),
NS_DISPATCH_EVENT_MAY_BLOCK);
if (NS_SUCCEEDED(rv)) {
pendingAsyncOperations = true;
}
}
# endif
}
#endif
if (!pendingAsyncOperations) {
promise->MaybeResolveWithUndefined();
}
promise.forget(aPromise);
return rv;
}
nsresult DownloadPlatform::MapUrlToZone(const nsAString& aURL,
uint32_t* aZone) {
#ifdef XP_WIN
RefPtr<IInternetSecurityManager> inetSecMgr;
if (FAILED(CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_ALL,
IID_IInternetSecurityManager,
getter_AddRefs(inetSecMgr)))) {
return NS_ERROR_UNEXPECTED;
}
DWORD zone;
if (inetSecMgr->MapUrlToZone(PromiseFlatString(aURL).get(), &zone, 0) !=
S_OK) {
return NS_ERROR_UNEXPECTED;
} else {
*aZone = zone;
}
return NS_OK;
#else
return NS_ERROR_NOT_IMPLEMENTED;
#endif
}
// Check if a URI is likely to be web-based, by checking its URI flags.
// If in doubt (e.g. if anything fails during the check) claims things
// are from the web.
bool DownloadPlatform::IsURLPossiblyFromWeb(nsIURI* aURI) {
nsCOMPtr<nsIIOService> ios = do_GetIOService();
nsCOMPtr<nsIURI> uri = aURI;
if (!ios) {
return true;
}
while (uri) {
// We're not using NS_URIChainHasFlags because we're checking for *any* of 3
// flags to be present on *all* of the nested URIs, which it can't do.
uint32_t flags;
nsresult rv = ios->GetDynamicProtocolFlags(uri, &flags);
if (NS_FAILED(rv)) {
return true;
}
// If not dangerous to load, not a UI resource and not a local file,
// assume this is from the web:
if (!(flags & nsIProtocolHandler::URI_DANGEROUS_TO_LOAD) &&
!(flags & nsIProtocolHandler::URI_IS_UI_RESOURCE) &&
!(flags & nsIProtocolHandler::URI_IS_LOCAL_FILE)) {
return true;
}
// Otherwise, check if the URI is nested, and if so go through
// the loop again:
nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(uri);
uri = nullptr;
if (nestedURI) {
rv = nestedURI->GetInnerURI(getter_AddRefs(uri));
if (NS_FAILED(rv)) {
return true;
}
}
}
return false;
}
|