atom_resource_dispatcher_host_delegate.cc 5.5 KB

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