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,10 +51,15 @@ class AudioPlayerBackground extends BackgroundBaseImpl { ...@@ -44,10 +51,15 @@ 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_() {
getAudioPlayer.then(audioPlayer => {
audioPlayer.reopen(function() { audioPlayer.reopen(function() {
// If the audioPlayer is reopened, change its window's icon. Otherwise // If the audioPlayer is reopened, change its window's icon. Otherwise
// there is no reopened window so just skip the call of setIcon. // there is no reopened window so just skip the call of setIcon.
...@@ -55,6 +67,7 @@ class AudioPlayerBackground extends BackgroundBaseImpl { ...@@ -55,6 +67,7 @@ class AudioPlayerBackground extends BackgroundBaseImpl {
audioPlayer.setIcon(AUDIO_PLAYER_ICON); 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,16 +97,16 @@ const audioPlayer = new SingletonAppWindowWrapper( ...@@ -77,16 +97,16 @@ 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) {
reject('No file to open.'); throw new Error('No file to open.');
return;
} }
try {
const entries = await new Promise(function(fulfill, reject) {
// Gets the current list of the children of the parent. // Gets the current list of the children of the parent.
window.webkitResolveLocalFileSystemURL(urls[0], function(fileEntry) { window.webkitResolveLocalFileSystemURL(urls[0], function(fileEntry) {
fileEntry.getParent(function(parentEntry) { fileEntry.getParent(function(parentEntry) {
...@@ -111,31 +131,29 @@ const audioPlayer = new SingletonAppWindowWrapper( ...@@ -111,31 +131,29 @@ const audioPlayer = new SingletonAppWindowWrapper(
readEntries(); readEntries();
}, reject); }, reject);
}, reject); }, reject);
}) });
.then(function(entries) {
// Omits non-audio files. // Omits non-audio files.
const audioEntries = entries.filter(FileType.isAudio); const audioEntries = entries.filter(FileType.isAudio);
// Adjusts the position to start playing. // Adjusts the position to start playing.
const maybePosition = const maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl);
util.entriesToURLs(audioEntries).indexOf(startUrl);
if (maybePosition !== -1) { if (maybePosition !== -1) {
position = maybePosition; position = maybePosition;
} }
// Opens the audio player. // Opens the audio player.
const urls = util.entriesToURLs(audioEntries); const urlsToOpen = util.entriesToURLs(audioEntries);
return audioPlayer.launch({items: urls, position: position}, false); const audioPlayer = await getAudioPlayer;
}) await audioPlayer.launch({items: urlsToOpen, position: position}, false);
.then(function() {
audioPlayer.setIcon(AUDIO_PLAYER_ICON); audioPlayer.setIcon(AUDIO_PLAYER_ICON);
audioPlayer.rawAppWindow.focus(); audioPlayer.rawAppWindow.focus();
return AUDIO_PLAYER_APP_URL; return AUDIO_PLAYER_APP_URL;
}) } catch (error) {
.catch(function(error) {
console.error('Launch failed: ' + (error.stack || error)); console.error('Launch failed: ' + (error.stack || error));
return Promise.reject(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