Skip to content

Commit

Permalink
Merge pull request #9564 from rreimann/pdf-plugins-preference-backport
Browse files Browse the repository at this point in the history
Backport suppress dispatching to PDF viewer if plugins are disabled to 1.6.x
  • Loading branch information
kevinsawicki committed May 24, 2017
2 parents 583e14e + c29316b commit 01c31ee
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
14 changes: 14 additions & 0 deletions atom/browser/atom_resource_dispatcher_host_delegate.cc
Expand Up @@ -4,13 +4,16 @@

#include "atom/browser/atom_resource_dispatcher_host_delegate.h"

#include "atom/browser/atom_browser_context.h"
#include "atom/browser/login_handler.h"
#include "atom/browser/web_contents_permission_helper.h"
#include "atom/browser/web_contents_preferences.h"
#include "atom/common/atom_constants.h"
#include "atom/common/platform_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/stream_info.h"
#include "net/base/escape.h"
#include "net/ssl/client_cert_store.h"
Expand Down Expand Up @@ -69,6 +72,17 @@ void OnPdfResourceIntercepted(
if (!web_contents)
return;

if (!WebContentsPreferences::IsPluginsEnabled(web_contents)) {
auto browser_context = web_contents->GetBrowserContext();
auto download_manager =
content::BrowserContext::GetDownloadManager(browser_context);

download_manager->DownloadUrl(
content::DownloadUrlParameters::CreateForWebContentsMainFrame(
web_contents, original_url));
return;
}

// The URL passes the original pdf resource url, that will be requested
// by the webui page.
// chrome://pdf-viewer/index.html?src=https://somepage/123.pdf
Expand Down
19 changes: 15 additions & 4 deletions atom/browser/web_contents_preferences.cc
Expand Up @@ -197,7 +197,9 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
}
}

bool WebContentsPreferences::IsSandboxed(content::WebContents* web_contents) {
bool WebContentsPreferences::IsPreferenceEnabled(
const std::string& attribute_name,
content::WebContents* web_contents) {
WebContentsPreferences* self;
if (!web_contents)
return false;
Expand All @@ -207,9 +209,18 @@ bool WebContentsPreferences::IsSandboxed(content::WebContents* web_contents) {
return false;

base::DictionaryValue& web_preferences = self->web_preferences_;
bool sandboxed = false;
web_preferences.GetBoolean("sandbox", &sandboxed);
return sandboxed;
bool bool_value = false;
web_preferences.GetBoolean(attribute_name, &bool_value);
return bool_value;
}

bool WebContentsPreferences::IsSandboxed(content::WebContents* web_contents) {
return IsPreferenceEnabled("sandbox", web_contents);
}

bool WebContentsPreferences::IsPluginsEnabled(
content::WebContents* web_contents) {
return IsPreferenceEnabled("plugins", web_contents);
}

// static
Expand Down
3 changes: 3 additions & 0 deletions atom/browser/web_contents_preferences.h
Expand Up @@ -37,7 +37,10 @@ class WebContentsPreferences
static void AppendExtraCommandLineSwitches(
content::WebContents* web_contents, base::CommandLine* command_line);

static bool IsPreferenceEnabled(const std::string& attribute_name,
content::WebContents* web_contents);
static bool IsSandboxed(content::WebContents* web_contents);
static bool IsPluginsEnabled(content::WebContents* web_contents);

// Modify the WebPreferences according to |web_contents|'s preferences.
static void OverrideWebkitPrefs(
Expand Down
22 changes: 19 additions & 3 deletions spec/chromium-spec.js
Expand Up @@ -952,16 +952,18 @@ describe('chromium feature', function () {
slashes: true
})

beforeEach(function () {
function createBrowserWindow ({plugins}) {
w = new BrowserWindow({
show: false,
webPreferences: {
preload: path.join(fixtures, 'module', 'preload-inject-ipc.js')
preload: path.join(fixtures, 'module', 'preload-inject-ipc.js'),
plugins: plugins
}
})
})
}

it('opens when loading a pdf resource as top level navigation', function (done) {
createBrowserWindow({plugins: true})
ipcMain.once('pdf-loaded', function (event, success) {
if (success) done()
})
Expand All @@ -983,7 +985,21 @@ describe('chromium feature', function () {
w.webContents.loadURL(pdfSource)
})

it('should download a pdf when plugins are disabled', function (done) {
createBrowserWindow({plugins: false})
ipcRenderer.sendSync('set-download-option', false, false)
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
assert.equal(state, 'completed')
assert.equal(filename, 'cat.pdf')
assert.equal(mimeType, 'application/pdf')
fs.unlinkSync(path.join(fixtures, 'mock.pdf'))
done()
})
w.webContents.loadURL(pdfSource)
})

it('should not open when pdf is requested as sub resource', function (done) {
createBrowserWindow({plugins: true})
webFrame.registerURLSchemeAsPrivileged('file', {
secure: false,
bypassCSP: false,
Expand Down

0 comments on commit 01c31ee

Please sign in to comment.