1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 (function() { 6 'use strict'; 7 8 /** 9 * A map of view ID (which identifies a particular PDF viewer instance) to 10 * stream object. 11 * @type {Object.<string, Object>} 12 */ 13 var streams = {}; 14 15 /** 16 * A map of view ID (which identifies a particular PDF viewer instance) to 17 * initialization function for that view. 18 * @type {Object.<string, Function>} 19 */ 20 var pluginInitFunctions = {}; 21 22 /** 23 * If we have received a stream object and an initialization function for a 24 * particular PDF viewer instance we know that the extension has loaded in 25 * and we can pass it the stream. We can then delete the corresponding map 26 * entries. 27 * @param {string} viewId The ID of the view to initialize with a stream. 28 */ 29 function flush(viewId) { 30 if (viewId in streams && viewId in pluginInitFunctions) { 31 pluginInitFunctions[viewId](streams[viewId]); 32 delete streams[viewId]; 33 delete pluginInitFunctions[viewId]; 34 } 35 } 36 37 /** 38 * This is called when loading a document with the PDF mime type and passes a 39 * stream that points to the PDF file. This may be run before or after we 40 * receive a message from the PDF viewer with its initialization function. 41 */ 42 chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener( 43 function(streamDetails) { 44 // Store the stream until we are contacted by the PDF viewer that owns the 45 // stream. 46 streams[streamDetails.viewId] = streamDetails; 47 flush(streamDetails.viewId); 48 } 49 ); 50 51 /** 52 * This is called when we receive a message from the PDF viewer indicating 53 * it has loaded and is ready to receive a stream of the data. 54 */ 55 chrome.runtime.onMessage.addListener( 56 function(request, sender, responseFunction) { 57 // Store the initialization function until we receive the stream which 58 // corresponds to the PDF viewer. 59 pluginInitFunctions[request.viewId] = responseFunction; 60 flush(request.viewId); 61 } 62 ); 63 }()); 64