ae6701bb65e9b3410e182ca659ec63e00ae9f2ae
[platform/framework/web/crosswalk.git] / src / android_webview / native / aw_contents.h
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
5 #ifndef ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_
6 #define ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_
7
8 #include <jni.h>
9 #include <list>
10 #include <string>
11 #include <utility>
12
13 #include "android_webview/browser/aw_browser_permission_request_delegate.h"
14 #include "android_webview/browser/browser_view_renderer.h"
15 #include "android_webview/browser/browser_view_renderer_client.h"
16 #include "android_webview/browser/find_helper.h"
17 #include "android_webview/browser/gl_view_renderer_manager.h"
18 #include "android_webview/browser/icon_helper.h"
19 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
20 #include "android_webview/browser/shared_renderer_state.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"
26
27 class SkBitmap;
28 class TabContents;
29 struct AwDrawGLInfo;
30
31 namespace content {
32 class WebContents;
33 }
34
35 namespace android_webview {
36
37 class AwContentsContainer;
38 class AwContentsClientBridge;
39 class AwPdfExporter;
40 class AwWebContentsDelegate;
41 class HardwareRenderer;
42 class PermissionRequestHandler;
43
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.
48 //
49 // Object lifetime:
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 {
63  public:
64   // Returns the AwContents instance associated with |web_contents|, or NULL.
65   static AwContents* FromWebContents(content::WebContents* web_contents);
66
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);
70
71   AwContents(scoped_ptr<content::WebContents> web_contents);
72   virtual ~AwContents();
73
74   AwRenderViewHostExt* render_view_host_ext() {
75     return render_view_host_ext_.get();
76   }
77
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);
83
84   // Methods called from Java.
85   void SetJavaPeers(JNIEnv* env,
86                     jobject obj,
87                     jobject aw_contents,
88                     jobject web_contents_delegate,
89                     jobject contents_client_bridge,
90                     jobject io_thread_client,
91                     jobject intercept_navigation_delegate);
92   jlong GetWebContents(JNIEnv* env, jobject obj);
93
94   void Destroy(JNIEnv* env, jobject obj);
95   void DocumentHasImages(JNIEnv* env, jobject obj, jobject message);
96   void GenerateMHTML(JNIEnv* env, jobject obj, jstring jpath, jobject callback);
97   void CreatePdfExporter(JNIEnv* env, jobject obj, jobject pdfExporter);
98   void AddVisitedLinks(JNIEnv* env, jobject obj, jobjectArray jvisited_links);
99   base::android::ScopedJavaLocalRef<jbyteArray> GetCertificate(
100       JNIEnv* env, jobject obj);
101   void RequestNewHitTestDataAt(JNIEnv* env, jobject obj, jint x, jint y);
102   void UpdateLastHitTestData(JNIEnv* env, jobject obj);
103   void OnSizeChanged(JNIEnv* env, jobject obj, int w, int h, int ow, int oh);
104   void SetViewVisibility(JNIEnv* env, jobject obj, bool visible);
105   void SetWindowVisibility(JNIEnv* env, jobject obj, bool visible);
106   void SetIsPaused(JNIEnv* env, jobject obj, bool paused);
107   void OnAttachedToWindow(JNIEnv* env, jobject obj, int w, int h);
108   void OnDetachedFromWindow(JNIEnv* env, jobject obj);
109   base::android::ScopedJavaLocalRef<jbyteArray> GetOpaqueState(
110       JNIEnv* env, jobject obj);
111   jboolean RestoreFromOpaqueState(JNIEnv* env, jobject obj, jbyteArray state);
112   void FocusFirstNode(JNIEnv* env, jobject obj);
113   void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
114   bool OnDraw(JNIEnv* env,
115               jobject obj,
116               jobject canvas,
117               jboolean is_hardware_accelerated,
118               jint scroll_x,
119               jint scroll_y,
120               jint visible_left,
121               jint visible_top,
122               jint visible_right,
123               jint visible_bottom,
124               jint clip_left,
125               jint clip_top,
126               jint clip_right,
127               jint clip_bottom);
128   jlong GetAwDrawGLViewContext(JNIEnv* env, jobject obj);
129   jlong CapturePicture(JNIEnv* env, jobject obj, int width, int height);
130   void EnableOnNewPicture(JNIEnv* env, jobject obj, jboolean enabled);
131   void ClearView(JNIEnv* env, jobject obj);
132   void SetExtraHeadersForUrl(JNIEnv* env, jobject obj,
133                              jstring url, jstring extra_headers);
134   void SendCheckRenderThreadResponsiveness(JNIEnv* env, jobject obj);
135
136   void DrawGL(AwDrawGLInfo* draw_info);
137
138   void InvokeGeolocationCallback(JNIEnv* env,
139                                  jobject obj,
140                                  jboolean value,
141                                  jstring origin);
142
143   // PermissionRequestHandlerClient implementation.
144   virtual void OnPermissionRequest(AwPermissionRequest* request) OVERRIDE;
145   virtual void OnPermissionRequestCanceled(
146       AwPermissionRequest* request) OVERRIDE;
147
148   PermissionRequestHandler* GetPermissionRequestHandler() {
149     return permission_request_handler_.get();
150   }
151
152   void PreauthorizePermission(JNIEnv* env,
153                               jobject obj,
154                               jstring origin,
155                               jlong resources);
156
157   // AwBrowserPermissionRequestDelegate implementation.
158   virtual void RequestProtectedMediaIdentifierPermission(
159       const GURL& origin,
160       const base::Callback<void(bool)>& callback) OVERRIDE;
161   virtual void CancelProtectedMediaIdentifierPermissionRequests(
162       const GURL& origin) OVERRIDE;
163   virtual void RequestGeolocationPermission(
164       const GURL& origin,
165       const base::Callback<void(bool)>& callback) OVERRIDE;
166   virtual void CancelGeolocationPermissionRequests(
167       const GURL& origin) OVERRIDE;
168
169
170   // Find-in-page API and related methods.
171   void FindAllAsync(JNIEnv* env, jobject obj, jstring search_string);
172   void FindNext(JNIEnv* env, jobject obj, jboolean forward);
173   void ClearMatches(JNIEnv* env, jobject obj);
174   FindHelper* GetFindHelper();
175
176   // Per WebView Cookie Policy
177   bool AllowThirdPartyCookies();
178
179   // FindHelper::Listener implementation.
180   virtual void OnFindResultReceived(int active_ordinal,
181                                     int match_count,
182                                     bool finished) OVERRIDE;
183   // IconHelper::Listener implementation.
184   virtual bool ShouldDownloadFavicon(const GURL& icon_url) OVERRIDE;
185   virtual void OnReceivedIcon(const GURL& icon_url,
186                               const SkBitmap& bitmap) OVERRIDE;
187   virtual void OnReceivedTouchIconUrl(const std::string& url,
188                                       const bool precomposed) OVERRIDE;
189
190   // AwRenderViewHostExtClient implementation.
191   virtual void OnWebLayoutPageScaleFactorChanged(
192       float page_scale_factor) OVERRIDE;
193   virtual void OnWebLayoutContentsSizeChanged(
194       const gfx::Size& contents_size) OVERRIDE;
195
196   // BrowserViewRendererClient implementation.
197   virtual bool RequestDrawGL(jobject canvas, bool wait_for_completion) OVERRIDE;
198   virtual void PostInvalidate() OVERRIDE;
199   virtual void OnNewPicture() OVERRIDE;
200   virtual gfx::Point GetLocationOnScreen() OVERRIDE;
201   virtual void ScrollContainerViewTo(gfx::Vector2d new_value) OVERRIDE;
202   virtual bool IsFlingActive() const OVERRIDE;
203   virtual void UpdateScrollState(gfx::Vector2d max_scroll_offset,
204                                  gfx::SizeF contents_size_dip,
205                                  float page_scale_factor,
206                                  float min_page_scale_factor,
207                                  float max_page_scale_factor) OVERRIDE;
208   virtual void DidOverscroll(gfx::Vector2d overscroll_delta) OVERRIDE;
209
210   const BrowserViewRenderer* GetBrowserViewRenderer() const;
211
212   void ClearCache(JNIEnv* env, jobject obj, jboolean include_disk_files);
213   void SetPendingWebContentsForPopup(scoped_ptr<content::WebContents> pending);
214   jlong ReleasePopupAwContents(JNIEnv* env, jobject obj);
215
216   void ScrollTo(JNIEnv* env, jobject obj, jint x, jint y);
217   void SetDipScale(JNIEnv* env, jobject obj, jfloat dip_scale);
218   void SetFixedLayoutSize(JNIEnv* env,
219                           jobject obj,
220                           jint width_dip,
221                           jint height_dip);
222   void SetSaveFormData(bool enabled);
223
224   // Sets the java client
225   void SetAwAutofillClient(jobject client);
226
227   void SetJsOnlineProperty(JNIEnv* env, jobject obj, jboolean network_up);
228   void TrimMemory(JNIEnv* env, jobject obj, jint level, jboolean visible);
229
230  private:
231   void InitDataReductionProxyIfNecessary();
232   void InitAutofillIfNecessary(bool enabled);
233
234   void InitializeHardwareDrawIfNeeded();
235
236   // Geolocation API support
237   void ShowGeolocationPrompt(const GURL& origin, base::Callback<void(bool)>);
238   void HideGeolocationPrompt(const GURL& origin);
239
240   JavaObjectWeakGlobalRef java_ref_;
241   scoped_ptr<content::WebContents> web_contents_;
242   scoped_ptr<AwWebContentsDelegate> web_contents_delegate_;
243   scoped_ptr<AwContentsClientBridge> contents_client_bridge_;
244   scoped_ptr<AwRenderViewHostExt> render_view_host_ext_;
245   scoped_ptr<FindHelper> find_helper_;
246   scoped_ptr<IconHelper> icon_helper_;
247   scoped_ptr<AwContents> pending_contents_;
248   SharedRendererState shared_renderer_state_;
249   BrowserViewRenderer browser_view_renderer_;
250   scoped_ptr<HardwareRenderer> hardware_renderer_;
251   scoped_ptr<AwPdfExporter> pdf_exporter_;
252   scoped_ptr<PermissionRequestHandler> permission_request_handler_;
253
254   // GURL is supplied by the content layer as requesting frame.
255   // Callback is supplied by the content layer, and is invoked with the result
256   // from the permission prompt.
257   typedef std::pair<const GURL, base::Callback<void(bool)> > OriginCallback;
258   // The first element in the list is always the currently pending request.
259   std::list<OriginCallback> pending_geolocation_prompts_;
260
261   base::Lock render_thread_lock_;
262   GLViewRendererManager::Key renderer_manager_key_;
263
264   DISALLOW_COPY_AND_ASSIGN(AwContents);
265 };
266
267 bool RegisterAwContents(JNIEnv* env);
268
269 }  // namespace android_webview
270
271 #endif  // ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_