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
content / web_test / browser / web_test_shell_platform_delegate_mac.mm [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/web_test/browser/web_test_shell_platform_delegate.h"
#import "base/apple/foundation_util.h"
#include "base/containers/contains.h"
#include "content/browser/renderer_host/render_widget_host_view_mac.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/shell/browser/shell.h"
namespace content {
// On mac, the WebTestShellPlatformDelegate replaces behaviour in the base class
// ShellPlatformDelegate when in headless mode. Otherwise it mostly defers to
// the base class.
struct WebTestShellPlatformDelegate::WebTestShellData {
gfx::Size initial_size;
};
struct WebTestShellPlatformDelegate::WebTestPlatformData {};
WebTestShellPlatformDelegate::WebTestShellPlatformDelegate() = default;
WebTestShellPlatformDelegate::~WebTestShellPlatformDelegate() = default;
void WebTestShellPlatformDelegate::Initialize(
const gfx::Size& default_window_size) {
ShellPlatformDelegate::Initialize(default_window_size);
}
void WebTestShellPlatformDelegate::CreatePlatformWindow(
Shell* shell,
const gfx::Size& initial_size) {
if (!IsHeadless()) {
ShellPlatformDelegate::CreatePlatformWindow(shell, initial_size);
return;
}
DCHECK(!base::Contains(web_test_shell_data_map_, shell));
WebTestShellData& shell_data = web_test_shell_data_map_[shell];
shell_data.initial_size = initial_size;
}
gfx::NativeWindow WebTestShellPlatformDelegate::GetNativeWindow(Shell* shell) {
if (!IsHeadless())
return ShellPlatformDelegate::GetNativeWindow(shell);
NOTREACHED_IN_MIGRATION();
return {};
}
void WebTestShellPlatformDelegate::CleanUp(Shell* shell) {
if (!IsHeadless()) {
ShellPlatformDelegate::CleanUp(shell);
return;
}
DCHECK(base::Contains(web_test_shell_data_map_, shell));
web_test_shell_data_map_.erase(shell);
if (shell == activated_headless_shell_)
activated_headless_shell_ = nullptr;
}
void WebTestShellPlatformDelegate::SetContents(Shell* shell) {
if (!IsHeadless()) {
ShellPlatformDelegate::SetContents(shell);
return;
}
}
void WebTestShellPlatformDelegate::EnableUIControl(Shell* shell,
UIControl control,
bool is_enabled) {
if (!IsHeadless()) {
ShellPlatformDelegate::EnableUIControl(shell, control, is_enabled);
return;
}
}
void WebTestShellPlatformDelegate::SetAddressBarURL(Shell* shell,
const GURL& url) {
if (!IsHeadless()) {
ShellPlatformDelegate::SetAddressBarURL(shell, url);
return;
}
}
void WebTestShellPlatformDelegate::SetTitle(Shell* shell,
const std::u16string& title) {
if (!IsHeadless()) {
ShellPlatformDelegate::SetTitle(shell, title);
return;
}
}
void WebTestShellPlatformDelegate::MainFrameCreated(Shell* shell) {
if (!IsHeadless()) {
ShellPlatformDelegate::MainFrameCreated(shell);
return;
}
DCHECK(base::Contains(web_test_shell_data_map_, shell));
WebTestShellData& shell_data = web_test_shell_data_map_[shell];
// In mac headless mode, the OS view for the WebContents is not attached to a
// window so the usual notifications from the OS about the bounds of the web
// contents do not occur. We need to make sure the renderer knows its bounds,
// and to do this we force a resize to happen on the WebContents. However, the
// WebContents can not be fully resized until after the RenderWidgetHostView
// is created, which may not be not done until the first navigation starts.
// Failing to do this resize *after* the navigation causes the
// RenderWidgetHostView to be created would leave the WebContents with invalid
// sizes (such as the window screen rect).
//
// We use the signal that the `blink::WebView` has been created in the
// renderer as a proxy for knowing when the top level RenderWidgetHostView is
// created, since they are created at the same time.
DCHECK(shell->web_contents()->GetPrimaryMainFrame()->GetView());
ResizeWebContent(shell, shell_data.initial_size);
}
bool WebTestShellPlatformDelegate::DestroyShell(Shell* shell) {
if (IsHeadless())
return false; // Shell destroys itself.
return ShellPlatformDelegate::DestroyShell(shell);
}
void WebTestShellPlatformDelegate::ResizeWebContent(
Shell* shell,
const gfx::Size& content_size) {
if (!IsHeadless()) {
ShellPlatformDelegate::ResizeWebContent(shell, content_size);
return;
}
NSView* web_view = shell->web_contents()->GetNativeView().GetNativeNSView();
web_view.frame =
NSMakeRect(0, 0, content_size.width(), content_size.height());
// The above code changes the RenderWidgetHostView's size, but does not change
// the widget's screen rects, since the RenderWidgetHostView is not attached
// to a window in headless mode. So this call causes them to be updated so
// they are not left as 0x0.
auto* rwhv_mac = shell->web_contents()->GetPrimaryMainFrame()->GetView();
if (rwhv_mac)
rwhv_mac->SetWindowFrameInScreen(gfx::Rect(content_size));
}
void WebTestShellPlatformDelegate::ActivateContents(Shell* shell,
WebContents* top_contents) {
if (!IsHeadless()) {
ShellPlatformDelegate::ActivateContents(shell, top_contents);
return;
}
// In headless mode, there are no system windows, so we can't go down the
// normal path which relies on calling the OS to move focus/active states.
// Instead we fake it out by just informing the RenderWidgetHost directly.
// For all windows other than this one, blur them.
for (Shell* window : Shell::windows()) {
if (window != shell) {
WebContents* other_top_contents = window->web_contents();
auto* other_rwhv_mac = static_cast<RenderWidgetHostViewMac*>(
other_top_contents->GetPrimaryMainFrame()->GetView());
other_rwhv_mac->OnFirstResponderChanged(false);
other_rwhv_mac->GetRenderWidgetHost()->SetActive(false);
}
}
auto* top_rwhv_mac = static_cast<RenderWidgetHostViewMac*>(
top_contents->GetPrimaryMainFrame()->GetView());
top_rwhv_mac->OnFirstResponderChanged(true);
top_rwhv_mac->GetRenderWidgetHost()->SetActive(true);
activated_headless_shell_ = shell;
}
void WebTestShellPlatformDelegate::DidNavigatePrimaryMainFramePostCommit(
Shell* shell,
WebContents* contents) {
if (!IsHeadless()) {
ShellPlatformDelegate::DidNavigatePrimaryMainFramePostCommit(shell,
contents);
return;
}
// Normally RenderFrameHostManager::CommitPending() transfers focus status to
// the new RenderWidgetHostView when a navigation creates a new view, but that
// doesn't work in Mac headless mode because RenderWidgetHostView depends on
// the native window (which doesn't exist in headless mode) to manage focus
// status. Instead we manually set focus status of the new RenderWidgetHost.
if (shell == activated_headless_shell_)
ActivateContents(shell, contents);
}
bool WebTestShellPlatformDelegate::HandleKeyboardEvent(
Shell* shell,
WebContents* source,
const input::NativeWebKeyboardEvent& event) {
if (IsHeadless())
return false;
return ShellPlatformDelegate::HandleKeyboardEvent(shell, source, event);
}
} // namespace content