@ -14,34 +14,18 @@
* /
* /
/* globals chrome */
/* globals chrome */
'use strict' ;
import * as pdfjsLib from 'pdfjs-web/pdfjs' ;
import { DefaultExternalServices , PDFViewerApplication } from 'pdfjs-web/app' ;
import { OverlayManager } from 'pdfjs-web/overlay_manager' ;
import { Preferences } from 'pdfjs-web/preferences' ;
( function ( root , factory ) {
if ( typeof PDFJSDev === 'undefined' || ! PDFJSDev . test ( 'CHROME' ) ) {
if ( typeof define === 'function' && define . amd ) {
define ( 'pdfjs-web/chromecom' , [ 'exports' , 'pdfjs-web/app' ,
'pdfjs-web/overlay_manager' , 'pdfjs-web/preferences' , 'pdfjs-web/pdfjs' ] ,
factory ) ;
} else if ( typeof exports !== 'undefined' ) {
factory ( exports , require ( './app.js' ) , require ( './overlay_manager.js' ) ,
require ( './preferences.js' ) , require ( './pdfjs.js' ) ) ;
} else {
factory ( ( root . pdfjsWebChromeCom = { } ) , root . pdfjsWebApp ,
root . pdfjsWebOverlayManager , root . pdfjsWebPreferences ,
root . pdfjsWebPDFJS ) ;
}
} ( this , function ( exports , app , overlayManager , preferences , pdfjsLib ) {
if ( typeof PDFJSDev === 'undefined' || ! PDFJSDev . test ( 'CHROME' ) ) {
throw new Error ( 'Module "pdfjs-web/chromecom" shall not be used outside ' +
throw new Error ( 'Module "pdfjs-web/chromecom" shall not be used outside ' +
'CHROME build.' ) ;
'CHROME build.' ) ;
}
}
var PDFViewerApplication = app . PDFViewerApplication ;
var ChromeCom = { } ;
var DefaultExternalServices = app . DefaultExternalServices ;
/ * *
var OverlayManager = overlayManager . OverlayManager ;
var Preferences = preferences . Preferences ;
var ChromeCom = { } ;
/ * *
* Creates an event that the extension is listening for and will
* Creates an event that the extension is listening for and will
* asynchronously respond by calling the callback .
* asynchronously respond by calling the callback .
*
*
@ -51,7 +35,7 @@
* with one data argument . When the request cannot be handled , the callback
* with one data argument . When the request cannot be handled , the callback
* is immediately invoked with no arguments .
* is immediately invoked with no arguments .
* /
* /
ChromeCom . request = function ChromeCom _request ( action , data , callback ) {
ChromeCom . request = function ChromeCom _request ( action , data , callback ) {
var message = {
var message = {
action : action ,
action : action ,
data : data
data : data
@ -66,15 +50,15 @@
} else {
} else {
chrome . runtime . sendMessage ( message ) ;
chrome . runtime . sendMessage ( message ) ;
}
}
} ;
} ;
/ * *
/ * *
* Resolves a PDF file path and attempts to detects length .
* Resolves a PDF file path and attempts to detects length .
*
*
* @ param { String } file Absolute URL of PDF file .
* @ param { String } file Absolute URL of PDF file .
* @ param { Function } callback A callback with resolved URL and file length .
* @ param { Function } callback A callback with resolved URL and file length .
* /
* /
ChromeCom . resolvePDFFile = function ChromeCom _resolvePDFFile ( file , callback ) {
ChromeCom . resolvePDFFile = function ChromeCom _resolvePDFFile ( file , callback ) {
// Expand drive:-URLs to filesystem URLs (Chrome OS)
// Expand drive:-URLs to filesystem URLs (Chrome OS)
file = file . replace ( /^drive:/i ,
file = file . replace ( /^drive:/i ,
'filesystem:' + location . origin + '/external/' ) ;
'filesystem:' + location . origin + '/external/' ) ;
@ -132,9 +116,9 @@
return ;
return ;
}
}
callback ( file ) ;
callback ( file ) ;
} ;
} ;
function getEmbedderOrigin ( callback ) {
function getEmbedderOrigin ( callback ) {
var origin = window === top ? location . origin : location . ancestorOrigins [ 0 ] ;
var origin = window === top ? location . origin : location . ancestorOrigins [ 0 ] ;
if ( origin === 'null' ) {
if ( origin === 'null' ) {
// file:-URLs, data-URLs, sandboxed frames, etc.
// file:-URLs, data-URLs, sandboxed frames, etc.
@ -142,17 +126,17 @@
} else {
} else {
callback ( origin ) ;
callback ( origin ) ;
}
}
}
}
function getParentOrigin ( callback ) {
function getParentOrigin ( callback ) {
ChromeCom . request ( 'getParentOrigin' , null , callback ) ;
ChromeCom . request ( 'getParentOrigin' , null , callback ) ;
}
}
function isAllowedFileSchemeAccess ( callback ) {
function isAllowedFileSchemeAccess ( callback ) {
ChromeCom . request ( 'isAllowedFileSchemeAccess' , null , callback ) ;
ChromeCom . request ( 'isAllowedFileSchemeAccess' , null , callback ) ;
}
}
function isRuntimeAvailable ( ) {
function isRuntimeAvailable ( ) {
try {
try {
// When the extension is reloaded, the extension runtime is destroyed and
// When the extension is reloaded, the extension runtime is destroyed and
// the extension APIs become unavailable.
// the extension APIs become unavailable.
@ -161,16 +145,16 @@
}
}
} catch ( e ) { }
} catch ( e ) { }
return false ;
return false ;
}
}
function reloadIfRuntimeIsUnavailable ( ) {
function reloadIfRuntimeIsUnavailable ( ) {
if ( ! isRuntimeAvailable ( ) ) {
if ( ! isRuntimeAvailable ( ) ) {
location . reload ( ) ;
location . reload ( ) ;
}
}
}
}
var chromeFileAccessOverlayPromise ;
var chromeFileAccessOverlayPromise ;
function requestAccessToLocalFile ( fileUrl ) {
function requestAccessToLocalFile ( fileUrl ) {
var onCloseOverlay = null ;
var onCloseOverlay = null ;
if ( top !== window ) {
if ( top !== window ) {
// When the extension reloads after receiving new permissions, the pages
// When the extension reloads after receiving new permissions, the pages
@ -232,9 +216,9 @@
OverlayManager . open ( 'chromeFileAccessOverlay' ) ;
OverlayManager . open ( 'chromeFileAccessOverlay' ) ;
} ) ;
} ) ;
}
}
if ( window === top ) {
if ( window === top ) {
// Chrome closes all extension tabs (crbug.com/511670) when the extension
// Chrome closes all extension tabs (crbug.com/511670) when the extension
// reloads. To counter this, the tab URL and history state is saved to
// reloads. To counter this, the tab URL and history state is saved to
// localStorage and restored by extension-router.js.
// localStorage and restored by extension-router.js.
@ -249,22 +233,22 @@
JSON . stringify ( history . state ) ) ;
JSON . stringify ( history . state ) ) ;
}
}
} ) ;
} ) ;
}
}
// This port is used for several purposes:
// This port is used for several purposes:
// 1. When disconnected, the background page knows that the frame has unload.
// 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
// 2. When the referrer was saved in history.state.chromecomState, it is sent
// to the background page.
// to the background page.
// 3. When the background page knows the referrer of the page, the referrer is
// 3. When the background page knows the referrer of the page, the referrer is
// saved in history.state.chromecomState.
// saved in history.state.chromecomState.
var port ;
var port ;
// Set the referer for the given URL.
// Set the referer for the given URL.
// 0. Background: If loaded via a http(s) URL: Save referer.
// 0. Background: If loaded via a http(s) URL: Save referer.
// 1. Page -> background: send URL and referer from history.state
// 1. Page -> background: send URL and referer from history.state
// 2. Background: Bind referer to URL (via webRequest).
// 2. Background: Bind referer to URL (via webRequest).
// 3. Background -> page: Send latest referer and save to history.
// 3. Background -> page: Send latest referer and save to history.
// 4. Page: Invoke callback.
// 4. Page: Invoke callback.
function setReferer ( url , callback ) {
function setReferer ( url , callback ) {
if ( ! port ) {
if ( ! port ) {
// The background page will accept the port, and keep adding the Referer
// The background page will accept the port, and keep adding the Referer
// request header to requests to |url| until the port is disconnected.
// request header to requests to |url| until the port is disconnected.
@ -301,14 +285,14 @@
port . onMessage . removeListener ( onMessage ) ;
port . onMessage . removeListener ( onMessage ) ;
callback ( ) ;
callback ( ) ;
}
}
}
}
// chrome.storage.sync is not supported in every Chromium-derivate.
// chrome.storage.sync is not supported in every Chromium-derivate.
// Note: The background page takes care of migrating values from
// Note: The background page takes care of migrating values from
// chrome.storage.local to chrome.storage.sync when needed.
// chrome.storage.local to chrome.storage.sync when needed.
var storageArea = chrome . storage . sync || chrome . storage . local ;
var storageArea = chrome . storage . sync || chrome . storage . local ;
Preferences . _writeToStorage = function ( prefObj ) {
Preferences . _writeToStorage = function ( prefObj ) {
return new Promise ( function ( resolve ) {
return new Promise ( function ( resolve ) {
if ( prefObj === Preferences . defaults ) {
if ( prefObj === Preferences . defaults ) {
var keysToRemove = Object . keys ( Preferences . defaults ) ;
var keysToRemove = Object . keys ( Preferences . defaults ) ;
@ -323,9 +307,9 @@
} ) ;
} ) ;
}
}
} ) ;
} ) ;
} ;
} ;
Preferences . _readFromStorage = function ( prefObj ) {
Preferences . _readFromStorage = function ( prefObj ) {
return new Promise ( function ( resolve ) {
return new Promise ( function ( resolve ) {
if ( chrome . storage . managed ) {
if ( chrome . storage . managed ) {
// Get preferences as set by the system administrator.
// Get preferences as set by the system administrator.
@ -347,17 +331,18 @@
} ) ;
} ) ;
}
}
} ) ;
} ) ;
} ;
} ;
var ChromeExternalServices = Object . create ( DefaultExternalServices ) ;
var ChromeExternalServices = Object . create ( DefaultExternalServices ) ;
ChromeExternalServices . initPassiveLoading = function ( callbacks ) {
ChromeExternalServices . initPassiveLoading = function ( callbacks ) {
var appConfig = PDFViewerApplication . appConfig ;
var appConfig = PDFViewerApplication . appConfig ;
ChromeCom . resolvePDFFile ( appConfig . defaultUrl ,
ChromeCom . resolvePDFFile ( appConfig . defaultUrl ,
function ( url , length , originalURL ) {
function ( url , length , originalURL ) {
callbacks . onOpenWithURL ( url , length , originalURL ) ;
callbacks . onOpenWithURL ( url , length , originalURL ) ;
} ) ;
} ) ;
} ;
} ;
PDFViewerApplication . externalServices = ChromeExternalServices ;
PDFViewerApplication . externalServices = ChromeExternalServices ;
exports . ChromeCom = ChromeCom ;
export {
} ) ) ;
ChromeCom ,
} ;