atom_resource_dispatcher_host_delegate.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/atom_resource_dispatcher_host_delegate.h"
  5. #include "atom/browser/atom_browser_context.h"
  6. #include "atom/browser/login_handler.h"
  7. #include "atom/browser/web_contents_permission_helper.h"
  8. #include "atom/browser/web_contents_preferences.h"
  9. #include "atom/common/platform_util.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "content/public/browser/browser_thread.h"
  12. #include "content/public/browser/download_manager.h"
  13. #include "content/public/browser/render_frame_host.h"
  14. #include "net/base/escape.h"
  15. #include "net/ssl/client_cert_store.h"
  16. #include "url/gurl.h"
  17. #if defined(USE_NSS_CERTS)
  18. #include "net/ssl/client_cert_store_nss.h"
  19. #elif defined(OS_WIN)
  20. #include "net/ssl/client_cert_store_win.h"
  21. #elif defined(OS_MACOSX)
  22. #include "net/ssl/client_cert_store_mac.h"
  23. #endif
  24. #if defined(ENABLE_PDF_VIEWER)
  25. #include "atom/common/atom_constants.h"
  26. #include "base/strings/stringprintf.h"
  27. #include "content/public/browser/stream_info.h"
  28. #include "net/url_request/url_request.h"
  29. #endif // defined(ENABLE_PDF_VIEWER)
  30. using content::BrowserThread;
  31. namespace atom {
  32. namespace {
  33. void OnOpenExternal(const GURL& escaped_url, bool allowed) {
  34. if (allowed)
  35. platform_util::OpenExternal(
  36. #if defined(OS_WIN)
  37. base::UTF8ToUTF16(escaped_url.spec()),
  38. #else
  39. escaped_url,
  40. #endif
  41. true);
  42. }
  43. void HandleExternalProtocolInUI(
  44. const GURL& url,
  45. const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
  46. bool has_user_gesture) {
  47. content::WebContents* web_contents = web_contents_getter.Run();
  48. if (!web_contents)
  49. return;
  50. auto* permission_helper =
  51. WebContentsPermissionHelper::FromWebContents(web_contents);
  52. if (!permission_helper)
  53. return;
  54. GURL escaped_url(net::EscapeExternalHandlerValue(url.spec()));
  55. auto callback = base::Bind(&OnOpenExternal, escaped_url);
  56. permission_helper->RequestOpenExternalPermission(callback, has_user_gesture,
  57. url);
  58. }
  59. #if defined(ENABLE_PDF_VIEWER)
  60. void OnPdfResourceIntercepted(
  61. const GURL& original_url,
  62. int render_process_host_id,
  63. int render_frame_id,
  64. const content::ResourceRequestInfo::WebContentsGetter&
  65. web_contents_getter) {
  66. content::WebContents* web_contents = web_contents_getter.Run();
  67. if (!web_contents)
  68. return;
  69. auto* web_preferences = WebContentsPreferences::From(web_contents);
  70. if (!web_preferences || !web_preferences->IsEnabled(options::kPlugins)) {
  71. auto* browser_context = web_contents->GetBrowserContext();
  72. auto* download_manager =
  73. content::BrowserContext::GetDownloadManager(browser_context);
  74. download_manager->DownloadUrl(
  75. content::DownloadUrlParameters::CreateForWebContentsMainFrame(
  76. web_contents, original_url, NO_TRAFFIC_ANNOTATION_YET));
  77. return;
  78. }
  79. // The URL passes the original pdf resource url, that will be requested
  80. // by the webui page.
  81. // chrome://pdf-viewer/index.html?src=https://somepage/123.pdf
  82. content::NavigationController::LoadURLParams params(GURL(base::StringPrintf(
  83. "%sindex.html?%s=%s", kPdfViewerUIOrigin, kPdfPluginSrc,
  84. net::EscapeUrlEncodedData(original_url.spec(), false).c_str())));
  85. content::RenderFrameHost* frame_host =
  86. content::RenderFrameHost::FromID(render_process_host_id, render_frame_id);
  87. if (!frame_host) {
  88. return;
  89. }
  90. params.frame_tree_node_id = frame_host->GetFrameTreeNodeId();
  91. web_contents->GetController().LoadURLWithParams(params);
  92. }
  93. #endif // defined(ENABLE_PDF_VIEWER)
  94. } // namespace
  95. AtomResourceDispatcherHostDelegate::AtomResourceDispatcherHostDelegate() {}
  96. bool AtomResourceDispatcherHostDelegate::HandleExternalProtocol(
  97. const GURL& url,
  98. content::ResourceRequestInfo* info) {
  99. BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
  100. base::BindOnce(&HandleExternalProtocolInUI, url,
  101. info->GetWebContentsGetterForRequest(),
  102. info->HasUserGesture()));
  103. return true;
  104. }
  105. content::ResourceDispatcherHostLoginDelegate*
  106. AtomResourceDispatcherHostDelegate::CreateLoginDelegate(
  107. net::AuthChallengeInfo* auth_info,
  108. net::URLRequest* request) {
  109. return new LoginHandler(auth_info, request);
  110. }
  111. std::unique_ptr<net::ClientCertStore>
  112. AtomResourceDispatcherHostDelegate::CreateClientCertStore(
  113. content::ResourceContext* resource_context) {
  114. #if defined(USE_NSS_CERTS)
  115. return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreNSS(
  116. net::ClientCertStoreNSS::PasswordDelegateFactory()));
  117. #elif defined(OS_WIN)
  118. return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreWin());
  119. #elif defined(OS_MACOSX)
  120. return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreMac());
  121. #elif defined(USE_OPENSSL)
  122. return std::unique_ptr<net::ClientCertStore>();
  123. #endif
  124. }
  125. bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
  126. net::URLRequest* request,
  127. const base::FilePath& plugin_path,
  128. const std::string& mime_type,
  129. GURL* origin,
  130. std::string* payload) {
  131. #if defined(ENABLE_PDF_VIEWER)
  132. const content::ResourceRequestInfo* info =
  133. content::ResourceRequestInfo::ForRequest(request);
  134. int render_process_host_id;
  135. int render_frame_id;
  136. if (!info->GetAssociatedRenderFrame(&render_process_host_id,
  137. &render_frame_id)) {
  138. return false;
  139. }
  140. if (mime_type == "application/pdf") {
  141. *origin = GURL(kPdfViewerUIOrigin);
  142. content::BrowserThread::PostTask(
  143. BrowserThread::UI, FROM_HERE,
  144. base::Bind(&OnPdfResourceIntercepted, request->url(),
  145. render_process_host_id, render_frame_id,
  146. info->GetWebContentsGetterForRequest()));
  147. return true;
  148. }
  149. #endif // defined(ENABLE_PDF_VIEWER)
  150. return false;
  151. }
  152. } // namespace atom