diff --git a/gulpfile.js b/gulpfile.js index 19467a77d118c..0410136fe729d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -684,7 +684,7 @@ function buildDefaultPreferences(defines, dir) { }); const inputStream = merge([ - gulp.src(["web/{app_options,viewer_compatibility}.js"], { + gulp.src(["web/app_options.js"], { base: ".", }), ]); diff --git a/web/app.js b/web/app.js index 2c09a3da68487..f1aa9581051c5 100644 --- a/web/app.js +++ b/web/app.js @@ -37,7 +37,7 @@ import { SpreadMode, TextLayerMode, } from "./ui_utils.js"; -import { AppOptions, OptionKind } from "./app_options.js"; +import { AppOptions, compatibilityParams, OptionKind } from "./app_options.js"; import { build, createPromiseCapability, @@ -78,7 +78,6 @@ import { PDFThumbnailViewer } from "./pdf_thumbnail_viewer.js"; import { PDFViewer } from "./pdf_viewer.js"; import { SecondaryToolbar } from "./secondary_toolbar.js"; import { Toolbar } from "./toolbar.js"; -import { viewerCompatibilityParams } from "./viewer_compatibility.js"; import { ViewHistory } from "./view_history.js"; const DEFAULT_SCALE_DELTA = 1.1; @@ -2484,7 +2483,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { } const file = evt.fileInput.files[0]; - if (!viewerCompatibilityParams.disableCreateObjectURL) { + if (!compatibilityParams.disableCreateObjectURL) { let url = URL.createObjectURL(file); if (file.name) { url = { url, originalUrl: file.name }; diff --git a/web/app_options.js b/web/app_options.js index 3a6be8ac88985..cd23a569123f3 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -13,7 +13,39 @@ * limitations under the License. */ -import { viewerCompatibilityParams } from "./viewer_compatibility.js"; +const compatibilityParams = Object.create(null); +if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { + const userAgent = + (typeof navigator !== "undefined" && navigator.userAgent) || ""; + const platform = + (typeof navigator !== "undefined" && navigator.platform) || ""; + const maxTouchPoints = + (typeof navigator !== "undefined" && navigator.maxTouchPoints) || 1; + + const isAndroid = /Android/.test(userAgent); + const isIOS = + /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent) || + (platform === "MacIntel" && maxTouchPoints > 1); + const isIOSChrome = /CriOS/.test(userAgent); + + // Disables URL.createObjectURL() usage in some environments. + // Support: Chrome on iOS + (function checkOnBlobSupport() { + // Sometimes Chrome on iOS loses data created with createObjectURL(), + // see issue 8081. + if (isIOSChrome) { + compatibilityParams.disableCreateObjectURL = true; + } + })(); + + // Limit canvas size to 5 mega-pixels on mobile. + // Support: Android, iOS + (function checkCanvasSizeLimitation() { + if (isIOS || isAndroid) { + compatibilityParams.maxCanvasPixels = 5242880; + } + })(); +} const OptionKind = { VIEWER: 0x02, @@ -95,7 +127,7 @@ const defaultOptions = { maxCanvasPixels: { /** @type {number} */ value: 16777216, - compatibility: viewerCompatibilityParams.maxCanvasPixels, + compatibility: compatibilityParams.maxCanvasPixels, kind: OptionKind.VIEWER, }, pdfBugEnabled: { @@ -348,4 +380,4 @@ class AppOptions { } } -export { AppOptions, OptionKind }; +export { AppOptions, compatibilityParams, OptionKind }; diff --git a/web/download_manager.js b/web/download_manager.js index 9d4b8cfd72d54..c57f71a14c6dd 100644 --- a/web/download_manager.js +++ b/web/download_manager.js @@ -14,7 +14,7 @@ */ import { createObjectURL, createValidAbsoluteUrl, isPdfFile } from "pdfjs-lib"; -import { viewerCompatibilityParams } from "./viewer_compatibility.js"; +import { compatibilityParams } from "./app_options.js"; if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("CHROME || GENERIC")) { throw new Error( @@ -58,7 +58,7 @@ class DownloadManager { const blobUrl = createObjectURL( data, contentType, - viewerCompatibilityParams.disableCreateObjectURL + compatibilityParams.disableCreateObjectURL ); download(blobUrl, filename); } @@ -70,7 +70,7 @@ class DownloadManager { const isPdfData = isPdfFile(filename); const contentType = isPdfData ? "application/pdf" : ""; - if (isPdfData && !viewerCompatibilityParams.disableCreateObjectURL) { + if (isPdfData && !compatibilityParams.disableCreateObjectURL) { let blobUrl = this._openBlobUrls.get(element); if (!blobUrl) { blobUrl = URL.createObjectURL(new Blob([data], { type: contentType })); @@ -113,7 +113,7 @@ class DownloadManager { * the "open with" dialog. */ download(blob, url, filename, sourceEventType = "download") { - if (viewerCompatibilityParams.disableCreateObjectURL) { + if (compatibilityParams.disableCreateObjectURL) { // URL.createObjectURL is not supported this.downloadUrl(url, filename); return; diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 50ea128e9af40..b959657f3e46b 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -27,9 +27,9 @@ import { RenderingCancelledException, SVGGraphics, } from "pdfjs-lib"; +import { compatibilityParams } from "./app_options.js"; import { NullL10n } from "./l10n_utils.js"; import { RenderingStates } from "./pdf_rendering_queue.js"; -import { viewerCompatibilityParams } from "./viewer_compatibility.js"; /** * @typedef {Object} PDFPageViewOptions @@ -63,7 +63,7 @@ import { viewerCompatibilityParams } from "./viewer_compatibility.js"; * @property {IL10n} l10n - Localization service. */ -const MAX_CANVAS_PIXELS = viewerCompatibilityParams.maxCanvasPixels || 16777216; +const MAX_CANVAS_PIXELS = compatibilityParams.maxCanvasPixels || 16777216; /** * @implements {IRenderableView} @@ -819,7 +819,7 @@ class PDFPageView { const svgGfx = new SVGGraphics( pdfPage.commonObjs, pdfPage.objs, - /* forceDataSchema = */ viewerCompatibilityParams.disableCreateObjectURL + /* forceDataSchema = */ compatibilityParams.disableCreateObjectURL ); return svgGfx.getSVG(opList, actualSizeViewport).then(svg => { ensureNotCancelled(); diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index 592607fb3bad0..f2bd607f66d4b 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -14,8 +14,8 @@ */ import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js"; +import { compatibilityParams } from "./app_options.js"; import { getXfaHtmlForPrinting } from "./print_utils.js"; -import { viewerCompatibilityParams } from "./viewer_compatibility.js"; let activeService = null; let overlayManager = null; @@ -177,7 +177,7 @@ PDFPrintService.prototype = { const scratchCanvas = this.scratchCanvas; if ( "toBlob" in scratchCanvas && - !viewerCompatibilityParams.disableCreateObjectURL + !compatibilityParams.disableCreateObjectURL ) { scratchCanvas.toBlob(function (blob) { img.src = URL.createObjectURL(blob); diff --git a/web/viewer_compatibility.js b/web/viewer_compatibility.js deleted file mode 100644 index 78d06781bd954..0000000000000 --- a/web/viewer_compatibility.js +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright 2018 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. - */ - -const compatibilityParams = Object.create(null); -if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - const userAgent = - (typeof navigator !== "undefined" && navigator.userAgent) || ""; - const platform = - (typeof navigator !== "undefined" && navigator.platform) || ""; - const maxTouchPoints = - (typeof navigator !== "undefined" && navigator.maxTouchPoints) || 1; - - const isAndroid = /Android/.test(userAgent); - const isIOS = - /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent) || - (platform === "MacIntel" && maxTouchPoints > 1); - const isIOSChrome = /CriOS/.test(userAgent); - - // Checks if possible to use URL.createObjectURL() - // Support: IE, Chrome on iOS - (function checkOnBlobSupport() { - // Sometimes Chrome on iOS loses data created with createObjectURL(), - // see issue #8081. - if (isIOSChrome) { - compatibilityParams.disableCreateObjectURL = true; - } - })(); - - // Limit canvas size to 5 mega-pixels on mobile. - // Support: Android, iOS - (function checkCanvasSizeLimitation() { - if (isIOS || isAndroid) { - compatibilityParams.maxCanvasPixels = 5242880; - } - })(); -} -const viewerCompatibilityParams = Object.freeze(compatibilityParams); - -export { viewerCompatibilityParams };