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 AwContents(scoped_ptr
<content::WebContents
> web_contents
);
72 ~AwContents() override
;
74 AwRenderViewHostExt
* render_view_host_ext() {
75 return render_view_host_ext_
.get();
78 // |handler| is an instance of
79 // org.chromium.android_webview.AwHttpAuthHandler.
80 bool OnReceivedHttpAuthRequest(const base::android::JavaRef
<jobject
>& handler
,
81 const std::string
& host
,
82 const std::string
& realm
);
84 void SetOffscreenPreRaster(bool enabled
);
86 // Methods called from Java.
87 void SetJavaPeers(JNIEnv
* env
,
90 jobject web_contents_delegate
,
91 jobject contents_client_bridge
,
92 jobject io_thread_client
,
93 jobject intercept_navigation_delegate
);
94 base::android::ScopedJavaLocalRef
<jobject
> GetWebContents(JNIEnv
* env
,
97 void Destroy(JNIEnv
* env
, jobject obj
);
98 void DocumentHasImages(JNIEnv
* env
, jobject obj
, jobject message
);
99 void GenerateMHTML(JNIEnv
* env
, jobject obj
, jstring jpath
, jobject callback
);
100 void CreatePdfExporter(JNIEnv
* env
, jobject obj
, jobject pdfExporter
);
101 void AddVisitedLinks(JNIEnv
* env
, jobject obj
, jobjectArray jvisited_links
);
102 base::android::ScopedJavaLocalRef
<jbyteArray
> GetCertificate(
103 JNIEnv
* env
, jobject obj
);
104 void RequestNewHitTestDataAt(JNIEnv
* env
,
109 void UpdateLastHitTestData(JNIEnv
* env
, jobject obj
);
110 void OnSizeChanged(JNIEnv
* env
, jobject obj
, int w
, int h
, int ow
, int oh
);
111 void SetViewVisibility(JNIEnv
* env
, jobject obj
, bool visible
);
112 void SetWindowVisibility(JNIEnv
* env
, jobject obj
, bool visible
);
113 void SetIsPaused(JNIEnv
* env
, jobject obj
, bool paused
);
114 void OnAttachedToWindow(JNIEnv
* env
, jobject obj
, int w
, int h
);
115 void OnDetachedFromWindow(JNIEnv
* env
, jobject obj
);
116 base::android::ScopedJavaLocalRef
<jbyteArray
> GetOpaqueState(
117 JNIEnv
* env
, jobject obj
);
118 jboolean
RestoreFromOpaqueState(JNIEnv
* env
, jobject obj
, jbyteArray state
);
119 void FocusFirstNode(JNIEnv
* env
, jobject obj
);
120 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
121 void OnComputeScroll(JNIEnv
* env
, jobject obj
, jlong animation_time_millis
);
122 bool OnDraw(JNIEnv
* env
,
125 jboolean is_hardware_accelerated
,
131 jint visible_bottom
);
132 jlong
GetAwDrawGLViewContext(JNIEnv
* env
, jobject obj
);
133 jlong
CapturePicture(JNIEnv
* env
, jobject obj
, int width
, int height
);
134 void EnableOnNewPicture(JNIEnv
* env
, jobject obj
, jboolean enabled
);
135 void InsertVisualStateCallback(JNIEnv
* env
,
139 void ClearView(JNIEnv
* env
, jobject obj
);
140 void SetExtraHeadersForUrl(JNIEnv
* env
, jobject obj
,
141 jstring url
, jstring extra_headers
);
143 void InvokeGeolocationCallback(JNIEnv
* env
,
148 // PermissionRequestHandlerClient implementation.
149 void OnPermissionRequest(base::android::ScopedJavaLocalRef
<jobject
> j_request
,
150 AwPermissionRequest
* request
) override
;
151 void OnPermissionRequestCanceled(AwPermissionRequest
* request
) override
;
153 PermissionRequestHandler
* GetPermissionRequestHandler() {
154 return permission_request_handler_
.get();
157 void PreauthorizePermission(JNIEnv
* env
,
162 // AwBrowserPermissionRequestDelegate implementation.
163 void RequestProtectedMediaIdentifierPermission(
165 const base::Callback
<void(bool)>& callback
) override
;
166 void CancelProtectedMediaIdentifierPermissionRequests(
167 const GURL
& origin
) override
;
168 void RequestGeolocationPermission(
170 const base::Callback
<void(bool)>& callback
) override
;
171 void CancelGeolocationPermissionRequests(const GURL
& origin
) override
;
172 void RequestMIDISysexPermission(
174 const base::Callback
<void(bool)>& callback
) override
;
175 void CancelMIDISysexPermissionRequests(const GURL
& origin
) override
;
177 // Find-in-page API and related methods.
178 void FindAllAsync(JNIEnv
* env
, jobject obj
, jstring search_string
);
179 void FindNext(JNIEnv
* env
, jobject obj
, jboolean forward
);
180 void ClearMatches(JNIEnv
* env
, jobject obj
);
181 FindHelper
* GetFindHelper();
183 // Per WebView Cookie Policy
184 bool AllowThirdPartyCookies();
186 // FindHelper::Listener implementation.
187 void OnFindResultReceived(int active_ordinal
,
189 bool finished
) override
;
190 // IconHelper::Listener implementation.
191 bool ShouldDownloadFavicon(const GURL
& icon_url
) override
;
192 void OnReceivedIcon(const GURL
& icon_url
, const SkBitmap
& bitmap
) override
;
193 void OnReceivedTouchIconUrl(const std::string
& url
,
194 const bool precomposed
) override
;
196 // AwRenderViewHostExtClient implementation.
197 void OnWebLayoutPageScaleFactorChanged(float page_scale_factor
) override
;
198 void OnWebLayoutContentsSizeChanged(const gfx::Size
& contents_size
) override
;
200 // BrowserViewRendererClient implementation.
201 bool RequestDrawGL(bool wait_for_completion
) override
;
202 void PostInvalidate() override
;
203 void DetachFunctorFromView() override
;
204 void OnNewPicture() override
;
205 gfx::Point
GetLocationOnScreen() override
;
206 void ScrollContainerViewTo(gfx::Vector2d new_value
) override
;
207 bool IsSmoothScrollingActive() const override
;
208 void UpdateScrollState(gfx::Vector2d max_scroll_offset
,
209 gfx::SizeF contents_size_dip
,
210 float page_scale_factor
,
211 float min_page_scale_factor
,
212 float max_page_scale_factor
) override
;
213 void DidOverscroll(gfx::Vector2d overscroll_delta
,
214 gfx::Vector2dF overscroll_velocity
) override
;
216 void ParentDrawConstraintsUpdated(
217 const ParentCompositorDrawConstraints
& draw_constraints
) override
{}
219 void ClearCache(JNIEnv
* env
, jobject obj
, jboolean include_disk_files
);
220 void SetPendingWebContentsForPopup(scoped_ptr
<content::WebContents
> pending
);
221 jlong
ReleasePopupAwContents(JNIEnv
* env
, jobject obj
);
223 void ScrollTo(JNIEnv
* env
, jobject obj
, jint x
, jint y
);
224 void SetDipScale(JNIEnv
* env
, jobject obj
, jfloat dip_scale
);
225 void SetSaveFormData(bool enabled
);
227 // Sets the java client
228 void SetAwAutofillClient(jobject client
);
230 void SetJsOnlineProperty(JNIEnv
* env
, jobject obj
, jboolean network_up
);
231 void TrimMemory(JNIEnv
* env
, jobject obj
, jint level
, jboolean visible
);
233 scoped_refptr
<AwMessagePortMessageFilter
> GetMessagePortMessageFilter();
234 void PostMessageToFrame(JNIEnv
* env
, jobject obj
, jstring frame_id
,
235 jstring message
, jstring target_origin
, jintArray sent_ports
);
236 void CreateMessageChannel(JNIEnv
* env
, jobject obj
, jobjectArray ports
);
239 void InitDataReductionProxyIfNecessary();
240 void InitAutofillIfNecessary(bool enabled
);
242 // Geolocation API support
243 void ShowGeolocationPrompt(const GURL
& origin
, base::Callback
<void(bool)>);
244 void HideGeolocationPrompt(const GURL
& origin
);
246 void SetDipScaleInternal(float dip_scale
);
248 JavaObjectWeakGlobalRef java_ref_
;
249 scoped_ptr
<AwWebContentsDelegate
> web_contents_delegate_
;
250 scoped_ptr
<AwContentsClientBridge
> contents_client_bridge_
;
251 scoped_ptr
<content::WebContents
> web_contents_
;
252 scoped_ptr
<AwRenderViewHostExt
> render_view_host_ext_
;
253 scoped_ptr
<FindHelper
> find_helper_
;
254 scoped_ptr
<IconHelper
> icon_helper_
;
255 scoped_ptr
<AwContents
> pending_contents_
;
256 BrowserViewRenderer browser_view_renderer_
;
257 scoped_ptr
<AwPdfExporter
> pdf_exporter_
;
258 scoped_ptr
<PermissionRequestHandler
> permission_request_handler_
;
259 scoped_refptr
<AwMessagePortMessageFilter
> message_port_message_filter_
;
261 // GURL is supplied by the content layer as requesting frame.
262 // Callback is supplied by the content layer, and is invoked with the result
263 // from the permission prompt.
264 typedef std::pair
<const GURL
, base::Callback
<void(bool)> > OriginCallback
;
265 // The first element in the list is always the currently pending request.
266 std::list
<OriginCallback
> pending_geolocation_prompts_
;
268 GLViewRendererManager::Key renderer_manager_key_
;
270 DISALLOW_COPY_AND_ASSIGN(AwContents
);
273 bool RegisterAwContents(JNIEnv
* env
);
275 } // namespace android_webview
277 #endif // ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_