Chromium Code Reviews| 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" | |
| 8 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
| 15 #include "ui/base/accessibility/accessible_view_state.h" | |
| 16 #include "ui/base/accessibility/accessibility_types.h" | |
| 9 #include "ui/views/controls/native/native_view_host.h" | 17 #include "ui/views/controls/native/native_view_host.h" |
| 18 #include "ui/views/focus/focus_manager.h" | |
| 10 | 19 |
| 11 namespace views { | 20 namespace views { |
| 12 | 21 |
| 13 //////////////////////////////////////////////////////////////////////////////// | 22 //////////////////////////////////////////////////////////////////////////////// |
| 14 // WebView, public: | 23 // WebView, public: |
| 15 | 24 |
| 16 WebView::WebView(content::BrowserContext* browser_context) | 25 WebView::WebView(content::BrowserContext* browser_context) |
| 17 : wcv_holder_(new NativeViewHost), | 26 : wcv_holder_(new NativeViewHost), |
| 27 web_contents_(NULL), | |
| 18 browser_context_(browser_context) { | 28 browser_context_(browser_context) { |
| 19 Init(); | 29 AddChildView(wcv_holder_); |
| 20 } | 30 } |
| 21 | 31 |
| 22 WebView::~WebView() { | 32 WebView::~WebView() { |
| 23 } | 33 } |
| 24 | 34 |
| 25 //////////////////////////////////////////////////////////////////////////////// | 35 content::WebContents* WebView::GetWebContents() { |
| 26 // WebView, private: | 36 if (!web_contents_) { |
|
sky
2012/04/13 19:19:08
I don't think this works quite right if invoked af
| |
| 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 } | |
| 45 return web_contents_; | |
| 46 } | |
| 27 | 47 |
| 28 void WebView::Init() { | 48 void WebView::SetWebContents(content::WebContents* web_contents) { |
| 29 AddChildView(wcv_holder_); | 49 DetachWebContents(web_contents_); |
| 30 web_contents_.reset( | 50 if (wc_owner_.get()) |
|
sky
2012/04/13 19:19:08
nit: no need to test, can always do wc_owner_.rese
| |
| 31 content::WebContents::Create(browser_context_, NULL, MSG_ROUTING_NONE, | 51 wc_owner_.reset(); |
| 32 NULL, NULL)); | 52 web_contents_ = web_contents; |
| 53 AttachWebContents(web_contents_); | |
| 54 } | |
| 55 | |
| 56 void WebView::SetFastResize(bool fast_resize) { | |
| 57 wcv_holder_->set_fast_resize(fast_resize); | |
| 58 } | |
| 59 | |
| 60 void WebView::OnWebContentsFocused(content::WebContents* web_contents) { | |
| 61 DCHECK(web_contents == web_contents_); | |
| 62 FocusManager* focus_manager = GetFocusManager(); | |
| 63 if (!focus_manager) { | |
| 64 NOTREACHED(); | |
| 65 return; | |
| 66 } | |
| 67 focus_manager->SetFocusedView(this); | |
| 33 } | 68 } |
| 34 | 69 |
| 35 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
| 36 // WebView, View overrides: | 71 // WebView, View overrides: |
| 37 | 72 |
| 38 void WebView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 73 void WebView::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 39 wcv_holder_->SetSize(bounds().size()); | 74 wcv_holder_->SetSize(bounds().size()); |
| 40 } | 75 } |
| 41 | 76 |
| 42 void WebView::ViewHierarchyChanged(bool is_add, View* parent, View* child) { | 77 void WebView::ViewHierarchyChanged(bool is_add, View* parent, View* child) { |
| 43 if (is_add && child == this) | 78 if (is_add && GetWidget()) |
| 44 wcv_holder_->Attach(web_contents_->GetNativeView()); | 79 AttachWebContents(web_contents_); |
|
sky
2012/04/13 19:19:08
Won't this lead to potentially invoking Attach mor
| |
| 80 } | |
| 81 | |
| 82 bool WebView::SkipDefaultKeyEventProcessing(const views::KeyEvent& event) { | |
| 83 // Don't look-up accelerators or tab-traversal if we are showing a non-crashed | |
| 84 // TabContents. | |
| 85 // We'll first give the page a chance to process the key events. If it does | |
| 86 // not process them, they'll be returned to us and we'll treat them as | |
| 87 // accelerators then. | |
| 88 return web_contents_ && !web_contents_->IsCrashed(); | |
| 89 } | |
| 90 | |
| 91 bool WebView::IsFocusable() const { | |
| 92 // We need to be focusable when our contents is not a view hierarchy, as | |
| 93 // clicking on the contents needs to focus us. | |
| 94 return !!web_contents_; | |
| 95 } | |
| 96 | |
| 97 void WebView::OnFocus() { | |
| 98 if (web_contents_) | |
| 99 web_contents_->Focus(); | |
| 100 } | |
| 101 | |
| 102 void WebView::AboutToRequestFocusFromTabTraversal(bool reverse) { | |
| 103 if (web_contents_) | |
| 104 web_contents_->FocusThroughTabTraversal(reverse); | |
| 105 } | |
| 106 | |
| 107 void WebView::RequestFocus() { | |
| 108 // TODO(beng): determine if code in NTCCWin is really necessary. | |
| 109 View::RequestFocus(); | |
| 110 } | |
| 111 | |
| 112 void WebView::GetAccessibleState(ui::AccessibleViewState* state) { | |
| 113 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | |
| 114 } | |
| 115 | |
| 116 gfx::NativeViewAccessible WebView::GetNativeViewAccessible() { | |
| 117 if (web_contents_) { | |
| 118 content::RenderWidgetHostView* host_view = | |
| 119 web_contents_->GetRenderWidgetHostView(); | |
| 120 if (host_view) | |
| 121 return host_view->GetNativeViewAccessible(); | |
| 122 } | |
| 123 return View::GetNativeViewAccessible(); | |
| 124 } | |
| 125 | |
| 126 //////////////////////////////////////////////////////////////////////////////// | |
| 127 // WebView, content::NotificationObserver implementation: | |
| 128 | |
| 129 void WebView::Observe(int type, | |
| 130 const content::NotificationSource& source, | |
| 131 const content::NotificationDetails& details) { | |
| 132 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { | |
| 133 std::pair<content::RenderViewHost*, content::RenderViewHost*>* | |
| 134 switched_details = | |
| 135 content::Details<std::pair<content::RenderViewHost*, | |
| 136 content::RenderViewHost*> >( | |
| 137 details).ptr(); | |
| 138 RenderViewHostChanged(switched_details->first, | |
| 139 switched_details->second); | |
| 140 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | |
| 141 WebContentsDestroyed(content::Source<content::WebContents>(source).ptr()); | |
| 142 } else { | |
| 143 NOTREACHED(); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 //////////////////////////////////////////////////////////////////////////////// | |
| 148 // WebView, content::WebContentsDelegate implementation: | |
| 149 | |
| 150 void WebView::WebContentsFocused(content::WebContents* web_contents) { | |
| 151 DCHECK(wc_owner_.get()); | |
| 152 // The WebView is only the delegate of WebContentses it creates itself. | |
| 153 WebContentsFocused(web_contents_); | |
| 154 } | |
| 155 | |
| 156 //////////////////////////////////////////////////////////////////////////////// | |
| 157 // WebView, private: | |
| 158 | |
| 159 void WebView::AttachWebContents(content::WebContents* web_contents) { | |
| 160 if (web_contents) { | |
| 161 wcv_holder_->Attach(web_contents->GetNativeView()); | |
| 162 | |
| 163 registrar_.Add( | |
| 164 this, | |
| 165 content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | |
| 166 content::Source<content::NavigationController>( | |
| 167 &web_contents_->GetController())); | |
| 168 registrar_.Add( | |
| 169 this, | |
| 170 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 171 content::Source<content::WebContents>(web_contents_)); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void WebView::DetachWebContents(content::WebContents* web_contents) { | |
| 176 if (web_contents) | |
| 177 wcv_holder_->Detach(); | |
| 178 registrar_.RemoveAll(); | |
| 179 } | |
| 180 | |
| 181 void WebView::RenderViewHostChanged(content::RenderViewHost* old_host, | |
| 182 content::RenderViewHost* new_host) { | |
| 183 if (GetFocusManager()->GetFocusedView() == this) | |
| 184 web_contents_->Focus(); | |
| 185 } | |
| 186 | |
| 187 void WebView::WebContentsDestroyed(content::WebContents* web_contents) { | |
| 188 DCHECK(web_contents == web_contents_); | |
| 189 SetWebContents(NULL); | |
| 45 } | 190 } |
| 46 | 191 |
| 47 } // namespace views | 192 } // namespace views |
| OLD | NEW |