Merge pull request #4191 from Rob--W/crx-chrome-os

Register PDF Viewer as PDF handler in Chrome OS's file browser
This commit is contained in:
Yury Delendik 2014-02-11 12:51:36 -06:00
commit 4563f6cd58
6 changed files with 145 additions and 30 deletions

View File

@ -22,16 +22,33 @@ limitations under the License.
var VIEWER_URL = chrome.extension.getURL('content/web/viewer.html');
var CRX_BASE_URL = chrome.extension.getURL('/');
var schemes = [
'http',
'https',
'ftp',
'file',
'chrome-extension',
// Chromium OS
'filesystem',
// Chromium OS, shorthand for filesystem:<origin>/external/
'drive'
];
/**
* @param {string} url The URL prefixed with chrome-extension://.../
* @return {string|undefined} The percent-encoded URL of the (PDF) file.
*/
function parseExtensionURL(url) {
url = url.substring(CRX_BASE_URL.length);
var matchingUrl = /^(?:https?|file|ftp|chrome-extension)(:|%3A)/i.exec(url);
if (matchingUrl) {
// Find the (url-encoded) colon and verify that the scheme is whitelisted.
var schemeIndex = url.search(/:|%3A/i);
if (schemeIndex === -1) {
return;
}
var scheme = url.slice(0, schemeIndex).toLowerCase();
if (schemes.indexOf(scheme) >= 0) {
url = url.split('#')[0];
if (matchingUrl[1] === ':') {
if (url.charAt(schemeIndex) === ':') {
url = encodeURIComponent(url);
}
return url;
@ -86,20 +103,15 @@ limitations under the License.
}
}, {
types: ['main_frame', 'sub_frame'],
urls: [
CRX_BASE_URL + 'http*', // and https
CRX_BASE_URL + 'file*',
CRX_BASE_URL + 'ftp*',
CRX_BASE_URL + 'chrome-extension*'
]
urls: schemes.map(function(scheme) {
// Format: "chrome-extension://[EXTENSIONID]/<scheme>*"
return CRX_BASE_URL + scheme + '*';
})
}, ['blocking']);
chrome.runtime.onMessage.addListener(function(message, sender) {
if (message === 'showPageAction' && sender.tab) {
if (sender.tab.url === sender.url) {
// Only respond to messages from the top-level frame
showPageAction(sender.tab.id, sender.url);
}
showPageAction(sender.tab.id, sender.tab.url);
}
});
@ -107,7 +119,7 @@ limitations under the License.
// webRequest event listener is attached (= page not found).
// Reload these tabs.
chrome.tabs.query({
url: CRX_BASE_URL + '*://*'
url: CRX_BASE_URL + '*:*'
}, function(tabsFromLastSession) {
for (var i = 0; i < tabsFromLastSession.length; ++i) {
chrome.tabs.reload(tabsFromLastSession[i].id);

View File

@ -9,6 +9,7 @@
"16": "icon16.png"
},
"permissions": [
"fileBrowserHandler",
"webRequest", "webRequestBlocking",
"<all_urls>",
"tabs",
@ -26,6 +27,13 @@
"run_at": "document_start",
"js": ["contentscript.js"]
}],
"file_browser_handlers": [{
"id": "open-as-pdf",
"default_title": "Open with PDF Viewer",
"file_filters": [
"filesystem:*.pdf"
]
}],
"mime_types": [
"application/pdf"
],
@ -48,6 +56,8 @@
"https:/*",
"ftp:/*",
"file:/*",
"chrome-extension:/*"
"chrome-extension:/*",
"filesystem:/*",
"drive:*"
]
}

View File

@ -0,0 +1,95 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/*
Copyright 2014 Mozilla Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* globals chrome, getViewerURL */
(function() {
'use strict';
if (!chrome.fileBrowserHandler) {
// Not on Chromium OS, bail out
return;
}
chrome.fileBrowserHandler.onExecute.addListener(onExecuteFileBrowserHandler);
/**
* Invoked when "Open with PDF Viewer" is chosen in the File browser.
*
* @param {String} id File browser action ID as specified in manifest.json
* @param {Object} details Object of type FileHandlerExecuteEventDetails
*/
function onExecuteFileBrowserHandler(id, details) {
if (id !== 'open-as-pdf') {
return;
}
var fileEntries = details.entries;
// "tab_id" is the currently documented format, but it is inconsistent with
// the other Chrome APIs that use "tabId" (http://crbug.com/179767)
var tabId = details.tab_id || details.tabId;
if (tabId > 0) {
chrome.tabs.get(tabId, function(tab) {
openViewer(tab && tab.windowId, fileEntries);
});
} else {
// Re-use existing window, if available.
chrome.windows.getLastFocused(function(chromeWindow) {
var windowId = chromeWindow && chromeWindow.id;
if (windowId) {
chrome.windows.update(windowId, { focused: true });
}
openViewer(windowId, fileEntries);
});
}
}
/**
* Open the PDF Viewer for the given list of PDF files.
*
* @param {number} windowId
* @param {Array} fileEntries List of Entry objects (HTML5 FileSystem API)
*/
function openViewer(windowId, fileEntries) {
if (!fileEntries.length) {
return;
}
var fileEntry = fileEntries.shift();
var url = fileEntry.toURL();
// Use drive: alias to get shorter (more human-readable) URLs.
url = url.replace(/^filesystem:chrome-extension:\/\/[a-p]{32}\/external\//,
'drive:');
url = getViewerURL(url);
if (windowId) {
chrome.tabs.create({
windowId: windowId,
active: true,
url: url
}, function() {
openViewer(windowId, fileEntries);
});
} else {
chrome.windows.create({
type: 'normal',
focused: true,
url: url
}, function(chromeWindow) {
openViewer(chromeWindow.id, fileEntries);
});
}
}
})();

View File

@ -18,3 +18,4 @@ limitations under the License.
<script src="pdfHandler.js"></script>
<script src="extension-router.js"></script>
<script src="pdfHandler-v2.js"></script>
<script src="pdfHandler-vcros.js"></script>

View File

@ -145,7 +145,9 @@ var PDFHistory = {
// window.history.pushState(stateObj, '');
//#endif
//#if CHROME
// if (top === window) {
// chrome.runtime.sendMessage('showPageAction');
// }
//#endif
}
},

View File

@ -1572,15 +1572,13 @@ var DocumentOutlineView = function documentOutlineView(outline) {
//(function rewriteUrlClosure() {
// // Run this code outside DOMContentLoaded to make sure that the URL
// // is rewritten as soon as possible.
// if (location.origin + '/' !== chrome.extension.getURL('/')) {
// DEFAULT_URL = window.location.href.split('#')[0];
// } else {
// var params = PDFView.parseQueryString(document.location.search.slice(1));
// DEFAULT_URL = params.file || DEFAULT_URL;
//
// // Example: chrome-extension://.../http://example.com/file.pdf
// var humanReadableUrl = '/' + DEFAULT_URL + location.hash;
// history.replaceState(history.state, '', humanReadableUrl);
// if (top === window) {
// chrome.runtime.sendMessage('showPageAction');
// }
//})();
@ -1598,12 +1596,9 @@ document.addEventListener('DOMContentLoaded', function webViewerLoad(evt) {
//#endif
//#if CHROME
//var file = DEFAULT_URL;
//#endif
//#if CHROME
//if (location.protocol !== 'chrome-extension:') {
// file = location.href.split('#')[0];
//}
//// XHR cannot get data from drive:-URLs, so expand to filesystem: (Chrome OS)
//file = file.replace(/^drive:/i,
// 'filesystem:' + location.origin + '/external/');
//#endif
//#if !(FIREFOX || MOZCENTRAL || CHROME || B2G)