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