2017-10-18 15:36:56 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#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/platform_util.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/render_frame_host.h"
|
|
|
|
#include "net/base/escape.h"
|
|
|
|
#include "net/ssl/client_cert_store.h"
|
|
|
|
#include "url/gurl.h"
|
|
|
|
|
|
|
|
#if defined(USE_NSS_CERTS)
|
|
|
|
#include "net/ssl/client_cert_store_nss.h"
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
#include "net/ssl/client_cert_store_win.h"
|
|
|
|
#elif defined(OS_MACOSX)
|
|
|
|
#include "net/ssl/client_cert_store_mac.h"
|
|
|
|
#endif
|
|
|
|
|
2018-03-15 08:51:48 +00:00
|
|
|
#if defined(ENABLE_PDF_VIEWER)
|
|
|
|
#include "atom/common/atom_constants.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "content/public/browser/stream_info.h"
|
|
|
|
#include "net/url_request/url_request.h"
|
|
|
|
#endif // defined(ENABLE_PDF_VIEWER)
|
|
|
|
|
2017-10-18 15:36:56 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void OnOpenExternal(const GURL& escaped_url, bool allowed) {
|
|
|
|
if (allowed)
|
|
|
|
platform_util::OpenExternal(
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
base::UTF8ToUTF16(escaped_url.spec()),
|
|
|
|
#else
|
|
|
|
escaped_url,
|
|
|
|
#endif
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleExternalProtocolInUI(
|
|
|
|
const GURL& url,
|
|
|
|
const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
|
|
|
|
bool has_user_gesture) {
|
|
|
|
content::WebContents* web_contents = web_contents_getter.Run();
|
|
|
|
if (!web_contents)
|
|
|
|
return;
|
|
|
|
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* permission_helper =
|
2017-10-18 15:36:56 +00:00
|
|
|
WebContentsPermissionHelper::FromWebContents(web_contents);
|
|
|
|
if (!permission_helper)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GURL escaped_url(net::EscapeExternalHandlerValue(url.spec()));
|
|
|
|
auto callback = base::Bind(&OnOpenExternal, escaped_url);
|
2017-11-11 03:44:57 +00:00
|
|
|
permission_helper->RequestOpenExternalPermission(callback, has_user_gesture,
|
|
|
|
url);
|
2017-10-18 15:36:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 08:51:48 +00:00
|
|
|
#if defined(ENABLE_PDF_VIEWER)
|
2017-10-18 15:36:56 +00:00
|
|
|
void OnPdfResourceIntercepted(
|
|
|
|
const GURL& original_url,
|
|
|
|
int render_process_host_id,
|
|
|
|
int render_frame_id,
|
|
|
|
const content::ResourceRequestInfo::WebContentsGetter&
|
|
|
|
web_contents_getter) {
|
|
|
|
content::WebContents* web_contents = web_contents_getter.Run();
|
|
|
|
if (!web_contents)
|
|
|
|
return;
|
|
|
|
|
2018-03-08 07:36:21 +00:00
|
|
|
auto* web_preferences = WebContentsPreferences::From(web_contents);
|
2018-05-29 08:09:51 +00:00
|
|
|
if (!web_preferences || !web_preferences->IsEnabled(options::kPlugins)) {
|
2018-03-08 07:36:21 +00:00
|
|
|
auto* browser_context = web_contents->GetBrowserContext();
|
|
|
|
auto* download_manager =
|
2017-10-18 15:36:56 +00:00
|
|
|
content::BrowserContext::GetDownloadManager(browser_context);
|
|
|
|
|
|
|
|
download_manager->DownloadUrl(
|
|
|
|
content::DownloadUrlParameters::CreateForWebContentsMainFrame(
|
2017-08-21 09:20:37 +00:00
|
|
|
web_contents, original_url, NO_TRAFFIC_ANNOTATION_YET));
|
2017-10-18 15:36:56 +00:00
|
|
|
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
|
|
|
|
content::NavigationController::LoadURLParams params(GURL(base::StringPrintf(
|
|
|
|
"%sindex.html?%s=%s", kPdfViewerUIOrigin, kPdfPluginSrc,
|
|
|
|
net::EscapeUrlEncodedData(original_url.spec(), false).c_str())));
|
|
|
|
|
|
|
|
content::RenderFrameHost* frame_host =
|
|
|
|
content::RenderFrameHost::FromID(render_process_host_id, render_frame_id);
|
|
|
|
if (!frame_host) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
params.frame_tree_node_id = frame_host->GetFrameTreeNodeId();
|
|
|
|
web_contents->GetController().LoadURLWithParams(params);
|
|
|
|
}
|
2018-03-15 08:51:48 +00:00
|
|
|
#endif // defined(ENABLE_PDF_VIEWER)
|
2017-10-18 15:36:56 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
AtomResourceDispatcherHostDelegate::AtomResourceDispatcherHostDelegate() {}
|
|
|
|
|
|
|
|
bool AtomResourceDispatcherHostDelegate::HandleExternalProtocol(
|
|
|
|
const GURL& url,
|
|
|
|
content::ResourceRequestInfo* info) {
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
2018-04-20 10:55:05 +00:00
|
|
|
base::BindOnce(&HandleExternalProtocolInUI, url,
|
|
|
|
info->GetWebContentsGetterForRequest(),
|
|
|
|
info->HasUserGesture()));
|
2017-10-18 15:36:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::ResourceDispatcherHostLoginDelegate*
|
|
|
|
AtomResourceDispatcherHostDelegate::CreateLoginDelegate(
|
|
|
|
net::AuthChallengeInfo* auth_info,
|
|
|
|
net::URLRequest* request) {
|
|
|
|
return new LoginHandler(auth_info, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<net::ClientCertStore>
|
|
|
|
AtomResourceDispatcherHostDelegate::CreateClientCertStore(
|
|
|
|
content::ResourceContext* resource_context) {
|
|
|
|
#if defined(USE_NSS_CERTS)
|
|
|
|
return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreNSS(
|
|
|
|
net::ClientCertStoreNSS::PasswordDelegateFactory()));
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreWin());
|
|
|
|
#elif defined(OS_MACOSX)
|
|
|
|
return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreMac());
|
|
|
|
#elif defined(USE_OPENSSL)
|
|
|
|
return std::unique_ptr<net::ClientCertStore>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
|
|
|
|
net::URLRequest* request,
|
|
|
|
const base::FilePath& plugin_path,
|
|
|
|
const std::string& mime_type,
|
|
|
|
GURL* origin,
|
|
|
|
std::string* payload) {
|
2018-03-15 08:51:48 +00:00
|
|
|
#if defined(ENABLE_PDF_VIEWER)
|
2017-10-18 15:36:56 +00:00
|
|
|
const content::ResourceRequestInfo* info =
|
|
|
|
content::ResourceRequestInfo::ForRequest(request);
|
|
|
|
|
|
|
|
int render_process_host_id;
|
|
|
|
int render_frame_id;
|
|
|
|
if (!info->GetAssociatedRenderFrame(&render_process_host_id,
|
|
|
|
&render_frame_id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mime_type == "application/pdf") {
|
|
|
|
*origin = GURL(kPdfViewerUIOrigin);
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&OnPdfResourceIntercepted, request->url(),
|
|
|
|
render_process_host_id, render_frame_id,
|
|
|
|
info->GetWebContentsGetterForRequest()));
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-15 08:51:48 +00:00
|
|
|
#endif // defined(ENABLE_PDF_VIEWER)
|
2017-10-18 15:36:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|