Home | History | Annotate | Download | only in native
      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 SetExtraHeadersForUrl(JNIEnv* env, jobject obj,
    124                              jstring url, jstring extra_headers);
    125 
    126   // Geolocation API support
    127   void ShowGeolocationPrompt(const GURL& origin, base::Callback<void(bool)>);
    128   void HideGeolocationPrompt(const GURL& origin);
    129   void InvokeGeolocationCallback(JNIEnv* env,
    130                                  jobject obj,
    131                                  jboolean value,
    132                                  jstring origin);
    133 
    134   // Find-in-page API and related methods.
    135   void FindAllAsync(JNIEnv* env, jobject obj, jstring search_string);
    136   void FindNext(JNIEnv* env, jobject obj, jboolean forward);
    137   void ClearMatches(JNIEnv* env, jobject obj);
    138   FindHelper* GetFindHelper();
    139 
    140   // FindHelper::Listener implementation.
    141   virtual void OnFindResultReceived(int active_ordinal,
    142                                     int match_count,
    143                                     bool finished) OVERRIDE;
    144   // IconHelper::Listener implementation.
    145   virtual bool ShouldDownloadFavicon(const GURL& icon_url) OVERRIDE;
    146   virtual void OnReceivedIcon(const GURL& icon_url,
    147                               const SkBitmap& bitmap) OVERRIDE;
    148   virtual void OnReceivedTouchIconUrl(const std::string& url,
    149                                       const bool precomposed) OVERRIDE;
    150 
    151   // AwRenderViewHostExtClient implementation.
    152   virtual void OnWebLayoutPageScaleFactorChanged(
    153       float page_scale_factor) OVERRIDE;
    154   virtual void OnWebLayoutContentsSizeChanged(
    155       const gfx::Size& contents_size) OVERRIDE;
    156 
    157   // BrowserViewRenderer::Client implementation.
    158   virtual bool RequestDrawGL(jobject canvas) OVERRIDE;
    159   virtual void PostInvalidate() OVERRIDE;
    160   virtual void UpdateGlobalVisibleRect() OVERRIDE;
    161   virtual void OnNewPicture() OVERRIDE;
    162   virtual gfx::Point GetLocationOnScreen() OVERRIDE;
    163   virtual void SetMaxContainerViewScrollOffset(
    164       gfx::Vector2d new_value) OVERRIDE;
    165   virtual void ScrollContainerViewTo(gfx::Vector2d new_value) OVERRIDE;
    166   virtual bool IsFlingActive() const OVERRIDE;
    167   virtual void SetPageScaleFactor(float page_scale_factor) OVERRIDE;
    168   virtual void SetContentsSize(gfx::SizeF contents_size_dip) OVERRIDE;
    169   virtual void DidOverscroll(gfx::Vector2d overscroll_delta) OVERRIDE;
    170 
    171   void ClearCache(JNIEnv* env, jobject obj, jboolean include_disk_files);
    172   void SetPendingWebContentsForPopup(scoped_ptr<content::WebContents> pending);
    173   jint ReleasePopupAwContents(JNIEnv* env, jobject obj);
    174 
    175   void ScrollTo(JNIEnv* env, jobject obj, jint x, jint y);
    176   void SetDipScale(JNIEnv* env, jobject obj, jfloat dip_scale);
    177   void SetFixedLayoutSize(JNIEnv* env,
    178                           jobject obj,
    179                           jint width_dip,
    180                           jint height_dip);
    181   void SetSaveFormData(bool enabled);
    182 
    183   // Sets the java delegate
    184   void SetAwAutofillManagerDelegate(jobject delegate);
    185 
    186   void SetJsOnlineProperty(JNIEnv* env, jobject obj, jboolean network_up);
    187   void TrimMemory(JNIEnv* env, jobject obj, jint level);
    188 
    189  private:
    190   void InitAutofillIfNecessary(bool enabled);
    191   void SetAndroidWebViewRendererPrefs();
    192 
    193   JavaObjectWeakGlobalRef java_ref_;
    194   scoped_ptr<content::WebContents> web_contents_;
    195   scoped_ptr<AwWebContentsDelegate> web_contents_delegate_;
    196   scoped_ptr<AwContentsClientBridge> contents_client_bridge_;
    197   scoped_ptr<AwRenderViewHostExt> render_view_host_ext_;
    198   scoped_ptr<FindHelper> find_helper_;
    199   scoped_ptr<IconHelper> icon_helper_;
    200   scoped_ptr<AwContents> pending_contents_;
    201   scoped_ptr<BrowserViewRenderer> browser_view_renderer_;
    202   scoped_ptr<AwPdfExporter> pdf_exporter_;
    203 
    204   // GURL is supplied by the content layer as requesting frame.
    205   // Callback is supplied by the content layer, and is invoked with the result
    206   // from the permission prompt.
    207   typedef std::pair<const GURL, base::Callback<void(bool)> > OriginCallback;
    208   // The first element in the list is always the currently pending request.
    209   std::list<OriginCallback> pending_geolocation_prompts_;
    210 
    211   DISALLOW_COPY_AND_ASSIGN(AwContents);
    212 };
    213 
    214 bool RegisterAwContents(JNIEnv* env);
    215 
    216 }  // namespace android_webview
    217 
    218 #endif  // ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_H_
    219