Home | History | Annotate | Download | only in web_contents_delegate_android
      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 COMPONENTS_WEB_CONTENTS_DELEGATE_ANDROID_WEB_CONTENTS_DELEGATE_ANDROID_H_
      6 #define COMPONENTS_WEB_CONTENTS_DELEGATE_ANDROID_WEB_CONTENTS_DELEGATE_ANDROID_H_
      7 
      8 #include "base/android/jni_helper.h"
      9 #include "base/android/scoped_java_ref.h"
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "content/public/browser/web_contents_delegate.h"
     13 #include "ui/gfx/vector2d.h"
     14 
     15 class GURL;
     16 
     17 namespace content {
     18 class WebContents;
     19 class WebContentsDelegate;
     20 struct NativeWebKeyboardEvent;
     21 struct OpenURLParams;
     22 }
     23 
     24 namespace web_contents_delegate_android {
     25 
     26 enum WebContentsDelegateLogLevel {
     27   // Equivalent of WebCore::WebConsoleMessage::LevelDebug.
     28   WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG = 0,
     29   // Equivalent of WebCore::WebConsoleMessage::LevelLog.
     30   WEB_CONTENTS_DELEGATE_LOG_LEVEL_LOG = 1,
     31   // Equivalent of WebCore::WebConsoleMessage::LevelWarning.
     32   WEB_CONTENTS_DELEGATE_LOG_LEVEL_WARNING = 2,
     33   // Equivalent of WebCore::WebConsoleMessage::LevelError.
     34   WEB_CONTENTS_DELEGATE_LOG_LEVEL_ERROR = 3,
     35 };
     36 
     37 
     38 // Native underpinnings of WebContentsDelegateAndroid.java. Provides a default
     39 // delegate for WebContents to forward calls to the java peer. The embedding
     40 // application may subclass and override methods on either the C++ or Java side
     41 // as required.
     42 class WebContentsDelegateAndroid : public content::WebContentsDelegate {
     43  public:
     44   WebContentsDelegateAndroid(JNIEnv* env, jobject obj);
     45   virtual ~WebContentsDelegateAndroid();
     46 
     47   // Binds this WebContentsDelegateAndroid to the passed WebContents instance,
     48   // such that when that WebContents is destroyed, this
     49   // WebContentsDelegateAndroid instance will be destroyed too.
     50   void SetOwnerWebContents(content::WebContents* contents);
     51 
     52   // Overridden from WebContentsDelegate:
     53   virtual content::WebContents* OpenURLFromTab(
     54       content::WebContents* source,
     55       const content::OpenURLParams& params) OVERRIDE;
     56   virtual content::ColorChooser* OpenColorChooser(content::WebContents* source,
     57                                                   SkColor color) OVERRIDE;
     58   virtual void NavigationStateChanged(const content::WebContents* source,
     59                                       unsigned changed_flags) OVERRIDE;
     60   virtual void AddNewContents(content::WebContents* source,
     61                               content::WebContents* new_contents,
     62                               WindowOpenDisposition disposition,
     63                               const gfx::Rect& initial_pos,
     64                               bool user_gesture,
     65                               bool* was_blocked) OVERRIDE;
     66   virtual void ActivateContents(content::WebContents* contents) OVERRIDE;
     67   virtual void DeactivateContents(content::WebContents* contents) OVERRIDE;
     68   virtual void LoadingStateChanged(content::WebContents* source) OVERRIDE;
     69   virtual void LoadProgressChanged(content::WebContents* source,
     70                                    double load_progress) OVERRIDE;
     71   virtual void RendererUnresponsive(content::WebContents* source) OVERRIDE;
     72   virtual void RendererResponsive(content::WebContents* source) OVERRIDE;
     73   virtual void CloseContents(content::WebContents* source) OVERRIDE;
     74   virtual void MoveContents(content::WebContents* source,
     75                             const gfx::Rect& pos) OVERRIDE;
     76   virtual bool AddMessageToConsole(content::WebContents* source,
     77                                    int32 level,
     78                                    const base::string16& message,
     79                                    int32 line_no,
     80                                    const base::string16& source_id) OVERRIDE;
     81   virtual void UpdateTargetURL(content::WebContents* source,
     82                                int32 page_id,
     83                                const GURL& url) OVERRIDE;
     84   virtual void HandleKeyboardEvent(
     85       content::WebContents* source,
     86       const content::NativeWebKeyboardEvent& event) OVERRIDE;
     87   virtual bool TakeFocus(content::WebContents* source, bool reverse) OVERRIDE;
     88   virtual void ShowRepostFormWarningDialog(
     89       content::WebContents* source) OVERRIDE;
     90   virtual void ToggleFullscreenModeForTab(content::WebContents* web_contents,
     91                                           bool enter_fullscreen) OVERRIDE;
     92   virtual bool IsFullscreenForTabOrPending(
     93       const content::WebContents* web_contents) const OVERRIDE;
     94   virtual void DidProgrammaticallyScroll(
     95       content::WebContents* web_contents,
     96       const gfx::Vector2d& scroll_point) OVERRIDE;
     97 
     98  protected:
     99   base::android::ScopedJavaLocalRef<jobject> GetJavaDelegate(JNIEnv* env) const;
    100 
    101  private:
    102   // We depend on the java side user of WebContentDelegateAndroid to hold a
    103   // strong reference to that object as long as they want to receive callbacks
    104   // on it. Using a weak ref here allows it to be correctly GCed.
    105   JavaObjectWeakGlobalRef weak_java_delegate_;
    106 };
    107 
    108 bool RegisterWebContentsDelegateAndroid(JNIEnv* env);
    109 
    110 }  // namespace web_contents_delegate_android
    111 
    112 #endif  // COMPONENTS_WEB_CONTENTS_DELEGATE_ANDROID_WEB_CONTENTS_DELEGATE_ANDROID_H_
    113