Commit 76af767b authored by Kuo Jen Wei's avatar Kuo Jen Wei Committed by Commit Bot

[CCA] Convert chrome_util.js into ES6 module.

Add symbol exported from chrome_util.js into cca namespace and transform it
into ES6 module.

Bug: 141518780
Test: Pass closure compiler check, tast run <DUT> 'camera.CCAUI*' and
validate all function of CCA on HALv1/v3 device works correctly.

Change-Id: Ie9fcea3e7ecc04f7b5bc844f8a786d5b1c4b3c4f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1977895
Commit-Queue: Kuo Jen Wei <inker@chromium.org>
Reviewed-by: default avatarShik Chen <shik@chromium.org>
Auto-Submit: Kuo Jen Wei <inker@chromium.org>
Cr-Commit-Position: refs/heads/master@{#727359}
parent 760f06cd
......@@ -37,6 +37,9 @@ js_type_check("compile_resources") {
}
js_library("chrome_util") {
deps = [
":namespace",
]
}
js_library("intent") {
......
......@@ -14,11 +14,6 @@ var cca = cca || {};
*/
cca.bg = {};
/**
* import {assert} from './chrome_util.js';
*/
var assert = assert || {};
/**
* Fixed minimum width of the window inner-bounds in pixels.
* @type {number}
......
......@@ -2,42 +2,23 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview Utility functions copied from chrome://resources/js.
* TODO(inker): Reference these functions directly from chrome://resources/js.
*/
/* eslint-disable */
/**
* Verify |condition| is truthy and return |condition| if so.
* @template T
* @param {T} condition A condition to check for truthiness. Note that this
* may be used to test whether a value is defined or not, and we don't want
* to force a cast to Boolean.
* @param {string=} opt_message A message to show on failure.
* @param {string=} optMessage A message to show on failure.
* @return {T} A non-null |condition|.
* @closurePrimitive {asserts.truthy}
*/
/* #export */ function assert(condition, opt_message) {
export function assert(condition, optMessage) {
if (!condition) {
let message = 'Assertion failed';
if (opt_message) {
message = message + ': ' + opt_message;
if (optMessage) {
message = message + ': ' + optMessage;
}
const error = new Error(message);
const global = function() {
const thisOrSelf = this || self;
/** @type {boolean} */
thisOrSelf.traceAssertionsForTesting;
return thisOrSelf;
}();
if (global.traceAssertionsForTesting) {
console.warn(error.stack);
}
throw error;
throw new Error(message);
}
return condition;
}
......@@ -61,55 +42,71 @@
* This code should only be hit in the case of serious programmer error or
* unexpected input.
*
* @param {string=} opt_message A message to show when this is hit.
* @param {string=} optMessage A message to show when this is hit.
* @closurePrimitive {asserts.fail}
*/
/* #export */ function assertNotReached(opt_message) {
assert(false, opt_message || 'Unreachable code hit');
export function assertNotReached(optMessage) {
assert(false, optMessage || 'Unreachable code hit');
}
// Disables eslint check for closure compiler constructor type.
/* eslint-disable valid-jsdoc */
/**
* @param {*} value The value to check.
* @param {function(new: T, ...)} type A user-defined constructor.
* @param {string=} opt_message A message to show when this is hit.
* @param {string=} optMessage A message to show when this is hit.
* @return {T}
* @template T
*/
/* #export */ function assertInstanceof(value, type, opt_message) {
export function assertInstanceof(value, type, optMessage) {
// We don't use assert immediately here so that we avoid constructing an error
// message if we don't have to.
if (!(value instanceof type)) {
assertNotReached(
opt_message ||
optMessage ||
'Value ' + value + ' is not a[n] ' + (type.name || typeof type));
}
return value;
}
/* eslint-enable valid-jsdoc */
/**
* @param {*} value The value to check.
* @param {string=} opt_message A message to show when this is hit.
* @param {string=} optMessage A message to show when this is hit.
* @return {string}
*/
/* #export */ function assertString(value, opt_message) {
export function assertString(value, optMessage) {
// We don't use assert immediately here so that we avoid constructing an error
// message if we don't have to.
if (typeof value !== 'string') {
assertNotReached(opt_message || 'Value ' + value + ' is not a string');
assertNotReached(optMessage || 'Value ' + value + ' is not a string');
}
return /** @type {string} */ (value);
}
/**
* @param {*} value The value to check.
* @param {string=} opt_message A message to show when this is hit.
* @param {string=} optMessage A message to show when this is hit.
* @return {boolean}
*/
/* #export */ function assertBoolean(value, opt_message) {
export function assertBoolean(value, optMessage) {
// We don't use assert immediately here so that we avoid constructing an error
// message if we don't have to.
if (typeof value !== 'boolean') {
assertNotReached(opt_message || 'Value ' + value + ' is not a boolean');
assertNotReached(optMessage || 'Value ' + value + ' is not a boolean');
}
return /** @type {boolean} */ (value);
}
/** @const */
cca.assert = assert;
/** @const */
cca.assertNotReached = assertNotReached;
/** @const */
cca.assertInstanceof = assertInstanceof;
/** @const */
cca.assertString = assertString;
/** @const */
cca.assertBoolean = assertBoolean;
......@@ -9,11 +9,6 @@
*/
var cca = cca || {};
/**
* import {assert, assertInstanceof} from './chrome_util.js';
*/
var {assert, assertInstanceof} = {assert, assertInstanceof};
/**
* Cover photo of gallery button.
*/
......@@ -94,7 +89,7 @@ cca.GalleryButton = class {
* @type {!HTMLButtonElement}
* @private
*/
this.button_ = assertInstanceof(
this.button_ = cca.assertInstanceof(
document.querySelector('#gallery-enter'), HTMLButtonElement);
/**
......@@ -213,7 +208,7 @@ cca.GalleryButton = class {
cca.util.orientPhoto(blob, resolve, () => resolve(blob));
});
const file = await cca.models.FileSystem.saveBlob(orientedPhoto, name);
assert(file !== null);
cca.assert(file !== null);
await this.updateCover_(file);
}
......@@ -231,7 +226,7 @@ cca.GalleryButton = class {
async finishSaveVideo(video, name) {
const tempFile = await video.endWrite();
const file = await cca.models.FileSystem.saveVideo(tempFile, name);
assert(file !== null);
cca.assert(file !== null);
await this.updateCover_(file);
}
};
......@@ -14,11 +14,6 @@ var cca = cca || {};
*/
cca.intent = cca.intent || {};
/**
* import {assertNotReached} from './chrome_util.js';
*/
var assertNotReached = assertNotReached || {};
/**
* Thrown when fails to parse intent url.
*/
......
......@@ -9,11 +9,6 @@
*/
var cca = cca || {};
/**
* import {assert, assertInstanceof} from './chrome_util.js';
*/
var {assert, assertInstanceof} = {assert, assertInstanceof};
/**
* Creates the Camera App main object.
* @implements {cca.bg.ForegroundOps}
......@@ -139,7 +134,7 @@ cca.App = class {
cca.proxy.browserProxy.localStorageGet(
payload(element),
(values) => cca.util.toggleChecked(
assertInstanceof(element, HTMLInputElement),
cca.assertInstanceof(element, HTMLInputElement),
values[element.dataset.key]));
}
});
......@@ -165,7 +160,7 @@ cca.App = class {
})
.then((external) => {
cca.state.set('ext-fs', external);
assert(cca.models.FileSystem.externalDir !== null);
cca.assert(cca.models.FileSystem.externalDir !== null);
this.galleryButton_.initialize(cca.models.FileSystem.externalDir);
cca.nav.open('camera');
})
......@@ -237,7 +232,7 @@ document.addEventListener('DOMContentLoaded', async () => {
if (cca.App.instance_ !== null) {
return;
}
assert(window['backgroundOps'] !== undefined);
cca.assert(window['backgroundOps'] !== undefined);
const /** !cca.bg.BackgroundOps */ bgOps = window['backgroundOps'];
const perfLogger = bgOps.getPerfLogger();
......
......@@ -14,11 +14,6 @@ var cca = cca || {};
*/
cca.views = cca.views || {};
/**
* import {assert} from '../chrome_util.js';
*/
var assert = assert || {};
/**
* Thrown when app window suspended during stream reconfiguration.
*/
......@@ -344,7 +339,7 @@ cca.views.Camera = class extends cca.views.View {
}
try {
if (deviceOperator !== null) {
assert(deviceId !== null);
cca.assert(deviceId !== null);
await deviceOperator.setFpsRange(deviceId, constraints);
await deviceOperator.setCaptureIntent(
deviceId, this.modes_.getCaptureIntent(mode));
......@@ -398,7 +393,7 @@ cca.views.Camera = class extends cca.views.View {
if (await this.startWithDevice_(id)) {
// Make the different active camera announced by screen reader.
const currentId = this.options_.currentDeviceId;
assert(currentId !== null);
cca.assert(currentId !== null);
if (currentId === this.activeDeviceId_) {
return;
}
......@@ -432,7 +427,7 @@ cca.views.Camera = class extends cca.views.View {
this.configuring_ = this.start_();
}, 100);
assert(window['backgroundOps'] !== undefined);
cca.assert(window['backgroundOps'] !== undefined);
const /** !cca.bg.BackgroundOps */ bgOps = window['backgroundOps'];
bgOps.getPerfLogger().interrupt();
return false;
......
......@@ -19,11 +19,6 @@ cca.views = cca.views || {};
*/
cca.views.camera = cca.views.camera || {};
/**
* import {assert} from '../chrome_util.js';
*/
var assert = assert || {};
/**
* Creates a controller to handle layouts of Camera view.
*/
......@@ -65,8 +60,8 @@ cca.views.camera.Layout = class {
static cssStyle_(selector) {
const rule = cca.views.camera.Layout.cssRules_.find(
(rule) => rule.selectorText === selector);
assert(rule !== undefined);
assert(rule.style !== null);
cca.assert(rule !== undefined);
cca.assert(rule.style !== null);
return rule.style;
}
......
......@@ -19,11 +19,6 @@ cca.views = cca.views || {};
*/
cca.views.camera = cca.views.camera || {};
/**
* import {assert, assertInstanceof} from '../chrome_util.js';
*/
var {assert, assertInstanceof} = {assert, assertInstanceof};
/**
* Creates a controller for the video preview of Camera view.
*/
......@@ -43,7 +38,7 @@ cca.views.camera.Preview = class {
* @type {!HTMLVideoElement}
* @private
*/
this.video_ = assertInstanceof(
this.video_ = cca.assertInstanceof(
document.querySelector('#preview-video'), HTMLVideoElement);
/**
......@@ -51,7 +46,7 @@ cca.views.camera.Preview = class {
* @type {!HTMLElement}
* @private
*/
this.metadata_ = assertInstanceof(
this.metadata_ = cca.assertInstanceof(
document.querySelector('#preview-metadata'), HTMLElement);
/**
......@@ -129,7 +124,7 @@ cca.views.camera.Preview = class {
*/
setSource_(stream) {
const video =
assertInstanceof(document.createElement('video'), HTMLVideoElement);
cca.assertInstanceof(document.createElement('video'), HTMLVideoElement);
video.id = 'preview-video';
video.classList = this.video_.classList;
video.muted = true; // Mute to avoid echo from the captured audio.
......
......@@ -23,11 +23,6 @@ cca.views.camera = cca.views.camera || {};
*/
cca.views.camera.timertick = cca.views.camera.timertick || {};
/**
* import {assertInstanceof} from '../chrome_util.js';
*/
var assertInstanceof = assertInstanceof || {};
/**
* Handler to cancel the active running timer-ticks.
* @type {?function()}
......@@ -46,7 +41,7 @@ cca.views.camera.timertick.start = function() {
}
return new Promise((resolve, reject) => {
let tickTimeout = null;
const tickMsg = assertInstanceof(
const tickMsg = cca.assertInstanceof(
document.querySelector('#timer-tick-msg'), HTMLElement);
cca.views.camera.timertick.cancel_ = () => {
if (tickTimeout) {
......
......@@ -14,11 +14,6 @@ var cca = cca || {};
*/
cca.views = cca.views || {};
/**
* import {assert, assertNotReached} from '../chrome_util.js';
*/
var {assert, assertNotReached} = {assert, assertNotReached};
/**
* The maximum number of pixels in the downscaled intent photo result. Reference
* from GCA: https://goto.google.com/gca-inline-bitmap-max-pixel-num
......@@ -142,7 +137,7 @@ cca.views.CameraIntent = class extends cca.views.Camera {
} else if (this.videoResultFile_ !== null) {
return this.reviewResult_.openVideo(this.videoResultFile_);
} else {
assertNotReached('End take without intent result.');
cca.assertNotReached('End take without intent result.');
}
})();
const result = this.photoResult_ || this.videoResult_;
......
......@@ -14,21 +14,6 @@ var cca = cca || {};
*/
cca.views = cca.views || {};
/**
* import {assertString} from '../chrome_util.js';
*/
var assertString = assertString || {};
/**
* import {assertInstanceof, assertString, assertBoolean}
* from '../chrome_util.js';
*/
var {assertInstanceof, assertString, assertBoolean} = {
assertInstanceof,
assertString,
assertBoolean,
};
/**
* Creates the Dialog view controller.
*/
......@@ -43,7 +28,7 @@ cca.views.Dialog = class extends cca.views.View {
* @type {!HTMLButtonElement}
* @private
*/
this.positiveButton_ = assertInstanceof(
this.positiveButton_ = cca.assertInstanceof(
document.querySelector(`${viewId} .dialog-positive-button`),
HTMLButtonElement);
......@@ -51,7 +36,7 @@ cca.views.Dialog = class extends cca.views.View {
* @type {!HTMLButtonElement}
* @private
*/
this.negativeButton_ = assertInstanceof(
this.negativeButton_ = cca.assertInstanceof(
document.querySelector(`${viewId} .dialog-negative-button`),
HTMLButtonElement);
......@@ -59,7 +44,7 @@ cca.views.Dialog = class extends cca.views.View {
* @type {!HTMLElement}
* @private
*/
this.messageHolder_ = assertInstanceof(
this.messageHolder_ = cca.assertInstanceof(
document.querySelector(`${viewId} .dialog-msg-holder`), HTMLElement);
this.positiveButton_.addEventListener('click', () => this.leave(true));
......@@ -72,7 +57,7 @@ cca.views.Dialog = class extends cca.views.View {
* @override
*/
entering({message, cancellable = false} = {}) {
message = assertString(message);
message = cca.assertString(message);
this.messageHolder_.textContent = message;
if (this.negativeButton_) {
this.negativeButton_.hidden = !cancellable;
......
......@@ -14,11 +14,6 @@ var cca = cca || {};
*/
cca.views = cca.views || {};
/**
* import {assertString} from '../chrome_util.js';
*/
var assertString = assertString || {};
/**
* Creates the warning-view controller.
*/
......@@ -58,7 +53,7 @@ cca.views.Warning = class extends cca.views.View {
* @override
*/
entering(name) {
name = assertString(name);
name = cca.assertString(name);
// Remove the error-name from the stack to avoid duplication. Then make the
// error-name the latest one to show its message.
......@@ -78,7 +73,7 @@ cca.views.Warning = class extends cca.views.View {
* Recovered error-name for leaving the view.
* @type {string}
*/
const name = assertString(args[0]);
const name = cca.assertString(args[0]);
// Remove the recovered error from the stack but don't leave the view until
// there is no error left in the stack.
......
......@@ -5,7 +5,6 @@
<html>
<head>
<script src="../js/namespace.js"></script>
<script src="../js/chrome_util.js"></script>
<script src="../js/mojo/mojo_bindings_lite.js"></script>
<script src="../js/mojo/camera_intent.mojom-lite.js"></script>
<script src="../js/mojo/camera_app_helper.mojom-lite.js"></script>
......
......@@ -10,7 +10,7 @@
<meta charset="utf-8">
<link rel="stylesheet" href="../css/main.css">
<script src="../js/namespace.js"></script>
<script src="../js/chrome_util.js"></script>
<script type="module" src="../js/chrome_util.js"></script>
<script src="../js/browser_proxy/browser_proxy.js"></script>
<script src="../js/google-analytics-bundle.js"></script>
<script type="module" src="../js/type.js"></script>
......
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