Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Unified Diff: chrome/browser/resources/pdf/browser_api.js

Issue 2503633002: Propagate browser zoom changes to embedded PDFs. (Closed)
Patch Set: Apply internal pdf zoom to browser zoom. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | chrome/browser/resources/pdf/pdf.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/pdf/browser_api.js
diff --git a/chrome/browser/resources/pdf/browser_api.js b/chrome/browser/resources/pdf/browser_api.js
index 6b75caff30f654827f468143bf46bed3961bd154..5d00c038440f61e1af60cf334521ab6ee8367e2e 100644
--- a/chrome/browser/resources/pdf/browser_api.js
+++ b/chrome/browser/resources/pdf/browser_api.js
@@ -5,26 +5,6 @@
'use strict';
/**
- * Returns a promise that will resolve to the default zoom factor.
- * @param {!Object} streamInfo The stream object pointing to the data contained
- * in the PDF.
- * @return {Promise<number>} A promise that will resolve to the default zoom
- * factor.
- */
-function lookupDefaultZoom(streamInfo) {
- // Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within
- // a webview.
- if (!chrome.tabs || streamInfo.tabId < 0)
- return Promise.resolve(1);
-
- return new Promise(function(resolve, reject) {
- chrome.tabs.getZoomSettings(streamInfo.tabId, function(zoomSettings) {
- resolve(zoomSettings.defaultZoomFactor);
- });
- });
-}
-
-/**
* Returns a promise that will resolve to the initial zoom factor
* upon starting the plugin. This may differ from the default zoom
* if, for example, the page is zoomed before the plugin is run.
@@ -52,31 +32,26 @@ class BrowserApi {
* @constructor
* @param {!Object} streamInfo The stream object which points to the data
* contained in the PDF.
- * @param {number} defaultZoom The default browser zoom.
* @param {number} initialZoom The initial browser zoom
* upon starting the plugin.
- * @param {boolean} manageZoom Whether to manage zoom.
+ * @param {BrowserApi.ZoomBehaviour} zoomBehaviour How to manage zoom.
*/
- constructor(streamInfo, defaultZoom, initialZoom, manageZoom) {
+ constructor(streamInfo, initialZoom, zoomBehaviour) {
this.streamInfo_ = streamInfo;
- this.defaultZoom_ = defaultZoom;
this.initialZoom_ = initialZoom;
- this.manageZoom_ = manageZoom;
+ this.zoomBehaviour_ = zoomBehaviour;
}
/**
* Returns a promise to a BrowserApi.
* @param {!Object} streamInfo The stream object pointing to the data
* contained in the PDF.
- * @param {boolean} manageZoom Whether to manage zoom.
+ * @param {BrowserApi.ZoomBehaviour} zoomBehaviour How to manage zoom.
*/
- static create(streamInfo, manageZoom) {
- return Promise.all([
- lookupDefaultZoom(streamInfo),
- lookupInitialZoom(streamInfo)
- ]).then(function(zoomFactors) {
+ static create(streamInfo, zoomBehaviour) {
+ return lookupInitialZoom(streamInfo).then(function(initialZoom) {
return new BrowserApi(
- streamInfo, zoomFactors[0], zoomFactors[1], manageZoom);
+ streamInfo, initialZoom, zoomBehaviour);
});
}
@@ -103,22 +78,14 @@ class BrowserApi {
* has been updated.
*/
setZoom(zoom) {
- if (!this.manageZoom_)
- return Promise.resolve();
+ if (this.zoomBehaviour_ != BrowserApi.ZoomBehaviour.MANAGE)
+ return Promise.reject(new Error('Viewer does not manage browser zoom.'));
return new Promise(function(resolve, reject) {
chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve);
}.bind(this));
}
/**
- * Returns the default browser zoom factor.
- * @return {number} The default browser zoom factor.
- */
- getDefaultZoom() {
- return this.defaultZoom_;
- }
-
- /**
* Returns the initial browser zoom factor.
* @return {number} The initial browser zoom factor.
*/
@@ -127,12 +94,21 @@ class BrowserApi {
}
/**
+ * Returns how to manage the zoom.
+ * @return {BrowserApi.ZoomBehaviour} How to manage zoom.
+ */
+ getZoomBehaviour() {
+ return this.zoomBehaviour_;
+ }
+
+ /**
* Adds an event listener to be notified when the browser zoom changes.
* @param {function} listener The listener to be called with the new zoom
* factor.
*/
addZoomEventListener(listener) {
- if (!this.manageZoom_)
+ if (!(this.zoomBehaviour_ == BrowserApi.ZoomBehaviour.MANAGE ||
+ this.zoomBehaviour_ == BrowserApi.ZoomBehaviour.PROPAGATE_PARENT))
return;
chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) {
@@ -144,6 +120,16 @@ class BrowserApi {
};
/**
+ * Enumeration of ways to manage zoom changes.
+ * @enum {number}
+ */
+BrowserApi.ZoomBehaviour = {
+ NONE: 0,
+ MANAGE: 1,
+ PROPAGATE_PARENT: 2
+};
+
+/**
* Creates a BrowserApi for an extension running as a mime handler.
* @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
* using the mimeHandlerPrivate API.
@@ -153,8 +139,11 @@ function createBrowserApiForMimeHandlerView() {
chrome.mimeHandlerPrivate.getStreamInfo(resolve);
}).then(function(streamInfo) {
let promises = [];
- let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1;
+ let zoomBehaviour = BrowserApi.ZoomBehaviour.NONE;
if (streamInfo.tabId != -1) {
+ zoomBehaviour = streamInfo.embedded ?
+ BrowserApi.ZoomBehaviour.PROPAGATE_PARENT :
+ BrowserApi.ZoomBehaviour.MANAGE;
promises.push(new Promise(function(resolve) {
chrome.tabs.get(streamInfo.tabId, resolve);
}).then(function(tab) {
@@ -162,14 +151,14 @@ function createBrowserApiForMimeHandlerView() {
streamInfo.tabUrl = tab.url;
}));
}
- if (manageZoom) {
+ if (zoomBehaviour == BrowserApi.ZoomBehaviour.MANAGE) {
promises.push(new Promise(function(resolve) {
chrome.tabs.setZoomSettings(
streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
}));
}
return Promise.all(promises).then(
- function() { return BrowserApi.create(streamInfo, manageZoom); });
+ function() { return BrowserApi.create(streamInfo, zoomBehaviour); });
});
}
@@ -197,7 +186,9 @@ function createBrowserApiForPrintPreview() {
streamInfo.tabUrl = tab.url;
resolve();
});
- }).then(function() { return BrowserApi.create(streamInfo, false); });
+ }).then(function() {
+ return BrowserApi.create(streamInfo, BrowserApi.ZoomBehaviour.NONE);
+ });
}
/**
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | chrome/browser/resources/pdf/pdf.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698