pdfHandler-vcros.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2014 Mozilla Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /* eslint strict: ["error", "function"] */
  14. /* import-globals-from pdfHandler.js */
  15. (function () {
  16. "use strict";
  17. if (!chrome.fileBrowserHandler) {
  18. // Not on Chromium OS, bail out
  19. return;
  20. }
  21. chrome.fileBrowserHandler.onExecute.addListener(onExecuteFileBrowserHandler);
  22. /**
  23. * Invoked when "Open with PDF Viewer" is chosen in the File browser.
  24. *
  25. * @param {string} id File browser action ID as specified in
  26. * manifest.json
  27. * @param {Object} details Object of type FileHandlerExecuteEventDetails
  28. */
  29. function onExecuteFileBrowserHandler(id, details) {
  30. if (id !== "open-as-pdf") {
  31. return;
  32. }
  33. var fileEntries = details.entries;
  34. // "tab_id" is the currently documented format, but it is inconsistent with
  35. // the other Chrome APIs that use "tabId" (http://crbug.com/179767)
  36. var tabId = details.tab_id || details.tabId;
  37. if (tabId > 0) {
  38. chrome.tabs.get(tabId, function (tab) {
  39. openViewer(tab && tab.windowId, fileEntries);
  40. });
  41. } else {
  42. // Re-use existing window, if available.
  43. chrome.windows.getLastFocused(function (chromeWindow) {
  44. var windowId = chromeWindow && chromeWindow.id;
  45. if (windowId) {
  46. chrome.windows.update(windowId, { focused: true });
  47. }
  48. openViewer(windowId, fileEntries);
  49. });
  50. }
  51. }
  52. /**
  53. * Open the PDF Viewer for the given list of PDF files.
  54. *
  55. * @param {number} windowId
  56. * @param {Array} fileEntries List of Entry objects (HTML5 FileSystem API)
  57. */
  58. function openViewer(windowId, fileEntries) {
  59. if (!fileEntries.length) {
  60. return;
  61. }
  62. var fileEntry = fileEntries.shift();
  63. var url = fileEntry.toURL();
  64. // Use drive: alias to get shorter (more human-readable) URLs.
  65. url = url.replace(
  66. /^filesystem:chrome-extension:\/\/[a-p]{32}\/external\//,
  67. "drive:"
  68. );
  69. url = getViewerURL(url);
  70. if (windowId) {
  71. chrome.tabs.create(
  72. {
  73. windowId,
  74. active: true,
  75. url,
  76. },
  77. function () {
  78. openViewer(windowId, fileEntries);
  79. }
  80. );
  81. } else {
  82. chrome.windows.create(
  83. {
  84. type: "normal",
  85. focused: true,
  86. url,
  87. },
  88. function (chromeWindow) {
  89. openViewer(chromeWindow.id, fileEntries);
  90. }
  91. );
  92. }
  93. }
  94. })();