Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / webview / webview.h
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   // Swaps the owned WebContents |wc_owner_| with |new_web_contents|. Returns
101   // the previously owned WebContents.
102   scoped_ptr<content::WebContents> SwapWebContents(
103       scoped_ptr<content::WebContents> new_web_contents);
104
105   // Overridden from View:
106   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
107   virtual void ViewHierarchyChanged(
108       const ViewHierarchyChangedDetails& details) OVERRIDE;
109   virtual bool SkipDefaultKeyEventProcessing(
110       const ui::KeyEvent& event) OVERRIDE;
111   virtual void OnFocus() OVERRIDE;
112   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
113   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
114   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
115   virtual gfx::Size GetPreferredSize() const OVERRIDE;
116
117   // Overridden from content::WebContentsDelegate:
118   virtual void WebContentsFocused(content::WebContents* web_contents) OVERRIDE;
119   virtual bool EmbedsFullscreenWidget() const OVERRIDE;
120
121   // Overridden from content::WebContentsObserver:
122   virtual void RenderViewDeleted(
123       content::RenderViewHost* render_view_host) OVERRIDE;
124   virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
125   virtual void RenderViewHostChanged(
126       content::RenderViewHost* old_host,
127       content::RenderViewHost* new_host) OVERRIDE;
128   virtual void DidShowFullscreenWidget(int routing_id) OVERRIDE;
129   virtual void DidDestroyFullscreenWidget(int routing_id) OVERRIDE;
130   virtual void DidToggleFullscreenModeForTab(bool entered_fullscreen) OVERRIDE;
131   virtual void DidAttachInterstitialPage() OVERRIDE;
132   virtual void DidDetachInterstitialPage() OVERRIDE;
133   // Workaround for MSVC++ linker bug/feature that requires
134   // instantiation of the inline IPC::Listener methods in all translation units.
135   virtual void OnChannelConnected(int32 peer_id) OVERRIDE {}
136   virtual void OnChannelError() OVERRIDE {}
137   virtual void OnBadMessageReceived(const IPC::Message& message) OVERRIDE {}
138
139  private:
140   void AttachWebContents();
141   void DetachWebContents();
142   void ReattachForFullscreenChange(bool enter_fullscreen);
143   void NotifyMaybeTextInputClientChanged();
144
145   // Create a regular or test web contents (based on whether we're running
146   // in a unit test or not).
147   content::WebContents* CreateWebContents(
148       content::BrowserContext* browser_context);
149
150   NativeViewHost* const holder_;
151   // Non-NULL if |web_contents()| was created and is owned by this WebView.
152   scoped_ptr<content::WebContents> wc_owner_;
153   // When true, WebView auto-embeds fullscreen widgets as a child view.
154   bool embed_fullscreen_widget_mode_enabled_;
155   // Set to true while WebView is embedding a fullscreen widget view as a child
156   // view instead of the normal WebContentsView render view. Note: This will be
157   // false in the case of non-Flash fullscreen.
158   bool is_embedding_fullscreen_widget_;
159   content::BrowserContext* browser_context_;
160   bool allow_accelerators_;
161   gfx::Size preferred_size_;
162
163   DISALLOW_COPY_AND_ASSIGN(WebView);
164 };
165
166 }  // namespace views
167
168 #endif  // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_