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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | chrome/browser/resources/pdf/pdf.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 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.
13 */
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);
19
20 return new Promise(function(resolve, reject) {
21 chrome.tabs.getZoomSettings(streamInfo.tabId, function(zoomSettings) {
22 resolve(zoomSettings.defaultZoomFactor);
23 });
24 });
25 }
26
27 /**
28 * Returns a promise that will resolve to the initial zoom factor 8 * Returns a promise that will resolve to the initial zoom factor
29 * upon starting the plugin. This may differ from the default zoom 9 * upon starting the plugin. This may differ from the default zoom
30 * if, for example, the page is zoomed before the plugin is run. 10 * if, for example, the page is zoomed before the plugin is run.
31 * @param {!Object} streamInfo The stream object pointing to the data contained 11 * @param {!Object} streamInfo The stream object pointing to the data contained
32 * in the PDF. 12 * in the PDF.
33 * @return {Promise<number>} A promise that will resolve to the initial zoom 13 * @return {Promise<number>} A promise that will resolve to the initial zoom
34 * factor. 14 * factor.
35 */ 15 */
36 function lookupInitialZoom(streamInfo) { 16 function lookupInitialZoom(streamInfo) {
37 // Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within 17 // Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within
38 // a webview. 18 // a webview.
39 if (!chrome.tabs || streamInfo.tabId < 0) 19 if (!chrome.tabs || streamInfo.tabId < 0)
40 return Promise.resolve(1); 20 return Promise.resolve(1);
41 21
42 return new Promise(function(resolve, reject) { 22 return new Promise(function(resolve, reject) {
43 chrome.tabs.getZoom(streamInfo.tabId, resolve); 23 chrome.tabs.getZoom(streamInfo.tabId, resolve);
44 }); 24 });
45 } 25 }
46 26
47 /** 27 /**
48 * A class providing an interface to the browser. 28 * A class providing an interface to the browser.
49 */ 29 */
50 class BrowserApi { 30 class BrowserApi {
51 /** 31 /**
52 * @constructor 32 * @constructor
53 * @param {!Object} streamInfo The stream object which points to the data 33 * @param {!Object} streamInfo The stream object which points to the data
54 * contained in the PDF. 34 * contained in the PDF.
55 * @param {number} defaultZoom The default browser zoom.
56 * @param {number} initialZoom The initial browser zoom 35 * @param {number} initialZoom The initial browser zoom
57 * upon starting the plugin. 36 * upon starting the plugin.
58 * @param {boolean} manageZoom Whether to manage zoom. 37 * @param {BrowserApi.ZoomBehaviour} zoomBehaviour How to manage zoom.
59 */ 38 */
60 constructor(streamInfo, defaultZoom, initialZoom, manageZoom) { 39 constructor(streamInfo, initialZoom, zoomBehaviour) {
61 this.streamInfo_ = streamInfo; 40 this.streamInfo_ = streamInfo;
62 this.defaultZoom_ = defaultZoom;
63 this.initialZoom_ = initialZoom; 41 this.initialZoom_ = initialZoom;
64 this.manageZoom_ = manageZoom; 42 this.zoomBehaviour_ = zoomBehaviour;
65 } 43 }
66 44
67 /** 45 /**
68 * Returns a promise to a BrowserApi. 46 * Returns a promise to a BrowserApi.
69 * @param {!Object} streamInfo The stream object pointing to the data 47 * @param {!Object} streamInfo The stream object pointing to the data
70 * contained in the PDF. 48 * contained in the PDF.
71 * @param {boolean} manageZoom Whether to manage zoom. 49 * @param {BrowserApi.ZoomBehaviour} zoomBehaviour How to manage zoom.
72 */ 50 */
73 static create(streamInfo, manageZoom) { 51 static create(streamInfo, zoomBehaviour) {
74 return Promise.all([ 52 return lookupInitialZoom(streamInfo).then(function(initialZoom) {
75 lookupDefaultZoom(streamInfo),
76 lookupInitialZoom(streamInfo)
77 ]).then(function(zoomFactors) {
78 return new BrowserApi( 53 return new BrowserApi(
79 streamInfo, zoomFactors[0], zoomFactors[1], manageZoom); 54 streamInfo, initialZoom, zoomBehaviour);
80 }); 55 });
81 } 56 }
82 57
83 /** 58 /**
84 * Returns the stream info pointing to the data contained in the PDF. 59 * Returns the stream info pointing to the data contained in the PDF.
85 * @return {Object} The stream info object. 60 * @return {Object} The stream info object.
86 */ 61 */
87 getStreamInfo() { 62 getStreamInfo() {
88 return this.streamInfo_; 63 return this.streamInfo_;
89 } 64 }
90 65
91 /** 66 /**
92 * Aborts the stream. 67 * Aborts the stream.
93 */ 68 */
94 abortStream() { 69 abortStream() {
95 if (chrome.mimeHandlerPrivate) 70 if (chrome.mimeHandlerPrivate)
96 chrome.mimeHandlerPrivate.abortStream(); 71 chrome.mimeHandlerPrivate.abortStream();
97 } 72 }
98 73
99 /** 74 /**
100 * Sets the browser zoom. 75 * Sets the browser zoom.
101 * @param {number} zoom The zoom factor to send to the browser. 76 * @param {number} zoom The zoom factor to send to the browser.
102 * @return {Promise} A promise that will be resolved when the browser zoom 77 * @return {Promise} A promise that will be resolved when the browser zoom
103 * has been updated. 78 * has been updated.
104 */ 79 */
105 setZoom(zoom) { 80 setZoom(zoom) {
106 if (!this.manageZoom_) 81 if (this.zoomBehaviour_ != BrowserApi.ZoomBehaviour.MANAGE)
107 return Promise.resolve(); 82 return Promise.reject(new Error('Viewer does not manage browser zoom.'));
108 return new Promise(function(resolve, reject) { 83 return new Promise(function(resolve, reject) {
109 chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve); 84 chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve);
110 }.bind(this)); 85 }.bind(this));
111 } 86 }
112 87
113 /** 88 /**
114 * Returns the default browser zoom factor.
115 * @return {number} The default browser zoom factor.
116 */
117 getDefaultZoom() {
118 return this.defaultZoom_;
119 }
120
121 /**
122 * Returns the initial browser zoom factor. 89 * Returns the initial browser zoom factor.
123 * @return {number} The initial browser zoom factor. 90 * @return {number} The initial browser zoom factor.
124 */ 91 */
125 getInitialZoom() { 92 getInitialZoom() {
126 return this.initialZoom_; 93 return this.initialZoom_;
127 } 94 }
128 95
129 /** 96 /**
97 * Returns how to manage the zoom.
98 * @return {BrowserApi.ZoomBehaviour} How to manage zoom.
99 */
100 getZoomBehaviour() {
101 return this.zoomBehaviour_;
102 }
103
104 /**
130 * Adds an event listener to be notified when the browser zoom changes. 105 * 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 106 * @param {function} listener The listener to be called with the new zoom
132 * factor. 107 * factor.
133 */ 108 */
134 addZoomEventListener(listener) { 109 addZoomEventListener(listener) {
135 if (!this.manageZoom_) 110 if (!(this.zoomBehaviour_ == BrowserApi.ZoomBehaviour.MANAGE ||
111 this.zoomBehaviour_ == BrowserApi.ZoomBehaviour.PROPAGATE_PARENT))
136 return; 112 return;
137 113
138 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { 114 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) {
139 if (zoomChangeInfo.tabId != this.streamInfo_.tabId) 115 if (zoomChangeInfo.tabId != this.streamInfo_.tabId)
140 return; 116 return;
141 listener(zoomChangeInfo.newZoomFactor); 117 listener(zoomChangeInfo.newZoomFactor);
142 }.bind(this)); 118 }.bind(this));
143 } 119 }
144 }; 120 };
145 121
146 /** 122 /**
123 * Enumeration of ways to manage zoom changes.
124 * @enum {number}
125 */
126 BrowserApi.ZoomBehaviour = {
127 NONE: 0,
128 MANAGE: 1,
129 PROPAGATE_PARENT: 2
130 };
131
132 /**
147 * Creates a BrowserApi for an extension running as a mime handler. 133 * Creates a BrowserApi for an extension running as a mime handler.
148 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed 134 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
149 * using the mimeHandlerPrivate API. 135 * using the mimeHandlerPrivate API.
150 */ 136 */
151 function createBrowserApiForMimeHandlerView() { 137 function createBrowserApiForMimeHandlerView() {
152 return new Promise(function(resolve, reject) { 138 return new Promise(function(resolve, reject) {
153 chrome.mimeHandlerPrivate.getStreamInfo(resolve); 139 chrome.mimeHandlerPrivate.getStreamInfo(resolve);
154 }).then(function(streamInfo) { 140 }).then(function(streamInfo) {
155 let promises = []; 141 let promises = [];
156 let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1; 142 let zoomBehaviour = BrowserApi.ZoomBehaviour.NONE;
157 if (streamInfo.tabId != -1) { 143 if (streamInfo.tabId != -1) {
144 zoomBehaviour = streamInfo.embedded ?
145 BrowserApi.ZoomBehaviour.PROPAGATE_PARENT :
146 BrowserApi.ZoomBehaviour.MANAGE;
158 promises.push(new Promise(function(resolve) { 147 promises.push(new Promise(function(resolve) {
159 chrome.tabs.get(streamInfo.tabId, resolve); 148 chrome.tabs.get(streamInfo.tabId, resolve);
160 }).then(function(tab) { 149 }).then(function(tab) {
161 if (tab) 150 if (tab)
162 streamInfo.tabUrl = tab.url; 151 streamInfo.tabUrl = tab.url;
163 })); 152 }));
164 } 153 }
165 if (manageZoom) { 154 if (zoomBehaviour == BrowserApi.ZoomBehaviour.MANAGE) {
166 promises.push(new Promise(function(resolve) { 155 promises.push(new Promise(function(resolve) {
167 chrome.tabs.setZoomSettings( 156 chrome.tabs.setZoomSettings(
168 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve); 157 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
169 })); 158 }));
170 } 159 }
171 return Promise.all(promises).then( 160 return Promise.all(promises).then(
172 function() { return BrowserApi.create(streamInfo, manageZoom); }); 161 function() { return BrowserApi.create(streamInfo, zoomBehaviour); });
173 }); 162 });
174 } 163 }
175 164
176 /** 165 /**
177 * Creates a BrowserApi instance for an extension not running as a mime handler. 166 * Creates a BrowserApi instance for an extension not running as a mime handler.
178 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed 167 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
179 * from the URL. 168 * from the URL.
180 */ 169 */
181 function createBrowserApiForPrintPreview() { 170 function createBrowserApiForPrintPreview() {
182 let url = window.location.search.substring(1); 171 let url = window.location.search.substring(1);
183 let streamInfo = { 172 let streamInfo = {
184 streamUrl: url, 173 streamUrl: url,
185 originalUrl: url, 174 originalUrl: url,
186 responseHeaders: {}, 175 responseHeaders: {},
187 embedded: window.parent != window, 176 embedded: window.parent != window,
188 tabId: -1, 177 tabId: -1,
189 }; 178 };
190 return new Promise(function(resolve, reject) { 179 return new Promise(function(resolve, reject) {
191 if (!chrome.tabs) { 180 if (!chrome.tabs) {
192 resolve(); 181 resolve();
193 return; 182 return;
194 } 183 }
195 chrome.tabs.getCurrent(function(tab) { 184 chrome.tabs.getCurrent(function(tab) {
196 streamInfo.tabId = tab.id; 185 streamInfo.tabId = tab.id;
197 streamInfo.tabUrl = tab.url; 186 streamInfo.tabUrl = tab.url;
198 resolve(); 187 resolve();
199 }); 188 });
200 }).then(function() { return BrowserApi.create(streamInfo, false); }); 189 }).then(function() {
190 return BrowserApi.create(streamInfo, BrowserApi.ZoomBehaviour.NONE);
191 });
201 } 192 }
202 193
203 /** 194 /**
204 * Returns a promise that will resolve to a BrowserApi instance. 195 * Returns a promise that will resolve to a BrowserApi instance.
205 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the 196 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the
206 * current environment. 197 * current environment.
207 */ 198 */
208 function createBrowserApi() { 199 function createBrowserApi() {
209 if (location.origin === 'chrome://print') { 200 if (location.origin === 'chrome://print') {
210 return createBrowserApiForPrintPreview(); 201 return createBrowserApiForPrintPreview();
211 } 202 }
212 203
213 return createBrowserApiForMimeHandlerView(); 204 return createBrowserApiForMimeHandlerView();
214 } 205 }
OLDNEW
« 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