OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 (function() { | 5 (function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * Keep a stack of stream details for requests. These are pushed onto the | 9 * A map of view ID (which identifies a particular PDF viewer instance) to |
10 * stack as requests come in and popped off the stack as they are handled by a | 10 * stream object. |
11 * renderer. | 11 * @type {Object.<string, Object>} |
12 * TODO(raymes): This is probably racy for multiple requests. We could | |
13 * associate an ID with the request but this code will probably change | |
14 * completely when MIME type handling is improved. | |
15 */ | 12 */ |
16 var streamsCache = []; | 13 var streams = {}; |
17 | 14 |
18 window.popStreamDetails = function() { | 15 /** |
19 if (streamsCache.length > 0) | 16 * A map of view ID (which identifies a particular PDF viewer instance) to |
20 return streamsCache.pop(); | 17 * initialization function for that view. |
21 }; | 18 * @type {Object.<string, Function>} |
| 19 */ |
| 20 var pluginInitFunctions = {}; |
22 | 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 */ |
23 chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener( | 42 chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener( |
24 function(streamDetails) { | 43 function(streamDetails) { |
25 // TODO(raymes): Currently this doesn't work with embedded PDFs (it | 44 // Store the stream until we are contacted by the PDF viewer that owns the |
26 // causes the entire frame to navigate). Also work out how we can | 45 // stream. |
27 // mask the URL with the URL of the PDF. | 46 streams[streamDetails.viewId] = streamDetails; |
28 streamsCache.push(streamDetails); | 47 flush(streamDetails.viewId); |
29 chrome.tabs.update(streamDetails.tabId, {url: 'index.html'}); | |
30 } | 48 } |
31 ); | 49 ); |
32 | 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 ); |
33 }()); | 63 }()); |
OLD | NEW |