Commit 154f9c48 authored by dstockwell's avatar dstockwell Committed by Commit Bot

pdf: Use assert.js instead of `new Error`

Change-Id: I851cd8143482f383f3c1d16c960085ff3911c348
Reviewed-on: https://chromium-review.googlesource.com/c/1377954
Commit-Queue: dstockwell <dstockwell@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#619361}
parent eff06c31
......@@ -18,6 +18,9 @@ group("closure_compile") {
}
js_library("browser_api") {
deps = [
"//ui/webui/resources/js:assert",
]
externs_list = [
"$externs_path/chrome_extensions.js",
"$externs_path/mime_handler_private.js",
......
......@@ -102,8 +102,9 @@ class BrowserApi {
* has been updated.
*/
setZoom(zoom) {
if (this.zoomBehavior_ != BrowserApi.ZoomBehavior.MANAGE)
return Promise.reject(new Error('Viewer does not manage browser zoom.'));
assert(
this.zoomBehavior_ == BrowserApi.ZoomBehavior.MANAGE,
'Viewer does not manage browser zoom.');
return new Promise((resolve, reject) => {
chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve);
});
......
......@@ -37,9 +37,10 @@
<script src="zoom_manager.js"></script>
<script src="gesture_detector.js"></script>
<script src="pdf_scripting_api.js"></script>
<link rel="import" href="chrome://resources/html/load_time_data.html">
<link rel="import" href="chrome://resources/html/promise_resolver.html">
<link rel="import" href="chrome://resources/html/util.html">
<script src="chrome://resources/js/assert.js"></script>
<script src="chrome://resources/js/load_time_data.js"></script>
<script src="chrome://resources/js/util.js"></script>
<script src="chrome://resources/js/promise_resolver.js"></script>
<script src="browser_api.js"></script>
<script src="metrics.js"></script>
<script src="pdf_viewer.js"></script>
......
......@@ -483,11 +483,7 @@ PDFViewer.prototype = {
annotationModeChanged_: async function(e) {
const annotationMode = e.detail.value;
if (annotationMode) {
// TODO(dstockwell): add assert lib and replace this with assert
if (this.currentController_ != this.pluginController_) {
throw new Error(
'Plugin controller is not current, cannot enter annotation mode');
}
assert(this.currentController_ == this.pluginController_);
// Enter annotation mode.
// TODO(dstockwell): set plugin read-only, begin transition
this.updateProgress(0);
......@@ -501,11 +497,7 @@ PDFViewer.prototype = {
this.updateProgress(100);
} else {
// Exit annotation mode.
// TODO(dstockwell): add assert lib and replace this with assert
if (this.currentController_ != this.inkController_) {
throw new Error(
'Ink controller is not current, cannot exit annotation mode');
}
assert(this.currentController_ == this.inkController_);
// TODO(dstockwell): set ink read-only, begin transition
this.updateProgress(0);
// This runs separately to allow other consumers of `loaded` to queue
......@@ -625,18 +617,17 @@ PDFViewer.prototype = {
if (this.loadState_ == loadState) {
return;
}
if ((loadState == LoadState.SUCCESS || loadState == LoadState.FAILURE) &&
this.loadState_ != LoadState.LOADING) {
throw new Error('Internal error: invalid loadState transition.');
}
this.loadState_ = loadState;
if (loadState == LoadState.SUCCESS) {
assert(this.loadState_ == LoadState.LOADING);
this.loaded_.resolve();
} else if (loadState == LoadState.FAILED) {
assert(this.loadState_ == LoadState.LOADING);
this.loaded_.reject();
} else {
assert(loadState == LoadState.LOADING);
this.loaded_ = new PromiseResolver();
}
this.loadState_ = loadState;
},
/**
......@@ -716,9 +707,7 @@ PDFViewer.prototype = {
* @private
*/
setUserInitiated_: function(userInitiated) {
if (this.isUserInitiatedEvent_ == userInitiated) {
throw new Error('Trying to set user initiated to current value.');
}
assert(this.isUserInitiatedEvent_ != userInitiated);
this.isUserInitiatedEvent_ = userInitiated;
},
......@@ -1403,8 +1392,7 @@ class PluginController extends ContentController {
break;
case 'consumeSaveToken':
const resolve = this.pendingTokens_.get(message.data.token);
if (!this.pendingTokens_.delete(message.data.token))
throw new Error('Internal error: save token not found.');
assert(this.pendingTokens_.delete(message.data.token));
resolve(null);
break;
}
......@@ -1417,15 +1405,14 @@ class PluginController extends ContentController {
* @private
*/
saveData_(messageData) {
if (!(loadTimeData.getBoolean('pdfFormSaveEnabled') ||
loadTimeData.getBoolean('pdfAnnotationsEnabled')))
throw new Error('Internal error: save not enabled.');
assert(
loadTimeData.getBoolean('pdfFormSaveEnabled') ||
loadTimeData.getBoolean('pdfAnnotationsEnabled'));
// Verify a token that was created by this instance is included to avoid
// being spammed.
const resolver = this.pendingTokens_.get(messageData.token);
if (!this.pendingTokens_.delete(messageData.token))
throw new Error('Internal error: save token not found, abort save.');
assert(this.pendingTokens_.delete(messageData.token));
if (!messageData.dataToSave) {
resolver.reject();
......@@ -1440,13 +1427,13 @@ class PluginController extends ContentController {
const buffer = messageData.dataToSave;
const bufView = new Uint8Array(buffer);
if (bufView.length > MAX_FILE_SIZE)
throw new Error(`File too large to be saved: ${bufView.length} bytes.`);
if (bufView.length < MIN_FILE_SIZE ||
String.fromCharCode(bufView[0], bufView[1], bufView[2], bufView[3]) !=
'%PDF') {
throw new Error('Not a PDF file.');
}
assert(
bufView.length <= MAX_FILE_SIZE,
`File too large to be saved: ${bufView.length} bytes.`);
assert(bufView.length >= MIN_FILE_SIZE);
assert(
String.fromCharCode(bufView[0], bufView[1], bufView[2], bufView[3]) ==
'%PDF');
resolver.resolve(messageData);
}
......
......@@ -441,11 +441,10 @@ Viewport.prototype = {
* @private
*/
setZoomInternal_: function(newZoom) {
if (!this.allowedToChangeZoom_) {
throw new Error(
'Called Viewport.setZoomInternal_ without calling ' +
'Viewport.mightZoom_.');
}
assert(
this.allowedToChangeZoom_,
'Called Viewport.setZoomInternal_ without calling ' +
'Viewport.mightZoom_.');
// Record the scroll position (relative to the top-left of the window).
const currentScrollPos = {
x: this.position.x / this.zoom,
......
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