Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / resources / pdf / browser_api.js
blob53f14b5460e770d538a8ff1eacd57811510b8bcd
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.
5 'use strict';
7 /**
8 * Returns a promise that will resolve to the default zoom factor.
9 * @param {!Object} streamInfo The stream object pointing to the data contained
10 * in the PDF.
11 * @return {Promise<number>} A promise that will resolve to the default zoom
12 * factor.
14 function lookupDefaultZoom(streamInfo) {
15 // Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within
16 // a webview.
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);
23 });
24 });
27 /**
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
32 * in the PDF.
33 * @return {Promise<number>} A promise that will resolve to the initial zoom
34 * factor.
36 function lookupInitialZoom(streamInfo) {
37 // Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within
38 // a webview.
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);
44 });
47 /**
48 * A class providing an interface to the browser.
50 class BrowserApi {
51 /**
52 * @constructor
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;
67 /**
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) {
74 return Promise.all([
75 lookupDefaultZoom(streamInfo),
76 lookupInitialZoom(streamInfo)
77 ]).then(function(zoomFactors) {
78 return new BrowserApi(
79 streamInfo, zoomFactors[0], zoomFactors[1], manageZoom);
80 });
83 /**
84 * Returns the stream info pointing to the data contained in the PDF.
85 * @return {Object} The stream info object.
87 getStreamInfo() {
88 return this.streamInfo_;
91 /**
92 * Aborts the stream.
94 abortStream() {
95 if (chrome.mimeHandlerPrivate)
96 chrome.mimeHandlerPrivate.abortStream();
99 /**
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
103 * has been updated.
105 setZoom(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);
110 }.bind(this));
114 * Returns the default browser zoom factor.
115 * @return {number} The default browser zoom factor.
117 getDefaultZoom() {
118 return this.defaultZoom_;
122 * Returns the initial browser zoom factor.
123 * @return {number} The initial browser zoom factor.
125 getInitialZoom() {
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
132 * factor.
134 addZoomEventListener(listener) {
135 if (!this.manageZoom_)
136 return;
138 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) {
139 if (zoomChangeInfo.tabId != this.streamInfo_.tabId)
140 return;
141 listener(zoomChangeInfo.newZoomFactor);
142 }.bind(this));
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) {
157 if (!manageZoom) {
158 resolve();
159 return;
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
170 * from the URL.
172 function createBrowserApiForStandaloneExtension() {
173 let url = window.location.search.substring(1);
174 let streamInfo = {
175 streamUrl: url,
176 originalUrl: url,
177 responseHeaders: {},
178 embedded: window.parent != window,
179 tabId: -1,
181 return new Promise(function(resolve, reject) {
182 if (!chrome.tabs) {
183 resolve();
184 return;
186 chrome.tabs.getCurrent(function(tab) {
187 streamInfo.tabId = tab.id;
188 resolve();
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();