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