Commit 63332a19 authored by Zain Afzal's avatar Zain Afzal Committed by Commit Bot

Move openFile from clientAPIDelegate to ReceivedFileList.

This is the first cl in a set of CLs centred around moving navigation
order responsibility from launch.js to ReceivedFileList in receiver.js.

This cl does 3 things to set up ReceivedFile list to take over owning
open file functionality.
    * Loads a default file list into the media app on launch so we can
      have access to the openFile method regardless of if we were
      launched with files or not.
    * Moves the openFile function from clientAPIDelegate into
      receivedFileList.
    * Adds a currentFileIndex property. This is unused at the moment
      but needs to be on the api so observers can detect when the
      current file changes.

Once Google3 updates to use this openFile function and updates the
loaded file on a currentFile change, the old openFile code path can be
removed. Then the ReceivedFileList can be updated to no longer rely on
launch.js to do file navigation to the newly opened file but to do it
internally.

See go/bl-file-navigation-order for the full timeline for this effort.

Change-Id: Idbbddf1338b66d37917413e67767b54b9709a7a7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2494181Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Commit-Queue: Zain Afzal <zafzal@google.com>
Cr-Commit-Position: refs/heads/master@{#821035}
parent 7920d719
......@@ -99,6 +99,12 @@ mediaApp.AbstractFile.prototype.saveAs;
mediaApp.AbstractFileList = function() {};
/** @type {number} */
mediaApp.AbstractFileList.prototype.length;
/**
* The index of the currently active file which navigation and other file
* operations are performed relative to. Defaults to -1 if file list is empty.
* @type {number}
*/
mediaApp.AbstractFileList.prototype.currentFileIndex;
/**
* @param {number} index
* @return {(null|!mediaApp.AbstractFile)}
......@@ -128,7 +134,13 @@ mediaApp.AbstractFileList.prototype.loadPrev = function(currentFileToken) {};
* size or contents of the file list changes.
*/
mediaApp.AbstractFileList.prototype.addObserver = function(observer) {};
/**
* Request for the user to be prompted with a open file picker. Once the user
* selects a file, the file is inserted into the navigation order after the
* current file and navigated to.
* @return {!Promise<undefined>}
*/
mediaApp.AbstractFileList.prototype.openFile = function() {};
/**
* The delegate which exposes open source privileged WebUi functions to
* MediaApp.
......
......@@ -134,6 +134,7 @@ class ReceivedFileList {
}
this.length = files.length;
this.currentFileIndex = files.length ? 0 : -1;
/** @type {!Array<!ReceivedFile>} */
this.files = files.map(f => new ReceivedFile(f));
/** @type {number} */
......@@ -177,6 +178,10 @@ class ReceivedFileList {
this.observers.push(observer);
}
async openFile() {
await parentMessagePipe.sendMessage(Message.OPEN_FILE);
}
/** @param {!Array<!ReceivedFile>} files */
addFiles(files) {
if (files.length === 0) {
......@@ -319,6 +324,12 @@ window.addEventListener('DOMContentLoaded', () => {
observer.observe(document.body, {childList: true});
});
// Ensure that if no files are loaded into the media app there is a default
// empty file list available.
window.customLaunchData = {
files: new ReceivedFileList({files: [], writableFileIndex: 0})
};
// Attempting to show file pickers in the sandboxed <iframe> is guaranteed to
// result in a SecurityError: hide them.
// TODO(crbug/1040328): Remove this when we have a polyfill that allows us to
......
......@@ -13,6 +13,7 @@ let TestMessageResponseData;
/**
* Object sent over postMessage to run a command or extract data.
* TODO(b/165720635): Remove legacyOpenFile when google3 updates.
* @typedef {{
* deleteLastFile: (boolean|undefined),
* getFileErrors: (boolean|undefined),
......@@ -25,6 +26,7 @@ let TestMessageResponseData;
* requestFullscreen: (boolean|undefined),
* requestSaveFile: (boolean|undefined),
* saveAs: (string|undefined),
* legacyOpenFile: (boolean|undefined),
* openFile: (boolean|undefined),
* testQuery: string,
* }}
......
......@@ -124,8 +124,12 @@ async function runTestQuery(data) {
} else if (data.getFileErrors) {
result =
assertCast(lastReceivedFileList).files.map(file => file.error).join();
} else if (data.openFile) {
} else if (data.legacyOpenFile) {
// TODO(b/165720635): Remove this once google3 shifts over to using openFile
// on the fileList.
await DELEGATE.openFile();
} else if (data.openFile) {
await assertCast(lastReceivedFileList).openFile();
} else if (data.getLastFileName) {
result = firstReceivedItem().name;
}
......
......@@ -74,3 +74,9 @@ GUEST_TEST('GuestCanLoadWithCspRestrictions', async () => {
videoBlob.src = 'blob:chrome-untrusted://media-app/my-fake-blob-hash';
await test_util.eventToPromise('error', videoBlob);
});
GUEST_TEST('GuestStartsWithDefaultFileList', async () => {
chai.assert.isDefined(window.customLaunchData);
chai.assert.isDefined(window.customLaunchData.files);
chai.assert.isTrue(window.customLaunchData.files.length === 0);
});
......@@ -74,10 +74,8 @@ var MediaAppUIBrowserTest = class extends testing.Test {
// flags to prevent tests crashing on Media App load due to
// window.launchQueue being undefined. See http://crbug.com/1071320.
return {
enabled: [
'chromeos::features::kMediaApp',
'blink::features::kFileHandlingAPI'
]
enabled:
['chromeos::features::kMediaApp', 'blink::features::kFileHandlingAPI']
};
}
......@@ -1008,11 +1006,13 @@ TEST_F('MediaAppUIBrowserTest', 'SaveAsErrorHandling', async () => {
});
// Tests the IPC behind the openFile delegate function.
TEST_F('MediaAppUIBrowserTest', 'OpenFileIPC', async () => {
// TODO(b/165720635): Remove this once google3 shifts over to using openFile on
// the fileList.
TEST_F('MediaAppUIBrowserTest', 'LegacyOpenFileIPC', async () => {
const pickedFileHandle = new FakeFileSystemFileHandle('picked_file.jpg');
window.showOpenFilePicker = () => Promise.resolve([pickedFileHandle]);
await sendTestMessage({openFile: true});
await sendTestMessage({legacyOpenFile: true});
const lastToken = [...tokenMap.keys()].slice(-1)[0];
assertEquals(entryIndex, 0);
......@@ -1024,6 +1024,25 @@ TEST_F('MediaAppUIBrowserTest', 'OpenFileIPC', async () => {
testDone();
});
// Tests the IPC behind the openFile function on receivedFileList.
TEST_F('MediaAppUIBrowserTest', 'OpenFileIPC', async () => {
const pickedFileHandle = new FakeFileSystemFileHandle('picked_file.jpg');
window.showOpenFilePicker = () => Promise.resolve([pickedFileHandle]);
await launchWithFiles(
[await createTestImageFile(10, 10, 'original_file.jpg')]);
await sendTestMessage({openFile: true});
const lastToken = [...tokenMap.keys()].slice(-1)[0];
assertEquals(entryIndex, 1);
assertEquals(currentFiles.length, 2);
assertEquals(currentFiles[1].handle, pickedFileHandle);
assertEquals(currentFiles[1].handle.name, 'picked_file.jpg');
assertEquals(currentFiles[1].token, lastToken);
assertEquals(tokenMap.get(currentFiles[1].token), currentFiles[1].handle);
testDone();
});
TEST_F('MediaAppUIBrowserTest', 'RelatedFiles', async () => {
sortOrder = SortOrder.A_FIRST;
// These files all have a last modified time of 0 so the order they end up in
......@@ -1153,3 +1172,8 @@ TEST_F('MediaAppUIBrowserTest', 'GuestCanLoadWithCspRestrictions', async () => {
await runTestInGuest('GuestCanLoadWithCspRestrictions');
testDone();
});
TEST_F('MediaAppUIBrowserTest', 'GuestStartsWithDefaultFileList', async () => {
await runTestInGuest('GuestStartsWithDefaultFileList');
testDone();
});
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