Merge pull request #9507 from rreimann/pdf-plugins-preference

Suppress dispatching to PDF viewer if plugins are disabled
This commit is contained in:
Kevin Sawicki 2017-05-22 14:34:35 -07:00 committed by GitHub
commit 021b5a9ae0
4 changed files with 55 additions and 22 deletions

View file

@ -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"
@ -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

View file

@ -206,22 +206,8 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
}
}
bool WebContentsPreferences::IsSandboxed(content::WebContents* web_contents) {
WebContentsPreferences* self;
if (!web_contents)
return false;
self = FromWebContents(web_contents);
if (!self)
return false;
base::DictionaryValue& web_preferences = self->web_preferences_;
bool sandboxed = false;
web_preferences.GetBoolean("sandbox", &sandboxed);
return sandboxed;
}
bool WebContentsPreferences::UsesNativeWindowOpen(
bool WebContentsPreferences::IsPreferenceEnabled(
const std::string& attribute_name,
content::WebContents* web_contents) {
WebContentsPreferences* self;
if (!web_contents)
@ -232,9 +218,23 @@ bool WebContentsPreferences::UsesNativeWindowOpen(
return false;
base::DictionaryValue& web_preferences = self->web_preferences_;
bool use = false;
web_preferences.GetBoolean("nativeWindowOpen", &use);
return use;
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::UsesNativeWindowOpen(
content::WebContents* web_contents) {
return IsPreferenceEnabled("nativeWindowOpen", web_contents);
}
bool WebContentsPreferences::IsPluginsEnabled(
content::WebContents* web_contents) {
return IsPreferenceEnabled("plugins", web_contents);
}
// static

View file

@ -37,8 +37,11 @@ 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 UsesNativeWindowOpen(content::WebContents* web_contents);
static bool IsPluginsEnabled(content::WebContents* web_contents);
// Modify the WebPreferences according to |web_contents|'s preferences.
static void OverrideWebkitPrefs(

View file

@ -1012,16 +1012,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()
})
@ -1043,7 +1045,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,