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,
dict->SetBoolean("FILES_SINGLE_PARTITION_FORMAT_ENABLED",
base::FeatureList::IsEnabled(
chromeos::features::kFilesSinglePartitionFormat));
dict->SetBoolean(
"AUDIO_PLAYER_JS_MODULES_ENABLED",
base::FeatureList::IsEnabled(chromeos::features::kAudioPlayerJsModules));
dict->SetString("UI_LOCALE", locale);
}
......@@ -25,6 +25,13 @@ const AUDIO_PLAYER_ICON = 'icons/audio-player-192.png';
*/
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.
* @type {!Object}
......@@ -44,10 +51,15 @@ class AudioPlayerBackground extends BackgroundBaseImpl {
super();
}
async ready() {
return await this.initializationPromise_;
}
/**
* Called when an audio player app is restarted.
*/
onRestarted_() {
getAudioPlayer.then(audioPlayer => {
audioPlayer.reopen(function() {
// If the audioPlayer is reopened, change its window's icon. Otherwise
// there is no reopened window so just skip the call of setIcon.
......@@ -55,6 +67,7 @@ class AudioPlayerBackground extends BackgroundBaseImpl {
audioPlayer.setIcon(AUDIO_PLAYER_ICON);
}
});
});
}
}
......@@ -64,12 +77,19 @@ class AudioPlayerBackground extends BackgroundBaseImpl {
*/
window.background = new AudioPlayerBackground();
/**
* Audio player app window wrapper.
* @type {!SingletonAppWindowWrapper}
* @type {!Promise<!SingletonAppWindowWrapper>}
*/
const audioPlayer = new SingletonAppWindowWrapper(
AUDIO_PLAYER_APP_URL, audioPlayerCreateOptions);
const getAudioPlayer = new Promise(async (resolve) => {
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.
......@@ -77,16 +97,16 @@ const audioPlayer = new SingletonAppWindowWrapper(
* playing.
* @return {!Promise} Promise to be fulfilled on success, or rejected on error.
*/
/* #export */ function open(urls) {
/* #export */ async function open(urls) {
let position = 0;
const startUrl = (position < urls.length) ? urls[position] : '';
return new Promise(function(fulfill, reject) {
if (urls.length === 0) {
reject('No file to open.');
return;
throw new Error('No file to open.');
}
try {
const entries = await new Promise(function(fulfill, reject) {
// Gets the current list of the children of the parent.
window.webkitResolveLocalFileSystemURL(urls[0], function(fileEntry) {
fileEntry.getParent(function(parentEntry) {
......@@ -111,31 +131,29 @@ const audioPlayer = new SingletonAppWindowWrapper(
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);
const maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl);
if (maybePosition !== -1) {
position = maybePosition;
}
// Opens the audio player.
const urls = util.entriesToURLs(audioEntries);
return audioPlayer.launch({items: urls, position: position}, false);
})
.then(function() {
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(function(error) {
} catch (error) {
console.error('Launch failed: ' + (error.stack || error));
return Promise.reject(error);
});
throw error;
}
}
window.background.setLaunchHandler(open);
......@@ -1495,6 +1495,14 @@ util.isSinglePartitionFormatEnabled = () => {
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|.
* @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