From 08114187465abe56fee8a80bfe57e5e10d719f85 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 12 Nov 2015 22:49:50 +0100 Subject: [PATCH] pdfjs: don't embed data in html We don't need more than one copy of the pdf. --- qutebrowser/browser/webpage.py | 35 ++++++++-------------------- tests/unit/browser/test_webpage.py | 37 ------------------------------ 2 files changed, 9 insertions(+), 63 deletions(-) delete mode 100644 tests/unit/browser/test_webpage.py diff --git a/qutebrowser/browser/webpage.py b/qutebrowser/browser/webpage.py index 6c92c8206..657f7396a 100644 --- a/qutebrowser/browser/webpage.py +++ b/qutebrowser/browser/webpage.py @@ -221,8 +221,7 @@ class BrowserPage(QWebPage): def _show_pdfjs(self, reply): """Show the reply with pdfjs.""" - data = reply.readAll().data() - script = _generate_pdfjs_script(data) + script = _generate_pdfjs_script(reply.url()) viewer = utils.read_file('pdfjs/web/viewer.html') html_page = viewer.replace('%% QUTE_SCRIPT_CONTENT %%', script) html_page = html_page.encode('utf-8') @@ -319,10 +318,7 @@ class BrowserPage(QWebPage): elif (mimetype in {'application/pdf', 'application/x-pdf'} and config.get('content', 'enable-pdfjs')): # Use pdf.js to display the page - if reply.isFinished(): - self._show_pdfjs(reply) - else: - reply.finished.connect(lambda: self._show_pdfjs(reply)) + self._show_pdfjs(reply) else: # Unknown mimetype, so download anyways. download_manager.fetch(reply, @@ -605,27 +601,14 @@ class BrowserPage(QWebPage): return True -def _generate_pdfjs_script(data): +def _generate_pdfjs_script(url): """Generate the script that shows the pdf with pdf.js. Args: - data: The binary data of the pdf page. + url: The url of the pdf page as QUrl. """ - script = io.StringIO() - script.write('var data = new Uint8Array([\n') - - newline = 0 - for byte in data: - newline += script.write('{},'.format(byte)) - if newline > 75: - script.write('\n') - newline = 0 - - script.write("]);\n" - "PDFJS.getDocument(data).then(function(pdf) {\n" - " PDFView.load(pdf);\n" - "});" - ) - source = script.getvalue() - script.close() - return source + return ( + 'PDFJS.getDocument("{url}").then(function(pdf) {{\n' + ' PDFView.load(pdf);\n' + '}});' + ).format(url=url.toString()) diff --git a/tests/unit/browser/test_webpage.py b/tests/unit/browser/test_webpage.py deleted file mode 100644 index 660aa3398..000000000 --- a/tests/unit/browser/test_webpage.py +++ /dev/null @@ -1,37 +0,0 @@ -# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: - -# Copyright 2015 Daniel Schadt -# -# This file is part of qutebrowser. -# -# qutebrowser is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# qutebrowser is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with qutebrowser. If not, see . - -"""Tests for browser.webpage.""" - -import textwrap - -from qutebrowser.browser import webpage - - -def test_generate_pdfjs_script(): - data = b'\x00foobar\xFF' - expected = textwrap.dedent(""" - var data = new Uint8Array([ - 0,102,111,111,98,97,114,255,]); - PDFJS.getDocument(data).then(function(pdf) { - PDFView.load(pdf); - }); - """) - actual = webpage._generate_pdfjs_script(data) - assert actual.strip() == expected.strip()