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

View file

@ -13,6 +13,8 @@
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<link rel="stylesheet" href="chrome://resources/css/roboto.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>
<body>

View file

@ -10,43 +10,43 @@
*/
var viewer;
/**
* Stores any pending messages received which should be passed to the
* PDFViewer when it is created.
* @type Array
*/
var pendingMessages = [];
(function() {
/**
* Stores any pending messages received which should be passed to the
* PDFViewer when it is created.
* @type Array
*/
var pendingMessages = [];
/**
* Handles events that are received prior to the PDFViewer being created.
* @param {Object} message A message event received.
*/
function handleScriptingMessage(message) {
pendingMessages.push(message);
}
/**
* Handles events that are received prior to the PDFViewer being created.
* @param {Object} message A message event received.
*/
function handleScriptingMessage(message) {
pendingMessages.push(message);
}
/**
* Initialize the global PDFViewer and pass any outstanding messages to it.
* @param {Object} browserApi An object providing an API to the browser.
*/
function initViewer(browserApi) {
// PDFViewer will handle any messages after it is created.
window.removeEventListener('message', handleScriptingMessage, false);
viewer = new PDFViewer(browserApi);
while (pendingMessages.length > 0)
viewer.handleScriptingMessage(pendingMessages.shift());
}
/**
* Initialize the global PDFViewer and pass any outstanding messages to it.
* @param {Object} browserApi An object providing an API to the browser.
*/
function initViewer(browserApi) {
// PDFViewer will handle any messages after it is created.
window.removeEventListener('message', handleScriptingMessage, false);
viewer = new PDFViewer(browserApi);
while (pendingMessages.length > 0)
viewer.handleScriptingMessage(pendingMessages.shift());
}
/**
* Entrypoint for starting the PDF viewer. This function obtains the browser
* API for the PDF and constructs a PDFViewer object with it.
*/
function main(streamURL, originalURL) {
// Set up an event listener to catch scripting messages which are sent prior
// to the PDFViewer being created.
window.addEventListener('message', handleScriptingMessage, false);
/**
* Entrypoint for starting the PDF viewer. This function obtains the browser
* API for the PDF and constructs a PDFViewer object with it.
*/
cr.sendWithPromise('initialize').then(function(opts) {
// Set up an event listener to catch scripting messages which are sent prior
// to the PDFViewer being created.
window.addEventListener('message', handleScriptingMessage, false);
createBrowserApi(streamURL, originalURL).then(initViewer);
};
chrome.send('initialize');
createBrowserApi(opts).then(initViewer);
});
})()

View file

@ -41,32 +41,18 @@ function getFilenameFromURL(url) {
/**
* 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.
*/
function onNavigateInCurrentTab(isInTab, isSourceFileUrl, 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;
function onNavigateInCurrentTab(url) {
window.location.href = url;
}
/**
* Called when navigation happens 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) {
// 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);
function onNavigateInNewTab(url) {
window.open(url);
}
/**
@ -151,10 +137,10 @@ function PDFViewer(browserApi) {
this.onPasswordSubmitted_.bind(this));
this.errorScreen_ = $('error-screen');
// 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() {
chrome.tabs.reload(this.browserApi_.getStreamInfo().tabId);
}.bind(this);
chrome.send('reload');
};
}
// Create the viewport.
@ -258,13 +244,9 @@ function PDFViewer(browserApi) {
document.addEventListener('mousemove', 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.viewport_, this.paramsParser_,
onNavigateInCurrentTab.bind(undefined,
isInTab,
isSourceFileUrl),
onNavigateInCurrentTab,
onNavigateInNewTab);
this.viewportScroller_ =
new ViewportScroller(this.viewport_, this.plugin_, window);
@ -645,7 +627,7 @@ PDFViewer.prototype = {
this.viewport_.position = position;
break;
case 'cancelStreamUrl':
chrome.mimeHandlerPrivate.abortStream();
//chrome.mimeHandlerPrivate.abortStream();
break;
case 'metadata':
if (message.data.title) {

View file

@ -56,7 +56,7 @@ function PDFScriptingAPI(window, plugin) {
this.setPlugin(plugin);
window.addEventListener('message', function(event) {
if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' &&
if (event.origin != 'chrome://pdf-viewer' &&
event.origin != 'chrome://print') {
console.error('Received message that was not from the extension: ' +
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_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_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_ICONS_HTML" file="elements/icons.html" type="BINDATA" />