Commit ff6e8ed0 authored by Wei Lee's avatar Wei Lee Committed by Commit Bot

[CCA WebUI] Promisify existing callbacks in browser_proxy

Bug: 980846
Test: Launch CCA with no error shows

Change-Id: I808d0c2c5a99e8c068227645e03c8a619310da59
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2058932
Commit-Queue: Wei Lee <wtlee@chromium.org>
Reviewed-by: default avatarShik Chen <shik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743041}
parent d15a4619
......@@ -12,28 +12,43 @@ import {BrowserProxy} from './browser_proxy_interface.js';
*/
class ChromeAppBrowserProxy {
/** @override */
getVolumeList(callback) {
chrome.fileSystem.getVolumeList(callback);
async getVolumeList() {
try {
// Await here to handle the case if it is rejected.
return await util.promisify(chrome.fileSystem.getVolumeList)();
} catch (e) {
console.error('Failed to get volume list', e);
return null;
}
}
/** @override */
requestFileSystem(options, callback) {
chrome.fileSystem.requestFileSystem(options, callback);
async requestFileSystem(options) {
try {
// Await here to handle the case if it is rejected.
return await util.promisify(chrome.fileSystem.requestFileSystem)(options);
} catch (e) {
console.error('Failed to request file system', e);
return null;
}
}
/** @override */
localStorageGet(keys, callback) {
chrome.storage.local.get(keys, callback);
localStorageGet(keys) {
return util.promisify(chrome.storage.local.get.bind(chrome.storage.local))(
keys);
}
/** @override */
localStorageSet(items, callback) {
chrome.storage.local.set(items, callback);
localStorageSet(items) {
return util.promisify(chrome.storage.local.set.bind(chrome.storage.local))(
items);
}
/** @override */
localStorageRemove(items, callback) {
chrome.storage.local.remove(items, callback);
localStorageRemove(items) {
return util.promisify(
chrome.storage.local.remove.bind(chrome.storage.local))(items);
}
/** @override */
......
......@@ -7,32 +7,39 @@
* @interface
*/
export class BrowserProxy {
/** @param {function(!Array<!chrome.fileSystem.Volume>=)} callback */
getVolumeList(callback) {}
/**
* @return {!Promise<?Array<!chrome.fileSystem.Volume>>}
* @abstract
*/
async getVolumeList() {}
/**
* @param {!chrome.fileSystem.RequestFileSystemOptions} options
* @param {function(!FileSystem=)} callback
* @return {!Promise<?FileSystem>}
* @abstract
*/
requestFileSystem(options, callback) {}
async requestFileSystem(options) {}
/**
* @param {(string|!Array<string>|!Object)} keys
* @param {function(!Object)} callback
* @return {!Promise<!Object>}
* @abstract
*/
localStorageGet(keys, callback) {}
async localStorageGet(keys) {}
/**
* @param {!Object<string>} items
* @param {function()=} callback
* @return {!Promise}
* @abstract
*/
localStorageSet(items, callback) {}
async localStorageSet(items) {}
/**
* @param {(string|!Array<string>)} items
* @param {function()=} callback
* @return {!Promise}
* @abstract
*/
localStorageRemove(items, callback) {}
async localStorageRemove(items) {}
/**
* @return {!Promise<boolean>}
......
......@@ -18,17 +18,19 @@ function NOTIMPLEMENTED() {
*/
class WebUIBrowserProxy {
/** @override */
getVolumeList(callback) {
async getVolumeList() {
NOTIMPLEMENTED();
return null;
}
/** @override */
requestFileSystem(options, callback) {
async requestFileSystem(options) {
NOTIMPLEMENTED();
return null;
}
/** @override */
localStorageGet(keys, callback) {
async localStorageGet(keys) {
let sanitizedKeys = [];
if (typeof keys === 'string') {
sanitizedKeys = [keys];
......@@ -49,30 +51,24 @@ class WebUIBrowserProxy {
result[key] = value === null ? {} : value;
}
callback(result);
return result;
}
/** @override */
localStorageSet(items, callback) {
async localStorageSet(items) {
for (const [key, val] of Object.entries(items)) {
window.localStorage.setItem(key, JSON.stringify(val));
}
if (callback) {
callback();
}
}
/** @override */
localStorageRemove(items, callback) {
async localStorageRemove(items) {
if (typeof items === 'string') {
items = [items];
}
for (const key of items) {
window.localStorage.removeItem(key);
}
if (callback) {
callback();
}
}
/** @override */
......
......@@ -80,7 +80,7 @@ export class ConstraintsPreferrer {
restoreResolutionPreference_(key) {
// TODO(inker): Return promise and await it to assure preferences are loaded
// before any access.
browserProxy.localStorageGet({[key]: {}}, (values) => {
browserProxy.localStorageGet({[key]: {}}).then((values) => {
this.prefResolution_ = {};
for (const [deviceId, {width, height}] of Object.entries(values[key])) {
this.prefResolution_[deviceId] = new Resolution(width, height);
......@@ -234,9 +234,8 @@ export class VideoConstraintsPreferrer extends ConstraintsPreferrer {
* @private
*/
restoreFpsPreference_() {
browserProxy.localStorageGet(
{deviceVideoFps: {}},
(values) => this.prefFpses_ = values.deviceVideoFps);
browserProxy.localStorageGet({deviceVideoFps: {}})
.then((values) => this.prefFpses_ = values.deviceVideoFps);
}
/**
......
......@@ -128,8 +128,8 @@ export class App {
* @private
*/
setupToggles_() {
browserProxy.localStorageGet(
{expert: false}, ({expert}) => state.set(state.State.EXPERT, expert));
browserProxy.localStorageGet({expert: false})
.then(({expert}) => state.set(state.State.EXPERT, expert));
document.querySelectorAll('input').forEach((element) => {
element.addEventListener(
'keypress',
......@@ -160,11 +160,11 @@ export class App {
});
if (element.dataset.key !== undefined) {
// Restore the previously saved state on startup.
browserProxy.localStorageGet(
payload(element),
(values) => util.toggleChecked(
assertInstanceof(element, HTMLInputElement),
values[element.dataset.key]));
browserProxy.localStorageGet(payload(element))
.then(
(values) => util.toggleChecked(
assertInstanceof(element, HTMLInputElement),
values[element.dataset.key]));
}
});
}
......
......@@ -92,7 +92,7 @@ function initInternalTempDir() {
/**
* Reads file entries from the directory.
* @param {?DirectoryEntry} dir Directory entry to be read.
* @return {!Promise<!Array<!FileEntry>>} Promise for the read file entries.
* @return {!Promise<!Array<!Entry>>} Promise for the read file entries.
*/
function readDir(dir) {
return !dir ? Promise.resolve([]) : new Promise((resolve, reject) => {
......@@ -116,32 +116,28 @@ function readDir(dir) {
* Initializes the directory in the external file system.
* @return {!Promise<?DirectoryEntry>} Promise for the directory result.
*/
function initExternalDir() {
return new Promise((resolve) => {
browserProxy.getVolumeList((volumes) => {
if (volumes) {
for (let i = 0; i < volumes.length; i++) {
const volumeId = volumes[i].volumeId;
if (volumeId.indexOf('downloads:Downloads') !== -1 ||
volumeId.indexOf('downloads:MyFiles') !== -1) {
browserProxy.requestFileSystem(
volumes[i], (fs) => resolve([fs && fs.root, volumeId]));
return;
}
}
}
resolve([null, null]);
});
})
.then(([dir, volumeId]) => {
if (volumeId && volumeId.indexOf('downloads:MyFiles') !== -1) {
return readDir(dir).then((entries) => {
return entries.find(
(entry) => entry.name === 'Downloads' && entry.isDirectory);
});
}
return dir;
});
async function initExternalDir() {
const volumes = await browserProxy.getVolumeList();
if (volumes === null) {
return null;
}
const getFileSystemRoot = async (volume) => {
const fs = await browserProxy.requestFileSystem(volume);
return fs === null ? null : fs.root;
};
for (const volume of volumes) {
if (!volume.volumeId.includes('downloads:MyFiles')) {
continue;
}
const root = await getFileSystemRoot(volume);
const entries = await readDir(root);
const downloadsDir = entries.find(
(entry) => entry.name === 'Downloads' && entry.isDirectory);
return downloadsDir ? /** @type {!DirectoryEntry} */ (downloadsDir) : null;
}
return null;
}
/**
......@@ -258,9 +254,8 @@ export function initialize(promptMigrate) {
const checkAcked = new Promise((resolve) => {
// ack 0: User has not yet acknowledged to migrate pictures.
// ack 1: User acknowledges to migrate pictures to Downloads.
browserProxy.localStorageGet(
{ackMigratePictures: 0},
(values) => resolve(values.ackMigratePictures >= 1));
browserProxy.localStorageGet({ackMigratePictures: 0})
.then((values) => resolve(values.ackMigratePictures >= 1));
});
const ackMigrate = () =>
browserProxy.localStorageSet({ackMigratePictures: 1});
......@@ -289,7 +284,13 @@ export function initialize(promptMigrate) {
// Pictures taken by old Camera App may not have IMG_ or VID_ prefix.
return readDir(internalDir)
.then((entries) => {
return entries.some((entry) => !hasThumbnailPrefix(entry));
return entries.some((entry) => {
if (entry.isDirectory) {
return false;
}
const fileEntry = /** @type {!FileEntry} */ (entry);
return !hasThumbnailPrefix(fileEntry);
});
})
.then((migrateNeeded) => {
if (migrateNeeded) {
......@@ -482,7 +483,11 @@ export function getFile(dir, name, create) {
export function getEntries() {
return readDir(externalDir).then((entries) => {
return entries.filter((entry) => {
if (!hasVideoPrefix(entry) && !hasImagePrefix(entry)) {
if (entry.isDirectory) {
return false;
}
const fileEntry = /** @type {!FileEntry} */ (entry);
if (!hasVideoPrefix(fileEntry) && !hasImagePrefix(fileEntry)) {
return false;
}
return entry.name.match(/_(\d{8})_(\d{6})(?: \((\d+)\))?/);
......
......@@ -102,9 +102,8 @@ export class Options {
this.toggleMirror_.addEventListener('click', () => this.saveMirroring_());
// Restore saved mirroring states per video device.
browserProxy.localStorageGet(
{mirroringToggles: {}},
(values) => this.mirroringToggles_ = values.mirroringToggles);
browserProxy.localStorageGet({mirroringToggles: {}})
.then((values) => this.mirroringToggles_ = values.mirroringToggles);
// Remove the deprecated values.
browserProxy.localStorageRemove(
['effectIndex', 'toggleMulti', 'toggleMirror']);
......
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