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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
content / shell / browser / 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/shell/browser/shell_platform_delegate.h"
#import <Cocoa/Cocoa.h>
#include <algorithm>
#import "base/apple/foundation_util.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/strings/sys_string_conversions.h"
#include "components/input/native_web_keyboard_event.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/shell/app/resource.h"
#include "content/shell/browser/shell.h"
#include "url/gurl.h"
// Receives notification that the window is closing so that it can start the
// tear-down process.
@interface ContentShellWindowDelegate : NSObject <NSWindowDelegate>
@property(readonly) NSWindow* window;
- (id)initWithShell:(content::Shell*)shell window:(NSWindow*)window;
- (void)showDevTools:(id)sender;
@end
@implementation ContentShellWindowDelegate {
raw_ptr<content::Shell> _shell;
NSWindow* __strong _window;
}
@synthesize window = _window;
- (id)initWithShell:(content::Shell*)shell window:(NSWindow*)window {
if ((self = [super init])) {
_shell = shell;
_window = window;
window.delegate = self;
}
return self;
}
// Called when the window is about to close. Perform the self-destruction
// sequence by getting rid of the shell and removing it and the window from
// the various global lists. By returning YES, we allow the window to be
// removed from the screen.
- (BOOL)windowShouldClose:(id)sender {
CHECK_EQ(base::apple::ObjCCastStrict<NSWindow>(sender), _window);
// Don't leave a dangling pointer if the window lives beyond
// this method. See crbug.com/719830.
_window.delegate = nil;
_window = nil;
_shell.ClearAndDelete();
return YES;
}
- (void)performAction:(id)sender {
_shell->ActionPerformed([sender tag]);
}
- (void)takeURLStringValueFrom:(id)sender {
_shell->URLEntered(base::SysNSStringToUTF8([sender stringValue]));
}
- (void)showDevTools:(id)sender {
_shell->ShowDevTools();
}
@end
namespace {
NSString* kWindowTitle = @"Content Shell";
// Layout constants (in view coordinates)
const CGFloat kButtonWidth = 72;
const CGFloat kURLBarHeight = 24;
// The minimum size of the window's content (in view coordinates)
const CGFloat kMinimumWindowWidth = 400;
const CGFloat kMinimumWindowHeight = 300;
void MakeShellButton(NSRect* rect,
NSString* title,
NSView* parent,
int control,
id target,
NSString* key,
NSUInteger modifier) {
NSButton* button = [[NSButton alloc] initWithFrame:*rect];
button.title = title;
button.bezelStyle = NSBezelStyleSmallSquare;
button.autoresizingMask = (NSViewMaxXMargin | NSViewMinYMargin);
button.target = target;
button.action = @selector(performAction:);
button.tag = control;
button.keyEquivalent = key;
button.keyEquivalentModifierMask = modifier;
[parent addSubview:button];
rect->origin.x += kButtonWidth;
}
} // namespace
namespace content {
struct ShellPlatformDelegate::ShellData {
ContentShellWindowDelegate* __strong delegate;
NSTextField* __weak url_edit_view;
};
struct ShellPlatformDelegate::PlatformData {};
ShellPlatformDelegate::ShellPlatformDelegate() = default;
ShellPlatformDelegate::~ShellPlatformDelegate() = default;
void ShellPlatformDelegate::Initialize(const gfx::Size& default_window_size) {
screen_ = std::make_unique<display::ScopedNativeScreen>();
}
void ShellPlatformDelegate::CreatePlatformWindow(
Shell* shell,
const gfx::Size& initial_size) {
DCHECK(!base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
int width = initial_size.width();
int height = initial_size.height();
if (!Shell::ShouldHideToolbar()) {
height += kURLBarHeight;
}
NSRect initial_window_bounds = NSMakeRect(0, 0, width, height);
NSRect content_rect = initial_window_bounds;
NSUInteger style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable |
NSWindowStyleMaskResizable;
NSWindow* window =
[[NSWindow alloc] initWithContentRect:content_rect
styleMask:style_mask
backing:NSBackingStoreBuffered
defer:NO];
window.title = kWindowTitle;
NSView* content = window.contentView;
// If the window is allowed to get too small, it will wreck the view bindings.
NSSize min_size = NSMakeSize(kMinimumWindowWidth, kMinimumWindowHeight);
min_size = [content convertSize:min_size toView:nil];
// Note that this takes window coordinates.
window.contentMinSize = min_size;
// Set the shell window to participate in fullscreen mode.
window.collectionBehavior |= NSWindowCollectionBehaviorFullScreenPrimary;
// Rely on the window delegate to clean us up rather than immediately
// releasing when the window gets closed. We use the delegate to do everything
// from the autorelease pool so the shell isn't on the stack during cleanup
// (ie, a window close from javascript). Also, releasedWhenClosed == YES is
// incompatible with ARC.
window.releasedWhenClosed = NO;
// Create a window delegate to watch for when it's asked to go away.
ContentShellWindowDelegate* delegate =
[[ContentShellWindowDelegate alloc] initWithShell:shell window:window];
if (!Shell::ShouldHideToolbar()) {
NSRect button_frame =
NSMakeRect(0, NSMaxY(initial_window_bounds) - kURLBarHeight,
kButtonWidth, kURLBarHeight);
MakeShellButton(&button_frame, @"Back", content, IDC_NAV_BACK, delegate,
@"[", NSEventModifierFlagCommand);
MakeShellButton(&button_frame, @"Forward", content, IDC_NAV_FORWARD,
delegate, @"]", NSEventModifierFlagCommand);
MakeShellButton(&button_frame, @"Reload", content, IDC_NAV_RELOAD, delegate,
@"r", NSEventModifierFlagCommand);
MakeShellButton(&button_frame, @"Stop", content, IDC_NAV_STOP, delegate,
@".", NSEventModifierFlagCommand);
button_frame.size.width =
NSWidth(initial_window_bounds) - NSMinX(button_frame);
NSTextField* url_edit_view =
[[NSTextField alloc] initWithFrame:button_frame];
[content addSubview:url_edit_view];
url_edit_view.autoresizingMask = NSViewWidthSizable | NSViewMinYMargin;
url_edit_view.target = delegate;
url_edit_view.action = @selector(takeURLStringValueFrom:);
url_edit_view.cell.wraps = NO;
url_edit_view.cell.scrollable = YES;
shell_data.url_edit_view = url_edit_view;
}
// Show the new window.
[window makeKeyAndOrderFront:nil];
shell_data.delegate = delegate;
}
gfx::NativeWindow ShellPlatformDelegate::GetNativeWindow(Shell* shell) {
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
return shell_data.delegate.window;
}
void ShellPlatformDelegate::CleanUp(Shell* shell) {
DCHECK(base::Contains(shell_data_map_, shell));
shell_data_map_.erase(shell);
}
void ShellPlatformDelegate::SetContents(Shell* shell) {
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
NSView* web_view = shell->web_contents()->GetNativeView().GetNativeNSView();
web_view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
NSWindow* window = shell_data.delegate.window;
[window.contentView addSubview:web_view];
NSRect frame = window.contentView.bounds;
if (!Shell::ShouldHideToolbar()) {
frame.size.height -= kURLBarHeight;
}
web_view.frame = frame;
web_view.needsDisplay = YES;
}
void ShellPlatformDelegate::ResizeWebContent(Shell* shell,
const gfx::Size& content_size) {
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
int toolbar_height = Shell::ShouldHideToolbar() ? 0 : kURLBarHeight;
NSRect frame = NSMakeRect(0, 0, content_size.width(),
content_size.height() + toolbar_height);
shell_data.delegate.window.contentView.frame = frame;
}
void ShellPlatformDelegate::EnableUIControl(Shell* shell,
UIControl control,
bool is_enabled) {
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
int id;
switch (control) {
case BACK_BUTTON:
id = IDC_NAV_BACK;
break;
case FORWARD_BUTTON:
id = IDC_NAV_FORWARD;
break;
case STOP_BUTTON:
id = IDC_NAV_STOP;
break;
default:
NOTREACHED_IN_MIGRATION() << "Unknown UI control";
return;
}
[[shell_data.delegate.window.contentView viewWithTag:id]
setEnabled:is_enabled];
}
void ShellPlatformDelegate::SetAddressBarURL(Shell* shell, const GURL& url) {
if (Shell::ShouldHideToolbar()) {
return;
}
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
NSString* url_string = base::SysUTF8ToNSString(url.spec());
shell_data.url_edit_view.stringValue = url_string;
}
void ShellPlatformDelegate::SetIsLoading(Shell* shell, bool loading) {}
void ShellPlatformDelegate::SetTitle(Shell* shell,
const std::u16string& title) {
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
shell_data.delegate.window.title = base::SysUTF16ToNSString(title);
}
void ShellPlatformDelegate::MainFrameCreated(Shell* shell) {}
bool ShellPlatformDelegate::DestroyShell(Shell* shell) {
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
[shell_data.delegate.window performClose:nil];
return true; // The performClose() will do the destruction of Shell.
}
void ShellPlatformDelegate::ActivateContents(Shell* shell,
WebContents* top_contents) {
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
// This focuses the main frame RenderWidgetHost in the window, but does not
// make the window itself active. The WebContentsDelegate (this class) is
// responsible for doing both.
top_contents->Focus();
// This makes the window the active window for the application, and when the
// app is active, the window will be also. That makes all RenderWidgetHosts
// for the window active (which is separate from focused on mac).
[shell_data.delegate.window makeKeyAndOrderFront:nil];
// This makes the application active so that we can actually move focus
// between windows and the renderer can receive focus/blur events.
[NSApp activateIgnoringOtherApps:YES];
}
void ShellPlatformDelegate::DidNavigatePrimaryMainFramePostCommit(
Shell* shell,
WebContents* contents) {}
bool ShellPlatformDelegate::HandleKeyboardEvent(
Shell* shell,
WebContents* source,
const input::NativeWebKeyboardEvent& event) {
if (event.skip_if_unhandled || Shell::ShouldHideToolbar()) {
return false;
}
DCHECK(base::Contains(shell_data_map_, shell));
ShellData& shell_data = shell_data_map_[shell];
// The event handling to get this strictly right is a tangle; cheat here a bit
// by just letting the menus have a chance at it.
NSEvent* ns_event = event.os_event.Get();
if (ns_event.type == NSEventTypeKeyDown) {
if ((ns_event.modifierFlags & NSEventModifierFlagCommand) &&
[ns_event.characters isEqual:@"l"]) {
[shell_data.delegate.window makeFirstResponder:shell_data.url_edit_view];
return true;
}
[NSApp.mainMenu performKeyEquivalent:ns_event];
return true;
}
return false;
}
} // namespace content