| OLD | NEW |
| (Empty) |
| 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 #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" | |
| 6 | |
| 7 #include "content/browser/renderer_host/render_view_host.h" | |
| 8 #include "content/browser/renderer_host/render_widget_host.h" | |
| 9 #include "content/browser/renderer_host/render_widget_host_view.h" | |
| 10 #include "content/browser/tab_contents/tab_contents.h" | |
| 11 #include "content/browser/tab_contents/tab_contents_view.h" | |
| 12 #include "content/common/view_messages.h" | |
| 13 #include "content/public/browser/notification_service.h" | |
| 14 #include "content/public/browser/notification_source.h" | |
| 15 #include "content/public/browser/notification_types.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/browser/web_contents_delegate.h" | |
| 18 | |
| 19 using content::WebContents; | |
| 20 | |
| 21 RenderViewHostDelegateViewHelper::RenderViewHostDelegateViewHelper() { | |
| 22 registrar_.Add(this, | |
| 23 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, | |
| 24 content::NotificationService::AllBrowserContextsAndSources()); | |
| 25 } | |
| 26 | |
| 27 RenderViewHostDelegateViewHelper::~RenderViewHostDelegateViewHelper() {} | |
| 28 | |
| 29 void RenderViewHostDelegateViewHelper::Observe( | |
| 30 int type, | |
| 31 const content::NotificationSource& source, | |
| 32 const content::NotificationDetails& details) { | |
| 33 DCHECK_EQ(type, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); | |
| 34 RenderWidgetHost* host = content::Source<RenderWidgetHost>(source).ptr(); | |
| 35 for (PendingWidgetViews::iterator i = pending_widget_views_.begin(); | |
| 36 i != pending_widget_views_.end(); ++i) { | |
| 37 if (host->view() == i->second) { | |
| 38 pending_widget_views_.erase(i); | |
| 39 break; | |
| 40 } | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 TabContents* RenderViewHostDelegateViewHelper::CreateNewWindow( | |
| 45 WebContents* web_contents, | |
| 46 int route_id, | |
| 47 const ViewHostMsg_CreateWindow_Params& params) { | |
| 48 bool should_create = true; | |
| 49 if (web_contents->GetDelegate()) { | |
| 50 should_create = web_contents->GetDelegate()->ShouldCreateWebContents( | |
| 51 web_contents, | |
| 52 route_id, | |
| 53 params.window_container_type, | |
| 54 params.frame_name); | |
| 55 } | |
| 56 | |
| 57 if (!should_create) | |
| 58 return NULL; | |
| 59 | |
| 60 // Create the new web contents. This will automatically create the new | |
| 61 // TabContentsView. In the future, we may want to create the view separately. | |
| 62 TabContents* new_contents = | |
| 63 new TabContents(web_contents->GetBrowserContext(), | |
| 64 web_contents->GetSiteInstance(), | |
| 65 route_id, | |
| 66 static_cast<TabContents*>(web_contents), | |
| 67 NULL); | |
| 68 new_contents->set_opener_web_ui_type( | |
| 69 web_contents->GetWebUITypeForCurrentState()); | |
| 70 TabContentsView* new_view = new_contents->GetView(); | |
| 71 | |
| 72 // TODO(brettw): It seems bogus that we have to call this function on the | |
| 73 // newly created object and give it one of its own member variables. | |
| 74 new_view->CreateViewForWidget(new_contents->GetRenderViewHost()); | |
| 75 | |
| 76 // Save the created window associated with the route so we can show it later. | |
| 77 pending_contents_[route_id] = new_contents; | |
| 78 | |
| 79 if (web_contents->GetDelegate()) | |
| 80 web_contents->GetDelegate()->WebContentsCreated(web_contents, | |
| 81 params.opener_frame_id, | |
| 82 params.target_url, | |
| 83 new_contents); | |
| 84 | |
| 85 return new_contents; | |
| 86 } | |
| 87 | |
| 88 RenderWidgetHostView* RenderViewHostDelegateViewHelper::CreateNewWidget( | |
| 89 WebContents* web_contents, | |
| 90 int route_id, | |
| 91 bool is_fullscreen, | |
| 92 WebKit::WebPopupType popup_type) { | |
| 93 content::RenderProcessHost* process = web_contents->GetRenderProcessHost(); | |
| 94 RenderWidgetHost* widget_host = new RenderWidgetHost(process, route_id); | |
| 95 RenderWidgetHostView* widget_view = | |
| 96 RenderWidgetHostView::CreateViewForWidget(widget_host); | |
| 97 if (!is_fullscreen) { | |
| 98 // Popups should not get activated. | |
| 99 widget_view->set_popup_type(popup_type); | |
| 100 } | |
| 101 // Save the created widget associated with the route so we can show it later. | |
| 102 pending_widget_views_[route_id] = widget_view; | |
| 103 return widget_view; | |
| 104 } | |
| 105 | |
| 106 TabContents* RenderViewHostDelegateViewHelper::GetCreatedWindow(int route_id) { | |
| 107 PendingContents::iterator iter = pending_contents_.find(route_id); | |
| 108 | |
| 109 // Certain systems can block the creation of new windows. If we didn't succeed | |
| 110 // in creating one, just return NULL. | |
| 111 if (iter == pending_contents_.end()) { | |
| 112 return NULL; | |
| 113 } | |
| 114 | |
| 115 TabContents* new_contents = iter->second; | |
| 116 pending_contents_.erase(route_id); | |
| 117 | |
| 118 if (!new_contents->GetRenderProcessHost()->HasConnection() || | |
| 119 !new_contents->GetRenderViewHost()->view()) | |
| 120 return NULL; | |
| 121 | |
| 122 // TODO(brettw): It seems bogus to reach into here and initialize the host. | |
| 123 new_contents->GetRenderViewHost()->Init(); | |
| 124 return new_contents; | |
| 125 } | |
| 126 | |
| 127 RenderWidgetHostView* RenderViewHostDelegateViewHelper::GetCreatedWidget( | |
| 128 int route_id) { | |
| 129 PendingWidgetViews::iterator iter = pending_widget_views_.find(route_id); | |
| 130 if (iter == pending_widget_views_.end()) { | |
| 131 DCHECK(false); | |
| 132 return NULL; | |
| 133 } | |
| 134 | |
| 135 RenderWidgetHostView* widget_host_view = iter->second; | |
| 136 pending_widget_views_.erase(route_id); | |
| 137 | |
| 138 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost(); | |
| 139 if (!widget_host->process()->HasConnection()) { | |
| 140 // The view has gone away or the renderer crashed. Nothing to do. | |
| 141 return NULL; | |
| 142 } | |
| 143 | |
| 144 return widget_host_view; | |
| 145 } | |
| 146 | |
| 147 TabContents* RenderViewHostDelegateViewHelper::ShowCreatedWindow( | |
| 148 WebContents* web_contents, | |
| 149 int route_id, | |
| 150 WindowOpenDisposition disposition, | |
| 151 const gfx::Rect& initial_pos, | |
| 152 bool user_gesture) { | |
| 153 TabContents* contents = GetCreatedWindow(route_id); | |
| 154 if (contents) { | |
| 155 web_contents->AddNewContents(contents, | |
| 156 disposition, | |
| 157 initial_pos, | |
| 158 user_gesture); | |
| 159 } | |
| 160 return contents; | |
| 161 } | |
| 162 | |
| 163 RenderWidgetHostView* RenderViewHostDelegateViewHelper::ShowCreatedWidget( | |
| 164 WebContents* web_contents, | |
| 165 int route_id, | |
| 166 bool is_fullscreen, | |
| 167 const gfx::Rect& initial_pos) { | |
| 168 if (web_contents->GetDelegate()) | |
| 169 web_contents->GetDelegate()->RenderWidgetShowing(); | |
| 170 | |
| 171 RenderWidgetHostView* widget_host_view = GetCreatedWidget(route_id); | |
| 172 if (is_fullscreen) { | |
| 173 widget_host_view->InitAsFullscreen(web_contents->GetRenderWidgetHostView()); | |
| 174 } else { | |
| 175 widget_host_view->InitAsPopup(web_contents->GetRenderWidgetHostView(), | |
| 176 initial_pos); | |
| 177 } | |
| 178 widget_host_view->GetRenderWidgetHost()->Init(); | |
| 179 return widget_host_view; | |
| 180 } | |
| OLD | NEW |