Skip to content

Commit 92a4711

Browse files
Bug 1868410 - MSIX set to default browser takes too long r=nalexander,nrishel
This fix addresses cleanup work from https://phabricator.services.mozilla.com/D194828 It also makes it so that all file/protocol handlers get set with one launch of Powershell, to speed things up. Which it does. A lot. It also: * makes somethings use of nsString (where it was easy to do) * moves the thread managing code out of SetDefaultBrowser.cpp and into DefaultAgent.cpp * puts auto in a couple of places to make the code easier to read * removes some logging statements in the powershell script code Differential Revision: https://phabricator.services.mozilla.com/D195839
1 parent baa791e commit 92a4711

File tree

5 files changed

+405
-222
lines changed

5 files changed

+405
-222
lines changed

browser/components/shell/WindowsUserChoice.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ UniquePtr<wchar_t[]> GetAssociationKeyPath(const wchar_t* aExt) {
284284
return keyPath;
285285
}
286286

287+
nsresult AppendAssociationKeyPath(const wchar_t* aExt, nsString& output) {
288+
if (aExt[0] == L'.') {
289+
output.AppendLiteral(
290+
u"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\");
291+
} else {
292+
output.AppendLiteral(
293+
u"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations"
294+
u"\\");
295+
}
296+
297+
output.Append(aExt);
298+
299+
return NS_OK;
300+
}
301+
287302
UniquePtr<wchar_t[]> GenerateUserChoiceHash(const wchar_t* aExt,
288303
const wchar_t* aUserSid,
289304
const wchar_t* aProgId,

browser/components/shell/WindowsUserChoice.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "ErrorList.h" // for nsresult
1212
#include "mozilla/UniquePtr.h"
13+
#include "nsString.h"
1314

1415
/*
1516
* Check the UserChoice Hashes for https, http, .html, .htm
@@ -60,6 +61,14 @@ CheckUserChoiceHashResult CheckUserChoiceHash(const wchar_t* aExt,
6061
*/
6162
mozilla::UniquePtr<wchar_t[]> GetAssociationKeyPath(const wchar_t* aExt);
6263

64+
/*
65+
* Appends the registry path for the given association, file extension or
66+
* protocol to the parameter string.
67+
*
68+
* @return The path, or nullptr on failure.
69+
*/
70+
nsresult AppendAssociationKeyPath(const wchar_t* aExt, nsString& output);
71+
6372
/*
6473
* Get the current user's SID
6574
*

toolkit/mozapps/defaultagent/DefaultAgent.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ NS_IMETHODIMP
370370
DefaultAgent::SetDefaultBrowserUserChoiceAsync(
371371
const nsAString& aAumid, const nsTArray<nsString>& aExtraFileExtensions,
372372
JSContext* aCx, dom::Promise** aPromise) {
373+
if (!NS_IsMainThread()) {
374+
return NS_ERROR_NOT_SAME_THREAD;
375+
}
376+
373377
ErrorResult rv;
374378
RefPtr<dom::Promise> promise =
375379
dom::Promise::Create(xpc::CurrentNativeGlobal(aCx), rv);
@@ -382,16 +386,29 @@ DefaultAgent::SetDefaultBrowserUserChoiceAsync(
382386
auto promiseHolder = MakeRefPtr<nsMainThreadPtrHolder<dom::Promise>>(
383387
"SetDefaultBrowserUserChoiceAsync promise", promise);
384388

385-
auto result = default_agent::SetDefaultBrowserUserChoiceAsync(
386-
PromiseFlatString(aAumid).get(), aExtraFileExtensions,
387-
[promiseHolder = std::move(promiseHolder)](nsresult result) {
388-
dom::Promise* promise = promiseHolder.get()->get();
389-
if (NS_SUCCEEDED(result)) {
390-
promise->MaybeResolveWithUndefined();
391-
} else {
392-
promise->MaybeReject(result);
393-
}
394-
});
389+
nsresult result = NS_DispatchBackgroundTask(
390+
NS_NewRunnableFunction(
391+
"SetDefaultBrowserUserChoiceAsync",
392+
// Make a local copy of the aAudmid parameter which is a reference
393+
// which will go out of scope
394+
[aumid = nsString(aAumid), promiseHolder = std::move(promiseHolder),
395+
aExtraFileExtensions =
396+
CopyableTArray<nsString>(aExtraFileExtensions)] {
397+
nsresult rv = default_agent::SetDefaultBrowserUserChoice(
398+
PromiseFlatString(aumid).get(), aExtraFileExtensions);
399+
400+
NS_DispatchToMainThread(NS_NewRunnableFunction(
401+
"SetDefaultBrowserUserChoiceAsync callback",
402+
[rv, promiseHolder = std::move(promiseHolder)] {
403+
dom::Promise* promise = promiseHolder.get()->get();
404+
if (NS_SUCCEEDED(rv)) {
405+
promise->MaybeResolveWithUndefined();
406+
} else {
407+
promise->MaybeReject(rv);
408+
}
409+
}));
410+
}),
411+
NS_DISPATCH_EVENT_MAY_BLOCK);
395412

396413
promise.forget(aPromise);
397414
return result;

0 commit comments

Comments
 (0)