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 * A class providing an interface to the browser.
33 * @param {!Object} streamInfo The stream object which points to the data
34 * contained in the PDF.
35 * @param {number} defaultZoom The default browser zoom.
36 * @param {boolean} manageZoom Whether to manage zoom.
38 constructor(streamInfo, defaultZoom, manageZoom) {
39 this.streamInfo_ = streamInfo;
40 this.defaultZoom_ = defaultZoom;
41 this.manageZoom_ = manageZoom;
45 * Returns a promise to a BrowserApi.
46 * @param {!Object} streamInfo The stream object pointing to the data
47 * contained in the PDF.
48 * @param {boolean} manageZoom Whether to manage zoom.
50 static create(streamInfo, manageZoom) {
51 return lookupDefaultZoom(streamInfo).then(function(defaultZoom) {
52 return new BrowserApi(streamInfo, defaultZoom, manageZoom);
57 * Returns the stream info pointing to the data contained in the PDF.
58 * @return {Object} The stream info object.
61 return this.streamInfo_;
68 if (chrome.mimeHandlerPrivate)
69 chrome.mimeHandlerPrivate.abortStream();
73 * Sets the browser zoom.
74 * @param {number} zoom The zoom factor to send to the browser.
75 * @return {Promise} A promise that will be resolved when the browser zoom
79 if (!this.manageZoom_)
80 return Promise.resolve();
81 return new Promise(function(resolve, reject) {
82 chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve);
87 * Returns the default browser zoom factor.
88 * @return {number} The default browser zoom factor.
91 return this.defaultZoom_;
95 * Adds an event listener to be notified when the browser zoom changes.
96 * @param {function} listener The listener to be called with the new zoom
99 addZoomEventListener(listener) {
100 if (!this.manageZoom_)
103 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) {
104 if (zoomChangeInfo.tabId != this.streamInfo_.tabId)
106 listener(zoomChangeInfo.newZoomFactor);
112 * Creates a BrowserApi for an extension running as a mime handler.
113 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
114 * using the mimeHandlerPrivate API.
116 function createBrowserApiForMimeHandlerView() {
117 return new Promise(function(resolve, reject) {
118 chrome.mimeHandlerPrivate.getStreamInfo(resolve);
119 }).then(function(streamInfo) {
120 let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1;
121 return new Promise(function(resolve, reject) {
126 chrome.tabs.setZoomSettings(
127 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
128 }).then(function() { return BrowserApi.create(streamInfo, manageZoom); });
133 * Creates a BrowserApi instance for an extension not running as a mime handler.
134 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
137 function createBrowserApiForStandaloneExtension() {
138 let url = window.location.search.substring(1);
143 embedded: window.parent != window,
146 return new Promise(function(resolve, reject) {
151 chrome.tabs.getCurrent(function(tab) {
152 streamInfo.tabId = tab.id;
155 }).then(function() { return BrowserApi.create(streamInfo, false); });
159 * Returns a promise that will resolve to a BrowserApi instance.
160 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the
161 * current environment.
163 function createBrowserApi() {
164 if (window.location.search)
165 return createBrowserApiForStandaloneExtension();
167 return createBrowserApiForMimeHandlerView();