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") { ...@@ -9,10 +9,14 @@ js_type_check("closure_compile") {
} }
js_library("browser_proxy") { js_library("browser_proxy") {
deps = [ "..:chrome_util" ]
sources = [ sources = [
"browser_proxy.js", "browser_proxy.js",
"browser_proxy_interface.js", "browser_proxy_interface.js",
"webui_browser_proxy.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 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import * as util from '../chrome_util.js';
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
import {BrowserProxy} from './browser_proxy_interface.js'; import {BrowserProxy} from './browser_proxy_interface.js';
...@@ -34,6 +35,25 @@ class ChromeAppBrowserProxy { ...@@ -34,6 +35,25 @@ class ChromeAppBrowserProxy {
localStorageRemove(items, callback) { localStorageRemove(items, callback) {
chrome.storage.local.remove(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(); export const browserProxy = new ChromeAppBrowserProxy();
...@@ -33,4 +33,22 @@ export class BrowserProxy { ...@@ -33,4 +33,22 @@ export class BrowserProxy {
* @param {function()=} callback * @param {function()=} callback
*/ */
localStorageRemove(items, 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 { ...@@ -74,6 +74,23 @@ class WebUIBrowserProxy {
callback(); callback();
} }
} }
/** @override */
async checkMigrated() {
NOTIMPLEMENTED();
return false;
}
/** @override */
async doneMigrate() {
NOTIMPLEMENTED();
}
/** @override */
async getBoard() {
NOTIMPLEMENTED();
return '';
}
} }
export const browserProxy = new WebUIBrowserProxy(); export const browserProxy = new WebUIBrowserProxy();
......
...@@ -99,3 +99,14 @@ export function assertBoolean(value, optMessage) { ...@@ -99,3 +99,14 @@ export function assertBoolean(value, optMessage) {
} }
return /** @type {boolean} */ (value); 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 @@ ...@@ -8,3 +8,11 @@
/** @type {string|undefined} */ /** @type {string|undefined} */
chrome.runtime.Manifest.prototype.version_name; chrome.runtime.Manifest.prototype.version_name;
/**
* @typedef {{
* get: function(Array<string>, function(Object)),
* set: function(string, ?),
* }}
*/
chrome.chromeosInfoPrivate;
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import {browserProxy} from './browser_proxy/browser_proxy.js';
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
import {Intent} from './intent.js'; import {Intent} from './intent.js';
import * as state from './state.js'; import * as state from './state.js';
...@@ -48,23 +49,12 @@ const ga = (function() { ...@@ -48,23 +49,12 @@ const ga = (function() {
} }
}); });
}; };
const initBuilder = () => { const initBuilder = async () => {
return new Promise((resolve) => { const board = await browserProxy.getBoard();
try { const boardName = /^(x86-)?(\w*)/.exec(board)[0];
chrome.chromeosInfoPrivate.get( const match = navigator.appVersion.match(/CrOS\s+\S+\s+([\d.]+)/);
['board'], (values) => resolve(values['board'])); const osVer = match ? match[1] : '';
} catch (e) { base = analytics.EventBuilder.builder().dimen(1, boardName).dimen(2, osVer);
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);
});
}; };
return Promise.all([getConfig(), checkEnabled(), initBuilder()]) return Promise.all([getConfig(), checkEnabled(), initBuilder()])
......
...@@ -262,19 +262,9 @@ export function initialize(promptMigrate) { ...@@ -262,19 +262,9 @@ export function initialize(promptMigrate) {
{ackMigratePictures: 0}, {ackMigratePictures: 0},
(values) => resolve(values.ackMigratePictures >= 1)); (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 = () => const ackMigrate = () =>
browserProxy.localStorageSet({ackMigratePictures: 1}); browserProxy.localStorageSet({ackMigratePictures: 1});
const doneMigrate = () => chrome.chromeosInfoPrivate && const doneMigrate = () => browserProxy.doneMigrate();
chrome.chromeosInfoPrivate.set('cameraMediaConsolidated', true);
return Promise return Promise
.all([ .all([
...@@ -282,7 +272,7 @@ export function initialize(promptMigrate) { ...@@ -282,7 +272,7 @@ export function initialize(promptMigrate) {
initInternalTempDir(), initInternalTempDir(),
initExternalDir(), initExternalDir(),
checkAcked, checkAcked,
checkMigrated, browserProxy.checkMigrated(),
]) ])
.then((results) => { .then((results) => {
let /** boolean */ acked; 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