Skip to content

Commit 75ac0fc

Browse files
Bug 1855045 - [bidi] Dismiss file picker prompts automatically r=Sasha
Differential Revision: https://phabricator.services.mozilla.com/D271792
1 parent 73fb108 commit 75ac0fc

File tree

7 files changed

+135
-27
lines changed

7 files changed

+135
-27
lines changed

remote/jar.mn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ remote.jar:
9191
content/shared/webdriver/Certificates.sys.mjs (shared/webdriver/Certificates.sys.mjs)
9292
content/shared/webdriver/Errors.sys.mjs (shared/webdriver/Errors.sys.mjs)
9393
content/shared/webdriver/Event.sys.mjs (shared/webdriver/Event.sys.mjs)
94+
content/shared/webdriver/FilePickerHandler.sys.mjs (shared/webdriver/FilePickerHandler.sys.mjs)
9495
content/shared/webdriver/KeyData.sys.mjs (shared/webdriver/KeyData.sys.mjs)
9596
content/shared/webdriver/NodeCache.sys.mjs (shared/webdriver/NodeCache.sys.mjs)
9697
content/shared/webdriver/Session.sys.mjs (shared/webdriver/Session.sys.mjs)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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/. */
4+
5+
const FILE_PICKER_HANDLER_CID = Services.uuid.generateUUID();
6+
const FILE_PICKER_CONTRACT_ID = "@mozilla.org/filepicker;1";
7+
8+
/**
9+
* The FilePickerHandler can override the default component factory for the file
10+
* picker to prevent showing file pickers if needed.
11+
*/
12+
class FilePickerHandlerClass {
13+
#callers;
14+
#originalFilePickerCID;
15+
#registrar;
16+
#registeredFactory;
17+
18+
constructor() {
19+
this.#registeredFactory = null;
20+
21+
this.#registrar = Components.manager.QueryInterface(
22+
Ci.nsIComponentRegistrar
23+
);
24+
this.#originalFilePickerCID = this.#registrar.contractIDToCID(
25+
FILE_PICKER_CONTRACT_ID
26+
);
27+
28+
// Set to keep track of all callers which requested to handle file pickers.
29+
this.#callers = new Set();
30+
}
31+
32+
/**
33+
* Stop requesting to dismiss all file pickers on behalf of the provided
34+
* caller.
35+
* Note that file pickers will only be displayed again once all callers
36+
* called allowFilePickers.
37+
*
38+
* @param {object} caller
39+
* A reference to identify the caller which requested to dismiss pickers.
40+
*/
41+
allowFilePickers(caller) {
42+
this.#callers.delete(caller);
43+
44+
if (this.#callers.size || !this.#registeredFactory) {
45+
return;
46+
}
47+
48+
// Unregister our proxy factory.
49+
this.#registrar.unregisterFactory(
50+
FILE_PICKER_HANDLER_CID,
51+
this.#registeredFactory
52+
);
53+
this.#registeredFactory = null;
54+
55+
// Restore the original factory.
56+
this.#registrar.registerFactory(
57+
this.#originalFilePickerCID,
58+
"",
59+
FILE_PICKER_CONTRACT_ID,
60+
null
61+
);
62+
}
63+
64+
/**
65+
* Request to dismiss all file picker dialogs by registering a custom file
66+
* picker factory instead of the default one.
67+
*
68+
* @param {object} caller
69+
* A reference to identify the caller which requested to dismiss pickers.
70+
*/
71+
dismissFilePickers(caller) {
72+
this.#callers.add(caller);
73+
74+
if (this.#registeredFactory) {
75+
return;
76+
}
77+
78+
this.#registeredFactory = {
79+
createInstance(iid) {
80+
const filePickerProxy = {
81+
init() {},
82+
open: openCallback => {
83+
openCallback.done(Ci.nsIFilePicker.returnCancel);
84+
},
85+
displayDirectory: null,
86+
file: null,
87+
QueryInterface: ChromeUtils.generateQI(["nsIFilePicker"]),
88+
};
89+
return filePickerProxy.QueryInterface(iid);
90+
},
91+
QueryInterface: ChromeUtils.generateQI(["nsIFactory"]),
92+
};
93+
94+
this.#registrar.registerFactory(
95+
FILE_PICKER_HANDLER_CID,
96+
"WebDriver FilePicker handler",
97+
FILE_PICKER_CONTRACT_ID,
98+
this.#registeredFactory
99+
);
100+
}
101+
}
102+
103+
// Expose a singleton shared by all WebDriver sessions.
104+
// The FilePickerHandler factory should only be registered once at most.
105+
export const FilePickerHandler = new FilePickerHandlerClass();

remote/shared/webdriver/Session.sys.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ChromeUtils.defineESModuleGetters(lazy, {
1414
Capabilities: "chrome://remote/content/shared/webdriver/Capabilities.sys.mjs",
1515
Certificates: "chrome://remote/content/shared/webdriver/Certificates.sys.mjs",
1616
error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
17+
FilePickerHandler:
18+
"chrome://remote/content/shared/webdriver/FilePickerHandler.sys.mjs",
1719
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
1820
Log: "chrome://remote/content/shared/Log.sys.mjs",
1921
NavigableManager: "chrome://remote/content/shared/NavigableManager.sys.mjs",
@@ -284,6 +286,11 @@ export class WebDriverSession {
284286
// Start the tracking of browsing contexts to create Navigable ids.
285287
lazy.NavigableManager.startTracking();
286288

289+
// Temporarily dismiss all file pickers.
290+
// Bug 1999693: File pickers should only be dismissed when the unhandled
291+
// prompt behaviour for type "file" is not set to "ignore".
292+
lazy.FilePickerHandler.dismissFilePickers(this);
293+
287294
webDriverSessions.set(this.#id, this);
288295
}
289296

@@ -294,6 +301,8 @@ export class WebDriverSession {
294301
// session exists anymore.
295302
lazy.NavigableManager.stopTracking();
296303

304+
lazy.FilePickerHandler.allowFilePickers(this);
305+
297306
lazy.unregisterProcessDataActor();
298307

299308
this.#navigableSeenNodes = null;

testing/web-platform/meta/webdriver/tests/bidi/input/file_dialog_opened/file_dialog_opened.py.ini

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[accept.py]
2-
disabled:
3-
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1855045
4-
2+
[test_file_accept]
3+
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1999693
4+
expected: ERROR
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[dismiss.py]
2-
disabled:
3-
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1855045
2+
[test_file_dismiss]
3+
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1999693
4+
expected: ERROR
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
[ignore.py]
2-
disabled:
3-
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1855045
2+
[test_no_capabilities]
3+
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1999693
4+
expected: FAIL
45

6+
[test_string_ignore]
7+
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1999693
8+
expected: FAIL
9+
10+
[test_default_ignore]
11+
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1999693
12+
expected: FAIL
13+
14+
[test_file_ignore]
15+
bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1999693
16+
expected: ERROR

0 commit comments

Comments
 (0)