| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 {boolean} manageZoom Whether to manage zoom. |
| 59 * @param {Function} abortStream A function that can be called to abort the |
| 60 * stream |
| 59 */ | 61 */ |
| 60 constructor(streamInfo, defaultZoom, initialZoom, manageZoom) { | 62 constructor(streamInfo, defaultZoom, initialZoom, manageZoom, abortStream) { |
| 61 this.streamInfo_ = streamInfo; | 63 this.streamInfo_ = streamInfo; |
| 62 this.defaultZoom_ = defaultZoom; | 64 this.defaultZoom_ = defaultZoom; |
| 63 this.initialZoom_ = initialZoom; | 65 this.initialZoom_ = initialZoom; |
| 64 this.manageZoom_ = manageZoom; | 66 this.manageZoom_ = manageZoom; |
| 67 this.abortStream_ = abortStream; |
| 65 } | 68 } |
| 66 | 69 |
| 67 /** | 70 /** |
| 68 * Returns a promise to a BrowserApi. | 71 * Returns a promise to a BrowserApi. |
| 69 * @param {!Object} streamInfo The stream object pointing to the data | 72 * @param {!Object} streamInfo The stream object pointing to the data |
| 70 * contained in the PDF. | 73 * contained in the PDF. |
| 71 * @param {boolean} manageZoom Whether to manage zoom. | 74 * @param {boolean} manageZoom Whether to manage zoom. |
| 75 * @param {Function} abortStream A function that can be called to abort the |
| 76 * stream |
| 72 */ | 77 */ |
| 73 static create(streamInfo, manageZoom) { | 78 static async create(streamInfo, manageZoom, abortStream) { |
| 74 return Promise.all([ | 79 let [defaultZoom, initialZoom] = await Promise.all([ |
| 75 lookupDefaultZoom(streamInfo), | 80 lookupDefaultZoom(streamInfo), lookupInitialZoom(streamInfo)]); |
| 76 lookupInitialZoom(streamInfo) | 81 return new BrowserApi( |
| 77 ]).then(function(zoomFactors) { | 82 streamInfo, defaultZoom, initialZoom, manageZoom, abortStream); |
| 78 return new BrowserApi( | |
| 79 streamInfo, zoomFactors[0], zoomFactors[1], manageZoom); | |
| 80 }); | |
| 81 } | 83 } |
| 82 | 84 |
| 83 /** | 85 /** |
| 84 * Returns the stream info pointing to the data contained in the PDF. | 86 * Returns the stream info pointing to the data contained in the PDF. |
| 85 * @return {Object} The stream info object. | 87 * @return {Object} The stream info object. |
| 86 */ | 88 */ |
| 87 getStreamInfo() { | 89 getStreamInfo() { |
| 88 return this.streamInfo_; | 90 return this.streamInfo_; |
| 89 } | 91 } |
| 90 | 92 |
| 91 /** | 93 /** |
| 92 * Aborts the stream. | 94 * Aborts the stream. |
| 93 */ | 95 */ |
| 94 abortStream() { | 96 abortStream() { |
| 95 if (chrome.mimeHandlerPrivate) | 97 this.abortStream_(); |
| 96 chrome.mimeHandlerPrivate.abortStream(); | |
| 97 } | 98 } |
| 98 | 99 |
| 99 /** | 100 /** |
| 100 * Sets the browser zoom. | 101 * Sets the browser zoom. |
| 101 * @param {number} zoom The zoom factor to send to the browser. | 102 * @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 * @return {Promise} A promise that will be resolved when the browser zoom |
| 103 * has been updated. | 104 * has been updated. |
| 104 */ | 105 */ |
| 105 setZoom(zoom) { | 106 setZoom(zoom) { |
| 106 if (!this.manageZoom_) | 107 if (!this.manageZoom_) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 136 return; | 137 return; |
| 137 | 138 |
| 138 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { | 139 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { |
| 139 if (zoomChangeInfo.tabId != this.streamInfo_.tabId) | 140 if (zoomChangeInfo.tabId != this.streamInfo_.tabId) |
| 140 return; | 141 return; |
| 141 listener(zoomChangeInfo.newZoomFactor); | 142 listener(zoomChangeInfo.newZoomFactor); |
| 142 }.bind(this)); | 143 }.bind(this)); |
| 143 } | 144 } |
| 144 }; | 145 }; |
| 145 | 146 |
| 147 function constructStreamInfoDict_(streamInfo) { |
| 148 var headers = {}; |
| 149 for (var header of streamInfo.response_headers) { |
| 150 headers[header[0]] = header[1]; |
| 151 } |
| 152 return { |
| 153 mimeType: streamInfo.mime_type, |
| 154 originalUrl: streamInfo.original_url, |
| 155 streamUrl: streamInfo.stream_url, |
| 156 tabId: streamInfo.tab_id, |
| 157 embedded: !!streamInfo.embedded, |
| 158 responseHeaders: headers, |
| 159 }; |
| 160 } |
| 161 |
| 146 /** | 162 /** |
| 147 * Creates a BrowserApi for an extension running as a mime handler. | 163 * Creates a BrowserApi for an extension running as a mime handler. |
| 148 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed | 164 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed |
| 149 * using the mimeHandlerPrivate API. | 165 * using the MimeHandlerService interface. |
| 150 */ | 166 */ |
| 151 function createBrowserApiForMimeHandlerView() { | 167 async function createBrowserApiForMimeHandlerView() { |
| 152 return new Promise(function(resolve, reject) { | 168 let mimeHandlerInterface = await (async function() { |
| 153 chrome.mimeHandlerPrivate.getStreamInfo(resolve); | 169 if (!define) |
| 154 }).then(function(streamInfo) { | 170 return; |
| 155 let promises = []; | 171 |
| 156 let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1; | 172 let [frameInterfaces, mojom, connectionModule] = await new Promise( |
| 157 if (streamInfo.tabId != -1) { | 173 resolve => { |
| 158 promises.push(new Promise(function(resolve) { | 174 define('pdf_browser_api', [ |
| 175 'content/public/renderer/frame_interfaces', |
| 176 'extensions/common/api/mime_handler.mojom', |
| 177 'mojo/public/js/connection', |
| 178 ], (...modules) => resolve(modules)); |
| 179 }); |
| 180 return connectionModule.bindHandleToProxy( |
| 181 frameInterfaces.getInterface(mojom.MimeHandlerService.name), |
| 182 mojom.MimeHandlerService); |
| 183 })(); |
| 184 |
| 185 let streamInfo = constructStreamInfoDict_( |
| 186 (await mimeHandlerInterface.getStreamInfo()).stream_info); |
| 187 |
| 188 let promises = []; |
| 189 let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1; |
| 190 if (streamInfo.tabId != -1) { |
| 191 promises.push((async function() { |
| 192 let tab = await new Promise(function(resolve) { |
| 159 chrome.tabs.get(streamInfo.tabId, resolve); | 193 chrome.tabs.get(streamInfo.tabId, resolve); |
| 160 }).then(function(tab) { | 194 }); |
| 161 if (tab) | 195 if (tab) |
| 162 streamInfo.tabUrl = tab.url; | 196 streamInfo.tabUrl = tab.url; |
| 163 })); | 197 })()); |
| 164 } | 198 } |
| 165 if (manageZoom) { | 199 if (manageZoom) { |
| 166 promises.push(new Promise(function(resolve) { | 200 promises.push(new Promise(function(resolve) { |
| 167 chrome.tabs.setZoomSettings( | 201 chrome.tabs.setZoomSettings( |
| 168 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve); | 202 streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve); |
| 169 })); | 203 })); |
| 170 } | 204 } |
| 171 return Promise.all(promises).then( | 205 await Promise.all(promises); |
| 172 function() { return BrowserApi.create(streamInfo, manageZoom); }); | 206 return BrowserApi.create( |
| 173 }); | 207 streamInfo, manageZoom, () => { mimeHandlerInterface.abortStream(); }); |
| 174 } | 208 } |
| 175 | 209 |
| 176 /** | 210 /** |
| 177 * Creates a BrowserApi instance for an extension not running as a mime handler. | 211 * Creates a BrowserApi instance for an extension not running as a mime handler. |
| 178 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed | 212 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed |
| 179 * from the URL. | 213 * from the URL. |
| 180 */ | 214 */ |
| 181 function createBrowserApiForStandaloneExtension() { | 215 async function createBrowserApiForStandaloneExtension() { |
| 182 let url = window.location.search.substring(1); | 216 let url = window.location.search.substring(1); |
| 183 let streamInfo = { | 217 let streamInfo = { |
| 184 streamUrl: url, | 218 streamUrl: url, |
| 185 originalUrl: url, | 219 originalUrl: url, |
| 186 responseHeaders: {}, | 220 responseHeaders: {}, |
| 187 embedded: window.parent != window, | 221 embedded: window.parent != window, |
| 188 tabId: -1, | 222 tabId: -1, |
| 189 }; | 223 }; |
| 190 return new Promise(function(resolve, reject) { | 224 if (chrome.tabs) { |
| 191 if (!chrome.tabs) { | 225 let tab = await new Promise(resolve => { |
| 192 resolve(); | 226 chrome.tabs.getCurrent(resolve); |
| 193 return; | |
| 194 } | |
| 195 chrome.tabs.getCurrent(function(tab) { | |
| 196 streamInfo.tabId = tab.id; | |
| 197 streamInfo.tabUrl = tab.url; | |
| 198 resolve(); | |
| 199 }); | 227 }); |
| 200 }).then(function() { return BrowserApi.create(streamInfo, false); }); | 228 streamInfo.tabId = tab.id; |
| 229 streamInfo.tabUrl = tab.url; |
| 230 } |
| 231 return BrowserApi.create(streamInfo, false, () => {}); |
| 201 } | 232 } |
| 202 | 233 |
| 203 /** | 234 /** |
| 204 * Returns a promise that will resolve to a BrowserApi instance. | 235 * Returns a promise that will resolve to a BrowserApi instance. |
| 205 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the | 236 * @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the |
| 206 * current environment. | 237 * current environment. |
| 207 */ | 238 */ |
| 208 function createBrowserApi() { | 239 function createBrowserApi() { |
| 209 if (window.location.search) | 240 if (window.location.search) |
| 210 return createBrowserApiForStandaloneExtension(); | 241 return createBrowserApiForStandaloneExtension(); |
| 211 | 242 |
| 212 return createBrowserApiForMimeHandlerView(); | 243 return createBrowserApiForMimeHandlerView(); |
| 213 } | 244 } |
| OLD | NEW |