Commit ac72cea3 authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

Audio Player: Refactor start up to allow use of feature flag

Change the initialization to wait for the background page to be ready,
which means the load time data is available, to be able to check for the
feature flag to launch different HTML files.

This is a preparation to enable JS modules for Audio Player behind a
feature flag.

Add the feature flag AudioPlayerJsModules to util to be able to check in
the Audio Player background before launching a new Audio Player window.

Bug: 1133186
Change-Id: I9809a200c97a0d7a651f8012dd57904b2ef797ca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2546344
Commit-Queue: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatarJeremie Boulic <jboulic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828625}
parent 6698950e
...@@ -1061,6 +1061,9 @@ void AddFileManagerFeatureStrings(const std::string& locale, ...@@ -1061,6 +1061,9 @@ void AddFileManagerFeatureStrings(const std::string& locale,
dict->SetBoolean("FILES_SINGLE_PARTITION_FORMAT_ENABLED", dict->SetBoolean("FILES_SINGLE_PARTITION_FORMAT_ENABLED",
base::FeatureList::IsEnabled( base::FeatureList::IsEnabled(
chromeos::features::kFilesSinglePartitionFormat)); chromeos::features::kFilesSinglePartitionFormat));
dict->SetBoolean(
"AUDIO_PLAYER_JS_MODULES_ENABLED",
base::FeatureList::IsEnabled(chromeos::features::kAudioPlayerJsModules));
dict->SetString("UI_LOCALE", locale); dict->SetString("UI_LOCALE", locale);
} }
...@@ -25,6 +25,13 @@ const AUDIO_PLAYER_ICON = 'icons/audio-player-192.png'; ...@@ -25,6 +25,13 @@ const AUDIO_PLAYER_ICON = 'icons/audio-player-192.png';
*/ */
const AUDIO_PLAYER_APP_URL = 'audio_player.html'; const AUDIO_PLAYER_APP_URL = 'audio_player.html';
/**
* HTML source of the audio player as JS module.
* @type {!string}
* @const
*/
const AUDIO_PLAYER_MODULE_APP_URL = 'audio_player_module.html';
/** /**
* Configuration of the audio player. * Configuration of the audio player.
* @type {!Object} * @type {!Object}
...@@ -44,16 +51,22 @@ class AudioPlayerBackground extends BackgroundBaseImpl { ...@@ -44,16 +51,22 @@ class AudioPlayerBackground extends BackgroundBaseImpl {
super(); super();
} }
async ready() {
return await this.initializationPromise_;
}
/** /**
* Called when an audio player app is restarted. * Called when an audio player app is restarted.
*/ */
onRestarted_() { onRestarted_() {
audioPlayer.reopen(function() { getAudioPlayer.then(audioPlayer => {
// If the audioPlayer is reopened, change its window's icon. Otherwise audioPlayer.reopen(function() {
// there is no reopened window so just skip the call of setIcon. // If the audioPlayer is reopened, change its window's icon. Otherwise
if (audioPlayer.rawAppWindow) { // there is no reopened window so just skip the call of setIcon.
audioPlayer.setIcon(AUDIO_PLAYER_ICON); if (audioPlayer.rawAppWindow) {
} audioPlayer.setIcon(AUDIO_PLAYER_ICON);
}
});
}); });
} }
} }
...@@ -64,12 +77,19 @@ class AudioPlayerBackground extends BackgroundBaseImpl { ...@@ -64,12 +77,19 @@ class AudioPlayerBackground extends BackgroundBaseImpl {
*/ */
window.background = new AudioPlayerBackground(); window.background = new AudioPlayerBackground();
/** /**
* Audio player app window wrapper. * Audio player app window wrapper.
* @type {!SingletonAppWindowWrapper} * @type {!Promise<!SingletonAppWindowWrapper>}
*/ */
const audioPlayer = new SingletonAppWindowWrapper( const getAudioPlayer = new Promise(async (resolve) => {
AUDIO_PLAYER_APP_URL, audioPlayerCreateOptions); await window.background.ready();
const url = util.isAudioPlayerJsModulesEnabled() ?
AUDIO_PLAYER_MODULE_APP_URL :
AUDIO_PLAYER_APP_URL;
resolve(new SingletonAppWindowWrapper(
AUDIO_PLAYER_APP_URL, audioPlayerCreateOptions));
});
/** /**
* Opens the audio player window. * Opens the audio player window.
...@@ -77,65 +97,63 @@ const audioPlayer = new SingletonAppWindowWrapper( ...@@ -77,65 +97,63 @@ const audioPlayer = new SingletonAppWindowWrapper(
* playing. * playing.
* @return {!Promise} Promise to be fulfilled on success, or rejected on error. * @return {!Promise} Promise to be fulfilled on success, or rejected on error.
*/ */
/* #export */ function open(urls) { /* #export */ async function open(urls) {
let position = 0; let position = 0;
const startUrl = (position < urls.length) ? urls[position] : ''; const startUrl = (position < urls.length) ? urls[position] : '';
return new Promise(function(fulfill, reject) { if (urls.length === 0) {
if (urls.length === 0) { throw new Error('No file to open.');
reject('No file to open.'); }
return;
}
// Gets the current list of the children of the parent.
window.webkitResolveLocalFileSystemURL(urls[0], function(fileEntry) {
fileEntry.getParent(function(parentEntry) {
const dirReader = parentEntry.createReader();
let entries = [];
// Call the reader.readEntries() until no more results are
// returned.
const readEntries = function() {
dirReader.readEntries(function(results) {
if (!results.length) {
fulfill(entries.sort(util.compareName));
} else {
entries =
entries.concat(Array.prototype.slice.call(results, 0));
readEntries();
}
}, reject);
};
// Start reading.
readEntries();
}, reject);
}, reject);
})
.then(function(entries) {
// Omits non-audio files.
const audioEntries = entries.filter(FileType.isAudio);
// Adjusts the position to start playing.
const maybePosition =
util.entriesToURLs(audioEntries).indexOf(startUrl);
if (maybePosition !== -1) {
position = maybePosition;
}
// Opens the audio player. try {
const urls = util.entriesToURLs(audioEntries); const entries = await new Promise(function(fulfill, reject) {
return audioPlayer.launch({items: urls, position: position}, false); // Gets the current list of the children of the parent.
}) window.webkitResolveLocalFileSystemURL(urls[0], function(fileEntry) {
.then(function() { fileEntry.getParent(function(parentEntry) {
audioPlayer.setIcon(AUDIO_PLAYER_ICON); const dirReader = parentEntry.createReader();
audioPlayer.rawAppWindow.focus(); let entries = [];
return AUDIO_PLAYER_APP_URL;
}) // Call the reader.readEntries() until no more results are
.catch(function(error) { // returned.
console.error('Launch failed: ' + (error.stack || error)); const readEntries = function() {
return Promise.reject(error); dirReader.readEntries(function(results) {
}); if (!results.length) {
fulfill(entries.sort(util.compareName));
} else {
entries =
entries.concat(Array.prototype.slice.call(results, 0));
readEntries();
}
}, reject);
};
// Start reading.
readEntries();
}, reject);
}, reject);
});
// Omits non-audio files.
const audioEntries = entries.filter(FileType.isAudio);
// Adjusts the position to start playing.
const maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl);
if (maybePosition !== -1) {
position = maybePosition;
}
// Opens the audio player.
const urlsToOpen = util.entriesToURLs(audioEntries);
const audioPlayer = await getAudioPlayer;
await audioPlayer.launch({items: urlsToOpen, position: position}, false);
audioPlayer.setIcon(AUDIO_PLAYER_ICON);
audioPlayer.rawAppWindow.focus();
return AUDIO_PLAYER_APP_URL;
} catch (error) {
console.error('Launch failed: ' + (error.stack || error));
throw error;
}
} }
window.background.setLaunchHandler(open); window.background.setLaunchHandler(open);
...@@ -1495,6 +1495,14 @@ util.isSinglePartitionFormatEnabled = () => { ...@@ -1495,6 +1495,14 @@ util.isSinglePartitionFormatEnabled = () => {
return loadTimeData.getBoolean('FILES_SINGLE_PARTITION_FORMAT_ENABLED'); return loadTimeData.getBoolean('FILES_SINGLE_PARTITION_FORMAT_ENABLED');
}; };
/**
* Returns true if flag is enabled.
* @return {boolean}
*/
util.isAudioPlayerJsModulesEnabled = () => {
return loadTimeData.getBoolean('AUDIO_PLAYER_JS_MODULES_ENABLED');
};
/** /**
* Retrieves all entries inside the given |rootEntry|. * Retrieves all entries inside the given |rootEntry|.
* @param {!DirectoryEntry} rootEntry * @param {!DirectoryEntry} rootEntry
......
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