Commit 689fafbc authored by Trent Apted's avatar Trent Apted Committed by Commit Bot

Migrate chrome://media-app window.{chooseFileSystemEntries=>openSavePicker}

The API was transitioned in r781392.

Bug: 1096225, 996088
Change-Id: I4c83e0e6cddd4b5ee22bb0308636fb5a3fad6723
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2260296Reviewed-by: default avatarRachel Carpenter <carpenterr@chromium.org>
Commit-Queue: Trent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781632}
parent db31d71b
...@@ -152,20 +152,18 @@ guestMessagePipe.registerHandler(Message.SAVE_COPY, async (message) => { ...@@ -152,20 +152,18 @@ guestMessagePipe.registerHandler(Message.SAVE_COPY, async (message) => {
const extension = suggestedName.split('.').reverse()[0]; const extension = suggestedName.split('.').reverse()[0];
// TODO(b/141587270): Add a default filename when it's supported by the native // TODO(b/141587270): Add a default filename when it's supported by the native
// file api. // file api.
/** @type {!ChooseFileSystemEntriesOptions} */ /** @type {!FilePickerOptions} */
const options = { const options = {
type: 'save-file', types: [
accepts: [{extension, mimeTypes: [blob.type]}], {description: extension, accept: {[blob.type]: [extension]}},
],
// Without this, the file picker defaults to "All files" and will refuse to
// provide an extension automatically. See crbug/1082624#c23.
excludeAcceptAllOption: true, excludeAcceptAllOption: true,
}; };
// This may throw an error, but we can handle and recover from it on the // This may throw an error, but we can handle and recover from it on the
// unprivileged side. // unprivileged side.
/** @type {!FileSystemHandle} */ /** @type {!FileSystemHandle} */
const fileSystemHandle = /** @type {!FileSystemHandle} */ ( const fileSystemHandle = /** @type {!FileSystemHandle} */ (
await window.chooseFileSystemEntries(options)); await window.showSaveFilePicker(options));
const {handle} = await getFileFromHandle(fileSystemHandle); const {handle} = await getFileFromHandle(fileSystemHandle);
// Note `handle` could be the same as a `FileSystemFileHandle` that exists in // Note `handle` could be the same as a `FileSystemFileHandle` that exists in
// `tokenMap`. Possibly even the `File` currently open. But that's OK. E.g. // `tokenMap`. Possibly even the `File` currently open. But that's OK. E.g.
......
...@@ -253,10 +253,11 @@ window.addEventListener('DOMContentLoaded', () => { ...@@ -253,10 +253,11 @@ window.addEventListener('DOMContentLoaded', () => {
observer.observe(document.body, {childList: true}); observer.observe(document.body, {childList: true});
}); });
// Attempting to execute chooseFileSystemEntries is guaranteed to result in a // Attempting to show file pickers in the sandboxed <iframe> is guaranteed to
// SecurityError due to the fact that we are running in a unprivileged iframe. // result in a SecurityError: hide them.
// Note, we can not do window.chooseFileSystemEntries due to the fact that
// closure does not yet know that 'chooseFileSystemEntries' is on the window.
// TODO(crbug/1040328): Remove this when we have a polyfill that allows us to // TODO(crbug/1040328): Remove this when we have a polyfill that allows us to
// talk to the privileged frame. // talk to the privileged frame.
window['chooseFileSystemEntries'] = null; window['chooseFileSystemEntries'] = null;
window['showOpenFilePicker'] = null;
window['showSaveFilePicker'] = null;
window['showDirectoryPicker'] = null;
...@@ -188,29 +188,49 @@ class LaunchQueue { ...@@ -188,29 +188,49 @@ class LaunchQueue {
} }
/** /**
* https://wicg.github.io/native-file-system/#dictdef-filepickeraccepttype
* @typedef {{ * @typedef {{
* description: (string|undefined), * description: string,
* mimeTypes: (!Array<string>|undefined), * accept: !Array<!Object<string, Array<string>>>,
* extensions: (!Array<string>|undefined)
* }} * }}
*/ */
let ChooseFileSystemEntriesOptionsAccepts; let FilePickerAcceptType;
/** /**
* https://wicg.github.io/native-file-system/#dictdef-filepickeroptions
* https://wicg.github.io/native-file-system/#dictdef-directorypickeroptions
* https://wicg.github.io/native-file-system/#dictdef-openfilepickeroptions
* https://wicg.github.io/native-file-system/#dictdef-savefilepickeroptions
* Note: `multiple` is only used for openfilepicker.
* `types` is required if excludeAcceptAllOption is true.
* @typedef {{ * @typedef {{
* type: (string|undefined),
* multiple: (boolean|undefined), * multiple: (boolean|undefined),
* accepts: (!Array<!ChooseFileSystemEntriesOptionsAccepts>|undefined), * types: (!Array<!FilePickerAcceptType>|undefined),
* excludeAcceptAllOption: (boolean|undefined) * excludeAcceptAllOption: (boolean|undefined)
* }} * }}
*/ */
let ChooseFileSystemEntriesOptions; let FilePickerOptions;
/** /**
* @param {(!ChooseFileSystemEntriesOptions|undefined)} options * https://wicg.github.io/native-file-system/#native-filesystem
* @param {(!FilePickerOptions|undefined)} options
* @return {!Promise<(!FileSystemHandle|!Array<!FileSystemHandle>)>} * @return {!Promise<(!FileSystemHandle|!Array<!FileSystemHandle>)>}
*/ */
window.chooseFileSystemEntries; window.showOpenFilePicker;
/**
* https://wicg.github.io/native-file-system/#native-filesystem
* @param {(!FilePickerOptions|undefined)} options
* @return {!Promise<(!FileSystemHandle|!Array<!FileSystemHandle>)>}
*/
window.showSaveFilePicker;
/**
* https://wicg.github.io/native-file-system/#native-filesystem
* @param {(!FilePickerOptions|undefined)} options
* @return {!Promise<(!FileSystemHandle|!Array<!FileSystemHandle>)>}
*/
window.showDirectoryPicker;
/** @type {LaunchQueue} */ /** @type {LaunchQueue} */
window.launchQueue; window.launchQueue;
...@@ -783,7 +783,7 @@ TEST_F('MediaAppUIBrowserTest', 'SaveCopyIPC', async () => { ...@@ -783,7 +783,7 @@ TEST_F('MediaAppUIBrowserTest', 'SaveCopyIPC', async () => {
// via trusted user gestures. // via trusted user gestures.
const newFileHandle = new FakeFileSystemFileHandle(); const newFileHandle = new FakeFileSystemFileHandle();
const chooseEntries = new Promise(resolve => { const chooseEntries = new Promise(resolve => {
window.chooseFileSystemEntries = options => { window.showSaveFilePicker = options => {
resolve(options); resolve(options);
return newFileHandle; return newFileHandle;
}; };
...@@ -795,11 +795,9 @@ TEST_F('MediaAppUIBrowserTest', 'SaveCopyIPC', async () => { ...@@ -795,11 +795,9 @@ TEST_F('MediaAppUIBrowserTest', 'SaveCopyIPC', async () => {
assertEquals(result.testQueryResult, 'boo yah!'); assertEquals(result.testQueryResult, 'boo yah!');
const options = await chooseEntries; const options = await chooseEntries;
assertEquals(options.type, 'save-file'); assertEquals(options.types.length, 1);
assertEquals(options.accepts.length, 1); assertEquals(options.types[0].description, 'png');
assertEquals(options.accepts[0].extension, 'png'); assertDeepEquals(options.types[0].accept['image/png'], ['png']);
assertEquals(options.accepts[0].mimeTypes.length, 1);
assertEquals(options.accepts[0].mimeTypes[0], 'image/png');
const writeResult = await newFileHandle.lastWritable.closePromise; const writeResult = await newFileHandle.lastWritable.closePromise;
assertEquals(await writeResult.text(), await testImage.text()); assertEquals(await writeResult.text(), await testImage.text());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment