[Pkg-javascript-commits] [pdf.js] 05/141: Improved FTP support for Chromium extension
David Prévot
taffit at moszumanska.debian.org
Sat Apr 19 22:40:24 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository pdf.js.
commit d04f81b96439897549d9223607a6d3c35fce928c
Author: Rob Wu <gwnRob at gmail.com>
Date: Sun Mar 30 00:08:02 2014 +0100
Improved FTP support for Chromium extension
---
extensions/chromium/feature-detect.js | 58 +++++++++++++++++++++++++++++++++++
extensions/chromium/pdfHandler-v2.js | 5 +--
extensions/chromium/pdfHandler.html | 1 +
extensions/chromium/pdfHandler.js | 23 +++++++++++++-
web/viewer.js | 7 +++--
5 files changed, 88 insertions(+), 6 deletions(-)
diff --git a/extensions/chromium/feature-detect.js b/extensions/chromium/feature-detect.js
new file mode 100644
index 0000000..013193d
--- /dev/null
+++ b/extensions/chromium/feature-detect.js
@@ -0,0 +1,58 @@
+/* -*- 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 */
+
+'use strict';
+
+var Features = {
+ featureDetectLastUA: '',
+ // Whether ftp: in XMLHttpRequest is allowed
+ extensionSupportsFTP: false,
+};
+
+chrome.storage.local.get(Features, function(features) {
+ Features = features;
+ if (features.featureDetectLastUA === navigator.userAgent) {
+ // Browser not upgraded, so the features did probably not change.
+ return;
+ }
+
+ if (!features.extensionSupportsFTP) {
+ features.extensionSupportsFTP = featureTestFTP();
+ }
+
+ Features.featureDetectLastUA = navigator.userAgent;
+ chrome.storage.local.set(Features);
+});
+
+// Tests whether the extension can perform a FTP request.
+// Feature is supported since Chromium 35.0.1888.0 (r256810).
+function featureTestFTP() {
+ var x = new XMLHttpRequest();
+ // The URL does not need to exist, as long as the scheme is ftp:.
+ x.open('GET', 'ftp://ftp.mozilla.org/');
+ try {
+ x.send();
+ // Previous call did not throw error, so the feature is supported!
+ // Immediately abort the request so that the network is not hit at all.
+ x.abort();
+ return true;
+ } catch (e) {
+ return false;
+ }
+}
diff --git a/extensions/chromium/pdfHandler-v2.js b/extensions/chromium/pdfHandler-v2.js
index 711c35d..9bfbc09 100644
--- a/extensions/chromium/pdfHandler-v2.js
+++ b/extensions/chromium/pdfHandler-v2.js
@@ -15,7 +15,7 @@ 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, URL, getViewerURL */
+/* globals chrome, URL, getViewerURL, Features */
(function() {
'use strict';
@@ -158,7 +158,8 @@ limitations under the License.
var streamInfo = getStream(sender.tab.id, pdfUrl) || {};
sendResponse({
streamUrl: streamInfo.streamUrl,
- contentLength: streamInfo.contentLength
+ contentLength: streamInfo.contentLength,
+ extensionSupportsFTP: Features.extensionSupportsFTP
});
}
});
diff --git a/extensions/chromium/pdfHandler.html b/extensions/chromium/pdfHandler.html
index ffb6c1a..c988513 100644
--- a/extensions/chromium/pdfHandler.html
+++ b/extensions/chromium/pdfHandler.html
@@ -15,6 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
<script src="chrome.tabs.executeScriptInFrame.js"></script>
+<script src="feature-detect.js"></script>
<script src="pdfHandler.js"></script>
<script src="extension-router.js"></script>
<script src="pdfHandler-v2.js"></script>
diff --git a/extensions/chromium/pdfHandler.js b/extensions/chromium/pdfHandler.js
index 99cdbae..b33b578 100644
--- a/extensions/chromium/pdfHandler.js
+++ b/extensions/chromium/pdfHandler.js
@@ -15,7 +15,7 @@ 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 */
+/* globals chrome, Features */
'use strict';
@@ -173,6 +173,27 @@ chrome.webRequest.onHeadersReceived.addListener(
['blocking','responseHeaders']);
chrome.webRequest.onBeforeRequest.addListener(
+ function onBeforeRequestForFTP(details) {
+ if (!Features.extensionSupportsFTP) {
+ chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestForFTP);
+ return;
+ }
+ if (isPdfDownloadable(details)) {
+ return;
+ }
+ var viewerUrl = getViewerURL(details.url);
+ return { redirectUrl: viewerUrl };
+ },
+ {
+ urls: [
+ 'ftp://*/*.pdf',
+ 'ftp://*/*.PDF'
+ ],
+ types: ['main_frame', 'sub_frame']
+ },
+ ['blocking']);
+
+chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (isPdfDownloadable(details)) {
return;
diff --git a/web/viewer.js b/web/viewer.js
index 0746b3b..d74dd2b 100644
--- a/web/viewer.js
+++ b/web/viewer.js
@@ -1876,9 +1876,10 @@ function webViewerLoad(evt) {
// PDFView.setTitleUsingUrl(file);
// return;
// }
-// if (isFTPFile) {
-// // Stream not found, and it's loaded from FTP. Reload the page, because
-// // it is not possible to get resources over ftp using XMLHttpRequest.
+// 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, then
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git
More information about the Pkg-javascript-commits
mailing list