summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorraymes <raymes@chromium.org>2014-09-18 18:51:49 -0700
committerCommit bot <commit-bot@chromium.org>2014-09-19 01:52:12 +0000
commit5fa52aea4b8a022c3bfc7323b33fca84fd76b8ab (patch)
tree95b812d72fca0c58f7a06871f002cc5462016cf5
parent484e71eb17d00092874f19cfcea624e0579ed976 (diff)
downloadchromium_src-5fa52aea4b8a022c3bfc7323b33fca84fd76b8ab.zip
chromium_src-5fa52aea4b8a022c3bfc7323b33fca84fd76b8ab.tar.gz
chromium_src-5fa52aea4b8a022c3bfc7323b33fca84fd76b8ab.tar.bz2
Change the OOP PDF plugin to use BrowserPlugin mime type handling.
This changes OOP PDF to use BrowserPlugin which allows it to be loaded without navigating to the extension page. BUG=303491 Review URL: https://codereview.chromium.org/582583002 Cr-Commit-Position: refs/heads/master@{#295641}
-rw-r--r--chrome/browser/resources/component_extension_resources.grd2
-rw-r--r--chrome/browser/resources/pdf/background.js62
-rw-r--r--chrome/browser/resources/pdf/index.html2
-rw-r--r--chrome/browser/resources/pdf/main.js52
-rw-r--r--chrome/browser/resources/pdf/manifest.json1
-rw-r--r--chrome/browser/resources/pdf/pdf.js38
6 files changed, 107 insertions, 50 deletions
diff --git a/chrome/browser/resources/component_extension_resources.grd b/chrome/browser/resources/component_extension_resources.grd
index f76b3ad..f408757 100644
--- a/chrome/browser/resources/component_extension_resources.grd
+++ b/chrome/browser/resources/component_extension_resources.grd
@@ -144,7 +144,7 @@
</if>
<if expr="enable_plugins">
<include name="IDR_PDF_INDEX_HTML" file="pdf/index.html" allowexternalscript="true" type="BINDATA" flattenhtml="true" />
- <include name="IDR_PDF_JS" file="pdf/pdf.js" type="BINDATA" flattenhtml="true" />
+ <include name="IDR_PDF_MAIN_JS" file="pdf/main.js" type="BINDATA" flattenhtml="true" />
<include name="IDR_PDF_BACKGROUND_JS" file="pdf/background.js" type="BINDATA" />
<include name="IDR_PDF_INCLUDES_JS" file="pdf/includes.js" type="BINDATA" flattenhtml="true" />
diff --git a/chrome/browser/resources/pdf/background.js b/chrome/browser/resources/pdf/background.js
index 2114010..1bf748a 100644
--- a/chrome/browser/resources/pdf/background.js
+++ b/chrome/browser/resources/pdf/background.js
@@ -6,28 +6,58 @@
'use strict';
/**
- * Keep a stack of stream details for requests. These are pushed onto the
- * stack as requests come in and popped off the stack as they are handled by a
- * renderer.
- * TODO(raymes): This is probably racy for multiple requests. We could
- * associate an ID with the request but this code will probably change
- * completely when MIME type handling is improved.
+ * A map of view ID (which identifies a particular PDF viewer instance) to
+ * stream object.
+ * @type {Object.<string, Object>}
*/
- var streamsCache = [];
+ var streams = {};
- window.popStreamDetails = function() {
- if (streamsCache.length > 0)
- return streamsCache.pop();
- };
+ /**
+ * A map of view ID (which identifies a particular PDF viewer instance) to
+ * initialization function for that view.
+ * @type {Object.<string, Function>}
+ */
+ var pluginInitFunctions = {};
+
+ /**
+ * If we have received a stream object and an initialization function for a
+ * particular PDF viewer instance we know that the extension has loaded in
+ * and we can pass it the stream. We can then delete the corresponding map
+ * entries.
+ * @param {string} viewId The ID of the view to initialize with a stream.
+ */
+ function flush(viewId) {
+ if (viewId in streams && viewId in pluginInitFunctions) {
+ pluginInitFunctions[viewId](streams[viewId]);
+ delete streams[viewId];
+ delete pluginInitFunctions[viewId];
+ }
+ }
+ /**
+ * This is called when loading a document with the PDF mime type and passes a
+ * stream that points to the PDF file. This may be run before or after we
+ * receive a message from the PDF viewer with its initialization function.
+ */
chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener(
function(streamDetails) {
- // TODO(raymes): Currently this doesn't work with embedded PDFs (it
- // causes the entire frame to navigate). Also work out how we can
- // mask the URL with the URL of the PDF.
- streamsCache.push(streamDetails);
- chrome.tabs.update(streamDetails.tabId, {url: 'index.html'});
+ // Store the stream until we are contacted by the PDF viewer that owns the
+ // stream.
+ streams[streamDetails.viewId] = streamDetails;
+ flush(streamDetails.viewId);
}
);
+ /**
+ * This is called when we receive a message from the PDF viewer indicating
+ * it has loaded and is ready to receive a stream of the data.
+ */
+ chrome.runtime.onMessage.addListener(
+ function(request, sender, responseFunction) {
+ // Store the initialization function until we receive the stream which
+ // corresponds to the PDF viewer.
+ pluginInitFunctions[request.viewId] = responseFunction;
+ flush(request.viewId);
+ }
+ );
}());
diff --git a/chrome/browser/resources/pdf/index.html b/chrome/browser/resources/pdf/index.html
index 456313c..c6f64f0 100644
--- a/chrome/browser/resources/pdf/index.html
+++ b/chrome/browser/resources/pdf/index.html
@@ -53,5 +53,5 @@
<viewer-error-screen id="error-screen"></viewer-error-screen>
</body>
-<script src="pdf.js"></script>
+<script src="main.js"></script>
</html>
diff --git a/chrome/browser/resources/pdf/main.js b/chrome/browser/resources/pdf/main.js
new file mode 100644
index 0000000..9217156
--- /dev/null
+++ b/chrome/browser/resources/pdf/main.js
@@ -0,0 +1,52 @@
+// Copyright 2014 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';
+
+<include src="../../../../ui/webui/resources/js/util.js">
+<include src="open_pdf_params_parser.js">
+<include src="pdf.js">
+<include src="pdf_scripting_api.js">
+<include src="viewport.js">
+
+/**
+ * Global PDFViewer object, accessible for testing.
+ * @type Object
+ */
+var viewer;
+
+/**
+ * Entrypoint for starting the PDF viewer. This function obtains the details
+ * of the PDF 'stream' (the data that points to the PDF) and constructs a
+ * PDFViewer object with it.
+ */
+(function main() {
+ // If the viewer is started from the browser plugin, the view ID will be
+ // passed in which identifies the instance of the plugin.
+ var params = window.location.search.substring(1).split('=');
+ if (params.length == 2 && params[0] == 'id') {
+ var viewId = params[1];
+
+ // Send a message to the background page to obtain the stream details. It
+ // will run the callback function passed in to initialize the viewer.
+ chrome.runtime.sendMessage(
+ 'mhjfbmdgcfjbbpaeojofohoefgiehjai',
+ {viewId: viewId},
+ function(streamDetails) { viewer = new PDFViewer(streamDetails); });
+ return;
+ }
+
+ // The viewer may be started directly by passing in the URL of the PDF to load
+ // as the query string. This is used for print preview in particular. The URL
+ // of this page will be of the form
+ // 'chrome-extension://<extension id>?<pdf url>'. We pull out the <pdf url>
+ // part here.
+ var url = window.location.search.substring(1);
+ var streamDetails = {
+ streamUrl: url,
+ originalUrl: url,
+ responseHeaders: ''
+ };
+ viewer = new PDFViewer(streamDetails);
+})();
diff --git a/chrome/browser/resources/pdf/manifest.json b/chrome/browser/resources/pdf/manifest.json
index f97e970..8dca0e1 100644
--- a/chrome/browser/resources/pdf/manifest.json
+++ b/chrome/browser/resources/pdf/manifest.json
@@ -20,6 +20,7 @@
"transient": true,
"persistent": false
},
+ "mime_types_handler": "index.html",
"mime_types": [
"application/pdf"
],
diff --git a/chrome/browser/resources/pdf/pdf.js b/chrome/browser/resources/pdf/pdf.js
index 0aa437a..cbdacca 100644
--- a/chrome/browser/resources/pdf/pdf.js
+++ b/chrome/browser/resources/pdf/pdf.js
@@ -4,11 +4,6 @@
'use strict';
-<include src="../../../../ui/webui/resources/js/util.js">
-<include src="open_pdf_params_parser.js">
-<include src="pdf_scripting_api.js">
-<include src="viewport.js">
-
/**
* @return {number} Width of a scrollbar in pixels
*/
@@ -34,8 +29,11 @@ PDFViewer.MIN_TOOLBAR_OFFSET = 15;
/**
* Creates a new PDFViewer. There should only be one of these objects per
* document.
+ * @param {Object} streamDetails The stream object which points to the data
+ * contained in the PDF.
*/
-function PDFViewer() {
+function PDFViewer(streamDetails) {
+ this.streamDetails = streamDetails;
this.loaded = false;
// The sizer element is placed behind the plugin element to cause scrollbars
@@ -77,28 +75,6 @@ function PDFViewer() {
false);
this.sendScriptingMessage_({type: 'readyToReceive'});
- // If the viewer is started from a MIME type request, there will be a
- // background page and stream details object with the details of the request.
- // Otherwise, we take the query string of the URL to indicate the URL of the
- // PDF to load. This is used for print preview in particular.
- if (chrome.extension.getBackgroundPage &&
- chrome.extension.getBackgroundPage()) {
- this.streamDetails =
- chrome.extension.getBackgroundPage().popStreamDetails();
- }
-
- if (!this.streamDetails) {
- // The URL of this page will be of the form
- // "chrome-extension://<extension id>?<pdf url>". We pull out the <pdf url>
- // part here.
- var url = window.location.search.substring(1);
- this.streamDetails = {
- streamUrl: url,
- originalUrl: url,
- responseHeaders: ''
- };
- }
-
this.plugin_.setAttribute('src', this.streamDetails.originalUrl);
this.plugin_.setAttribute('stream-url', this.streamDetails.streamUrl);
var headers = '';
@@ -182,7 +158,7 @@ PDFViewer.prototype = {
position.y -= this.viewport.size.height;
this.viewport.position = position;
}
- };
+ }.bind(this);
var pageDownHandler = function() {
// Go to the next page if we are fit-to-page.
if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) {
@@ -193,7 +169,7 @@ PDFViewer.prototype = {
position.y += this.viewport.size.height;
this.viewport.position = position;
}
- };
+ }.bind(this);
switch (e.keyCode) {
case 32: // Space key.
@@ -573,5 +549,3 @@ PDFViewer.prototype = {
return this.viewport_;
}
};
-
-var viewer = new PDFViewer();