aw_contents.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // Copyright (c) 2012 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_H_
  5. #define ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_H_
  6. #include <list>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "android_webview/browser/aw_browser_permission_request_delegate.h"
  11. #include "android_webview/browser/aw_render_process_gone_delegate.h"
  12. #include "android_webview/browser/find_helper.h"
  13. #include "android_webview/browser/gfx/browser_view_renderer.h"
  14. #include "android_webview/browser/gfx/browser_view_renderer_client.h"
  15. #include "android_webview/browser/icon_helper.h"
  16. #include "android_webview/browser/metrics/visibility_metrics_logger.h"
  17. #include "android_webview/browser/permission/permission_callback.h"
  18. #include "android_webview/browser/permission/permission_request_handler_client.h"
  19. #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
  20. #include "android_webview/browser/safe_browsing/aw_safe_browsing_ui_manager.h"
  21. #include "base/android/jni_weak_ref.h"
  22. #include "base/android/scoped_java_ref.h"
  23. #include "base/callback_forward.h"
  24. #include "components/js_injection/browser/js_communication_host.h"
  25. #include "content/public/browser/web_contents_observer.h"
  26. class SkBitmap;
  27. namespace content {
  28. class WebContents;
  29. }
  30. namespace android_webview {
  31. class AwContentsClientBridge;
  32. class AwPdfExporter;
  33. class AwWebContentsDelegate;
  34. class PermissionRequestHandler;
  35. // Native side of java-class of same name.
  36. //
  37. // Object lifetime:
  38. // For most purposes the java and native objects can be considered to have
  39. // 1:1 lifetime and relationship. The exception is the java instance that
  40. // hosts a popup will be rebound to a second native instance (carrying the
  41. // popup content) and discard the 'default' native instance it made on
  42. // construction. A native instance is only bound to at most one Java peer over
  43. // its entire lifetime - see Init() and SetPendingWebContentsForPopup() for the
  44. // construction points, and SetJavaPeers() where these paths join.
  45. class AwContents : public FindHelper::Listener,
  46. public IconHelper::Listener,
  47. public AwRenderViewHostExtClient,
  48. public BrowserViewRendererClient,
  49. public PermissionRequestHandlerClient,
  50. public AwBrowserPermissionRequestDelegate,
  51. public AwRenderProcessGoneDelegate,
  52. public content::WebContentsObserver,
  53. public AwSafeBrowsingUIManager::UIManagerClient,
  54. public VisibilityMetricsLogger::Client {
  55. public:
  56. // Returns the AwContents instance associated with |web_contents|, or NULL.
  57. static AwContents* FromWebContents(content::WebContents* web_contents);
  58. static std::string GetLocale();
  59. static std::string GetLocaleList();
  60. AwContents(std::unique_ptr<content::WebContents> web_contents);
  61. AwContents(const AwContents&) = delete;
  62. AwContents& operator=(const AwContents&) = delete;
  63. ~AwContents() override;
  64. AwRenderViewHostExt* render_view_host_ext() {
  65. return render_view_host_ext_.get();
  66. }
  67. // |handler| is an instance of
  68. // org.chromium.android_webview.AwHttpAuthHandler.
  69. bool OnReceivedHttpAuthRequest(const base::android::JavaRef<jobject>& handler,
  70. const std::string& host,
  71. const std::string& realm);
  72. void SetOffscreenPreRaster(bool enabled);
  73. // Methods called from Java.
  74. void SetJavaPeers(
  75. JNIEnv* env,
  76. const base::android::JavaParamRef<jobject>& aw_contents,
  77. const base::android::JavaParamRef<jobject>& web_contents_delegate,
  78. const base::android::JavaParamRef<jobject>& contents_client_bridge,
  79. const base::android::JavaParamRef<jobject>& io_thread_client,
  80. const base::android::JavaParamRef<jobject>&
  81. intercept_navigation_delegate);
  82. void InitializeAndroidAutofill(JNIEnv* env);
  83. base::android::ScopedJavaLocalRef<jobject> GetWebContents(JNIEnv* env);
  84. base::android::ScopedJavaLocalRef<jobject> GetBrowserContext(JNIEnv* env);
  85. void SetCompositorFrameConsumer(JNIEnv* env, jlong compositor_frame_consumer);
  86. base::android::ScopedJavaLocalRef<jobject> GetRenderProcess(JNIEnv* env);
  87. void Destroy(JNIEnv* env);
  88. void DocumentHasImages(JNIEnv* env,
  89. const base::android::JavaParamRef<jobject>& message);
  90. void GenerateMHTML(JNIEnv* env,
  91. const base::android::JavaParamRef<jstring>& jpath,
  92. const base::android::JavaParamRef<jobject>& callback);
  93. void CreatePdfExporter(
  94. JNIEnv* env,
  95. const base::android::JavaParamRef<jobject>& pdfExporter);
  96. void AddVisitedLinks(
  97. JNIEnv* env,
  98. const base::android::JavaParamRef<jobjectArray>& jvisited_links);
  99. base::android::ScopedJavaLocalRef<jbyteArray> GetCertificate(JNIEnv* env);
  100. void RequestNewHitTestDataAt(JNIEnv* env,
  101. jfloat x,
  102. jfloat y,
  103. jfloat touch_major);
  104. void UpdateLastHitTestData(JNIEnv* env);
  105. void OnSizeChanged(JNIEnv* env, int w, int h, int ow, int oh);
  106. void OnConfigurationChanged(JNIEnv* env);
  107. void SetViewVisibility(JNIEnv* env, bool visible);
  108. void SetWindowVisibility(JNIEnv* env, bool visible);
  109. void SetIsPaused(JNIEnv* env, bool paused);
  110. void OnAttachedToWindow(JNIEnv* env, int w, int h);
  111. void OnDetachedFromWindow(JNIEnv* env);
  112. bool IsVisible(JNIEnv* env);
  113. bool IsDisplayingInterstitialForTesting(JNIEnv* env);
  114. base::android::ScopedJavaLocalRef<jbyteArray> GetOpaqueState(JNIEnv* env);
  115. jboolean RestoreFromOpaqueState(
  116. JNIEnv* env,
  117. const base::android::JavaParamRef<jbyteArray>& state);
  118. void FocusFirstNode(JNIEnv* env);
  119. void SetBackgroundColor(JNIEnv* env, jint color);
  120. void ZoomBy(JNIEnv* env, jfloat delta);
  121. void OnComputeScroll(JNIEnv* env, jlong animation_time_millis);
  122. bool OnDraw(JNIEnv* env,
  123. const base::android::JavaParamRef<jobject>& canvas,
  124. jboolean is_hardware_accelerated,
  125. jint scroll_x,
  126. jint scroll_y,
  127. jint visible_left,
  128. jint visible_top,
  129. jint visible_right,
  130. jint visible_bottom,
  131. jboolean force_auxiliary_bitmap_rendering);
  132. bool NeedToDrawBackgroundColor(JNIEnv* env);
  133. jlong CapturePicture(JNIEnv* env, int width, int height);
  134. void EnableOnNewPicture(JNIEnv* env, jboolean enabled);
  135. void InsertVisualStateCallback(
  136. JNIEnv* env,
  137. jlong request_id,
  138. const base::android::JavaParamRef<jobject>& callback);
  139. void ClearView(JNIEnv* env);
  140. void SetExtraHeadersForUrl(
  141. JNIEnv* env,
  142. const base::android::JavaParamRef<jstring>& url,
  143. const base::android::JavaParamRef<jstring>& extra_headers);
  144. void InvokeGeolocationCallback(
  145. JNIEnv* env,
  146. jboolean value,
  147. const base::android::JavaParamRef<jstring>& origin);
  148. jint GetEffectivePriority(JNIEnv* env);
  149. js_injection::JsCommunicationHost* GetJsCommunicationHost();
  150. jint AddDocumentStartJavaScript(
  151. JNIEnv* env,
  152. const base::android::JavaParamRef<jstring>& script,
  153. const base::android::JavaParamRef<jobjectArray>& allowed_origin_rules);
  154. void RemoveDocumentStartJavaScript(JNIEnv* env, jint script_id);
  155. base::android::ScopedJavaLocalRef<jstring> AddWebMessageListener(
  156. JNIEnv* env,
  157. const base::android::JavaParamRef<jobject>& listener,
  158. const base::android::JavaParamRef<jstring>& js_object_name,
  159. const base::android::JavaParamRef<jobjectArray>& allowed_origins);
  160. void RemoveWebMessageListener(
  161. JNIEnv* env,
  162. const base::android::JavaParamRef<jstring>& js_object_name);
  163. base::android::ScopedJavaLocalRef<jobjectArray> GetJsObjectsInfo(
  164. JNIEnv* env,
  165. const base::android::JavaParamRef<jclass>& clazz);
  166. bool GetViewTreeForceDarkState() { return view_tree_force_dark_state_; }
  167. // PermissionRequestHandlerClient implementation.
  168. void OnPermissionRequest(base::android::ScopedJavaLocalRef<jobject> j_request,
  169. AwPermissionRequest* request) override;
  170. void OnPermissionRequestCanceled(AwPermissionRequest* request) override;
  171. PermissionRequestHandler* GetPermissionRequestHandler() {
  172. return permission_request_handler_.get();
  173. }
  174. void PreauthorizePermission(
  175. JNIEnv* env,
  176. const base::android::JavaParamRef<jstring>& origin,
  177. jlong resources);
  178. // AwBrowserPermissionRequestDelegate implementation.
  179. void RequestProtectedMediaIdentifierPermission(
  180. const GURL& origin,
  181. PermissionCallback callback) override;
  182. void CancelProtectedMediaIdentifierPermissionRequests(
  183. const GURL& origin) override;
  184. void RequestGeolocationPermission(const GURL& origin,
  185. PermissionCallback callback) override;
  186. void CancelGeolocationPermissionRequests(const GURL& origin) override;
  187. void RequestMIDISysexPermission(const GURL& origin,
  188. PermissionCallback callback) override;
  189. void CancelMIDISysexPermissionRequests(const GURL& origin) override;
  190. // Find-in-page API and related methods.
  191. void FindAllAsync(JNIEnv* env,
  192. const base::android::JavaParamRef<jstring>& search_string);
  193. void FindNext(JNIEnv* env, jboolean forward);
  194. void ClearMatches(JNIEnv* env);
  195. FindHelper* GetFindHelper();
  196. // Per WebView Cookie Policy
  197. bool AllowThirdPartyCookies();
  198. // FindHelper::Listener implementation.
  199. void OnFindResultReceived(int active_ordinal,
  200. int match_count,
  201. bool finished) override;
  202. // IconHelper::Listener implementation.
  203. bool ShouldDownloadFavicon(const GURL& icon_url) override;
  204. void OnReceivedIcon(const GURL& icon_url, const SkBitmap& bitmap) override;
  205. void OnReceivedTouchIconUrl(const std::string& url,
  206. const bool precomposed) override;
  207. // AwRenderViewHostExtClient implementation.
  208. void OnWebLayoutPageScaleFactorChanged(float page_scale_factor) override;
  209. void OnWebLayoutContentsSizeChanged(const gfx::Size& contents_size) override;
  210. // BrowserViewRendererClient implementation.
  211. void PostInvalidate(bool inside_vsync) override;
  212. void OnNewPicture() override;
  213. gfx::Point GetLocationOnScreen() override;
  214. void OnViewTreeForceDarkStateChanged(
  215. bool view_tree_force_dark_state) override;
  216. // |new_value| is in physical pixel scale.
  217. void ScrollContainerViewTo(const gfx::Point& new_value) override;
  218. void UpdateScrollState(const gfx::Point& max_scroll_offset,
  219. const gfx::SizeF& contents_size_dip,
  220. float page_scale_factor,
  221. float min_page_scale_factor,
  222. float max_page_scale_factor) override;
  223. void DidOverscroll(const gfx::Vector2d& overscroll_delta,
  224. const gfx::Vector2dF& overscroll_velocity,
  225. bool inside_vsync) override;
  226. ui::TouchHandleDrawable* CreateDrawable() override;
  227. void ClearCache(JNIEnv* env, jboolean include_disk_files);
  228. // See //android_webview/docs/how-does-on-create-window-work.md for more
  229. // details.
  230. void SetPendingWebContentsForPopup(
  231. std::unique_ptr<content::WebContents> pending);
  232. jlong ReleasePopupAwContents(JNIEnv* env);
  233. void ScrollTo(JNIEnv* env, jint x, jint y);
  234. void RestoreScrollAfterTransition(JNIEnv* env, jint x, jint y);
  235. void SmoothScroll(JNIEnv* env,
  236. jint target_x,
  237. jint target_y,
  238. jlong duration_ms);
  239. void SetDipScale(JNIEnv* env, jfloat dip_scale);
  240. base::android::ScopedJavaLocalRef<jstring> GetScheme(JNIEnv* env);
  241. void OnInputEvent(JNIEnv* env);
  242. void SetSaveFormData(bool enabled);
  243. // Sets the java client
  244. void SetAwAutofillClient(const base::android::JavaRef<jobject>& client);
  245. void SetJsOnlineProperty(JNIEnv* env, jboolean network_up);
  246. void TrimMemory(JNIEnv* env, jint level, jboolean visible);
  247. void GrantFileSchemeAccesstoChildProcess(JNIEnv* env);
  248. void ResumeLoadingCreatedPopupWebContents(JNIEnv* env);
  249. void RendererUnresponsive(content::RenderProcessHost* render_process_host);
  250. void RendererResponsive(content::RenderProcessHost* render_process_host);
  251. // content::WebContentsObserver overrides
  252. void RenderViewHostChanged(content::RenderViewHost* old_host,
  253. content::RenderViewHost* new_host) override;
  254. void PrimaryPageChanged(content::Page& page) override;
  255. void DidFinishNavigation(
  256. content::NavigationHandle* navigation_handle) override;
  257. // AwSafeBrowsingUIManager::UIManagerClient implementation
  258. bool CanShowInterstitial() override;
  259. int GetErrorUiType() override;
  260. // VisibilityMetricsLogger::Client implementation
  261. VisibilityMetricsLogger::VisibilityInfo GetVisibilityInfo() override;
  262. // AwRenderProcessGoneDelegate overrides
  263. RenderProcessGoneResult OnRenderProcessGone(int child_process_id,
  264. bool crashed) override;
  265. private:
  266. void InitAutofillIfNecessary(bool autocomplete_enabled);
  267. // Geolocation API support
  268. void ShowGeolocationPrompt(const GURL& origin, PermissionCallback);
  269. void HideGeolocationPrompt(const GURL& origin);
  270. void SetDipScaleInternal(float dip_scale);
  271. JavaObjectWeakGlobalRef java_ref_;
  272. BrowserViewRenderer browser_view_renderer_; // Must outlive |web_contents_|.
  273. std::unique_ptr<content::WebContents> web_contents_;
  274. std::unique_ptr<AwWebContentsDelegate> web_contents_delegate_;
  275. std::unique_ptr<AwContentsClientBridge> contents_client_bridge_;
  276. std::unique_ptr<AwRenderViewHostExt> render_view_host_ext_;
  277. std::unique_ptr<FindHelper> find_helper_;
  278. std::unique_ptr<IconHelper> icon_helper_;
  279. // See //android_webview/docs/how-does-on-create-window-work.md for more
  280. // details for |pending_contents_|.
  281. std::unique_ptr<AwContents> pending_contents_;
  282. std::unique_ptr<AwPdfExporter> pdf_exporter_;
  283. std::unique_ptr<PermissionRequestHandler> permission_request_handler_;
  284. std::unique_ptr<js_injection::JsCommunicationHost> js_communication_host_;
  285. bool view_tree_force_dark_state_ = false;
  286. std::string scheme_;
  287. // GURL is supplied by the content layer as requesting frame.
  288. // Callback is supplied by the content layer, and is invoked with the result
  289. // from the permission prompt.
  290. typedef std::pair<const GURL, PermissionCallback> OriginCallback;
  291. // The first element in the list is always the currently pending request.
  292. std::list<OriginCallback> pending_geolocation_prompts_;
  293. };
  294. } // namespace android_webview
  295. #endif // ANDROID_WEBVIEW_BROWSER_AW_CONTENTS_H_