update pdf ui to work with webui

This commit is contained in:
deepak1556 2017-01-18 21:56:58 +05:30
parent 41c1623824
commit 3c90fabab7
6 changed files with 63 additions and 119 deletions

View file

@ -6,42 +6,22 @@
/** /**
* Returns a promise that will resolve to the default zoom factor. * Returns a promise that will resolve to the default zoom factor.
* @param {!Object} streamInfo The stream object pointing to the data contained
* in the PDF.
* @return {Promise<number>} A promise that will resolve to the default zoom * @return {Promise<number>} A promise that will resolve to the default zoom
* factor. * factor.
*/ */
function lookupDefaultZoom(streamInfo) { function lookupDefaultZoom() {
// Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within return cr.sendWithPromise('getDefaultZoom');
// a webview.
if (!chrome.tabs || streamInfo.tabId < 0)
return Promise.resolve(1);
return new Promise(function(resolve, reject) {
chrome.tabs.getZoomSettings(streamInfo.tabId, function(zoomSettings) {
resolve(zoomSettings.defaultZoomFactor);
});
});
} }
/** /**
* Returns a promise that will resolve to the initial zoom factor * Returns a promise that will resolve to the initial zoom factor
* upon starting the plugin. This may differ from the default zoom * upon starting the plugin. This may differ from the default zoom
* if, for example, the page is zoomed before the plugin is run. * if, for example, the page is zoomed before the plugin is run.
* @param {!Object} streamInfo The stream object pointing to the data contained
* in the PDF.
* @return {Promise<number>} A promise that will resolve to the initial zoom * @return {Promise<number>} A promise that will resolve to the initial zoom
* factor. * factor.
*/ */
function lookupInitialZoom(streamInfo) { function lookupInitialZoom() {
// Webviews don't run in tabs so |streamInfo.tabId| is -1 when running within return cr.sendWithPromise('getInitialZoom');
// a webview.
if (!chrome.tabs || streamInfo.tabId < 0)
return Promise.resolve(1);
return new Promise(function(resolve, reject) {
chrome.tabs.getZoom(streamInfo.tabId, resolve);
});
} }
/** /**
@ -72,8 +52,8 @@ class BrowserApi {
*/ */
static create(streamInfo, manageZoom) { static create(streamInfo, manageZoom) {
return Promise.all([ return Promise.all([
lookupDefaultZoom(streamInfo), lookupDefaultZoom(),
lookupInitialZoom(streamInfo) lookupInitialZoom()
]).then(function(zoomFactors) { ]).then(function(zoomFactors) {
return new BrowserApi( return new BrowserApi(
streamInfo, zoomFactors[0], zoomFactors[1], manageZoom); streamInfo, zoomFactors[0], zoomFactors[1], manageZoom);
@ -88,14 +68,6 @@ class BrowserApi {
return this.streamInfo_; return this.streamInfo_;
} }
/**
* Aborts the stream.
*/
abortStream() {
if (chrome.mimeHandlerPrivate)
chrome.mimeHandlerPrivate.abortStream();
}
/** /**
* Sets the browser zoom. * Sets the browser zoom.
* @param {number} zoom The zoom factor to send to the browser. * @param {number} zoom The zoom factor to send to the browser.
@ -105,9 +77,7 @@ class BrowserApi {
setZoom(zoom) { setZoom(zoom) {
if (!this.manageZoom_) if (!this.manageZoom_)
return Promise.resolve(); return Promise.resolve();
return new Promise(function(resolve, reject) { return cr.sendWithPromise('setZoom', zoom);
chrome.tabs.setZoom(this.streamInfo_.tabId, zoom, resolve);
}.bind(this));
} }
/** /**
@ -135,11 +105,9 @@ class BrowserApi {
if (!this.manageZoom_) if (!this.manageZoom_)
return; return;
chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { cr.addWebUIListener('onZoomLevelChanged', function(newZoomFactor) {
if (zoomChangeInfo.tabId != this.streamInfo_.tabId) listener(newZoomFactor);
return; });
listener(zoomChangeInfo.newZoomFactor);
}.bind(this));
} }
}; };
@ -148,22 +116,15 @@ class BrowserApi {
* @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed * @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
* from the URL. * from the URL.
*/ */
function createBrowserApi(streamURL, originalURL) { function createBrowserApi(opts) {
let streamInfo = { let streamInfo = {
streamUrl: streamURL, streamUrl: opts.originalURL,
originalUrl: originalURL, originalUrl: opts.originalURL,
responseHeaders: {}, responseHeaders: {},
embedded: window.parent != window, embedded: window.parent != window,
tabId: -1, tabId: -1,
}; };
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if (!chrome.tabs) { resolve(BrowserApi.create(streamInfo, true));
resolve(); });
return;
}
//chrome.tabs.getCurrent(function(tab) {
streamInfo.tabId = -1;
resolve();
//});
}).then(function() { return BrowserApi.create(streamInfo, false); });
} }

View file

@ -13,6 +13,8 @@
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css"> <link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<link rel="stylesheet" href="chrome://resources/css/roboto.css"> <link rel="stylesheet" href="chrome://resources/css/roboto.css">
<link rel="stylesheet" href="index.css"> <link rel="stylesheet" href="index.css">
<script src="chrome://resources/js/promise_resolver.js"></script>
<script src="chrome://resources/js/cr.js"></script>
</head> </head>
<body> <body>

View file

@ -10,6 +10,7 @@
*/ */
var viewer; var viewer;
(function() {
/** /**
* Stores any pending messages received which should be passed to the * Stores any pending messages received which should be passed to the
* PDFViewer when it is created. * PDFViewer when it is created.
@ -41,12 +42,11 @@ function initViewer(browserApi) {
* Entrypoint for starting the PDF viewer. This function obtains the browser * Entrypoint for starting the PDF viewer. This function obtains the browser
* API for the PDF and constructs a PDFViewer object with it. * API for the PDF and constructs a PDFViewer object with it.
*/ */
function main(streamURL, originalURL) { cr.sendWithPromise('initialize').then(function(opts) {
// Set up an event listener to catch scripting messages which are sent prior // Set up an event listener to catch scripting messages which are sent prior
// to the PDFViewer being created. // to the PDFViewer being created.
window.addEventListener('message', handleScriptingMessage, false); window.addEventListener('message', handleScriptingMessage, false);
createBrowserApi(streamURL, originalURL).then(initViewer); createBrowserApi(opts).then(initViewer);
}; });
})()
chrome.send('initialize');

View file

@ -41,31 +41,17 @@ function getFilenameFromURL(url) {
/** /**
* Called when navigation happens in the current tab. * Called when navigation happens in the current tab.
* @param {boolean} isInTab Indicates if the PDF viewer is displayed in a tab.
* @param {boolean} isSourceFileUrl Indicates if the navigation source is a
* file:// URL.
* @param {string} url The url to be opened in the current tab. * @param {string} url The url to be opened in the current tab.
*/ */
function onNavigateInCurrentTab(isInTab, isSourceFileUrl, url) { function onNavigateInCurrentTab(url) {
// When the PDFviewer is inside a browser tab, prefer the tabs API because
// it can navigate from one file:// URL to another.
if (chrome.tabs && isInTab && isSourceFileUrl)
chrome.tabs.update({url: url});
else
window.location.href = url; window.location.href = url;
} }
/** /**
* Called when navigation happens in the new tab. * Called when navigation happens in the new tab.
* @param {string} url The url to be opened in the new tab. * @param {string} url The url to be opened in the new tab.
* @param {boolean} active Indicates if the new tab should be the active tab.
*/ */
function onNavigateInNewTab(url, active) { function onNavigateInNewTab(url) {
// Prefer the tabs API because it guarantees we can just open a new tab.
// window.open doesn't have this guarantee.
if (chrome.tabs)
chrome.tabs.create({url: url, active: active});
else
window.open(url); window.open(url);
} }
@ -151,10 +137,10 @@ function PDFViewer(browserApi) {
this.onPasswordSubmitted_.bind(this)); this.onPasswordSubmitted_.bind(this));
this.errorScreen_ = $('error-screen'); this.errorScreen_ = $('error-screen');
// Can only reload if we are in a normal tab. // Can only reload if we are in a normal tab.
if (chrome.tabs && this.browserApi_.getStreamInfo().tabId != -1) { if (!this.browserApi_.getStreamInfo().embedded) {
this.errorScreen_.reloadFn = function() { this.errorScreen_.reloadFn = function() {
chrome.tabs.reload(this.browserApi_.getStreamInfo().tabId); chrome.send('reload');
}.bind(this); };
} }
// Create the viewport. // Create the viewport.
@ -258,13 +244,9 @@ function PDFViewer(browserApi) {
document.addEventListener('mousemove', this.handleMouseEvent_.bind(this)); document.addEventListener('mousemove', this.handleMouseEvent_.bind(this));
document.addEventListener('mouseout', this.handleMouseEvent_.bind(this)); document.addEventListener('mouseout', this.handleMouseEvent_.bind(this));
var isInTab = this.browserApi_.getStreamInfo().tabId != -1;
var isSourceFileUrl = this.originalUrl_.indexOf('file://') == 0;
this.navigator_ = new Navigator(this.originalUrl_, this.navigator_ = new Navigator(this.originalUrl_,
this.viewport_, this.paramsParser_, this.viewport_, this.paramsParser_,
onNavigateInCurrentTab.bind(undefined, onNavigateInCurrentTab,
isInTab,
isSourceFileUrl),
onNavigateInNewTab); onNavigateInNewTab);
this.viewportScroller_ = this.viewportScroller_ =
new ViewportScroller(this.viewport_, this.plugin_, window); new ViewportScroller(this.viewport_, this.plugin_, window);
@ -645,7 +627,7 @@ PDFViewer.prototype = {
this.viewport_.position = position; this.viewport_.position = position;
break; break;
case 'cancelStreamUrl': case 'cancelStreamUrl':
chrome.mimeHandlerPrivate.abortStream(); //chrome.mimeHandlerPrivate.abortStream();
break; break;
case 'metadata': case 'metadata':
if (message.data.title) { if (message.data.title) {

View file

@ -56,7 +56,7 @@ function PDFScriptingAPI(window, plugin) {
this.setPlugin(plugin); this.setPlugin(plugin);
window.addEventListener('message', function(event) { window.addEventListener('message', function(event) {
if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && if (event.origin != 'chrome://pdf-viewer' &&
event.origin != 'chrome://print') { event.origin != 'chrome://print') {
console.error('Received message that was not from the extension: ' + console.error('Received message that was not from the extension: ' +
event); event);

View file

@ -22,7 +22,6 @@
<include name="IDR_PDF_PDF_SCRIPTING_API_JS" file="pdf_scripting_api.js" type="BINDATA" /> <include name="IDR_PDF_PDF_SCRIPTING_API_JS" file="pdf_scripting_api.js" type="BINDATA" />
<include name="IDR_PDF_ZOOM_MANAGER_JS" file="zoom_manager.js" type="BINDATA" /> <include name="IDR_PDF_ZOOM_MANAGER_JS" file="zoom_manager.js" type="BINDATA" />
<include name="IDR_PDF_BROWSER_API_JS" file="browser_api.js" type="BINDATA" /> <include name="IDR_PDF_BROWSER_API_JS" file="browser_api.js" type="BINDATA" />
<include name="IDR_PDF_ODS_CPP_PDF" file="ods-cpp.pdf" type="BINDATA" />
<include name="IDR_PDF_SHARED_ICON_STYLE_CSS" file="elements/shared-icon-style.css" type="BINDATA" /> <include name="IDR_PDF_SHARED_ICON_STYLE_CSS" file="elements/shared-icon-style.css" type="BINDATA" />
<include name="IDR_PDF_ICONS_HTML" file="elements/icons.html" type="BINDATA" /> <include name="IDR_PDF_ICONS_HTML" file="elements/icons.html" type="BINDATA" />