Commit 6a59243a authored by Wei Lee's avatar Wei Lee Committed by Commit Bot

[CCA WebUI] Abstract usage of chrome.chromeosInfoPrivate

Bug: 980846
Test: Launch CCA with no error shows
Change-Id: Iaa7e9ac65f55b49c2a656e17794f5892248cbece
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2053867
Commit-Queue: Wei Lee <wtlee@chromium.org>
Reviewed-by: default avatarShik Chen <shik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#741709}
parent 8823a329
......@@ -9,10 +9,14 @@ js_type_check("closure_compile") {
}
js_library("browser_proxy") {
deps = [ "..:chrome_util" ]
sources = [
"browser_proxy.js",
"browser_proxy_interface.js",
"webui_browser_proxy.js",
]
externs_list = [ "$externs_path/chrome_extensions.js" ]
externs_list = [
"../externs/chrome.js",
"$externs_path/chrome_extensions.js",
]
}
......@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as util from '../chrome_util.js';
// eslint-disable-next-line no-unused-vars
import {BrowserProxy} from './browser_proxy_interface.js';
......@@ -34,6 +35,25 @@ class ChromeAppBrowserProxy {
localStorageRemove(items, callback) {
chrome.storage.local.remove(items, callback);
}
/** @override */
async checkMigrated() {
const values = await util.promisify(chrome.chromeosInfoPrivate.get)(
['cameraMediaConsolidated']);
return values['cameraMediaConsolidated'];
}
/** @override */
async doneMigrate() {
chrome.chromeosInfoPrivate.set('cameraMediaConsolidated', true);
}
/** @override */
async getBoard() {
const values =
await util.promisify(chrome.chromeosInfoPrivate.get)(['board']);
return values['board'];
}
}
export const browserProxy = new ChromeAppBrowserProxy();
......@@ -33,4 +33,22 @@ export class BrowserProxy {
* @param {function()=} callback
*/
localStorageRemove(items, callback) {}
/**
* @return {!Promise<boolean>}
* @abstract
*/
async checkMigrated() {}
/**
* @return {!Promise}
* @abstract
*/
async doneMigrate() {}
/**
* @return {!Promise<string>}
* @abstract
*/
async getBoard() {}
}
......@@ -74,6 +74,23 @@ class WebUIBrowserProxy {
callback();
}
}
/** @override */
async checkMigrated() {
NOTIMPLEMENTED();
return false;
}
/** @override */
async doneMigrate() {
NOTIMPLEMENTED();
}
/** @override */
async getBoard() {
NOTIMPLEMENTED();
return '';
}
}
export const browserProxy = new WebUIBrowserProxy();
......
......@@ -99,3 +99,14 @@ export function assertBoolean(value, optMessage) {
}
return /** @type {boolean} */ (value);
}
/**
* Wraps a function with completion callback as a Promise.
* @param {function(...*)} func The last parameter of the function should be a
* completion callback.
* @return {function(...*): Promise}
*/
export function promisify(func) {
return (...args) =>
new Promise((resolve) => func(...args, (val) => resolve(val)));
}
......@@ -8,3 +8,11 @@
/** @type {string|undefined} */
chrome.runtime.Manifest.prototype.version_name;
/**
* @typedef {{
* get: function(Array<string>, function(Object)),
* set: function(string, ?),
* }}
*/
chrome.chromeosInfoPrivate;
......@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {browserProxy} from './browser_proxy/browser_proxy.js';
// eslint-disable-next-line no-unused-vars
import {Intent} from './intent.js';
import * as state from './state.js';
......@@ -48,23 +49,12 @@ const ga = (function() {
}
});
};
const initBuilder = () => {
return new Promise((resolve) => {
try {
chrome.chromeosInfoPrivate.get(
['board'], (values) => resolve(values['board']));
} catch (e) {
resolve('');
}
})
.then((board) => {
const boardName = /^(x86-)?(\w*)/.exec(board)[0];
const match = navigator.appVersion.match(/CrOS\s+\S+\s+([\d.]+)/);
const osVer = match ? match[1] : '';
base = analytics.EventBuilder.builder()
.dimen(1, boardName)
.dimen(2, osVer);
});
const initBuilder = async () => {
const board = await browserProxy.getBoard();
const boardName = /^(x86-)?(\w*)/.exec(board)[0];
const match = navigator.appVersion.match(/CrOS\s+\S+\s+([\d.]+)/);
const osVer = match ? match[1] : '';
base = analytics.EventBuilder.builder().dimen(1, boardName).dimen(2, osVer);
};
return Promise.all([getConfig(), checkEnabled(), initBuilder()])
......
......@@ -262,19 +262,9 @@ export function initialize(promptMigrate) {
{ackMigratePictures: 0},
(values) => resolve(values.ackMigratePictures >= 1));
});
const checkMigrated = new Promise((resolve) => {
if (chrome.chromeosInfoPrivate) {
chrome.chromeosInfoPrivate.get(
['cameraMediaConsolidated'],
(values) => resolve(values['cameraMediaConsolidated']));
} else {
resolve(false);
}
});
const ackMigrate = () =>
browserProxy.localStorageSet({ackMigratePictures: 1});
const doneMigrate = () => chrome.chromeosInfoPrivate &&
chrome.chromeosInfoPrivate.set('cameraMediaConsolidated', true);
const doneMigrate = () => browserProxy.doneMigrate();
return Promise
.all([
......@@ -282,7 +272,7 @@ export function initialize(promptMigrate) {
initInternalTempDir(),
initExternalDir(),
checkAcked,
checkMigrated,
browserProxy.checkMigrated(),
])
.then((results) => {
let /** boolean */ acked;
......
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