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

Files app, Gallery, Audio and Video Players: Refactor getting

VolumeManager from background page

Refactor FilteredVolumeManager() to not depend or reach to the
background window/page, instead it just receives a promise that resolves
to the `VolumeManager` which is what `FilteredVolumeManager()` needs.

This is a preparation for JS modules to avoid the foreground using
global variables from the background, this case is avoiding the
`volumeManagerFactory` from FilteredVolumeManager().

For the SWA is the same concept, centralize the interaction with
background page via the `BackgroundPage` interface.

No change in the behavior, so Closure and current tests cover this.

Bug: 1148545, 1133186, 1113981
Change-Id: I81b87534b26d10946dfa1dc0e287c63f003ea203
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2534553
Commit-Queue: Luciano Pacheco <lucmult@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827175}
parent 7ee258b7
......@@ -14,7 +14,18 @@ ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js';
*/
function AudioPlayer(container) {
this.container_ = container;
this.volumeManager_ = new FilteredVolumeManager(AllowedPaths.ANY_PATH, false);
const volumeManagerGetter = new Promise(resolve => {
chrome.runtime.getBackgroundPage(resolve);
}).then(backgroundWindow => {
/** @type {!BackgroundBase} */
const backgroundPage = (backgroundWindow).background;
return backgroundPage.getVolumeManager();
});
this.volumeManager_ = new FilteredVolumeManager(
AllowedPaths.ANY_PATH, false, volumeManagerGetter);
this.metadataModel_ = MetadataModel.create(this.volumeManager_);
this.selectedEntry_ = null;
this.invalidTracks_ = {};
......
......@@ -79,12 +79,10 @@
* @param {!AllowedPaths} allowedPaths Which paths are supported in the Files
* app dialog.
* @param {boolean} writableOnly If true, only writable volumes are returned.
* @param {Window=} opt_backgroundPage Window object of the background
* page. If this is specified, the class skips to get background page.
* TODO(hirono): Let all clients of the class pass the background page and
* make the argument not optional.
* @param {!Promise<!VolumeManager>} volumeManagerGetter Promise that resolves
* when the VolumeManager has been initialized.
*/
constructor(allowedPaths, writableOnly, opt_backgroundPage) {
constructor(allowedPaths, writableOnly, volumeManagerGetter) {
super();
this.allowedPaths_ = allowedPaths;
......@@ -104,8 +102,8 @@
this.disposed_ = false;
/** private {Window} */
this.backgroundPage_ = opt_backgroundPage;
/** private {!Promise<!VolumeManager>} */
this.volumeManagerGetter_ = volumeManagerGetter;
/**
* Tracks async initialization of volume manager.
......@@ -156,13 +154,7 @@
* @private
*/
async initialize_() {
if (!this.backgroundPage_) {
this.backgroundPage_ = await new Promise(
resolve => chrome.runtime.getBackgroundPage(resolve));
}
this.volumeManager_ =
await this.backgroundPage_.volumeManagerFactory.getInstance();
this.volumeManager_ = await this.volumeManagerGetter_;
if (this.disposed_) {
return;
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// #import {VolumeManager} from '../volume_manager.m.js';
/** @typedef {function(!Array<string>):!Promise} */
/* #export */ let LaunchHandler;
......@@ -28,4 +30,7 @@
* @param {!LaunchHandler} handler Function to be called.
*/
setLaunchHandler(handler) {}
/** @return {!Promise<!VolumeManager>} */
getVolumeManager() {}
}
......@@ -203,6 +203,7 @@ js_library("background_base.m") {
sources = [ "$root_gen_dir/ui/file_manager/file_manager/background/js/background_base.m.js" ]
deps = [
":volume_manager_factory.m",
"//ui/file_manager/externs:volume_manager.m",
"//ui/file_manager/externs/background:background_base.m",
"//ui/file_manager/file_manager/common/js:util.m",
"//ui/webui/resources/js:assert.m",
......
......@@ -4,6 +4,7 @@
// clang-format off
// #import {BackgroundBase, LaunchHandler} from '../../../externs/background/background_base.m.js';
// #import {VolumeManager} from '../../../externs/volume_manager.m.js';
// #import * as wrappedVolumeManagerFactory from './volume_manager_factory.m.js'; const {volumeManagerFactory} = wrappedVolumeManagerFactory;
// #import * as wrappedUtil from '../../common/js/util.m.js'; const {util} = wrappedUtil;
// #import {assert} from 'chrome://resources/js/assert.m.js';
......@@ -42,6 +43,13 @@
chrome.app.runtime.onRestarted.addListener(this.onRestarted_.bind(this));
}
/**
* @return {!Promise<!VolumeManager>}
*/
async getVolumeManager() {
return volumeManagerFactory.getInstance();
}
/**
* Called when an app is launched.
*
......
......@@ -890,7 +890,8 @@ class FileManager extends cr.EventTarget {
// Note that the Drive enabling preference change is listened by
// DriveIntegrationService, so here we don't need to take care about it.
this.volumeManager_ = new FilteredVolumeManager(
allowedPaths, writableOnly, this.backgroundPage_);
allowedPaths, writableOnly,
this.fileBrowserBackground_.getVolumeManager());
}
/**
......
......@@ -1082,7 +1082,15 @@ const loadTimeDataPromise = new Promise(function(fulfill, reject) {
* @type {!Promise}
*/
const volumeManagerPromise = new Promise(function(fulfill, reject) {
const volumeManager = new FilteredVolumeManager(AllowedPaths.ANY_PATH, false);
const volumeManagerGetter = new Promise(resolve => {
chrome.runtime.getBackgroundPage(resolve);
}).then(backgroundWindow => {
/** @type {!BackgroundBase} */
const backgroundPage = (backgroundWindow).background;
return backgroundPage.getVolumeManager();
});
const volumeManager = new FilteredVolumeManager(
AllowedPaths.ANY_PATH, false, volumeManagerGetter);
volumeManager.ensureInitialized(fulfill.bind(null, volumeManager));
});
......
......@@ -30,7 +30,15 @@ function initStrings(callback) {
* @param {function()} callback Called when the volume manager is ready.
*/
function initVolumeManager(callback) {
const volumeManager = new FilteredVolumeManager(AllowedPaths.ANY_PATH, false);
const volumeManagerGetter = new Promise(resolve => {
chrome.runtime.getBackgroundPage(resolve);
}).then(backgroundWindow => {
/** @type {!BackgroundBase} */
const backgroundPage = (backgroundWindow).background;
return backgroundPage.getVolumeManager();
});
const volumeManager = new FilteredVolumeManager(
AllowedPaths.ANY_PATH, false, volumeManagerGetter);
volumeManager.ensureInitialized(callback);
}
......
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