Move logic from viewer.js to chromecom.js

and also default to '' instead of DEFAULT_URL to avoid trying to
load a non-existent file when no file has been specified.
This commit is contained in:
Rob Wu 2014-04-11 17:28:35 +02:00
parent b64c06f68d
commit bfcc8af6ed
2 changed files with 92 additions and 80 deletions

View file

@ -14,35 +14,98 @@
* limitations under the License.
*/
/* globals chrome */
/* globals chrome, PDFJS, PDFView */
'use strict';
var ChromeCom = (function ChromeComClosure() {
return {
/**
* Creates an event that the extension is listening for and will
* asynchronously respond by calling the callback.
* @param {String} action The action to trigger.
* @param {String} data Optional data to send.
* @param {Function} callback Optional response callback that will be called
* with one data argument. When the request cannot be handled, the callback
* is immediately invoked with no arguments.
*/
request: function(action, data, callback) {
var message = {
action: action,
data: data
};
if (!chrome.runtime) {
console.error('chrome.runtime is undefined.');
if (callback) {
callback();
}
} else if (callback) {
chrome.runtime.sendMessage(message, callback);
} else {
chrome.runtime.sendMessage(message);
var ChromeCom = {};
/**
* Creates an event that the extension is listening for and will
* asynchronously respond by calling the callback.
*
* @param {String} action The action to trigger.
* @param {String} data Optional data to send.
* @param {Function} callback Optional response callback that will be called
* with one data argument. When the request cannot be handled, the callback
* is immediately invoked with no arguments.
*/
ChromeCom.request = function ChromeCom_request(action, data, callback) {
var message = {
action: action,
data: data
};
if (!chrome.runtime) {
console.error('chrome.runtime is undefined.');
if (callback) {
callback();
}
} else if (callback) {
chrome.runtime.sendMessage(message, callback);
} else {
chrome.runtime.sendMessage(message);
}
};
/**
* Opens a PDF file with the PDF viewer.
*
* @param {String} file Absolute URL of PDF file.
*/
ChromeCom.openPDFFile = function ChromeCom_openPDFFile(file) {
// Expand drive:-URLs to filesystem URLs (Chrome OS)
file = file.replace(/^drive:/i,
'filesystem:' + location.origin + '/external/');
ChromeCom.request('getPDFStream', file, function(response) {
if (response) {
// We will only get a response when the streamsPrivate API is available.
var isFTPFile = /^ftp:/i.test(file);
var streamUrl = response.streamUrl;
if (streamUrl) {
console.log('Found data stream for ' + file);
PDFView.open(streamUrl, 0, undefined, undefined, {
length: response.contentLength
});
PDFView.setTitleUsingUrl(file);
return;
}
if (isFTPFile && !response.extensionSupportsFTP) {
// Stream not found, and it's loaded from FTP.
// When the browser does not support loading ftp resources over
// XMLHttpRequest, just reload the page.
// NOTE: This will not lead to an infinite redirect loop, because
// if the file exists, then the streamsPrivate API will capture the
// stream and send back the response. If the stream does not exist,
// a "Webpage not available" error will be shown (not the PDF Viewer).
location.replace(file);
return;
}
}
if (/^filesystem:/.test(file) && !PDFJS.disableWorker) {
// The security origin of filesystem:-URLs are not preserved when the
// URL is passed to a Web worker, (http://crbug.com/362061), so we have
// to create an intermediate blob:-URL as a work-around.
var resolveLocalFileSystemURL = window.resolveLocalFileSystemURL ||
window.webkitResolveLocalFileSystemURL;
resolveLocalFileSystemURL(file, function onResolvedFSURL(fileEntry) {
fileEntry.file(function(fileObject) {
var blobUrl = URL.createObjectURL(fileObject);
PDFView.open(blobUrl, 0, undefined, undefined, {
length: fileObject.size
});
});
}, function onFileSystemError(error) {
// This should not happen. When it happens, just fall back to the
// usual way of getting the File's data (via the Web worker).
console.warn('Cannot resolve file ' + file + ', ' + error.name + ' ' +
error.message);
PDFView.open(file, 0);
});
return;
}
PDFView.open(file, 0);
});
};
return ChromeCom;
})();