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