OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/webview/webview.h" | 5 #include "ui/views/controls/webview/webview.h" |
6 | 6 |
7 #include "content/public/browser/browser_context.h" | 7 #include "content/public/browser/browser_context.h" |
8 #include "content/public/browser/notification_details.h" | |
9 #include "content/public/browser/notification_registrar.h" | |
10 #include "content/public/browser/notification_source.h" | |
11 #include "content/public/browser/notification_types.h" | |
12 #include "content/public/browser/render_view_host.h" | |
13 #include "content/public/browser/render_widget_host_view.h" | |
14 #include "ipc/ipc_message.h" | 8 #include "ipc/ipc_message.h" |
15 #include "ui/base/accessibility/accessible_view_state.h" | |
16 #include "ui/base/accessibility/accessibility_types.h" | |
17 #include "ui/views/controls/native/native_view_host.h" | 9 #include "ui/views/controls/native/native_view_host.h" |
18 #include "ui/views/focus/focus_manager.h" | |
19 | 10 |
20 namespace views { | 11 namespace views { |
21 | 12 |
22 //////////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////////// |
23 // WebView, public: | 14 // WebView, public: |
24 | 15 |
25 WebView::WebView(content::BrowserContext* browser_context) | 16 WebView::WebView(content::BrowserContext* browser_context) |
26 : wcv_holder_(new NativeViewHost), | 17 : wcv_holder_(new NativeViewHost), |
27 web_contents_(NULL), | |
28 browser_context_(browser_context) { | 18 browser_context_(browser_context) { |
29 AddChildView(wcv_holder_); | 19 Init(); |
30 } | 20 } |
31 | 21 |
32 WebView::~WebView() { | 22 WebView::~WebView() { |
33 } | 23 } |
34 | 24 |
35 content::WebContents* WebView::GetWebContents() { | 25 //////////////////////////////////////////////////////////////////////////////// |
36 if (!web_contents_) { | 26 // WebView, private: |
37 wc_owner_.reset(content::WebContents::Create(browser_context_, | |
38 NULL, | |
39 MSG_ROUTING_NONE, | |
40 NULL, | |
41 NULL)); | |
42 web_contents_ = wc_owner_.get(); | |
43 web_contents_->SetDelegate(this); | |
44 AttachWebContents(); | |
45 } | |
46 return web_contents_; | |
47 } | |
48 | 27 |
49 void WebView::SetWebContents(content::WebContents* web_contents) { | 28 void WebView::Init() { |
50 if (web_contents == web_contents_) | 29 AddChildView(wcv_holder_); |
51 return; | 30 web_contents_.reset( |
52 DetachWebContents(); | 31 content::WebContents::Create(browser_context_, NULL, MSG_ROUTING_NONE, |
53 wc_owner_.reset(); | 32 NULL, NULL)); |
54 web_contents_ = web_contents; | |
55 AttachWebContents(); | |
56 } | |
57 | |
58 void WebView::SetFastResize(bool fast_resize) { | |
59 wcv_holder_->set_fast_resize(fast_resize); | |
60 } | |
61 | |
62 void WebView::OnWebContentsFocused(content::WebContents* web_contents) { | |
63 DCHECK(web_contents == web_contents_); | |
64 FocusManager* focus_manager = GetFocusManager(); | |
65 if (!focus_manager) { | |
66 NOTREACHED(); | |
67 return; | |
68 } | |
69 focus_manager->SetFocusedView(this); | |
70 } | 33 } |
71 | 34 |
72 //////////////////////////////////////////////////////////////////////////////// | 35 //////////////////////////////////////////////////////////////////////////////// |
73 // WebView, View overrides: | 36 // WebView, View overrides: |
74 | 37 |
75 void WebView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 38 void WebView::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
76 wcv_holder_->SetSize(bounds().size()); | 39 wcv_holder_->SetSize(bounds().size()); |
77 } | 40 } |
78 | 41 |
79 void WebView::ViewHierarchyChanged(bool is_add, View* parent, View* child) { | 42 void WebView::ViewHierarchyChanged(bool is_add, View* parent, View* child) { |
80 if (is_add) | 43 if (is_add && child == this) |
81 AttachWebContents(); | |
82 } | |
83 | |
84 bool WebView::SkipDefaultKeyEventProcessing(const views::KeyEvent& event) { | |
85 // Don't look-up accelerators or tab-traversal if we are showing a non-crashed | |
86 // TabContents. | |
87 // We'll first give the page a chance to process the key events. If it does | |
88 // not process them, they'll be returned to us and we'll treat them as | |
89 // accelerators then. | |
90 return web_contents_ && !web_contents_->IsCrashed(); | |
91 } | |
92 | |
93 bool WebView::IsFocusable() const { | |
94 // We need to be focusable when our contents is not a view hierarchy, as | |
95 // clicking on the contents needs to focus us. | |
96 return !!web_contents_; | |
97 } | |
98 | |
99 void WebView::OnFocus() { | |
100 if (web_contents_) | |
101 web_contents_->Focus(); | |
102 } | |
103 | |
104 void WebView::AboutToRequestFocusFromTabTraversal(bool reverse) { | |
105 if (web_contents_) | |
106 web_contents_->FocusThroughTabTraversal(reverse); | |
107 } | |
108 | |
109 void WebView::GetAccessibleState(ui::AccessibleViewState* state) { | |
110 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | |
111 } | |
112 | |
113 gfx::NativeViewAccessible WebView::GetNativeViewAccessible() { | |
114 if (web_contents_) { | |
115 content::RenderWidgetHostView* host_view = | |
116 web_contents_->GetRenderWidgetHostView(); | |
117 if (host_view) | |
118 return host_view->GetNativeViewAccessible(); | |
119 } | |
120 return View::GetNativeViewAccessible(); | |
121 } | |
122 | |
123 //////////////////////////////////////////////////////////////////////////////// | |
124 // WebView, content::NotificationObserver implementation: | |
125 | |
126 void WebView::Observe(int type, | |
127 const content::NotificationSource& source, | |
128 const content::NotificationDetails& details) { | |
129 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { | |
130 std::pair<content::RenderViewHost*, content::RenderViewHost*>* | |
131 switched_details = | |
132 content::Details<std::pair<content::RenderViewHost*, | |
133 content::RenderViewHost*> >( | |
134 details).ptr(); | |
135 RenderViewHostChanged(switched_details->first, | |
136 switched_details->second); | |
137 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | |
138 WebContentsDestroyed(content::Source<content::WebContents>(source).ptr()); | |
139 } else { | |
140 NOTREACHED(); | |
141 } | |
142 } | |
143 | |
144 //////////////////////////////////////////////////////////////////////////////// | |
145 // WebView, content::WebContentsDelegate implementation: | |
146 | |
147 void WebView::WebContentsFocused(content::WebContents* web_contents) { | |
148 DCHECK(wc_owner_.get()); | |
149 // The WebView is only the delegate of WebContentses it creates itself. | |
150 WebContentsFocused(web_contents_); | |
151 } | |
152 | |
153 //////////////////////////////////////////////////////////////////////////////// | |
154 // WebView, private: | |
155 | |
156 void WebView::AttachWebContents() { | |
157 // Prevents attachment if the WebView isn't already in a Widget, or it's | |
158 // already attached. | |
159 if (!GetWidget() || !web_contents_ || | |
160 wcv_holder_->native_view() == web_contents_->GetNativeView()) { | |
161 return; | |
162 } | |
163 | |
164 if (web_contents_) { | |
165 wcv_holder_->Attach(web_contents_->GetNativeView()); | 44 wcv_holder_->Attach(web_contents_->GetNativeView()); |
166 | |
167 registrar_.Add( | |
168 this, | |
169 content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | |
170 content::Source<content::NavigationController>( | |
171 &web_contents_->GetController())); | |
172 registrar_.Add( | |
173 this, | |
174 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
175 content::Source<content::WebContents>(web_contents_)); | |
176 } | |
177 } | |
178 | |
179 void WebView::DetachWebContents() { | |
180 if (web_contents_) { | |
181 wcv_holder_->Detach(); | |
182 #if defined(OS_WIN) | |
183 // TODO(beng): This should either not be necessary, or be done implicitly by | |
184 // NativeViewHostWin on Detach(). As it stands, this is needed | |
185 // so that the view of the detached contents knows to tell the | |
186 // renderer its been hidden. | |
187 ShowWindow(web_contents_->GetNativeView(), SW_HIDE); | |
188 #endif | |
189 } | |
190 registrar_.RemoveAll(); | |
191 } | |
192 | |
193 void WebView::RenderViewHostChanged(content::RenderViewHost* old_host, | |
194 content::RenderViewHost* new_host) { | |
195 if (GetFocusManager()->GetFocusedView() == this) | |
196 web_contents_->Focus(); | |
197 } | |
198 | |
199 void WebView::WebContentsDestroyed(content::WebContents* web_contents) { | |
200 DCHECK(web_contents == web_contents_); | |
201 SetWebContents(NULL); | |
202 } | 45 } |
203 | 46 |
204 } // namespace views | 47 } // namespace views |
OLD | NEW |