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

Side by Side Diff: chrome/browser/resources/pdf/browser_api.js

Issue 2503633002: Propagate browser zoom changes to embedded PDFs. (Closed)
Patch Set: Use American spelling of behaviour. Created 4 years 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') | no next file with comments »
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. 8 * Returns a promise that will resolve to the default zoom factor.
9 * @param {!Object} streamInfo The stream object pointing to the data contained 9 * @param {!Object} streamInfo The stream object pointing to the data contained
10 * in the PDF. 10 * in the PDF.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 * A class providing an interface to the browser. 48 * A class providing an interface to the browser.
49 */ 49 */
50 class BrowserApi { 50 class BrowserApi {
51 /** 51 /**
52 * @constructor 52 * @constructor
53 * @param {!Object} streamInfo The stream object which points to the data 53 * @param {!Object} streamInfo The stream object which points to the data
54 * contained in the PDF. 54 * contained in the PDF.
55 * @param {number} defaultZoom The default browser zoom. 55 * @param {number} defaultZoom The default browser zoom.
56 * @param {number} initialZoom The initial browser zoom 56 * @param {number} initialZoom The initial browser zoom
57 * upon starting the plugin. 57 * upon starting the plugin.
58 * @param {boolean} manageZoom Whether to manage zoom. 58 * @param {BrowserApi.ZoomBehavior} zoomBehavior How to manage zoom.
59 */ 59 */
60 constructor(streamInfo, defaultZoom, initialZoom, manageZoom) { 60 constructor(streamInfo, defaultZoom, initialZoom, zoomBehavior) {
61 this.streamInfo_ = streamInfo; 61 this.streamInfo_ = streamInfo;
62 this.defaultZoom_ = defaultZoom; 62 this.defaultZoom_ = defaultZoom;
63 this.initialZoom_ = initialZoom; 63 this.initialZoom_ = initialZoom;
64 this.manageZoom_ = manageZoom; 64 this.zoomBehavior_ = zoomBehavior;
65 } 65 }
66 66
67 /** 67 /**
68 * Returns a promise to a BrowserApi. 68 * Returns a promise to a BrowserApi.
69 * @param {!Object} streamInfo The stream object pointing to the data 69 * @param {!Object} streamInfo The stream object pointing to the data
70 * contained in the PDF. 70 * contained in the PDF.
71 * @param {boolean} manageZoom Whether to manage zoom. 71 * @param {BrowserApi.ZoomBehavior} zoomBehavior How to manage zoom.
72 */ 72 */
73 static create(streamInfo, manageZoom) { 73 static create(streamInfo, zoomBehavior) {
74 return Promise.all([ 74 return Promise.all([
75 lookupDefaultZoom(streamInfo), 75 lookupDefaultZoom(streamInfo),
76 lookupInitialZoom(streamInfo) 76 lookupInitialZoom(streamInfo)
77 ]).then(function(zoomFactors) { 77 ]).then(function(zoomFactors) {
78 return new BrowserApi( 78 return new BrowserApi(
79 streamInfo, zoomFactors[0], zoomFactors[1], manageZoom); 79 streamInfo, zoomFactors[0], zoomFactors[1], zoomBehavior);
80 }); 80 });
81 } 81 }
82 82
83 /** 83 /**
84 * Returns the stream info pointing to the data contained in the PDF. 84 * Returns the stream info pointing to the data contained in the PDF.
85 * @return {Object} The stream info object. 85 * @return {Object} The stream info object.
86 */ 86 */
87 getStreamInfo() { 87 getStreamInfo() {
88 return this.streamInfo_; 88 return this.streamInfo_;
89 } 89 }
90 90
91 /** 91 /**
92 * Aborts the stream. 92 * Aborts the stream.
93 */ 93 */
94 abortStream() { 94 abortStream() {
95 if (chrome.mimeHandlerPrivate) 95 if (chrome.mimeHandlerPrivate)
96 chrome.mimeHandlerPrivate.abortStream(); 96 chrome.mimeHandlerPrivate.abortStream();
97 } 97 }
98 98
99 /** 99 /**
100 * Sets the browser zoom. 100 * Sets the browser zoom.
101 * @param {number} zoom The zoom factor to send to the browser. 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 102 * @return {Promise} A promise that will be resolved when the browser zoom
103 * has been updated. 103 * has been updated.
104 */ 104 */
105 setZoom(zoom) { 105 setZoom(zoom) {
106 if (!this.manageZoom_) 106 if (this.zoomBehavior_ != BrowserApi.ZoomBehavior.MANAGE)
107 return Promise.resolve(); 107 return Promise.reject(new Error('Viewer does not manage browser zoom.'));
108 return new Promise(function(resolve, reject) { 108 return new Promise(function(resolve, reject) {
109 chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve); 109 chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve);
110 }.bind(this)); 110 }.bind(this));
111 } 111 }
112 112
113 /** 113 /**
114 * Returns the default browser zoom factor. 114 * Returns the default browser zoom factor.
115 * @return {number} The default browser zoom factor. 115 * @return {number} The default browser zoom factor.
116 */ 116 */
117 getDefaultZoom() { 117 getDefaultZoom() {
118 return this.defaultZoom_; 118 return this.defaultZoom_;
119 } 119 }
120 120
121 /** 121 /**
122 * Returns the initial browser zoom factor. 122 * Returns the initial browser zoom factor.
123 * @return {number} The initial browser zoom factor. 123 * @return {number} The initial browser zoom factor.
124 */ 124 */
125 getInitialZoom() { 125 getInitialZoom() {
126 return this.initialZoom_; 126 return this.initialZoom_;
127 } 127 }
128 128
129 /** 129 /**
130 * Returns how to manage the zoom.
131 * @return {BrowserApi.ZoomBehavior} How to manage zoom.
132 */
133 getZoomBehavior() {
134 return this.zoomBehavior_;
135 }
136
137 /**
130 * Adds an event listener to be notified when the browser zoom changes. 138 * 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 139 * @param {function} listener The listener to be called with the new zoom
132 * factor. 140 * factor.
133 */ 141 */
134 addZoomEventListener(listener) { 142 addZoomEventListener(listener) {
135 if (!this.manageZoom_) 143 if (!(this.zoomBehavior_ == BrowserApi.ZoomBehavior.MANAGE ||
144 this.zoomBehavior_ == BrowserApi.ZoomBehavior.PROPAGATE_PARENT))
136 return; 145 return;
137 146
138 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { 147 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) {
139 if (zoomChangeInfo.tabId != this.streamInfo_.tabId) 148 if (zoomChangeInfo.tabId != this.streamInfo_.tabId)
140 return; 149 return;
141 listener(zoomChangeInfo.newZoomFactor); 150 listener(zoomChangeInfo.newZoomFactor);
142 }.bind(this)); 151 }.bind(this));
143 } 152 }
144 }; 153 };
145 154
146 /** 155 /**
156 * Enumeration of ways to manage zoom changes.
157 * @enum {number}
158 */
159 BrowserApi.ZoomBehavior = {
160 NONE: 0,
161 MANAGE: 1,
162 PROPAGATE_PARENT: 2
163 };
164
165 /**
147 * Creates a BrowserApi for an extension running as a mime handler. 166 * Creates a BrowserApi for an extension running as a mime handler.
148 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed 167 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
149 * using the mimeHandlerPrivate API. 168 * using the mimeHandlerPrivate API.
150 */ 169 */
151 function createBrowserApiForMimeHandlerView() { 170 function createBrowserApiForMimeHandlerView() {
152 return new Promise(function(resolve, reject) { 171 return new Promise(function(resolve, reject) {
153 chrome.mimeHandlerPrivate.getStreamInfo(resolve); 172 chrome.mimeHandlerPrivate.getStreamInfo(resolve);
154 }).then(function(streamInfo) { 173 }).then(function(streamInfo) {
155 let promises = []; 174 let promises = [];
156 let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1; 175 let zoomBehavior = BrowserApi.ZoomBehavior.NONE;
157 if (streamInfo.tabId != -1) { 176 if (streamInfo.tabId != -1) {
177 zoomBehavior = streamInfo.embedded ?
178 BrowserApi.ZoomBehavior.PROPAGATE_PARENT :
179 BrowserApi.ZoomBehavior.MANAGE;
158 promises.push(new Promise(function(resolve) { 180 promises.push(new Promise(function(resolve) {
159 chrome.tabs.get(streamInfo.tabId, resolve); 181 chrome.tabs.get(streamInfo.tabId, resolve);
160 }).then(function(tab) { 182 }).then(function(tab) {
161 if (tab) 183 if (tab)
162 streamInfo.tabUrl = tab.url; 184 streamInfo.tabUrl = tab.url;
163 })); 185 }));
164 } 186 }
165 if (manageZoom) { 187 if (zoomBehavior == BrowserApi.ZoomBehavior.MANAGE) {
166 promises.push(new Promise(function(resolve) { 188 promises.push(new Promise(function(resolve) {
167 chrome.tabs.setZoomSettings( 189 chrome.tabs.setZoomSettings(
168 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve); 190 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
169 })); 191 }));
170 } 192 }
171 return Promise.all(promises).then( 193 return Promise.all(promises).then(
172 function() { return BrowserApi.create(streamInfo, manageZoom); }); 194 function() { return BrowserApi.create(streamInfo, zoomBehavior); });
173 }); 195 });
174 } 196 }
175 197
176 /** 198 /**
177 * Creates a BrowserApi instance for an extension not running as a mime handler. 199 * Creates a BrowserApi instance for an extension not running as a mime handler.
178 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed 200 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
179 * from the URL. 201 * from the URL.
180 */ 202 */
181 function createBrowserApiForPrintPreview() { 203 function createBrowserApiForPrintPreview() {
182 let url = window.location.search.substring(1); 204 let url = window.location.search.substring(1);
183 let streamInfo = { 205 let streamInfo = {
184 streamUrl: url, 206 streamUrl: url,
185 originalUrl: url, 207 originalUrl: url,
186 responseHeaders: {}, 208 responseHeaders: {},
187 embedded: window.parent != window, 209 embedded: window.parent != window,
188 tabId: -1, 210 tabId: -1,
189 }; 211 };
190 return new Promise(function(resolve, reject) { 212 return new Promise(function(resolve, reject) {
191 if (!chrome.tabs) { 213 if (!chrome.tabs) {
192 resolve(); 214 resolve();
193 return; 215 return;
194 } 216 }
195 chrome.tabs.getCurrent(function(tab) { 217 chrome.tabs.getCurrent(function(tab) {
196 streamInfo.tabId = tab.id; 218 streamInfo.tabId = tab.id;
197 streamInfo.tabUrl = tab.url; 219 streamInfo.tabUrl = tab.url;
198 resolve(); 220 resolve();
199 }); 221 });
200 }).then(function() { return BrowserApi.create(streamInfo, false); }); 222 }).then(function() {
223 return BrowserApi.create(streamInfo, BrowserApi.ZoomBehavior.NONE);
224 });
201 } 225 }
202 226
203 /** 227 /**
204 * Returns a promise that will resolve to a BrowserApi instance. 228 * Returns a promise that will resolve to a BrowserApi instance.
205 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the 229 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the
206 * current environment. 230 * current environment.
207 */ 231 */
208 function createBrowserApi() { 232 function createBrowserApi() {
209 if (location.origin === 'chrome://print') { 233 if (location.origin === 'chrome://print') {
210 return createBrowserApiForPrintPreview(); 234 return createBrowserApiForPrintPreview();
211 } 235 }
212 236
213 return createBrowserApiForMimeHandlerView(); 237 return createBrowserApiForMimeHandlerView();
214 } 238 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698