1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * Returns a promise that will resolve to the default zoom factor.
9 * @param {!Object} streamInfo The stream object pointing to the data contained
11 * @return {Promise<number>} A promise that will resolve to the default zoom
14 function lookupDefaultZoom(streamInfo) {
15 // Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within
17 if (!chrome.tabs || streamInfo.tabId < 0)
18 return Promise.resolve(1);
20 return new Promise(function(resolve, reject) {
21 chrome.tabs.getZoomSettings(streamInfo.tabId, function(zoomSettings) {
22 resolve(zoomSettings.defaultZoomFactor);
28 * Returns a promise that will resolve to the initial zoom factor
29 * upon starting the plugin. This may differ from the default zoom
30 * if, for example, the page is zoomed before the plugin is run.
31 * @param {!Object} streamInfo The stream object pointing to the data contained
33 * @return {Promise<number>} A promise that will resolve to the initial zoom
36 function lookupInitialZoom(streamInfo) {
37 // Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within
39 if (!chrome.tabs || streamInfo.tabId < 0)
40 return Promise.resolve(1);
42 return new Promise(function(resolve, reject) {
43 chrome.tabs.getZoom(streamInfo.tabId, resolve);
48 * A class providing an interface to the browser.
53 * @param {!Object} streamInfo The stream object which points to the data
54 * contained in the PDF.
55 * @param {number} defaultZoom The default browser zoom.
56 * @param {number} initialZoom The initial browser zoom
57 * upon starting the plugin.
58 * @param {boolean} manageZoom Whether to manage zoom.
60 constructor(streamInfo, defaultZoom, initialZoom, manageZoom) {
61 this.streamInfo_ = streamInfo;
62 this.defaultZoom_ = defaultZoom;
63 this.initialZoom_ = initialZoom;
64 this.manageZoom_ = manageZoom;
68 * Returns a promise to a BrowserApi.
69 * @param {!Object} streamInfo The stream object pointing to the data
70 * contained in the PDF.
71 * @param {boolean} manageZoom Whether to manage zoom.
73 static create(streamInfo, manageZoom) {
75 lookupDefaultZoom(streamInfo),
76 lookupInitialZoom(streamInfo)
77 ]).then(function(zoomFactors) {
78 return new BrowserApi(
79 streamInfo, zoomFactors[0], zoomFactors[1], manageZoom);
84 * Returns the stream info pointing to the data contained in the PDF.
85 * @return {Object} The stream info object.
88 return this.streamInfo_;
95 if (chrome.mimeHandlerPrivate)
96 chrome.mimeHandlerPrivate.abortStream();
100 * Sets the browser zoom.
101 * @param {number} zoom The zoom factor to send to the browser.
102 * @return {Promise} A promise that will be resolved when the browser zoom
106 if (!this.manageZoom_)
107 return Promise.resolve();
108 return new Promise(function(resolve, reject) {
109 chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve);
114 * Returns the default browser zoom factor.
115 * @return {number} The default browser zoom factor.
118 return this.defaultZoom_;
122 * Returns the initial browser zoom factor.
123 * @return {number} The initial browser zoom factor.
126 return this.initialZoom_;
130 * Adds an event listener to be notified when the browser zoom changes.
131 * @param {function} listener The listener to be called with the new zoom
134 addZoomEventListener(listener) {
135 if (!this.manageZoom_)
138 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) {
139 if (zoomChangeInfo.tabId != this.streamInfo_.tabId)
141 listener(zoomChangeInfo.newZoomFactor);
147 * Creates a BrowserApi for an extension running as a mime handler.
148 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
149 * using the mimeHandlerPrivate API.
151 function createBrowserApiForMimeHandlerView() {
152 return new Promise(function(resolve, reject) {
153 chrome.mimeHandlerPrivate.getStreamInfo(resolve);
154 }).then(function(streamInfo) {
155 let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1;
156 return new Promise(function(resolve, reject) {
161 chrome.tabs.setZoomSettings(
162 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
163 }).then(function() { return BrowserApi.create(streamInfo, manageZoom); });
168 * Creates a BrowserApi instance for an extension not running as a mime handler.
169 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
172 function createBrowserApiForStandaloneExtension() {
173 let url = window.location.search.substring(1);
178 embedded: window.parent != window,
181 return new Promise(function(resolve, reject) {
186 chrome.tabs.getCurrent(function(tab) {
187 streamInfo.tabId = tab.id;
190 }).then(function() { return BrowserApi.create(streamInfo, false); });
194 * Returns a promise that will resolve to a BrowserApi instance.
195 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the
196 * current environment.
198 function createBrowserApi() {
199 if (window.location.search)
200 return createBrowserApiForStandaloneExtension();
202 return createBrowserApiForMimeHandlerView();