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 views {
     17 
     18 class NativeViewHost;
     19 
     20 // Provides a view of a WebContents instance.  WebView can be used standalone,
     21 // creating and displaying an internally-owned WebContents; or within a full
     22 // browser where the browser swaps its own WebContents instances in/out (e.g.,
     23 // for browser tabs).
     24 //
     25 // WebView creates and owns a single child view, a NativeViewHost, which will
     26 // hold and display the native view provided by a WebContents.
     27 //
     28 // EmbedFullscreenWidgetMode: When enabled, WebView will observe for WebContents
     29 // fullscreen changes and automatically swap the normal native view with the
     30 // fullscreen native view (if different).  In addition, if the WebContents is
     31 // being screen-captured, the view will be centered within WebView, sized to
     32 // the aspect ratio of the capture video resolution, and scaling will be avoided
     33 // whenever possible.
     34 class WEBVIEW_EXPORT WebView : public View,
     35                                public content::WebContentsDelegate,
     36                                public content::WebContentsObserver {
     37  public:
     38   static const char kViewClassName[];
     39 
     40   explicit WebView(content::BrowserContext* browser_context);
     41   virtual ~WebView();
     42 
     43   // This creates a WebContents if none is yet associated with this WebView. The
     44   // WebView owns this implicitly created WebContents.
     45   content::WebContents* GetWebContents();
     46 
     47   // WebView does not assume ownership of WebContents set via this method, only
     48   // those it implicitly creates via GetWebContents() above.
     49   void SetWebContents(content::WebContents* web_contents);
     50 
     51   // If |mode| is true, WebView will register itself with WebContents as a
     52   // WebContentsObserver, monitor for the showing/destruction of fullscreen
     53   // render widgets, and alter its child view hierarchy to embed the fullscreen
     54   // widget or restore the normal WebContentsView.
     55   void SetEmbedFullscreenWidgetMode(bool mode);
     56 
     57   content::WebContents* web_contents() const {
     58     return content::WebContentsObserver::web_contents();
     59   }
     60 
     61   content::BrowserContext* browser_context() { return browser_context_; }
     62 
     63   // Loads the initial URL to display in the attached WebContents. Creates the
     64   // WebContents if none is attached yet. Note that this is intended as a
     65   // convenience for loading the initial URL, and so URLs are navigated with
     66   // PAGE_TRANSITION_AUTO_TOPLEVEL, so this is not intended as a general purpose
     67   // navigation method - use WebContents' API directly.
     68   void LoadInitialURL(const GURL& url);
     69 
     70   // Controls how the attached WebContents is resized.
     71   // false = WebContents' views' bounds are updated continuously as the
     72   //         WebView's bounds change (default).
     73   // true  = WebContents' views' position is updated continuously but its size
     74   //         is not (which may result in some clipping or under-painting) until
     75   //         a continuous size operation completes. This allows for smoother
     76   //         resizing performance during interactive resizes and animations.
     77   void SetFastResize(bool fast_resize);
     78 
     79   // Called when the WebContents is focused.
     80   // TODO(beng): This view should become a WebContentsViewObserver when a
     81   //             WebContents is attached, and not rely on the delegate to
     82   //             forward this notification.
     83   void OnWebContentsFocused(content::WebContents* web_contents);
     84 
     85   // When used to host UI, we need to explicitly allow accelerators to be
     86   // processed. Default is false.
     87   void set_allow_accelerators(bool allow_accelerators) {
     88     allow_accelerators_ = allow_accelerators;
     89   }
     90 
     91   // Sets the preferred size. If empty, View's implementation of
     92   // GetPreferredSize() is used.
     93   void SetPreferredSize(const gfx::Size& preferred_size);
     94 
     95   // Overridden from View:
     96   virtual const char* GetClassName() const OVERRIDE;
     97   virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
     98 
     99  protected:
    100   // Overridden from View:
    101   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
    102   virtual void ViewHierarchyChanged(
    103       const ViewHierarchyChangedDetails& details) OVERRIDE;
    104   virtual bool SkipDefaultKeyEventProcessing(
    105       const ui::KeyEvent& event) OVERRIDE;
    106   virtual bool IsFocusable() const OVERRIDE;
    107   virtual void OnFocus() OVERRIDE;
    108   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
    109   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
    110   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
    111   virtual gfx::Size GetPreferredSize() const OVERRIDE;
    112 
    113   // Overridden from content::WebContentsDelegate:
    114   virtual void WebContentsFocused(content::WebContents* web_contents) OVERRIDE;
    115   virtual bool EmbedsFullscreenWidget() const OVERRIDE;
    116 
    117   // Overridden from content::WebContentsObserver:
    118   virtual void RenderViewDeleted(
    119       content::RenderViewHost* render_view_host) OVERRIDE;
    120   virtual void RenderViewHostChanged(
    121       content::RenderViewHost* old_host,
    122       content::RenderViewHost* new_host) OVERRIDE;
    123   virtual void DidShowFullscreenWidget(int routing_id) OVERRIDE;
    124   virtual void DidDestroyFullscreenWidget(int routing_id) OVERRIDE;
    125   virtual void DidToggleFullscreenModeForTab(bool entered_fullscreen) OVERRIDE;
    126   // Workaround for MSVC++ linker bug/feature that requires
    127   // instantiation of the inline IPC::Listener methods in all translation units.
    128   virtual void OnChannelConnected(int32 peer_id) OVERRIDE {}
    129   virtual void OnChannelError() OVERRIDE {}
    130   virtual void OnBadMessageReceived(const IPC::Message& message) OVERRIDE {}
    131 
    132  private:
    133   void AttachWebContents();
    134   void DetachWebContents();
    135   void ReattachForFullscreenChange(bool enter_fullscreen);
    136   void NotifyMaybeTextInputClientChanged();
    137 
    138   // Create a regular or test web contents (based on whether we're running
    139   // in a unit test or not).
    140   content::WebContents* CreateWebContents(
    141       content::BrowserContext* browser_context);
    142 
    143   NativeViewHost* const holder_;
    144   // Non-NULL if |web_contents()| was created and is owned by this WebView.
    145   scoped_ptr<content::WebContents> wc_owner_;
    146   // When true, WebView auto-embeds fullscreen widgets as a child view.
    147   bool embed_fullscreen_widget_mode_enabled_;
    148   // Set to true while WebView is embedding a fullscreen widget view as a child
    149   // view instead of the normal WebContentsView render view. Note: This will be
    150   // false in the case of non-Flash fullscreen.
    151   bool is_embedding_fullscreen_widget_;
    152   content::BrowserContext* browser_context_;
    153   bool allow_accelerators_;
    154   gfx::Size preferred_size_;
    155 
    156   DISALLOW_COPY_AND_ASSIGN(WebView);
    157 };
    158 
    159 }  // namespace views
    160 
    161 #endif  // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
    162