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