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.
5 #ifndef ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_
6 #define ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_
13 #include "android_webview/browser/aw_browser_permission_request_delegate.h"
14 #include "android_webview/browser/aw_message_port_message_filter.h"
15 #include "android_webview/browser/browser_view_renderer.h"
16 #include "android_webview/browser/browser_view_renderer_client.h"
17 #include "android_webview/browser/find_helper.h"
18 #include "android_webview/browser/gl_view_renderer_manager.h"
19 #include "android_webview/browser/icon_helper.h"
20 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
21 #include "android_webview/native/permission/permission_request_handler_client.h"
22 #include "base/android/jni_weak_ref.h"
23 #include "base/android/scoped_java_ref.h"
24 #include "base/callback_forward.h"
25 #include "base/memory/scoped_ptr.h"
35 namespace android_webview
{
37 class AwContentsContainer
;
38 class AwContentsClientBridge
;
40 class AwWebContentsDelegate
;
41 class HardwareRenderer
;
42 class PermissionRequestHandler
;
44 // Native side of java-class of same name.
45 // Provides the ownership of and access to browser components required for
46 // WebView functionality; analogous to chrome's TabContents, but with a
47 // level of indirection provided by the AwContentsContainer abstraction.
50 // For most purposes the java and native objects can be considered to have
51 // 1:1 lifetime and relationship. The exception is the java instance that
52 // hosts a popup will be rebound to a second native instance (carrying the
53 // popup content) and discard the 'default' native instance it made on
54 // construction. A native instance is only bound to at most one Java peer over
55 // its entire lifetime - see Init() and SetPendingWebContentsForPopup() for the
56 // construction points, and SetJavaPeers() where these paths join.
57 class AwContents
: public FindHelper::Listener
,
58 public IconHelper::Listener
,
59 public AwRenderViewHostExtClient
,
60 public BrowserViewRendererClient
,
61 public PermissionRequestHandlerClient
,
62 public AwBrowserPermissionRequestDelegate
{
64 // Returns the AwContents instance associated with |web_contents|, or NULL.
65 static AwContents
* FromWebContents(content::WebContents
* web_contents
);
67 // Returns the AwContents instance associated with with the given
68 // render_process_id and render_view_id, or NULL.
69 static AwContents
* FromID(int render_process_id
, int render_view_id
);
71 static std::string
GetLocale();
73 AwContents(scoped_ptr
<content::WebContents
> web_contents
);
74 ~AwContents() override
;
76 AwRenderViewHostExt
* render_view_host_ext() {
77 return render_view_host_ext_
.get();
80 // |handler| is an instance of
81 // org.chromium.android_webview.AwHttpAuthHandler.
82 bool OnReceivedHttpAuthRequest(const base::android::JavaRef
<jobject
>& handler
,
83 const std::string
& host
,
84 const std::string
& realm
);
86 void SetOffscreenPreRaster(bool enabled
);
88 // Methods called from Java.
89 void SetJavaPeers(JNIEnv
* env
,
92 jobject web_contents_delegate
,
93 jobject contents_client_bridge
,
94 jobject io_thread_client
,
95 jobject intercept_navigation_delegate
);
96 base::android::ScopedJavaLocalRef
<jobject
> GetWebContents(JNIEnv
* env
,
99 void Destroy(JNIEnv
* env
, jobject obj
);
100 void DocumentHasImages(JNIEnv
* env
, jobject obj
, jobject message
);
101 void GenerateMHTML(JNIEnv
* env
, jobject obj
, jstring jpath
, jobject callback
);
102 void CreatePdfExporter(JNIEnv
* env
, jobject obj
, jobject pdfExporter
);
103 void AddVisitedLinks(JNIEnv
* env
, jobject obj
, jobjectArray jvisited_links
);
104 base::android::ScopedJavaLocalRef
<jbyteArray
> GetCertificate(
105 JNIEnv
* env
, jobject obj
);
106 void RequestNewHitTestDataAt(JNIEnv
* env
,
111 void UpdateLastHitTestData(JNIEnv
* env
, jobject obj
);
112 void OnSizeChanged(JNIEnv
* env
, jobject obj
, int w
, int h
, int ow
, int oh
);
113 void SetViewVisibility(JNIEnv
* env
, jobject obj
, bool visible
);
114 void SetWindowVisibility(JNIEnv
* env
, jobject obj
, bool visible
);
115 void SetIsPaused(JNIEnv
* env
, jobject obj
, bool paused
);
116 void OnAttachedToWindow(JNIEnv
* env
, jobject obj
, int w
, int h
);
117 void OnDetachedFromWindow(JNIEnv
* env
, jobject obj
);
118 base::android::ScopedJavaLocalRef
<jbyteArray
> GetOpaqueState(
119 JNIEnv
* env
, jobject obj
);
120 jboolean
RestoreFromOpaqueState(JNIEnv
* env
, jobject obj
, jbyteArray state
);
121 void FocusFirstNode(JNIEnv
* env
, jobject obj
);
122 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
123 void OnComputeScroll(JNIEnv
* env
, jobject obj
, jlong animation_time_millis
);
124 bool OnDraw(JNIEnv
* env
,
127 jboolean is_hardware_accelerated
,
133 jint visible_bottom
);
134 jlong
GetAwDrawGLViewContext(JNIEnv
* env
, jobject obj
);
135 jlong
CapturePicture(JNIEnv
* env
, jobject obj
, int width
, int height
);
136 void EnableOnNewPicture(JNIEnv
* env
, jobject obj
, jboolean enabled
);
137 void InsertVisualStateCallback(JNIEnv
* env
,
141 void ClearView(JNIEnv
* env
, jobject obj
);
142 void SetExtraHeadersForUrl(JNIEnv
* env
, jobject obj
,
143 jstring url
, jstring extra_headers
);
145 void InvokeGeolocationCallback(JNIEnv
* env
,
150 // PermissionRequestHandlerClient implementation.
151 void OnPermissionRequest(base::android::ScopedJavaLocalRef
<jobject
> j_request
,
152 AwPermissionRequest
* request
) override
;
153 void OnPermissionRequestCanceled(AwPermissionRequest
* request
) override
;
155 PermissionRequestHandler
* GetPermissionRequestHandler() {
156 return permission_request_handler_
.get();
159 void PreauthorizePermission(JNIEnv
* env
,
164 // AwBrowserPermissionRequestDelegate implementation.
165 void RequestProtectedMediaIdentifierPermission(
167 const base::Callback
<void(bool)>& callback
) override
;
168 void CancelProtectedMediaIdentifierPermissionRequests(
169 const GURL
& origin
) override
;
170 void RequestGeolocationPermission(
172 const base::Callback
<void(bool)>& callback
) override
;
173 void CancelGeolocationPermissionRequests(const GURL
& origin
) override
;
174 void RequestMIDISysexPermission(
176 const base::Callback
<void(bool)>& callback
) override
;
177 void CancelMIDISysexPermissionRequests(const GURL
& origin
) override
;
179 // Find-in-page API and related methods.
180 void FindAllAsync(JNIEnv
* env
, jobject obj
, jstring search_string
);
181 void FindNext(JNIEnv
* env
, jobject obj
, jboolean forward
);
182 void ClearMatches(JNIEnv
* env
, jobject obj
);
183 FindHelper
* GetFindHelper();
185 // Per WebView Cookie Policy
186 bool AllowThirdPartyCookies();
188 // FindHelper::Listener implementation.
189 void OnFindResultReceived(int active_ordinal
,
191 bool finished
) override
;
192 // IconHelper::Listener implementation.
193 bool ShouldDownloadFavicon(const GURL
& icon_url
) override
;
194 void OnReceivedIcon(const GURL
& icon_url
, const SkBitmap
& bitmap
) override
;
195 void OnReceivedTouchIconUrl(const std::string
& url
,
196 const bool precomposed
) override
;
198 // AwRenderViewHostExtClient implementation.
199 void OnWebLayoutPageScaleFactorChanged(float page_scale_factor
) override
;
200 void OnWebLayoutContentsSizeChanged(const gfx::Size
& contents_size
) override
;
202 // BrowserViewRendererClient implementation.
203 bool RequestDrawGL(bool wait_for_completion
) override
;
204 void PostInvalidate() override
;
205 void DetachFunctorFromView() override
;
206 void OnNewPicture() override
;
207 gfx::Point
GetLocationOnScreen() override
;
208 void ScrollContainerViewTo(gfx::Vector2d new_value
) override
;
209 void UpdateScrollState(gfx::Vector2d max_scroll_offset
,
210 gfx::SizeF contents_size_dip
,
211 float page_scale_factor
,
212 float min_page_scale_factor
,
213 float max_page_scale_factor
) override
;
214 void DidOverscroll(gfx::Vector2d overscroll_delta
,
215 gfx::Vector2dF overscroll_velocity
) override
;
217 void ParentDrawConstraintsUpdated(
218 const ParentCompositorDrawConstraints
& draw_constraints
) override
{}
220 void ClearCache(JNIEnv
* env
, jobject obj
, jboolean include_disk_files
);
221 void SetPendingWebContentsForPopup(scoped_ptr
<content::WebContents
> pending
);
222 jlong
ReleasePopupAwContents(JNIEnv
* env
, jobject obj
);
224 void ScrollTo(JNIEnv
* env
, jobject obj
, jint x
, jint y
);
225 void SetDipScale(JNIEnv
* env
, jobject obj
, jfloat dip_scale
);
226 void SetSaveFormData(bool enabled
);
228 // Sets the java client
229 void SetAwAutofillClient(jobject client
);
231 void SetJsOnlineProperty(JNIEnv
* env
, jobject obj
, jboolean network_up
);
232 void TrimMemory(JNIEnv
* env
, jobject obj
, jint level
, jboolean visible
);
234 scoped_refptr
<AwMessagePortMessageFilter
> GetMessagePortMessageFilter();
235 void PostMessageToFrame(JNIEnv
* env
, jobject obj
, jstring frame_id
,
236 jstring message
, jstring target_origin
, jintArray sent_ports
);
237 void CreateMessageChannel(JNIEnv
* env
, jobject obj
, jobjectArray ports
);
239 void GrantFileSchemeAccesstoChildProcess(JNIEnv
* env
, jobject obj
);
242 void InitDataReductionProxyIfNecessary();
243 void InitAutofillIfNecessary(bool enabled
);
245 // Geolocation API support
246 void ShowGeolocationPrompt(const GURL
& origin
, base::Callback
<void(bool)>);
247 void HideGeolocationPrompt(const GURL
& origin
);
249 void SetDipScaleInternal(float dip_scale
);
251 JavaObjectWeakGlobalRef java_ref_
;
252 scoped_ptr
<AwWebContentsDelegate
> web_contents_delegate_
;
253 scoped_ptr
<AwContentsClientBridge
> contents_client_bridge_
;
254 scoped_ptr
<content::WebContents
> web_contents_
;
255 scoped_ptr
<AwRenderViewHostExt
> render_view_host_ext_
;
256 scoped_ptr
<FindHelper
> find_helper_
;
257 scoped_ptr
<IconHelper
> icon_helper_
;
258 scoped_ptr
<AwContents
> pending_contents_
;
259 BrowserViewRenderer browser_view_renderer_
;
260 scoped_ptr
<AwPdfExporter
> pdf_exporter_
;
261 scoped_ptr
<PermissionRequestHandler
> permission_request_handler_
;
262 scoped_refptr
<AwMessagePortMessageFilter
> message_port_message_filter_
;
264 // GURL is supplied by the content layer as requesting frame.
265 // Callback is supplied by the content layer, and is invoked with the result
266 // from the permission prompt.
267 typedef std::pair
<const GURL
, base::Callback
<void(bool)> > OriginCallback
;
268 // The first element in the list is always the currently pending request.
269 std::list
<OriginCallback
> pending_geolocation_prompts_
;
271 GLViewRendererManager::Key renderer_manager_key_
;
273 DISALLOW_COPY_AND_ASSIGN(AwContents
);
276 bool RegisterAwContents(JNIEnv
* env
);
278 } // namespace android_webview
280 #endif // ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_