Upstream version 5.34.104.0
[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/browser_view_renderer.h"
14 #include "android_webview/browser/find_helper.h"
15 #include "android_webview/browser/icon_helper.h"
16 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
17 #include "base/android/scoped_java_ref.h"
18 #include "base/android/jni_helper.h"
19 #include "base/callback_forward.h"
20 #include "base/memory/scoped_ptr.h"
21
22 class SkBitmap;
23 class TabContents;
24
25 namespace content {
26 class WebContents;
27 }
28
29 namespace android_webview {
30
31 class AwContentsContainer;
32 class AwContentsClientBridge;
33 class AwPdfExporter;
34 class AwWebContentsDelegate;
35
36 // Native side of java-class of same name.
37 // Provides the ownership of and access to browser components required for
38 // WebView functionality; analogous to chrome's TabContents, but with a
39 // level of indirection provided by the AwContentsContainer abstraction.
40 //
41 // Object lifetime:
42 // For most purposes the java and native objects can be considered to have
43 // 1:1 lifetime and relationship. The exception is the java instance that
44 // hosts a popup will be rebound to a second native instance (carrying the
45 // popup content) and discard the 'default' native instance it made on
46 // construction. A native instance is only bound to at most one Java peer over
47 // its entire lifetime - see Init() and SetPendingWebContentsForPopup() for the
48 // construction points, and SetJavaPeers() where these paths join.
49 class AwContents : public FindHelper::Listener,
50                    public IconHelper::Listener,
51                    public AwRenderViewHostExtClient,
52                    public BrowserViewRenderer::Client {
53  public:
54   // Returns the AwContents instance associated with |web_contents|, or NULL.
55   static AwContents* FromWebContents(content::WebContents* web_contents);
56
57   // Returns the AwContents instance associated with with the given
58   // render_process_id and render_view_id, or NULL.
59   static AwContents* FromID(int render_process_id, int render_view_id);
60
61   AwContents(scoped_ptr<content::WebContents> web_contents);
62   virtual ~AwContents();
63
64   AwRenderViewHostExt* render_view_host_ext() {
65     return render_view_host_ext_.get();
66   }
67
68   // |handler| is an instance of
69   // org.chromium.android_webview.AwHttpAuthHandler.
70   bool OnReceivedHttpAuthRequest(const base::android::JavaRef<jobject>& handler,
71                                  const std::string& host,
72                                  const std::string& realm);
73
74   // Methods called from Java.
75   void SetJavaPeers(JNIEnv* env,
76                     jobject obj,
77                     jobject aw_contents,
78                     jobject web_contents_delegate,
79                     jobject contents_client_bridge,
80                     jobject io_thread_client,
81                     jobject intercept_navigation_delegate);
82   jint GetWebContents(JNIEnv* env, jobject obj);
83
84   void Destroy(JNIEnv* env, jobject obj);
85   void DocumentHasImages(JNIEnv* env, jobject obj, jobject message);
86   void GenerateMHTML(JNIEnv* env, jobject obj, jstring jpath, jobject callback);
87   void CreatePdfExporter(JNIEnv* env, jobject obj, jobject pdfExporter);
88   void AddVisitedLinks(JNIEnv* env, jobject obj, jobjectArray jvisited_links);
89   base::android::ScopedJavaLocalRef<jbyteArray> GetCertificate(
90       JNIEnv* env, jobject obj);
91   void RequestNewHitTestDataAt(JNIEnv* env, jobject obj, jint x, jint y);
92   void UpdateLastHitTestData(JNIEnv* env, jobject obj);
93   void OnSizeChanged(JNIEnv* env, jobject obj, int w, int h, int ow, int oh);
94   void SetViewVisibility(JNIEnv* env, jobject obj, bool visible);
95   void SetWindowVisibility(JNIEnv* env, jobject obj, bool visible);
96   void SetIsPaused(JNIEnv* env, jobject obj, bool paused);
97   void OnAttachedToWindow(JNIEnv* env, jobject obj, int w, int h);
98   void OnDetachedFromWindow(JNIEnv* env, jobject obj);
99   base::android::ScopedJavaLocalRef<jbyteArray> GetOpaqueState(
100       JNIEnv* env, jobject obj);
101   jboolean RestoreFromOpaqueState(JNIEnv* env, jobject obj, jbyteArray state);
102   void FocusFirstNode(JNIEnv* env, jobject obj);
103   void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
104   bool OnDraw(JNIEnv* env,
105               jobject obj,
106               jobject canvas,
107               jboolean is_hardware_accelerated,
108               jint scroll_x,
109               jint scroll_y,
110               jint clip_left,
111               jint clip_top,
112               jint clip_right,
113               jint clip_bottom);
114   void SetGlobalVisibleRect(JNIEnv* env,
115                             jobject obj,
116                             jint visible_left,
117                             jint visible_top,
118                             jint visible_right,
119                             jint visible_bottom);
120   jint GetAwDrawGLViewContext(JNIEnv* env, jobject obj);
121   jlong CapturePicture(JNIEnv* env, jobject obj, int width, int height);
122   void EnableOnNewPicture(JNIEnv* env, jobject obj, jboolean enabled);
123   void ClearView(JNIEnv* env, jobject obj);
124   void SetExtraHeadersForUrl(JNIEnv* env, jobject obj,
125                              jstring url, jstring extra_headers);
126
127   // Geolocation API support
128   void ShowGeolocationPrompt(const GURL& origin, base::Callback<void(bool)>);
129   void HideGeolocationPrompt(const GURL& origin);
130   void InvokeGeolocationCallback(JNIEnv* env,
131                                  jobject obj,
132                                  jboolean value,
133                                  jstring origin);
134
135   // Find-in-page API and related methods.
136   void FindAllAsync(JNIEnv* env, jobject obj, jstring search_string);
137   void FindNext(JNIEnv* env, jobject obj, jboolean forward);
138   void ClearMatches(JNIEnv* env, jobject obj);
139   FindHelper* GetFindHelper();
140
141   // FindHelper::Listener implementation.
142   virtual void OnFindResultReceived(int active_ordinal,
143                                     int match_count,
144                                     bool finished) OVERRIDE;
145   // IconHelper::Listener implementation.
146   virtual bool ShouldDownloadFavicon(const GURL& icon_url) OVERRIDE;
147   virtual void OnReceivedIcon(const GURL& icon_url,
148                               const SkBitmap& bitmap) OVERRIDE;
149   virtual void OnReceivedTouchIconUrl(const std::string& url,
150                                       const bool precomposed) OVERRIDE;
151
152   // AwRenderViewHostExtClient implementation.
153   virtual void OnWebLayoutPageScaleFactorChanged(
154       float page_scale_factor) OVERRIDE;
155   virtual void OnWebLayoutContentsSizeChanged(
156       const gfx::Size& contents_size) OVERRIDE;
157
158   // BrowserViewRenderer::Client implementation.
159   virtual bool RequestDrawGL(jobject canvas) OVERRIDE;
160   virtual void PostInvalidate() OVERRIDE;
161   virtual void UpdateGlobalVisibleRect() OVERRIDE;
162   virtual void OnNewPicture() OVERRIDE;
163   virtual gfx::Point GetLocationOnScreen() OVERRIDE;
164   virtual void SetMaxContainerViewScrollOffset(
165       gfx::Vector2d new_value) OVERRIDE;
166   virtual void ScrollContainerViewTo(gfx::Vector2d new_value) OVERRIDE;
167   virtual bool IsFlingActive() const OVERRIDE;
168   virtual void SetPageScaleFactorAndLimits(
169       float page_scale_factor,
170       float min_page_scale_factor,
171       float max_page_scale_factor) OVERRIDE;
172   virtual void SetContentsSize(gfx::SizeF contents_size_dip) OVERRIDE;
173   virtual void DidOverscroll(gfx::Vector2d overscroll_delta) OVERRIDE;
174
175   void ClearCache(JNIEnv* env, jobject obj, jboolean include_disk_files);
176   void SetPendingWebContentsForPopup(scoped_ptr<content::WebContents> pending);
177   jint ReleasePopupAwContents(JNIEnv* env, jobject obj);
178
179   void ScrollTo(JNIEnv* env, jobject obj, jint x, jint y);
180   void SetDipScale(JNIEnv* env, jobject obj, jfloat dip_scale);
181   void SetFixedLayoutSize(JNIEnv* env,
182                           jobject obj,
183                           jint width_dip,
184                           jint height_dip);
185   void SetSaveFormData(bool enabled);
186
187   // Sets the java delegate
188   void SetAwAutofillManagerDelegate(jobject delegate);
189
190   void SetJsOnlineProperty(JNIEnv* env, jobject obj, jboolean network_up);
191   void TrimMemory(JNIEnv* env, jobject obj, jint level);
192
193  private:
194   void InitAutofillIfNecessary(bool enabled);
195   void SetAndroidWebViewRendererPrefs();
196
197   JavaObjectWeakGlobalRef java_ref_;
198   scoped_ptr<content::WebContents> web_contents_;
199   scoped_ptr<AwWebContentsDelegate> web_contents_delegate_;
200   scoped_ptr<AwContentsClientBridge> contents_client_bridge_;
201   scoped_ptr<AwRenderViewHostExt> render_view_host_ext_;
202   scoped_ptr<FindHelper> find_helper_;
203   scoped_ptr<IconHelper> icon_helper_;
204   scoped_ptr<AwContents> pending_contents_;
205   scoped_ptr<BrowserViewRenderer> browser_view_renderer_;
206   scoped_ptr<AwPdfExporter> pdf_exporter_;
207
208   // GURL is supplied by the content layer as requesting frame.
209   // Callback is supplied by the content layer, and is invoked with the result
210   // from the permission prompt.
211   typedef std::pair<const GURL, base::Callback<void(bool)> > OriginCallback;
212   // The first element in the list is always the currently pending request.
213   std::list<OriginCallback> pending_geolocation_prompts_;
214
215   DISALLOW_COPY_AND_ASSIGN(AwContents);
216 };
217
218 bool RegisterAwContents(JNIEnv* env);
219
220 }  // namespace android_webview
221
222 #endif  // ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_