OLD | NEW |
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 this.zoomBehavior_ = zoomBehavior; | 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 {BrowserApi.ZoomBehavior} zoomBehavior How to manage zoom. | 71 * @param {BrowserApi.ZoomBehavior} zoomBehavior How to manage zoom. |
72 */ | 72 */ |
73 static create(streamInfo, zoomBehavior) { | 73 static create(streamInfo, zoomBehavior) { |
74 return Promise.all([ | 74 return Promise |
75 lookupDefaultZoom(streamInfo), | 75 .all([lookupDefaultZoom(streamInfo), lookupInitialZoom(streamInfo)]) |
76 lookupInitialZoom(streamInfo) | 76 .then(function(zoomFactors) { |
77 ]).then(function(zoomFactors) { | 77 return new BrowserApi( |
78 return new BrowserApi( | 78 streamInfo, zoomFactors[0], zoomFactors[1], zoomBehavior); |
79 streamInfo, zoomFactors[0], zoomFactors[1], zoomBehavior); | 79 }); |
80 }); | |
81 } | 80 } |
82 | 81 |
83 /** | 82 /** |
84 * Returns the stream info pointing to the data contained in the PDF. | 83 * Returns the stream info pointing to the data contained in the PDF. |
85 * @return {Object} The stream info object. | 84 * @return {Object} The stream info object. |
86 */ | 85 */ |
87 getStreamInfo() { | 86 getStreamInfo() { |
88 return this.streamInfo_; | 87 return this.streamInfo_; |
89 } | 88 } |
90 | 89 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 PROPAGATE_PARENT: 2 | 163 PROPAGATE_PARENT: 2 |
165 }; | 164 }; |
166 | 165 |
167 /** | 166 /** |
168 * Creates a BrowserApi for an extension running as a mime handler. | 167 * Creates a BrowserApi for an extension running as a mime handler. |
169 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed | 168 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed |
170 * using the mimeHandlerPrivate API. | 169 * using the mimeHandlerPrivate API. |
171 */ | 170 */ |
172 function createBrowserApiForMimeHandlerView() { | 171 function createBrowserApiForMimeHandlerView() { |
173 return new Promise(function(resolve, reject) { | 172 return new Promise(function(resolve, reject) { |
174 chrome.mimeHandlerPrivate.getStreamInfo(resolve); | 173 chrome.mimeHandlerPrivate.getStreamInfo(resolve); |
175 }).then(function(streamInfo) { | 174 }) |
176 let promises = []; | 175 .then(function(streamInfo) { |
177 let zoomBehavior = BrowserApi.ZoomBehavior.NONE; | 176 let promises = []; |
178 if (streamInfo.tabId != -1) { | 177 let zoomBehavior = BrowserApi.ZoomBehavior.NONE; |
179 zoomBehavior = streamInfo.embedded ? | 178 if (streamInfo.tabId != -1) { |
180 BrowserApi.ZoomBehavior.PROPAGATE_PARENT : | 179 zoomBehavior = streamInfo.embedded ? |
181 BrowserApi.ZoomBehavior.MANAGE; | 180 BrowserApi.ZoomBehavior.PROPAGATE_PARENT : |
182 promises.push(new Promise(function(resolve) { | 181 BrowserApi.ZoomBehavior.MANAGE; |
183 chrome.tabs.get(streamInfo.tabId, resolve); | 182 promises.push(new Promise(function(resolve) { |
184 }).then(function(tab) { | 183 chrome.tabs.get(streamInfo.tabId, resolve); |
185 if (tab) | 184 }).then(function(tab) { |
186 streamInfo.tabUrl = tab.url; | 185 if (tab) |
187 })); | 186 streamInfo.tabUrl = tab.url; |
188 } | 187 })); |
189 if (zoomBehavior == BrowserApi.ZoomBehavior.MANAGE) { | 188 } |
190 promises.push(new Promise(function(resolve) { | 189 if (zoomBehavior == BrowserApi.ZoomBehavior.MANAGE) { |
191 chrome.tabs.setZoomSettings( | 190 promises.push(new Promise(function(resolve) { |
192 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve); | 191 chrome.tabs.setZoomSettings( |
193 })); | 192 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve); |
194 } | 193 })); |
195 return Promise.all(promises).then( | 194 } |
196 function() { return BrowserApi.create(streamInfo, zoomBehavior); }); | 195 return Promise.all(promises).then(function() { |
197 }); | 196 return BrowserApi.create(streamInfo, zoomBehavior); |
| 197 }); |
| 198 }); |
198 } | 199 } |
199 | 200 |
200 /** | 201 /** |
201 * Creates a BrowserApi instance for an extension not running as a mime handler. | 202 * Creates a BrowserApi instance for an extension not running as a mime handler. |
202 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed | 203 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed |
203 * from the URL. | 204 * from the URL. |
204 */ | 205 */ |
205 function createBrowserApiForPrintPreview() { | 206 function createBrowserApiForPrintPreview() { |
206 let url = window.location.search.substring(1); | 207 let url = window.location.search.substring(1); |
207 let streamInfo = { | 208 let streamInfo = { |
208 streamUrl: url, | 209 streamUrl: url, |
209 originalUrl: url, | 210 originalUrl: url, |
210 responseHeaders: {}, | 211 responseHeaders: {}, |
211 embedded: window.parent != window, | 212 embedded: window.parent != window, |
212 tabId: -1, | 213 tabId: -1, |
213 }; | 214 }; |
214 return new Promise(function(resolve, reject) { | 215 return new Promise(function(resolve, reject) { |
215 if (!chrome.tabs) { | 216 if (!chrome.tabs) { |
216 resolve(); | 217 resolve(); |
217 return; | 218 return; |
218 } | 219 } |
219 chrome.tabs.getCurrent(function(tab) { | 220 chrome.tabs.getCurrent(function(tab) { |
220 streamInfo.tabId = tab.id; | 221 streamInfo.tabId = tab.id; |
221 streamInfo.tabUrl = tab.url; | 222 streamInfo.tabUrl = tab.url; |
222 resolve(); | 223 resolve(); |
223 }); | 224 }); |
224 }).then(function() { | 225 }) |
225 return BrowserApi.create(streamInfo, BrowserApi.ZoomBehavior.NONE); | 226 .then(function() { |
226 }); | 227 return BrowserApi.create(streamInfo, BrowserApi.ZoomBehavior.NONE); |
| 228 }); |
227 } | 229 } |
228 | 230 |
229 /** | 231 /** |
230 * Returns a promise that will resolve to a BrowserApi instance. | 232 * Returns a promise that will resolve to a BrowserApi instance. |
231 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the | 233 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the |
232 * current environment. | 234 * current environment. |
233 */ | 235 */ |
234 function createBrowserApi() { | 236 function createBrowserApi() { |
235 if (location.origin === 'chrome://print') { | 237 if (location.origin === 'chrome://print') { |
236 return createBrowserApiForPrintPreview(); | 238 return createBrowserApiForPrintPreview(); |
237 } | 239 } |
238 | 240 |
239 return createBrowserApiForMimeHandlerView(); | 241 return createBrowserApiForMimeHandlerView(); |
240 } | 242 } |
OLD | NEW |