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
content / shell / browser / shell_web_contents_view_delegate_ios.mm [blame]
// Copyright 2023 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_web_contents_view_delegate.h"
#import <UIKit/UIKit.h>
#include <memory>
#include "base/apple/foundation_util.h"
#include "base/command_line.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
#include "content/shell/common/shell_switches.h"
#include "third_party/blink/public/common/context_menu_data/edit_flags.h"
enum {
ShellContextMenuItemCutTag = 0,
ShellContextMenuItemCopyTag,
ShellContextMenuItemCopyLinkTag,
ShellContextMenuItemPasteTag,
ShellContextMenuItemDeleteTag,
ShellContextMenuItemOpenLinkTag
};
// A hidden button used only for creating context menus. The only way to
// programmatically trigger a context menu on iOS is to trigger the primary
// action of a button that shows a context menu as its primary action.
@interface ContextMenuHiddenButton : UIButton
// The frame determines the position at which the context menu is shown.
+ (instancetype)buttonWithFrame:(CGRect)frame
contextMenuParams:(content::ContextMenuParams)params
forWebContents:(content::WebContents*)webContents;
@end
@implementation ContextMenuHiddenButton {
content::ContextMenuParams _params;
base::WeakPtr<content::WebContents> _webContents;
}
+ (instancetype)buttonWithFrame:(CGRect)frame
contextMenuParams:(content::ContextMenuParams)params
forWebContents:(content::WebContents*)webContents {
ContextMenuHiddenButton* button =
[ContextMenuHiddenButton buttonWithType:UIButtonTypeSystem];
button.hidden = YES;
button.userInteractionEnabled = NO;
button.contextMenuInteractionEnabled = YES;
button.showsMenuAsPrimaryAction = YES;
button.frame = frame;
button.layer.zPosition = CGFLOAT_MIN;
button->_params = params;
button->_webContents = webContents->GetWeakPtr();
return button;
}
- (UIContextMenuConfiguration*)contextMenuInteraction:
(UIContextMenuInteraction*)interaction
configurationForMenuAtLocation:(CGPoint)location {
UIContextMenuConfiguration* config = [UIContextMenuConfiguration
configurationWithIdentifier:nil
previewProvider:nil
actionProvider:^UIMenu* _Nullable(
NSArray<UIMenuElement*>* _Nonnull suggestedActions) {
return [self buildContextMenuItems];
}];
[super contextMenuInteraction:interaction
configurationForMenuAtLocation:location];
return config;
}
- (void)contextMenuInteraction:(UIContextMenuInteraction*)interaction
willEndForConfiguration:(UIContextMenuConfiguration*)configuration
animator:(id<UIContextMenuInteractionAnimating>)animator {
[super contextMenuInteraction:interaction
willEndForConfiguration:configuration
animator:animator];
if (_webContents) {
_webContents->NotifyContextMenuClosed(_params.link_followed);
}
}
- (UIAction*)makeMenuItem:(NSString*)title menuTag:(NSInteger)tag {
auto menuActionHandler = ^(UIAction* action) {
switch (tag) {
case ShellContextMenuItemCutTag:
self->_webContents->Cut();
break;
case ShellContextMenuItemCopyTag:
self->_webContents->Copy();
break;
case ShellContextMenuItemCopyLinkTag: {
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [NSString
stringWithUTF8String:self->_params.link_url.spec().c_str()];
break;
}
case ShellContextMenuItemPasteTag:
self->_webContents->Paste();
break;
case ShellContextMenuItemDeleteTag:
self->_webContents->Delete();
break;
case ShellContextMenuItemOpenLinkTag: {
content::NavigationController::LoadURLParams params(
self->_params.link_url);
self->_webContents->GetController().LoadURLWithParams(params);
break;
}
}
};
UIAction* menu = [UIAction actionWithTitle:title
image:nil
identifier:nil
handler:menuActionHandler];
return menu;
}
- (UIMenu*)buildContextMenuItems {
bool hasLink = !_params.unfiltered_link_url.is_empty();
bool hasSelection = !_params.selection_text.empty();
bool isEditable = _params.is_editable;
NSMutableArray* menuItems = [[NSMutableArray alloc] init];
if (hasLink) {
[menuItems addObject:[self makeMenuItem:@"Go to the Link"
menuTag:ShellContextMenuItemOpenLinkTag]];
[menuItems addObject:[self makeMenuItem:@"Copy Link"
menuTag:ShellContextMenuItemCopyLinkTag]];
}
if (isEditable) {
if (_params.edit_flags & blink::ContextMenuDataEditFlags::kCanCut) {
[menuItems addObject:[self makeMenuItem:@"Cut"
menuTag:ShellContextMenuItemCutTag]];
}
if (_params.edit_flags & blink::ContextMenuDataEditFlags::kCanCopy) {
[menuItems addObject:[self makeMenuItem:@"Copy"
menuTag:ShellContextMenuItemCopyTag]];
}
if (_params.edit_flags & blink::ContextMenuDataEditFlags::kCanPaste) {
[menuItems addObject:[self makeMenuItem:@"Paste"
menuTag:ShellContextMenuItemPasteTag]];
}
if (_params.edit_flags & blink::ContextMenuDataEditFlags::kCanDelete) {
[menuItems addObject:[self makeMenuItem:@"Delete"
menuTag:ShellContextMenuItemDeleteTag]];
}
} else if (hasSelection) {
[menuItems addObject:[self makeMenuItem:@"Copy"
menuTag:ShellContextMenuItemCopyTag]];
}
NSString* title =
hasLink ? [NSString
stringWithUTF8String:self->_params.link_url.spec().c_str()]
: @"";
return [UIMenu menuWithTitle:title children:menuItems];
}
@end
namespace content {
namespace {
gfx::NativeView GetContentNativeView(WebContents* web_contents) {
RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
if (!rwhv) {
return gfx::NativeView();
}
return rwhv->GetNativeView();
}
} // namespace
class ShellWebContentsUIButtonHolder {
public:
UIButton* __strong button_;
};
std::unique_ptr<WebContentsViewDelegate> CreateShellWebContentsViewDelegate(
WebContents* web_contents) {
return std::make_unique<ShellWebContentsViewDelegate>(web_contents);
}
ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
WebContents* web_contents)
: web_contents_(web_contents) {
DCHECK(web_contents_); // Avoids 'unused private field' build error.
hidden_button_ = std::make_unique<ShellWebContentsUIButtonHolder>();
}
ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {}
void ShellWebContentsViewDelegate::ShowContextMenu(
RenderFrameHost& render_frame_host,
const ContextMenuParams& params) {
if (switches::IsRunWebTestsSwitchPresent()) {
return;
}
UIView* view = base::apple::ObjCCastStrict<UIView>(
GetContentNativeView(web_contents_).Get());
CGRect frame = CGRectMake(params.x, params.y, 0, 0);
[hidden_button_->button_ removeFromSuperview];
hidden_button_->button_ =
[ContextMenuHiddenButton buttonWithFrame:frame
contextMenuParams:params
forWebContents:web_contents_];
[view addSubview:hidden_button_->button_];
[hidden_button_->button_ performPrimaryAction];
}
} // namespace content