diff --git a/atom/browser/atom_resource_dispatcher_host_delegate.cc b/atom/browser/atom_resource_dispatcher_host_delegate.cc index abd2fb53f1..12038ff1bf 100644 --- a/atom/browser/atom_resource_dispatcher_host_delegate.cc +++ b/atom/browser/atom_resource_dispatcher_host_delegate.cc @@ -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 diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index 2bd0900311..a17c44fe05 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -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 diff --git a/atom/browser/web_contents_preferences.h b/atom/browser/web_contents_preferences.h index f046cdfc99..a2312e4ab4 100644 --- a/atom/browser/web_contents_preferences.h +++ b/atom/browser/web_contents_preferences.h @@ -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( diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index 6567477b07..fe132166d6 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -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,