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
content / browser / permissions / permission_service_context_unittest.cc [blame]
// Copyright 2022 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/browser/permissions/permission_service_context.h"
#include <memory>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "content/browser/permissions/permission_controller_impl.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/weak_document_ptr.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "content/test/test_render_frame_host.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom.h"
#include "url/origin.h"
namespace content {
namespace {
constexpr char kTestUrl[] = "https://google.com";
}
class TestPermissionObserver : public blink::mojom::PermissionObserver {
public:
TestPermissionObserver() = default;
TestPermissionObserver(const TestPermissionObserver&) = delete;
TestPermissionObserver& operator=(const TestPermissionObserver&) = delete;
~TestPermissionObserver() override = default;
// Closes the bindings associated with this observer.
void Close() { receiver_.reset(); }
// Returns a pipe to this observer.
mojo::PendingRemote<blink::mojom::PermissionObserver> GetRemote() {
mojo::PendingRemote<blink::mojom::PermissionObserver> remote;
receiver_.Bind(remote.InitWithNewPipeAndPassReceiver());
return remote;
}
// Returns the number of events received by this observer.
size_t change_event_count() const { return change_event_count_; }
// blink::mojom::PermissionObserver implementation.
void OnPermissionStatusChange(
blink::mojom::PermissionStatus status) override {
change_event_count_++;
}
private:
size_t change_event_count_ = 0;
mojo::Receiver<blink::mojom::PermissionObserver> receiver_{this};
};
class PermissionServiceContextTest : public RenderViewHostTestHarness {
public:
PermissionServiceContextTest() = default;
PermissionServiceContextTest(const PermissionServiceContextTest&) = delete;
PermissionServiceContextTest& operator=(const PermissionServiceContextTest&) =
delete;
~PermissionServiceContextTest() override = default;
void SetUp() override {
RenderViewHostTestHarness::SetUp();
origin_ = url::Origin::Create(GURL(kTestUrl));
NavigateAndCommit(origin_.GetURL());
permission_controller_ =
PermissionControllerImpl::FromBrowserContext(browser_context());
auto* render_frame_host = main_rfh();
render_frame_host_impl_ =
static_cast<RenderFrameHostImpl*>(render_frame_host);
permission_service_context_ =
PermissionServiceContext::GetOrCreateForCurrentDocument(
render_frame_host);
}
void TearDown() override {
permission_controller_ = nullptr;
render_frame_host_impl_ = nullptr;
permission_service_context_ = nullptr;
RenderViewHostTestHarness::TearDown();
}
std::unique_ptr<TestPermissionObserver> CreateSubscription(
PermissionType type,
blink::mojom::PermissionStatus last_status,
blink::mojom::PermissionStatus current_status) {
permission_controller()->SetOverrideForDevTools(origin_, type, last_status);
auto observer = std::make_unique<TestPermissionObserver>();
permission_service_context()->CreateSubscription(
type, origin_, current_status, last_status,
/*should_include_device_status=*/false, observer->GetRemote());
WaitForAsyncTasksToComplete();
return observer;
}
void SimulatePermissionChangedEvent(PermissionType type,
blink::mojom::PermissionStatus status) {
permission_controller()->SetOverrideForDevTools(origin_, type, status);
WaitForAsyncTasksToComplete();
}
// Waits until the Mojo task (async) has finished.
void WaitForAsyncTasksToComplete() { task_environment()->RunUntilIdle(); }
PermissionControllerImpl* permission_controller() {
return permission_controller_;
}
PermissionServiceContext* permission_service_context() {
return permission_service_context_;
}
RenderFrameHostImpl* render_frame_host() { return render_frame_host_impl_; }
private:
url::Origin origin_;
raw_ptr<PermissionControllerImpl> permission_controller_;
raw_ptr<RenderFrameHostImpl> render_frame_host_impl_;
raw_ptr<PermissionServiceContext> permission_service_context_;
};
TEST_F(PermissionServiceContextTest, DispatchPermissionChangeEvent) {
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
auto observer = CreateSubscription(PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
EXPECT_EQ(observer->change_event_count(), 1U);
}
TEST_F(PermissionServiceContextTest,
DispatchPermissionChangeEventInBackForwardCache) {
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
auto observer = CreateSubscription(PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
// After dispatching changed events when the render frame host is active,
// the event counter should increment as expected.
EXPECT_EQ(observer->change_event_count(), 1U);
// Same origin child sub-frame should also receive changed events but should
// not double increment the parent's counter.
RenderFrameHost* child =
RenderFrameHostTester::For(render_frame_host())->AppendChild("");
RenderFrameHostTester::For(child)->InitializeRenderFrameIfNeeded();
auto navigation_simulator =
content::NavigationSimulator::CreateRendererInitiated(GURL(kTestUrl),
child);
navigation_simulator->Commit();
child = navigation_simulator->GetFinalRenderFrameHost();
auto* permission_service_context =
PermissionServiceContext::GetOrCreateForCurrentDocument(child);
auto observer_child = std::make_unique<TestPermissionObserver>();
permission_service_context->CreateSubscription(
PermissionType::GEOLOCATION, url::Origin::Create(GURL(kTestUrl)),
blink::mojom::PermissionStatus::ASK, blink::mojom::PermissionStatus::ASK,
/*should_include_device_status=*/false, observer_child->GetRemote());
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 2U);
EXPECT_EQ(observer_child->change_event_count(), 1U);
// Simulate the render frame host is put into the back/forward cache
render_frame_host()->DidEnterBackForwardCache();
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kInBackForwardCache));
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
// Trigger a permission status change event.
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK);
// Now the change events should not should increment the counter.
EXPECT_EQ(observer->change_event_count(), 2U);
EXPECT_EQ(observer_child->change_event_count(), 1U);
// Simulate the render frame host is back to active state by setting the
// lifecycle state.
render_frame_host()->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kActive);
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
// Since the render frame host is active, the dispatched events should
// increment the counter.
EXPECT_EQ(observer->change_event_count(), 3U);
EXPECT_EQ(observer_child->change_event_count(), 2U);
}
TEST_F(PermissionServiceContextTest,
DispatchMultiplePermissionChangeEventsInBackForwardCache) {
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
auto observer = CreateSubscription(PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
// Simulate the render frame host is put into the back/forward cache.
// Trigger a permission status change event, the event should not should
// increment the counter.
render_frame_host()->DidEnterBackForwardCache();
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kInBackForwardCache));
for (size_t i = 0; i < 10; ++i) {
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
EXPECT_EQ(observer->change_event_count(), 0U);
}
// Simulate the render frame host is back to active state by setting the
// lifecycle state. The last event should be dispatched and increment the
// counter.
render_frame_host()->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kActive);
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
WaitForAsyncTasksToComplete();
EXPECT_EQ(observer->change_event_count(), 1U);
}
TEST_F(PermissionServiceContextTest, CreateSubscriptionInBackForwardCache) {
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
render_frame_host()->DidEnterBackForwardCache();
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kInBackForwardCache));
// Create a subscription in BFCache
auto observer = CreateSubscription(PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK,
blink::mojom::PermissionStatus::ASK);
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
EXPECT_EQ(observer->change_event_count(), 0U);
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::GRANTED);
EXPECT_EQ(observer->change_event_count(), 0U);
// Simulate the render frame host is back to active state by setting the
// lifecycle state. The last event should be dispatched.
render_frame_host()->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kActive);
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
WaitForAsyncTasksToComplete();
EXPECT_EQ(observer->change_event_count(), 1U);
}
TEST_F(PermissionServiceContextTest,
DispatchSameStatusAfterLeaveBackForwardCache) {
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
auto observer = CreateSubscription(PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
// Simulate the render frame host is put into the back/forward cache.
// Trigger a permission status change event, the event should not should
// increment the counter.
render_frame_host()->DidEnterBackForwardCache();
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kInBackForwardCache));
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
EXPECT_EQ(observer->change_event_count(), 0U);
// Permission status changes back to the status at BFCache entry
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
// Simulate the render frame host is back to active state by setting the
// lifecycle state. No event should be dispatched.
render_frame_host()->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kActive);
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
WaitForAsyncTasksToComplete();
EXPECT_EQ(observer->change_event_count(), 0U);
}
TEST_F(PermissionServiceContextTest,
DispatchDifferentStatusAfterLeaveBackForwardCache) {
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
auto observer = CreateSubscription(PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::ASK,
blink::mojom::PermissionStatus::ASK);
EXPECT_EQ(observer->change_event_count(), 0U);
// Simulate the render frame host is put into the back/forward cache.
// Trigger a permission status change event, the event should not should
// increment the counter.
render_frame_host()->DidEnterBackForwardCache();
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kInBackForwardCache));
SimulatePermissionChangedEvent(blink::PermissionType::GEOLOCATION,
blink::mojom::PermissionStatus::DENIED);
EXPECT_EQ(observer->change_event_count(), 0U);
// Simulate the render frame host is back to active state by setting the
// lifecycle state. The last event should be dispatched.
render_frame_host()->SetLifecycleState(
RenderFrameHostImpl::LifecycleStateImpl::kActive);
EXPECT_TRUE(render_frame_host()->IsInLifecycleState(
RenderFrameHost::LifecycleState::kActive));
WaitForAsyncTasksToComplete();
EXPECT_EQ(observer->change_event_count(), 1U);
}
} // namespace content