Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / browser / resources / pdf / background.js
blob1bf748a8ac1ea11f86369e233ab92a690094b07c
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.
5 (function() {
6 'use strict';
8 /**
9 * A map of view ID (which identifies a particular PDF viewer instance) to
10 * stream object.
11 * @type {Object.<string, Object>}
13 var streams = {};
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>}
20 var pluginInitFunctions = {};
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.
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];
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.
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);
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.
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);
63 }());