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.
9 * The timeout in ms before which a stream is closed.
12 var STREAM_TIMEOUT
= 10000;
15 * A map of view ID (which identifies a particular PDF viewer instance) to
17 * @type {Object.<string, Object>}
22 * A map of view ID (which identifies a particular PDF viewer instance) to
23 * initialization function for that view.
24 * @type {Object.<string, Function>}
26 var pluginInitFunctions
= {};
29 * If we have received a stream object and an initialization function for a
30 * particular PDF viewer instance we know that the extension has loaded in
31 * and we can pass it the stream. We can then delete the corresponding map
33 * @param {string} viewId The ID of the view to initialize with a stream.
34 * @return {boolean} Whether the stream has been sent to the viewer.
36 function flush(viewId
) {
37 if (viewId
in streams
&& viewId
in pluginInitFunctions
) {
38 var streamDetails
= streams
[viewId
];
39 // If the stream has been aborted we pass the stream details to the PDF
40 // viewer with the stream URL removed. This allows the viewer to report an
41 // error to the user rather than trying to load from an invalid stream
43 if (streamDetails
.aborted
)
44 delete streamDetails
.streamUrl
;
45 pluginInitFunctions
[viewId
](streamDetails
);
46 delete streams
[viewId
];
47 delete pluginInitFunctions
[viewId
];
48 if (!streamDetails
.aborted
) {
49 // Once a stream has been opened, by the plugin, it is safe to abort.
50 // Abort after STREAM_TIMEOUT in case the plugin was closed without
51 // reading from the stream.
53 // This is a temporary work-around until the streamsPrivate API is made
54 // more robust to its clients failing to finish reading from or abort
56 setTimeout(function() {
57 chrome
.streamsPrivate
.abort(streamDetails
.streamUrl
);
66 * This is called when loading a document with the PDF mime type and passes a
67 * stream that points to the PDF file. This may be run before or after we
68 * receive a message from the PDF viewer with its initialization function.
70 chrome
.streamsPrivate
.onExecuteMimeTypeHandler
.addListener(
71 function(streamDetails
) {
72 // Store the stream until we are contacted by the PDF viewer that owns the
74 streams
[streamDetails
.viewId
] = streamDetails
;
75 if (!flush(streamDetails
.viewId
)) {
76 // If a stream has not been claimed by a PDF viewer instance within
77 // STREAM_TIMEOUT, abort the stream.
78 setTimeout(function() {
79 if (streamDetails
.viewId
in streams
) {
80 chrome
.streamsPrivate
.abort(streamDetails
.streamUrl
);
81 streamDetails
.aborted
= true;
89 * This is called when we receive a message from the PDF viewer indicating
90 * it has loaded and is ready to receive a stream of the data.
92 chrome
.runtime
.onMessage
.addListener(
93 function(request
, sender
, responseFunction
) {
94 // Store the initialization function until we receive the stream which
95 // corresponds to the PDF viewer.
96 pluginInitFunctions
[request
.viewId
] = responseFunction
;
97 flush(request
.viewId
);