| Index: chrome/browser/resources/pdf/browser_api.js
|
| diff --git a/chrome/browser/resources/pdf/browser_api.js b/chrome/browser/resources/pdf/browser_api.js
|
| index 90033b84085322370cabe103d2970fd738817a56..6ecc3bd269b4d10576a66634d816508fd84f18e1 100644
|
| --- a/chrome/browser/resources/pdf/browser_api.js
|
| +++ b/chrome/browser/resources/pdf/browser_api.js
|
| @@ -56,12 +56,15 @@ class BrowserApi {
|
| * @param {number} initialZoom The initial browser zoom
|
| * upon starting the plugin.
|
| * @param {boolean} manageZoom Whether to manage zoom.
|
| + * @param {Function} abortStream A function that can be called to abort the
|
| + * stream
|
| */
|
| - constructor(streamInfo, defaultZoom, initialZoom, manageZoom) {
|
| + constructor(streamInfo, defaultZoom, initialZoom, manageZoom, abortStream) {
|
| this.streamInfo_ = streamInfo;
|
| this.defaultZoom_ = defaultZoom;
|
| this.initialZoom_ = initialZoom;
|
| this.manageZoom_ = manageZoom;
|
| + this.abortStream_ = abortStream;
|
| }
|
|
|
| /**
|
| @@ -69,15 +72,14 @@ class BrowserApi {
|
| * @param {!Object} streamInfo The stream object pointing to the data
|
| * contained in the PDF.
|
| * @param {boolean} manageZoom Whether to manage zoom.
|
| + * @param {Function} abortStream A function that can be called to abort the
|
| + * stream
|
| */
|
| - static create(streamInfo, manageZoom) {
|
| - return Promise.all([
|
| - lookupDefaultZoom(streamInfo),
|
| - lookupInitialZoom(streamInfo)
|
| - ]).then(function(zoomFactors) {
|
| - return new BrowserApi(
|
| - streamInfo, zoomFactors[0], zoomFactors[1], manageZoom);
|
| - });
|
| + static async create(streamInfo, manageZoom, abortStream) {
|
| + let [defaultZoom, initialZoom] = await Promise.all([
|
| + lookupDefaultZoom(streamInfo), lookupInitialZoom(streamInfo)]);
|
| + return new BrowserApi(
|
| + streamInfo, defaultZoom, initialZoom, manageZoom, abortStream);
|
| }
|
|
|
| /**
|
| @@ -92,8 +94,7 @@ class BrowserApi {
|
| * Aborts the stream.
|
| */
|
| abortStream() {
|
| - if (chrome.mimeHandlerPrivate)
|
| - chrome.mimeHandlerPrivate.abortStream();
|
| + this.abortStream_();
|
| }
|
|
|
| /**
|
| @@ -143,34 +144,67 @@ class BrowserApi {
|
| }
|
| };
|
|
|
| +function constructStreamInfoDict_(streamInfo) {
|
| + var headers = {};
|
| + for (var header of streamInfo.response_headers) {
|
| + headers[header[0]] = header[1];
|
| + }
|
| + return {
|
| + mimeType: streamInfo.mime_type,
|
| + originalUrl: streamInfo.original_url,
|
| + streamUrl: streamInfo.stream_url,
|
| + tabId: streamInfo.tab_id,
|
| + embedded: !!streamInfo.embedded,
|
| + responseHeaders: headers,
|
| + };
|
| +}
|
| +
|
| /**
|
| * Creates a BrowserApi for an extension running as a mime handler.
|
| * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
|
| - * using the mimeHandlerPrivate API.
|
| + * using the MimeHandlerService interface.
|
| */
|
| -function createBrowserApiForMimeHandlerView() {
|
| - return new Promise(function(resolve, reject) {
|
| - chrome.mimeHandlerPrivate.getStreamInfo(resolve);
|
| - }).then(function(streamInfo) {
|
| - let promises = [];
|
| - let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1;
|
| - if (streamInfo.tabId != -1) {
|
| - promises.push(new Promise(function(resolve) {
|
| +async function createBrowserApiForMimeHandlerView() {
|
| + let mimeHandlerInterface = await (async function() {
|
| + if (!define)
|
| + return;
|
| +
|
| + let [frameInterfaces, mojom, connectionModule] = await new Promise(
|
| + resolve => {
|
| + define('pdf_browser_api', [
|
| + 'content/public/renderer/frame_interfaces',
|
| + 'extensions/common/api/mime_handler.mojom',
|
| + 'mojo/public/js/connection',
|
| + ], (...modules) => resolve(modules));
|
| + });
|
| + return connectionModule.bindHandleToProxy(
|
| + frameInterfaces.getInterface(mojom.MimeHandlerService.name),
|
| + mojom.MimeHandlerService);
|
| + })();
|
| +
|
| + let streamInfo = constructStreamInfoDict_(
|
| + (await mimeHandlerInterface.getStreamInfo()).stream_info);
|
| +
|
| + let promises = [];
|
| + let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1;
|
| + if (streamInfo.tabId != -1) {
|
| + promises.push((async function() {
|
| + let tab = await new Promise(function(resolve) {
|
| chrome.tabs.get(streamInfo.tabId, resolve);
|
| - }).then(function(tab) {
|
| - if (tab)
|
| - streamInfo.tabUrl = tab.url;
|
| - }));
|
| - }
|
| - if (manageZoom) {
|
| - promises.push(new Promise(function(resolve) {
|
| - chrome.tabs.setZoomSettings(
|
| - streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
|
| - }));
|
| - }
|
| - return Promise.all(promises).then(
|
| - function() { return BrowserApi.create(streamInfo, manageZoom); });
|
| - });
|
| + });
|
| + if (tab)
|
| + streamInfo.tabUrl = tab.url;
|
| + })());
|
| + }
|
| + if (manageZoom) {
|
| + promises.push(new Promise(function(resolve) {
|
| + chrome.tabs.setZoomSettings(
|
| + streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
|
| + }));
|
| + }
|
| + await Promise.all(promises);
|
| + return BrowserApi.create(
|
| + streamInfo, manageZoom, () => { mimeHandlerInterface.abortStream(); });
|
| }
|
|
|
| /**
|
| @@ -178,7 +212,7 @@ function createBrowserApiForMimeHandlerView() {
|
| * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
|
| * from the URL.
|
| */
|
| -function createBrowserApiForStandaloneExtension() {
|
| +async function createBrowserApiForStandaloneExtension() {
|
| let url = window.location.search.substring(1);
|
| let streamInfo = {
|
| streamUrl: url,
|
| @@ -187,17 +221,14 @@ function createBrowserApiForStandaloneExtension() {
|
| embedded: window.parent != window,
|
| tabId: -1,
|
| };
|
| - return new Promise(function(resolve, reject) {
|
| - if (!chrome.tabs) {
|
| - resolve();
|
| - return;
|
| - }
|
| - chrome.tabs.getCurrent(function(tab) {
|
| - streamInfo.tabId = tab.id;
|
| - streamInfo.tabUrl = tab.url;
|
| - resolve();
|
| + if (chrome.tabs) {
|
| + let tab = await new Promise(resolve => {
|
| + chrome.tabs.getCurrent(resolve);
|
| });
|
| - }).then(function() { return BrowserApi.create(streamInfo, false); });
|
| + streamInfo.tabId = tab.id;
|
| + streamInfo.tabUrl = tab.url;
|
| + }
|
| + return BrowserApi.create(streamInfo, false, () => {});
|
| }
|
|
|
| /**
|
|
|