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
content / public / test / content_browser_test_utils_mac.mm [blame]
// Copyright 2012 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/public/test/content_browser_test_utils.h"
#include <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
#include <memory>
#include "base/apple/scoped_objc_class_swizzler.h"
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "base/strings/sys_string_conversions.h"
#import "content/app_shim_remote_cocoa/render_widget_host_view_cocoa.h"
#include "content/browser/renderer_host/render_widget_host_view_mac.h"
#include "content/browser/renderer_host/text_input_client_mac.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/mac/attributed_string_type_converters.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/mojom/attributed_string.mojom.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/range/range.h"
// The interface class used to override the implementation of some of
// RenderWidgetHostViewCocoa methods for tests.
@interface RenderWidgetHostViewCocoaSwizzler : NSObject
- (void)didAddSubview:(NSView*)view;
- (void)showDefinitionForAttributedString:(NSAttributedString*)attrString
atPoint:(NSPoint)textBaselineOrigin;
@end
namespace content {
using base::apple::ScopedObjCClassSwizzler;
// static
constexpr char RenderWidgetHostViewCocoaObserver::kDidAddSubview[];
constexpr char
RenderWidgetHostViewCocoaObserver::kShowDefinitionForAttributedString[];
// static
std::map<std::string, std::unique_ptr<base::apple::ScopedObjCClassSwizzler>>
RenderWidgetHostViewCocoaObserver::rwhvcocoa_swizzlers_;
// static
std::map<WebContents*, RenderWidgetHostViewCocoaObserver*>
RenderWidgetHostViewCocoaObserver::observers_;
namespace {
content::RenderWidgetHostViewMac* GetRenderWidgetHostViewMac(NSObject* object) {
for (auto* contents : WebContentsImpl::GetAllWebContents()) {
auto* rwhv_base = static_cast<RenderWidgetHostViewBase*>(
contents->GetRenderWidgetHostView());
if (rwhv_base && !rwhv_base->IsRenderWidgetHostViewChildFrame()) {
auto* rwhv_mac = static_cast<RenderWidgetHostViewMac*>(rwhv_base);
if (rwhv_mac->GetInProcessNSView() == object)
return rwhv_mac;
}
}
return nullptr;
}
} // namespace
base::apple::ScopedObjCClassSwizzler*
RenderWidgetHostViewCocoaObserver::GetSwizzler(const std::string& method_name) {
return rwhvcocoa_swizzlers_.count(method_name)
? rwhvcocoa_swizzlers_.at(method_name).get()
: nullptr;
}
// static
RenderWidgetHostViewCocoaObserver*
RenderWidgetHostViewCocoaObserver::GetObserver(WebContents* web_contents) {
return observers_.count(web_contents) ? observers_.at(web_contents) : nullptr;
}
RenderWidgetHostViewCocoaObserver::RenderWidgetHostViewCocoaObserver(
WebContents* web_contents)
: web_contents_(web_contents) {
if (rwhvcocoa_swizzlers_.empty())
SetUpSwizzlers();
DCHECK(!observers_.count(web_contents));
observers_[web_contents] = this;
}
RenderWidgetHostViewCocoaObserver::~RenderWidgetHostViewCocoaObserver() {
observers_.erase(web_contents_);
if (observers_.empty())
rwhvcocoa_swizzlers_.clear();
}
void RenderWidgetHostViewCocoaObserver::SetUpSwizzlers() {
if (!rwhvcocoa_swizzlers_.empty())
return;
// [RenderWidgetHostViewCocoa didAddSubview:NSView*].
rwhvcocoa_swizzlers_[kDidAddSubview] =
std::make_unique<ScopedObjCClassSwizzler>(
GetRenderWidgetHostViewCocoaClassForTesting(),
[RenderWidgetHostViewCocoaSwizzler class],
NSSelectorFromString(@(kDidAddSubview)));
// [RenderWidgetHostViewCocoa showDefinitionForAttributedString:atPoint].
rwhvcocoa_swizzlers_[kShowDefinitionForAttributedString] =
std::make_unique<ScopedObjCClassSwizzler>(
GetRenderWidgetHostViewCocoaClassForTesting(),
[RenderWidgetHostViewCocoaSwizzler class],
NSSelectorFromString(@(kShowDefinitionForAttributedString)));
}
void SetWindowBounds(gfx::NativeWindow window, const gfx::Rect& bounds) {
NSRect new_bounds = NSRectFromCGRect(bounds.ToCGRect());
if (NSScreen.screens.count > 0) {
new_bounds.origin.y = NSScreen.screens.firstObject.frame.size.height -
new_bounds.origin.y - new_bounds.size.height;
}
[window.GetNativeNSWindow() setFrame:new_bounds display:NO];
}
void GetStringAtPointForRenderWidget(
RenderWidgetHost* rwh,
const gfx::Point& point,
base::OnceCallback<void(const std::string&, const gfx::Point&)>
result_callback) {
TextInputClientMac::GetInstance()->GetStringAtPoint(
rwh, point,
base::BindOnce(
[](base::OnceCallback<void(const std::string&, const gfx::Point&)>
callback,
ui::mojom::AttributedStringPtr attributed_string,
const gfx::Point& baseline_point) {
std::string string =
attributed_string
? base::SysCFStringRefToUTF8(CFAttributedStringGetString(
attributed_string.To<CFAttributedStringRef>()))
: std::string();
std::move(callback).Run(string, baseline_point);
},
std::move(result_callback)));
}
void GetStringFromRangeForRenderWidget(
RenderWidgetHost* rwh,
const gfx::Range& range,
base::OnceCallback<void(const std::string&, const gfx::Point&)>
result_callback) {
TextInputClientMac::GetInstance()->GetStringFromRange(
rwh, range,
base::BindOnce(
[](base::OnceCallback<void(const std::string&, const gfx::Point&)>
callback,
ui::mojom::AttributedStringPtr attributed_string,
const gfx::Point& baseline_point) {
std::string string =
attributed_string
? base::SysCFStringRefToUTF8(CFAttributedStringGetString(
attributed_string.To<CFAttributedStringRef>()))
: std::string();
std::move(callback).Run(string, baseline_point);
},
std::move(result_callback)));
}
} // namespace content
@implementation RenderWidgetHostViewCocoaSwizzler
- (void)didAddSubview:(NSView*)view {
content::RenderWidgetHostViewCocoaObserver::GetSwizzler(
content::RenderWidgetHostViewCocoaObserver::kDidAddSubview)
->InvokeOriginal<void, NSView*>(self, _cmd, view);
content::RenderWidgetHostViewMac* rwhv_mac =
content::GetRenderWidgetHostViewMac(self);
if (!rwhv_mac)
return;
content::RenderWidgetHostViewCocoaObserver* observer =
content::RenderWidgetHostViewCocoaObserver::GetObserver(
rwhv_mac->GetWebContents());
if (!observer)
return;
NSRect bounds_in_cocoa_view =
[view convertRect:view.bounds toView:rwhv_mac->GetInProcessNSView()];
gfx::Rect rect =
[rwhv_mac->GetInProcessNSView() flipNSRectToRect:bounds_in_cocoa_view];
observer->DidAddSubviewWillBeDismissed(rect);
// This override is useful for testing popups. To make sure the run loops end
// after the call it is best to dismiss the popup soon.
NSEvent* dismissal_event =
[NSEvent mouseEventWithType:NSEventTypeLeftMouseDown
location:NSZeroPoint
modifierFlags:0
timestamp:0.0
windowNumber:0
context:nil
eventNumber:0
clickCount:1
pressure:1.0];
[NSApplication.sharedApplication postEvent:dismissal_event atStart:false];
}
- (void)showDefinitionForAttributedString:(NSAttributedString*)attrString
atPoint:(NSPoint)textBaselineOrigin {
content::RenderWidgetHostViewCocoaObserver::GetSwizzler(
content::RenderWidgetHostViewCocoaObserver::
kShowDefinitionForAttributedString)
->InvokeOriginal<void, NSAttributedString*, NSPoint>(
self, _cmd, attrString, textBaselineOrigin);
auto* rwhv_mac = content::GetRenderWidgetHostViewMac(self);
auto* observer = content::RenderWidgetHostViewCocoaObserver::GetObserver(
rwhv_mac->GetWebContents());
if (!observer)
return;
observer->OnShowDefinitionForAttributedString(
base::SysNSStringToUTF8(attrString.string));
}
@end