| 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 "content/browser/tab_contents/tab_contents_view_helper.h" | |
| 6 | |
| 7 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 8 #include "content/browser/tab_contents/tab_contents.h" | |
| 9 #include "content/common/view_messages.h" | |
| 10 #include "content/port/browser/render_widget_host_view_port.h" | |
| 11 #include "content/public/browser/notification_service.h" | |
| 12 #include "content/public/browser/notification_source.h" | |
| 13 #include "content/public/browser/notification_types.h" | |
| 14 #include "content/public/browser/site_instance.h" | |
| 15 #include "content/public/browser/web_contents.h" | |
| 16 #include "content/public/browser/web_contents_delegate.h" | |
| 17 #include "content/public/browser/web_contents_view.h" | |
| 18 | |
| 19 using content::RenderViewHostImpl; | |
| 20 using content::RenderWidgetHost; | |
| 21 using content::RenderWidgetHostImpl; | |
| 22 using content::RenderWidgetHostView; | |
| 23 using content::RenderWidgetHostViewPort; | |
| 24 using content::WebContents; | |
| 25 | |
| 26 TabContentsViewHelper::TabContentsViewHelper() { | |
| 27 registrar_.Add(this, | |
| 28 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, | |
| 29 content::NotificationService::AllBrowserContextsAndSources()); | |
| 30 } | |
| 31 | |
| 32 TabContentsViewHelper::~TabContentsViewHelper() {} | |
| 33 | |
| 34 void TabContentsViewHelper::Observe( | |
| 35 int type, | |
| 36 const content::NotificationSource& source, | |
| 37 const content::NotificationDetails& details) { | |
| 38 DCHECK_EQ(type, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); | |
| 39 RenderWidgetHost* host = content::Source<RenderWidgetHost>(source).ptr(); | |
| 40 for (PendingWidgetViews::iterator i = pending_widget_views_.begin(); | |
| 41 i != pending_widget_views_.end(); ++i) { | |
| 42 if (host->GetView() == i->second) { | |
| 43 pending_widget_views_.erase(i); | |
| 44 break; | |
| 45 } | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 TabContents* TabContentsViewHelper::CreateNewWindow( | |
| 50 WebContents* web_contents, | |
| 51 int route_id, | |
| 52 const ViewHostMsg_CreateWindow_Params& params) { | |
| 53 bool should_create = true; | |
| 54 if (web_contents->GetDelegate()) { | |
| 55 should_create = web_contents->GetDelegate()->ShouldCreateWebContents( | |
| 56 web_contents, | |
| 57 route_id, | |
| 58 params.window_container_type, | |
| 59 params.frame_name, | |
| 60 params.target_url); | |
| 61 } | |
| 62 | |
| 63 if (!should_create) | |
| 64 return NULL; | |
| 65 | |
| 66 // We usually create the new window in the same BrowsingInstance (group of | |
| 67 // script-related windows), by passing in the current SiteInstance. However, | |
| 68 // if the opener is being suppressed, we create a new SiteInstance in its own | |
| 69 // BrowsingInstance. | |
| 70 scoped_refptr<content::SiteInstance> site_instance = | |
| 71 params.opener_suppressed ? | |
| 72 content::SiteInstance::Create(web_contents->GetBrowserContext()) : | |
| 73 web_contents->GetSiteInstance(); | |
| 74 | |
| 75 // Create the new web contents. This will automatically create the new | |
| 76 // WebContentsView. In the future, we may want to create the view separately. | |
| 77 TabContents* new_contents = | |
| 78 new TabContents(web_contents->GetBrowserContext(), | |
| 79 site_instance, | |
| 80 route_id, | |
| 81 static_cast<TabContents*>(web_contents), | |
| 82 NULL); | |
| 83 new_contents->set_opener_web_ui_type( | |
| 84 web_contents->GetWebUITypeForCurrentState()); | |
| 85 new_contents->set_has_opener(!params.opener_url.is_empty()); | |
| 86 | |
| 87 if (!params.opener_suppressed) { | |
| 88 content::WebContentsView* new_view = new_contents->GetView(); | |
| 89 | |
| 90 // TODO(brettw): It seems bogus that we have to call this function on the | |
| 91 // newly created object and give it one of its own member variables. | |
| 92 new_view->CreateViewForWidget(new_contents->GetRenderViewHost()); | |
| 93 | |
| 94 // Save the created window associated with the route so we can show it | |
| 95 // later. | |
| 96 DCHECK_NE(MSG_ROUTING_NONE, route_id); | |
| 97 pending_contents_[route_id] = new_contents; | |
| 98 } | |
| 99 | |
| 100 if (web_contents->GetDelegate()) | |
| 101 web_contents->GetDelegate()->WebContentsCreated(web_contents, | |
| 102 params.opener_frame_id, | |
| 103 params.target_url, | |
| 104 new_contents); | |
| 105 | |
| 106 if (params.opener_suppressed) { | |
| 107 // When the opener is suppressed, the original renderer cannot access the | |
| 108 // new window. As a result, we need to show and navigate the window here. | |
| 109 gfx::Rect initial_pos; | |
| 110 web_contents->AddNewContents(new_contents, | |
| 111 params.disposition, | |
| 112 initial_pos, | |
| 113 params.user_gesture); | |
| 114 | |
| 115 content::OpenURLParams open_params(params.target_url, content::Referrer(), | |
| 116 CURRENT_TAB, | |
| 117 content::PAGE_TRANSITION_LINK, | |
| 118 true /* is_renderer_initiated */); | |
| 119 new_contents->OpenURL(open_params); | |
| 120 } | |
| 121 | |
| 122 return new_contents; | |
| 123 } | |
| 124 | |
| 125 RenderWidgetHostView* TabContentsViewHelper::CreateNewWidget( | |
| 126 WebContents* web_contents, | |
| 127 int route_id, | |
| 128 bool is_fullscreen, | |
| 129 WebKit::WebPopupType popup_type) { | |
| 130 content::RenderProcessHost* process = web_contents->GetRenderProcessHost(); | |
| 131 RenderWidgetHostImpl* widget_host = | |
| 132 new RenderWidgetHostImpl(process, route_id); | |
| 133 RenderWidgetHostViewPort* widget_view = | |
| 134 RenderWidgetHostViewPort::CreateViewForWidget(widget_host); | |
| 135 if (!is_fullscreen) { | |
| 136 // Popups should not get activated. | |
| 137 widget_view->SetPopupType(popup_type); | |
| 138 } | |
| 139 // Save the created widget associated with the route so we can show it later. | |
| 140 pending_widget_views_[route_id] = widget_view; | |
| 141 return widget_view; | |
| 142 } | |
| 143 | |
| 144 TabContents* TabContentsViewHelper::GetCreatedWindow(int route_id) { | |
| 145 PendingContents::iterator iter = pending_contents_.find(route_id); | |
| 146 | |
| 147 // Certain systems can block the creation of new windows. If we didn't succeed | |
| 148 // in creating one, just return NULL. | |
| 149 if (iter == pending_contents_.end()) { | |
| 150 return NULL; | |
| 151 } | |
| 152 | |
| 153 TabContents* new_contents = iter->second; | |
| 154 pending_contents_.erase(route_id); | |
| 155 | |
| 156 if (!new_contents->GetRenderProcessHost()->HasConnection() || | |
| 157 !new_contents->GetRenderViewHost()->GetView()) | |
| 158 return NULL; | |
| 159 | |
| 160 // TODO(brettw): It seems bogus to reach into here and initialize the host. | |
| 161 static_cast<RenderViewHostImpl*>(new_contents->GetRenderViewHost())->Init(); | |
| 162 return new_contents; | |
| 163 } | |
| 164 | |
| 165 RenderWidgetHostView* TabContentsViewHelper::GetCreatedWidget(int route_id) { | |
| 166 PendingWidgetViews::iterator iter = pending_widget_views_.find(route_id); | |
| 167 if (iter == pending_widget_views_.end()) { | |
| 168 DCHECK(false); | |
| 169 return NULL; | |
| 170 } | |
| 171 | |
| 172 RenderWidgetHostView* widget_host_view = iter->second; | |
| 173 pending_widget_views_.erase(route_id); | |
| 174 | |
| 175 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost(); | |
| 176 if (!widget_host->GetProcess()->HasConnection()) { | |
| 177 // The view has gone away or the renderer crashed. Nothing to do. | |
| 178 return NULL; | |
| 179 } | |
| 180 | |
| 181 return widget_host_view; | |
| 182 } | |
| 183 | |
| 184 TabContents* TabContentsViewHelper::ShowCreatedWindow( | |
| 185 WebContents* web_contents, | |
| 186 int route_id, | |
| 187 WindowOpenDisposition disposition, | |
| 188 const gfx::Rect& initial_pos, | |
| 189 bool user_gesture) { | |
| 190 TabContents* contents = GetCreatedWindow(route_id); | |
| 191 if (contents) { | |
| 192 web_contents->AddNewContents(contents, | |
| 193 disposition, | |
| 194 initial_pos, | |
| 195 user_gesture); | |
| 196 } | |
| 197 return contents; | |
| 198 } | |
| 199 | |
| 200 RenderWidgetHostView* TabContentsViewHelper::ShowCreatedWidget( | |
| 201 WebContents* web_contents, | |
| 202 int route_id, | |
| 203 bool is_fullscreen, | |
| 204 const gfx::Rect& initial_pos) { | |
| 205 if (web_contents->GetDelegate()) | |
| 206 web_contents->GetDelegate()->RenderWidgetShowing(); | |
| 207 | |
| 208 RenderWidgetHostViewPort* widget_host_view = | |
| 209 RenderWidgetHostViewPort::FromRWHV(GetCreatedWidget(route_id)); | |
| 210 if (is_fullscreen) { | |
| 211 widget_host_view->InitAsFullscreen(web_contents->GetRenderWidgetHostView()); | |
| 212 } else { | |
| 213 widget_host_view->InitAsPopup(web_contents->GetRenderWidgetHostView(), | |
| 214 initial_pos); | |
| 215 } | |
| 216 RenderWidgetHostImpl::From(widget_host_view->GetRenderWidgetHost())->Init(); | |
| 217 return widget_host_view; | |
| 218 } | |
| OLD | NEW |