aw_contents_client_bridge.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_CLIENT_BRIDGE_H_
  5. #define ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_CLIENT_BRIDGE_H_
  6. #include <memory>
  7. #include "android_webview/browser/network_service/aw_web_resource_request.h"
  8. #include "android_webview/browser/safe_browsing/aw_url_checker_delegate_impl.h"
  9. #include "base/android/jni_weak_ref.h"
  10. #include "base/android/scoped_java_ref.h"
  11. #include "base/callback.h"
  12. #include "base/containers/id_map.h"
  13. #include "base/supports_user_data.h"
  14. #include "components/security_interstitials/core/unsafe_resource.h"
  15. #include "content/public/browser/certificate_request_result_type.h"
  16. #include "content/public/browser/javascript_dialog_manager.h"
  17. #include "content/public/browser/web_contents.h"
  18. #include "net/http/http_response_headers.h"
  19. class GURL;
  20. namespace content {
  21. class ClientCertificateDelegate;
  22. class WebContents;
  23. }
  24. namespace net {
  25. class SSLCertRequestInfo;
  26. class X509Certificate;
  27. }
  28. namespace android_webview {
  29. // A class that handles the Java<->Native communication for the
  30. // AwContentsClient. AwContentsClientBridge is created and owned by
  31. // native AwContents class and it only has a weak reference to the
  32. // its Java peer. Since the Java AwContentsClientBridge can have
  33. // indirect refs from the Application (via callbacks) and so can outlive
  34. // webview, this class notifies it before being destroyed and to nullify
  35. // any references.
  36. class AwContentsClientBridge {
  37. public:
  38. // Used to package up information needed by OnReceivedHttpError for transfer
  39. // between IO and UI threads.
  40. struct HttpErrorInfo {
  41. HttpErrorInfo();
  42. ~HttpErrorInfo();
  43. int status_code;
  44. std::string status_text;
  45. std::string mime_type;
  46. std::string encoding;
  47. std::vector<std::string> response_header_names;
  48. std::vector<std::string> response_header_values;
  49. };
  50. using CertErrorCallback =
  51. base::OnceCallback<void(content::CertificateRequestResultType)>;
  52. using SafeBrowsingActionCallback =
  53. base::OnceCallback<void(AwUrlCheckerDelegateImpl::SafeBrowsingAction,
  54. bool)>;
  55. // Adds the handler to the UserData registry. Dissociate should be called
  56. // before handler is deleted.
  57. static void Associate(content::WebContents* web_contents,
  58. AwContentsClientBridge* handler);
  59. // Removes any handlers associated to the UserData registry.
  60. static void Dissociate(content::WebContents* web_contents);
  61. static AwContentsClientBridge* FromWebContents(
  62. content::WebContents* web_contents);
  63. AwContentsClientBridge(JNIEnv* env,
  64. const base::android::JavaRef<jobject>& obj);
  65. ~AwContentsClientBridge();
  66. // AwContentsClientBridge implementation
  67. void AllowCertificateError(int cert_error,
  68. net::X509Certificate* cert,
  69. const GURL& request_url,
  70. CertErrorCallback callback,
  71. bool* cancel_request);
  72. void SelectClientCertificate(
  73. net::SSLCertRequestInfo* cert_request_info,
  74. std::unique_ptr<content::ClientCertificateDelegate> delegate);
  75. void RunJavaScriptDialog(
  76. content::JavaScriptDialogType dialog_type,
  77. const GURL& origin_url,
  78. const std::u16string& message_text,
  79. const std::u16string& default_prompt_text,
  80. content::JavaScriptDialogManager::DialogClosedCallback callback);
  81. void RunBeforeUnloadDialog(
  82. const GURL& origin_url,
  83. content::JavaScriptDialogManager::DialogClosedCallback callback);
  84. bool ShouldOverrideUrlLoading(const std::u16string& url,
  85. bool has_user_gesture,
  86. bool is_redirect,
  87. bool is_outermost_main_frame,
  88. bool* ignore_navigation);
  89. bool SendBrowseIntent(const std::u16string& url);
  90. void NewDownload(const GURL& url,
  91. const std::string& user_agent,
  92. const std::string& content_disposition,
  93. const std::string& mime_type,
  94. int64_t content_length);
  95. // Called when a new login request is detected. See the documentation for
  96. // WebViewClient.onReceivedLoginRequest for arguments. Note that |account|
  97. // may be empty.
  98. void NewLoginRequest(const std::string& realm,
  99. const std::string& account,
  100. const std::string& args);
  101. // Called when a resource loading error has occured (e.g. an I/O error,
  102. // host name lookup failure etc.)
  103. void OnReceivedError(const AwWebResourceRequest& request,
  104. int error_code,
  105. bool safebrowsing_hit,
  106. bool should_omit_notifications_for_safebrowsing_hit);
  107. void OnSafeBrowsingHit(const AwWebResourceRequest& request,
  108. const safe_browsing::SBThreatType& threat_type,
  109. SafeBrowsingActionCallback callback);
  110. // Called when a response from the server is received with status code >= 400.
  111. void OnReceivedHttpError(const AwWebResourceRequest& request,
  112. std::unique_ptr<HttpErrorInfo> error_info);
  113. // This should be called from IO thread.
  114. static std::unique_ptr<HttpErrorInfo> ExtractHttpErrorInfo(
  115. const net::HttpResponseHeaders* response_headers);
  116. // Methods called from Java.
  117. void ProceedSslError(JNIEnv* env,
  118. const base::android::JavaRef<jobject>& obj,
  119. jboolean proceed,
  120. jint id);
  121. void ProvideClientCertificateResponse(
  122. JNIEnv* env,
  123. const base::android::JavaRef<jobject>& object,
  124. jint request_id,
  125. const base::android::JavaRef<jobjectArray>& encoded_chain_ref,
  126. const base::android::JavaRef<jobject>& private_key_ref);
  127. void ConfirmJsResult(JNIEnv*,
  128. const base::android::JavaRef<jobject>&,
  129. int id,
  130. const base::android::JavaRef<jstring>& prompt);
  131. void CancelJsResult(JNIEnv*, const base::android::JavaRef<jobject>&, int id);
  132. void TakeSafeBrowsingAction(JNIEnv*,
  133. const base::android::JavaRef<jobject>&,
  134. int action,
  135. bool reporting,
  136. int request_id);
  137. private:
  138. JavaObjectWeakGlobalRef java_ref_;
  139. base::IDMap<std::unique_ptr<CertErrorCallback>> pending_cert_error_callbacks_;
  140. base::IDMap<std::unique_ptr<SafeBrowsingActionCallback>>
  141. safe_browsing_callbacks_;
  142. base::IDMap<
  143. std::unique_ptr<content::JavaScriptDialogManager::DialogClosedCallback>>
  144. pending_js_dialog_callbacks_;
  145. base::IDMap<std::unique_ptr<content::ClientCertificateDelegate>>
  146. pending_client_cert_request_delegates_;
  147. };
  148. } // namespace android_webview
  149. #endif // ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_CLIENT_BRIDGE_H_