Merge pull request #6233 from Rob--W/crx-local-files

Improve support for viewing PDFs from file://-URLs in the Chrome extension
This commit is contained in:
Jonas Jenwald 2015-08-15 14:15:36 +02:00
commit 421289c7bc
11 changed files with 353 additions and 1 deletions

View file

@ -83,6 +83,7 @@ limitations under the License.
// When session restore is used, viewer pages may be loaded before the
// webRequest event listener is attached (= page not found).
// Or the extension could have been crashed (OOM), leaving a sad tab behind.
// Reload these tabs.
chrome.tabs.query({
url: CRX_BASE_URL + '*:*'
@ -92,4 +93,25 @@ limitations under the License.
}
});
console.log('Set up extension URL router.');
Object.keys(localStorage).forEach(function(key) {
// The localStorage item is set upon unload by chromecom.js.
var parsedKey = /^unload-(\d+)-(true|false)-(.+)/.exec(key);
if (parsedKey) {
var timeStart = parseInt(parsedKey[1], 10);
var isHidden = parsedKey[2] === 'true';
var url = parsedKey[3];
if (Date.now() - timeStart < 3000) {
// Is it a new item (younger than 3 seconds)? Assume that the extension
// just reloaded, so restore the tab (work-around for crbug.com/511670).
chrome.tabs.create({
url: chrome.runtime.getURL('restoretab.html') +
'?' + encodeURIComponent(url) +
'#' + encodeURIComponent(localStorage.getItem(key)),
active: !isHidden
});
}
localStorage.removeItem(key);
}
});
})();

View file

@ -22,3 +22,4 @@ limitations under the License.
<script src="pdfHandler-v2.js"></script>
<script src="pdfHandler-vcros.js"></script>
<script src="pageAction/background.js"></script>
<script src="suppress-update.js"></script>

View file

@ -226,3 +226,51 @@ chrome.webRequest.onBeforeRequest.addListener(
types: ['main_frame', 'sub_frame']
},
['blocking']);
chrome.extension.isAllowedFileSchemeAccess(function(isAllowedAccess) {
if (isAllowedAccess) {
return;
}
// If the user has not granted access to file:-URLs, then the webRequest API
// will not catch the request. It is still visible through the webNavigation
// API though, and we can replace the tab with the viewer.
// The viewer will detect that it has no access to file:-URLs, and prompt the
// user to activate file permissions.
chrome.webNavigation.onBeforeNavigate.addListener(function(details) {
if (details.frameId === 0 && !isPdfDownloadable(details)) {
chrome.tabs.update(details.tabId, {
url: getViewerURL(details.url)
});
}
}, {
url: [{
urlPrefix: 'file://',
pathSuffix: '.pdf'
}, {
urlPrefix: 'file://',
pathSuffix: '.PDF'
}]
});
});
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message && message.action === 'isAllowedFileSchemeAccess') {
chrome.extension.isAllowedFileSchemeAccess(sendResponse);
return true;
}
if (message && message.action === 'openExtensionsPageForFileAccess') {
var url = 'chrome://extensions/?id=' + chrome.runtime.id;
if (message.data.newTab) {
chrome.tabs.create({
windowId: sender.tab.windowId,
index: sender.tab.index + 1,
url: url,
openerTabId: sender.tab.id
});
} else {
chrome.tabs.update(sender.tab.id, {
url: url
});
}
}
});

View file

@ -0,0 +1,17 @@
<!doctype html>
<!--
Copyright 2015 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.
-->
<script src="restoretab.js"></script>

View file

@ -0,0 +1,33 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/*
Copyright 2015 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.
*/
/**
* This is part of the work-around for crbug.com/511670.
* - chromecom.js sets the URL and history state upon unload.
* - extension-router.js retrieves the saved state and opens restoretab.html
* - restoretab.html (this script) restores the URL and history state.
*/
'use strict';
var url = decodeURIComponent(location.search.slice(1));
var historyState = decodeURIComponent(location.hash.slice(1));
historyState = historyState === 'undefined' ? null : JSON.parse(historyState);
history.replaceState(historyState, null, url);
location.reload();

View file

@ -0,0 +1,29 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/*
Copyright 2015 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 */
'use strict';
// Do not reload the extension when an update becomes available, UNLESS the PDF
// viewer is not displaying any PDF files. Otherwise the tabs would close, which
// is quite disruptive (crbug.com/511670).
chrome.runtime.onUpdateAvailable.addListener(function() {
if (chrome.extension.getViews({type: 'tab'}).length === 0) {
chrome.runtime.reload();
}
});