Home | History | Annotate | Download | only in webview
      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 UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
      6 #define UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "content/public/browser/web_contents_delegate.h"
     11 #include "content/public/browser/web_contents_observer.h"
     12 #include "ui/views/accessibility/native_view_accessibility.h"
     13 #include "ui/views/controls/webview/webview_export.h"
     14 #include "ui/views/view.h"
     15 
     16 namespace content {
     17 class SiteInstance;
     18 }
     19 
     20 namespace views {
     21 
     22 class NativeViewHost;
     23 
     24 class WEBVIEW_EXPORT WebView : public View,
     25                                public content::WebContentsDelegate,
     26                                public content::WebContentsObserver {
     27  public:
     28   static const char kViewClassName[];
     29 
     30   explicit WebView(content::BrowserContext* browser_context);
     31   virtual ~WebView();
     32 
     33   // This creates a WebContents if none is yet associated with this WebView. The
     34   // WebView owns this implicitly created WebContents.
     35   content::WebContents* GetWebContents();
     36 
     37   // Creates a WebContents if none is yet assocaited with this WebView, with the
     38   // specified site instance. The WebView owns this WebContents.
     39   void CreateWebContentsWithSiteInstance(content::SiteInstance* site_instance);
     40 
     41   // WebView does not assume ownership of WebContents set via this method, only
     42   // those it implicitly creates via GetWebContents() above.
     43   void SetWebContents(content::WebContents* web_contents);
     44 
     45   // If |mode| is true, WebView will register itself with WebContents as a
     46   // WebContentsObserver, monitor for the showing/destruction of fullscreen
     47   // render widgets, and alter its child view hierarchy to embed the fullscreen
     48   // widget or restore the normal WebContentsView.
     49   void SetEmbedFullscreenWidgetMode(bool mode);
     50 
     51   content::WebContents* web_contents() { return web_contents_; }
     52 
     53   content::BrowserContext* browser_context() { return browser_context_; }
     54 
     55   // Loads the initial URL to display in the attached WebContents. Creates the
     56   // WebContents if none is attached yet. Note that this is intended as a
     57   // convenience for loading the initial URL, and so URLs are navigated with
     58   // PAGE_TRANSITION_AUTO_TOPLEVEL, so this is not intended as a general purpose
     59   // navigation method - use WebContents' API directly.
     60   void LoadInitialURL(const GURL& url);
     61 
     62   // Controls how the attached WebContents is resized.
     63   // false = WebContents' views' bounds are updated continuously as the
     64   //         WebView's bounds change (default).
     65   // true  = WebContents' views' position is updated continuously but its size
     66   //         is not (which may result in some clipping or under-painting) until
     67   //         a continuous size operation completes. This allows for smoother
     68   //         resizing performance during interactive resizes and animations.
     69   void SetFastResize(bool fast_resize);
     70 
     71   // Called when the WebContents is focused.
     72   // TODO(beng): This view should become a WebContentsViewObserver when a
     73   //             WebContents is attached, and not rely on the delegate to
     74   //             forward this notification.
     75   void OnWebContentsFocused(content::WebContents* web_contents);
     76 
     77   // When used to host UI, we need to explicitly allow accelerators to be
     78   // processed. Default is false.
     79   void set_allow_accelerators(bool allow_accelerators) {
     80     allow_accelerators_ = allow_accelerators;
     81   }
     82 
     83   // Sets the preferred size. If empty, View's implementation of
     84   // GetPreferredSize() is used.
     85   void SetPreferredSize(const gfx::Size& preferred_size);
     86 
     87   // Overridden from View:
     88   virtual const char* GetClassName() const OVERRIDE;
     89 
     90  private:
     91   // Overridden from View:
     92   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
     93   virtual void ViewHierarchyChanged(
     94       const ViewHierarchyChangedDetails& details) OVERRIDE;
     95   virtual bool SkipDefaultKeyEventProcessing(
     96       const ui::KeyEvent& event) OVERRIDE;
     97   virtual bool IsFocusable() const OVERRIDE;
     98   virtual void OnFocus() OVERRIDE;
     99   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
    100   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
    101   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
    102   virtual gfx::Size GetPreferredSize() OVERRIDE;
    103 
    104   // Overridden from content::WebContentsDelegate:
    105   virtual void WebContentsFocused(content::WebContents* web_contents) OVERRIDE;
    106   virtual bool EmbedsFullscreenWidget() const OVERRIDE;
    107 
    108   // Overridden from content::WebContentsObserver:
    109   virtual void RenderViewHostChanged(
    110       content::RenderViewHost* old_host,
    111       content::RenderViewHost* new_host) OVERRIDE;
    112   virtual void WebContentsDestroyed(
    113       content::WebContents* web_contents) OVERRIDE;
    114   virtual void DidShowFullscreenWidget(int routing_id) OVERRIDE;
    115   virtual void DidDestroyFullscreenWidget(int routing_id) OVERRIDE;
    116   // Workaround for MSVC++ linker bug/feature that requires
    117   // instantiation of the inline IPC::Listener methods in all translation units.
    118   virtual void OnChannelConnected(int32 peer_id) OVERRIDE {}
    119   virtual void OnChannelError() OVERRIDE {}
    120 
    121   void AttachWebContents();
    122   void DetachWebContents();
    123   void ReattachForFullscreenChange(bool enter_fullscreen);
    124 
    125   // Create a regular or test web contents (based on whether we're running
    126   // in a unit test or not).
    127   content::WebContents* CreateWebContents(
    128       content::BrowserContext* browser_context,
    129       content::SiteInstance* site_instance);
    130 
    131   NativeViewHost* wcv_holder_;
    132   scoped_ptr<content::WebContents> wc_owner_;
    133   content::WebContents* web_contents_;
    134   // When true, WebView auto-embeds fullscreen widgets as a child view.
    135   bool embed_fullscreen_widget_mode_enabled_;
    136   // Set to true while WebView is embedding a fullscreen widget view as a child
    137   // view instead of the normal WebContentsView render view.
    138   bool is_embedding_fullscreen_widget_;
    139   content::BrowserContext* browser_context_;
    140   bool allow_accelerators_;
    141   gfx::Size preferred_size_;
    142 
    143   DISALLOW_COPY_AND_ASSIGN(WebView);
    144 };
    145 
    146 }  // namespace views
    147 
    148 #endif  // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
    149