| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  | // Copyright 2015 The Chromium Authors. All rights reserved.
 | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Returns a promise that will resolve to the default zoom factor. | 
					
						
							|  |  |  |  * @return {Promise<number>} A promise that will resolve to the default zoom | 
					
						
							|  |  |  |  *     factor. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  | function lookupDefaultZoom() { | 
					
						
							|  |  |  |   return cr.sendWithPromise('getDefaultZoom'); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Returns a promise that will resolve to the initial zoom factor | 
					
						
							|  |  |  |  * upon starting the plugin. This may differ from the default zoom | 
					
						
							|  |  |  |  * if, for example, the page is zoomed before the plugin is run. | 
					
						
							|  |  |  |  * @return {Promise<number>} A promise that will resolve to the initial zoom | 
					
						
							|  |  |  |  *     factor. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  | function lookupInitialZoom() { | 
					
						
							|  |  |  |   return cr.sendWithPromise('getInitialZoom'); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * A class providing an interface to the browser. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BrowserApi { | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @constructor | 
					
						
							|  |  |  |    * @param {!Object} streamInfo The stream object which points to the data | 
					
						
							|  |  |  |    *     contained in the PDF. | 
					
						
							|  |  |  |    * @param {number} defaultZoom The default browser zoom. | 
					
						
							|  |  |  |    * @param {number} initialZoom The initial browser zoom | 
					
						
							|  |  |  |    *     upon starting the plugin. | 
					
						
							|  |  |  |    * @param {boolean} manageZoom Whether to manage zoom. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   constructor(streamInfo, defaultZoom, initialZoom, manageZoom) { | 
					
						
							|  |  |  |     this.streamInfo_ = streamInfo; | 
					
						
							|  |  |  |     this.defaultZoom_ = defaultZoom; | 
					
						
							|  |  |  |     this.initialZoom_ = initialZoom; | 
					
						
							|  |  |  |     this.manageZoom_ = manageZoom; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Returns a promise to a BrowserApi. | 
					
						
							|  |  |  |    * @param {!Object} streamInfo The stream object pointing to the data | 
					
						
							|  |  |  |    *     contained in the PDF. | 
					
						
							|  |  |  |    * @param {boolean} manageZoom Whether to manage zoom. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   static create(streamInfo, manageZoom) { | 
					
						
							| 
									
										
										
										
											2017-01-17 22:40:15 +05:30
										 |  |  |     return Promise.all([ | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  |       lookupDefaultZoom(), | 
					
						
							|  |  |  |       lookupInitialZoom() | 
					
						
							| 
									
										
										
										
											2017-01-17 22:40:15 +05:30
										 |  |  |     ]).then(function(zoomFactors) { | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  |       return new BrowserApi( | 
					
						
							| 
									
										
										
										
											2017-01-17 22:40:15 +05:30
										 |  |  |         streamInfo, zoomFactors[0], zoomFactors[1], manageZoom); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Returns the stream info pointing to the data contained in the PDF. | 
					
						
							|  |  |  |    * @return {Object} The stream info object. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   getStreamInfo() { | 
					
						
							|  |  |  |     return this.streamInfo_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Sets the browser zoom. | 
					
						
							|  |  |  |    * @param {number} zoom The zoom factor to send to the browser. | 
					
						
							|  |  |  |    * @return {Promise} A promise that will be resolved when the browser zoom | 
					
						
							|  |  |  |    *     has been updated. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   setZoom(zoom) { | 
					
						
							|  |  |  |     if (!this.manageZoom_) | 
					
						
							|  |  |  |       return Promise.resolve(); | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  |     return cr.sendWithPromise('setZoom', zoom); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Returns the default browser zoom factor. | 
					
						
							|  |  |  |    * @return {number} The default browser zoom factor. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   getDefaultZoom() { | 
					
						
							|  |  |  |     return this.defaultZoom_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Returns the initial browser zoom factor. | 
					
						
							|  |  |  |    * @return {number} The initial browser zoom factor. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   getInitialZoom() { | 
					
						
							|  |  |  |     return this.initialZoom_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Adds an event listener to be notified when the browser zoom changes. | 
					
						
							|  |  |  |    * @param {function} listener The listener to be called with the new zoom | 
					
						
							|  |  |  |    *     factor. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   addZoomEventListener(listener) { | 
					
						
							|  |  |  |     if (!this.manageZoom_) | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  |     cr.addWebUIListener('onZoomLevelChanged', function(newZoomFactor) { | 
					
						
							|  |  |  |       listener(newZoomFactor); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Creates a BrowserApi instance for an extension not running as a mime handler. | 
					
						
							|  |  |  |  * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed | 
					
						
							|  |  |  |  *     from the URL. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  | function createBrowserApi(opts) { | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  |   let streamInfo = { | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  |     streamUrl: opts.originalURL, | 
					
						
							|  |  |  |     originalUrl: opts.originalURL, | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  |     responseHeaders: {}, | 
					
						
							|  |  |  |     embedded: window.parent != window, | 
					
						
							|  |  |  |     tabId: -1, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   return new Promise(function(resolve, reject) { | 
					
						
							| 
									
										
										
										
											2017-01-18 21:56:58 +05:30
										 |  |  |     resolve(BrowserApi.create(streamInfo, true)); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:57:16 +05:30
										 |  |  | } |