[Pkg-javascript-commits] [pdf.js] 20/109: Teach users how to enable access to local files.

David Prévot taffit at moszumanska.debian.org
Fri Sep 25 03:04:13 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository pdf.js.

commit 775d4e69cff755b62351b14494fcb902eac1718b
Author: Rob Wu <rob at robwu.nl>
Date:   Sat Jul 18 17:11:33 2015 +0200

    Teach users how to enable access to local files.
---
 extensions/chromium/pdfHandler.js       | 48 ++++++++++++++++++++
 web/chromecom.js                        | 80 ++++++++++++++++++++++++++++++++-
 web/viewer-snippet-chrome-overlays.html | 24 ++++++++++
 web/viewer.css                          |  4 ++
 web/viewer.html                         |  3 ++
 5 files changed, 158 insertions(+), 1 deletion(-)

diff --git a/extensions/chromium/pdfHandler.js b/extensions/chromium/pdfHandler.js
index 6cb82ef..56da078 100644
--- a/extensions/chromium/pdfHandler.js
+++ b/extensions/chromium/pdfHandler.js
@@ -220,3 +220,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
+      });
+    }
+  }
+});
diff --git a/web/chromecom.js b/web/chromecom.js
index 3c4a17e..35e40a5 100644
--- a/web/chromecom.js
+++ b/web/chromecom.js
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-/* globals chrome, PDFJS, PDFViewerApplication */
+/* globals chrome, PDFJS, PDFViewerApplication, OverlayManager */
 'use strict';
 
 var ChromeCom = (function ChromeComClosure() {
@@ -120,11 +120,89 @@ var ChromeCom = (function ChromeComClosure() {
               ' non-local page for security reasons.');
           return;
         }
+        isAllowedFileSchemeAccess(function(isAllowedAccess) {
+          if (isAllowedAccess) {
+            PDFViewerApplication.open(file, 0);
+          } else {
+            requestAccessToLocalFile(file);
+          }
+        });
+        return;
       }
       PDFViewerApplication.open(file, 0);
     });
   };
 
+  function isAllowedFileSchemeAccess(callback) {
+    ChromeCom.request('isAllowedFileSchemeAccess', null, callback);
+  }
+
+  function isRuntimeAvailable() {
+    try {
+      // When the extension is reloaded, the extension runtime is destroyed and
+      // the extension APIs become unavailable.
+      if (chrome.runtime && chrome.runtime.getManifest()) {
+        return true;
+      }
+    } catch (e) {}
+    return false;
+  }
+
+  function reloadIfRuntimeIsUnavailable() {
+    if (!isRuntimeAvailable()) {
+      location.reload();
+    }
+  }
+
+  var chromeFileAccessOverlayPromise;
+  function requestAccessToLocalFile(fileUrl) {
+    var onCloseOverlay = null;
+    if (top !== window) {
+      // When the extension reloads after receiving new permissions, the pages
+      // have to be reloaded to restore the extension runtime. Auto-reload
+      // frames, because users should not have to reload the whole page just to
+      // update the viewer.
+      // Top-level frames are closed by Chrome upon reload, so there is no need
+      // for detecting unload of the top-level frame. Should this ever change
+      // (crbug.com/511670), then the user can just reload the tab.
+      window.addEventListener('focus', reloadIfRuntimeIsUnavailable);
+      onCloseOverlay = function() {
+        window.removeEventListener('focus', reloadIfRuntimeIsUnavailable);
+        reloadIfRuntimeIsUnavailable();
+        OverlayManager.close('chromeFileAccessOverlay');
+      };
+    }
+    if (!chromeFileAccessOverlayPromise) {
+      chromeFileAccessOverlayPromise = OverlayManager.register(
+          'chromeFileAccessOverlay', onCloseOverlay, true);
+    }
+    chromeFileAccessOverlayPromise.then(function() {
+      var iconPath = chrome.runtime.getManifest().icons[48];
+      document.getElementById('chrome-pdfjs-logo-bg').style.backgroundImage =
+        'url(' + chrome.runtime.getURL(iconPath) + ')';
+
+      var link = document.getElementById('chrome-link-to-extensions-page');
+      link.href = 'chrome://extensions/?id=' + chrome.runtime.id;
+      link.onclick = function(e) {
+        // Direct navigation to chrome:// URLs is blocked by Chrome, so we
+        // have to ask the background page to open chrome://extensions/?id=...
+        e.preventDefault();
+        // Open in the current tab by default, because toggling the file access
+        // checkbox causes the extension to reload, and Chrome will close all
+        // tabs upon reload.
+        ChromeCom.request('openExtensionsPageForFileAccess', {
+          newTab: e.ctrlKey || e.metaKey || e.button === 1 || window !== top
+        });
+      };
+
+      // Show which file is being opened to help the user with understanding
+      // why this permission request is shown.
+      document.getElementById('chrome-url-of-local-file').textContent = fileUrl;
+
+      OverlayManager.open('chromeFileAccessOverlay');
+    });
+  }
+
   // This port is used for several purposes:
   // 1. When disconnected, the background page knows that the frame has unload.
   // 2. When the referrer was saved in history.state.chromecomState, it is sent
diff --git a/web/viewer-snippet-chrome-overlays.html b/web/viewer-snippet-chrome-overlays.html
new file mode 100644
index 0000000..f4d23e8
--- /dev/null
+++ b/web/viewer-snippet-chrome-overlays.html
@@ -0,0 +1,24 @@
+<div id="chromeFileAccessOverlay" class="container hidden">
+  <div class="dialog">
+    <div class="row">
+      <!-- The extension icon (PDF.js logo) will be shown at the left, to help
+           users with recognizing which checkbox they have to click when they
+           visit chrome://extensions.
+      -->
+      <p id="chrome-pdfjs-logo-bg" style="
+          display: block;
+          padding-left: 60px;
+          min-height: 48px;
+          background-size: 48px;
+          background-repeat: no-repeat;
+          font-size: 14px;
+          line-height: 1.8em;
+          word-break: break-all;">
+        Click on "Allow access to file URLs" at
+        <a id="chrome-link-to-extensions-page">chrome://extensions</a>
+        <br>
+        to view <span id="chrome-url-of-local-file">this PDF file.</span>
+      </p>
+    </div>
+  </div>
+</div>
diff --git a/web/viewer.css b/web/viewer.css
index 4602351..7489b9e 100644
--- a/web/viewer.css
+++ b/web/viewer.css
@@ -1371,6 +1371,10 @@ html[dir='rtl'] .attachmentsItem > button {
   vertical-align: middle;
 }
 
+.dialog :link {
+  color: white;
+}
+
 #passwordOverlay > .dialog {
   text-align: center;
 }
diff --git a/web/viewer.html b/web/viewer.html
index e807f33..4c42073 100644
--- a/web/viewer.html
+++ b/web/viewer.html
@@ -388,6 +388,9 @@ See https://github.com/adobe-type-tools/cmap-resources
             </div>
           </div>
         </div>
+<!--#if CHROME-->
+<!--#include viewer-snippet-chrome-overlays.html-->
+<!--#endif-->
       </div>  <!-- overlayContainer -->
 
     </div> <!-- outerContainer -->

-- 
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