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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
#include <set>
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/containers/hash_tables.h"
#include "base/memory/scoped_vector.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/geolocation/chrome_geolocation_permission_context_factory.h"
#include "chrome/browser/geolocation/geolocation_permission_request_id.h"
#include "chrome/browser/infobars/confirm_infobar_delegate.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/view_type_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_ANDROID)
#include "base/prefs/pref_service.h"
#include "chrome/browser/android/mock_google_location_settings_helper.h"
#include "chrome/common/pref_names.h"
#endif
using content::MockRenderProcessHost;
// ClosedInfoBarTracker -------------------------------------------------------
// We need to track which infobars were closed.
class ClosedInfoBarTracker : public content::NotificationObserver {
public:
ClosedInfoBarTracker();
virtual ~ClosedInfoBarTracker();
// content::NotificationObserver:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
size_t size() const { return removed_infobars_.size(); }
bool Contains(InfoBarDelegate* infobar) const;
void Clear();
private:
FRIEND_TEST_ALL_PREFIXES(GeolocationPermissionContextTests, TabDestroyed);
content::NotificationRegistrar registrar_;
std::set<InfoBarDelegate*> removed_infobars_;
};
ClosedInfoBarTracker::ClosedInfoBarTracker() {
registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
content::NotificationService::AllSources());
}
ClosedInfoBarTracker::~ClosedInfoBarTracker() {
}
void ClosedInfoBarTracker::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED);
removed_infobars_.insert(
content::Details<InfoBarRemovedDetails>(details)->first);
}
bool ClosedInfoBarTracker::Contains(InfoBarDelegate* infobar) const {
return removed_infobars_.count(infobar) != 0;
}
void ClosedInfoBarTracker::Clear() {
removed_infobars_.clear();
}
// GeolocationPermissionContextTests ------------------------------------------
// This class sets up GeolocationArbitrator.
class GeolocationPermissionContextTests
: public ChromeRenderViewHostTestHarness {
protected:
// ChromeRenderViewHostTestHarness:
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
GeolocationPermissionRequestID RequestID(int bridge_id);
GeolocationPermissionRequestID RequestIDForTab(int tab, int bridge_id);
InfoBarService* infobar_service() {
return InfoBarService::FromWebContents(web_contents());
}
InfoBarService* infobar_service_for_tab(int tab) {
return InfoBarService::FromWebContents(extra_tabs_[tab]);
}
void RequestGeolocationPermission(const GeolocationPermissionRequestID& id,
const GURL& requesting_frame);
void CancelGeolocationPermissionRequest(
const GeolocationPermissionRequestID& id,
const GURL& requesting_frame);
void PermissionResponse(const GeolocationPermissionRequestID& id,
bool allowed);
void CheckPermissionMessageSent(int bridge_id, bool allowed);
void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed);
void CheckPermissionMessageSentInternal(MockRenderProcessHost* process,
int bridge_id,
bool allowed);
void AddNewTab(const GURL& url);
void CheckTabContentsState(const GURL& requesting_frame,
ContentSetting expected_content_setting);
scoped_refptr<ChromeGeolocationPermissionContext>
geolocation_permission_context_;
ClosedInfoBarTracker closed_infobar_tracker_;
ScopedVector<content::WebContents> extra_tabs_;
// A map between renderer child id and a pair represending the bridge id and
// whether the requested permission was allowed.
base::hash_map<int, std::pair<int, bool> > responses_;
};
GeolocationPermissionRequestID GeolocationPermissionContextTests::RequestID(
int bridge_id) {
return GeolocationPermissionRequestID(
web_contents()->GetRenderProcessHost()->GetID(),
web_contents()->GetRenderViewHost()->GetRoutingID(),
bridge_id);
}
GeolocationPermissionRequestID
GeolocationPermissionContextTests::RequestIDForTab(int tab, int bridge_id) {
return GeolocationPermissionRequestID(
extra_tabs_[tab]->GetRenderProcessHost()->GetID(),
extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(),
bridge_id);
}
void GeolocationPermissionContextTests::RequestGeolocationPermission(
const GeolocationPermissionRequestID& id,
const GURL& requesting_frame) {
geolocation_permission_context_->RequestGeolocationPermission(
id.render_process_id(), id.render_view_id(), id.bridge_id(),
requesting_frame,
base::Bind(&GeolocationPermissionContextTests::PermissionResponse,
base::Unretained(this), id));
}
void GeolocationPermissionContextTests::CancelGeolocationPermissionRequest(
const GeolocationPermissionRequestID& id,
const GURL& requesting_frame) {
geolocation_permission_context_->CancelGeolocationPermissionRequest(
id.render_process_id(), id.render_view_id(), id.bridge_id(),
requesting_frame);
}
void GeolocationPermissionContextTests::PermissionResponse(
const GeolocationPermissionRequestID& id,
bool allowed) {
responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed);
}
void GeolocationPermissionContextTests::CheckPermissionMessageSent(
int bridge_id,
bool allowed) {
CheckPermissionMessageSentInternal(process(), bridge_id, allowed);
}
void GeolocationPermissionContextTests::CheckPermissionMessageSentForTab(
int tab,
int bridge_id,
bool allowed) {
CheckPermissionMessageSentInternal(static_cast<MockRenderProcessHost*>(
extra_tabs_[tab]->GetRenderProcessHost()),
bridge_id, allowed);
}
void GeolocationPermissionContextTests::CheckPermissionMessageSentInternal(
MockRenderProcessHost* process,
int bridge_id,
bool allowed) {
ASSERT_EQ(responses_.count(process->GetID()), 1U);
EXPECT_EQ(bridge_id, responses_[process->GetID()].first);
EXPECT_EQ(allowed, responses_[process->GetID()].second);
responses_.erase(process->GetID());
}
void GeolocationPermissionContextTests::AddNewTab(const GURL& url) {
content::WebContents* new_tab = content::WebContents::Create(
content::WebContents::CreateParams(profile()));
new_tab->GetController().LoadURL(
url, content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string());
content::RenderViewHostTester::For(new_tab->GetRenderViewHost())->
SendNavigate(extra_tabs_.size() + 1, url);
// Set up required helpers, and make this be as "tabby" as the code requires.
extensions::SetViewType(new_tab, extensions::VIEW_TYPE_TAB_CONTENTS);
InfoBarService::CreateForWebContents(new_tab);
extra_tabs_.push_back(new_tab);
}
void GeolocationPermissionContextTests::CheckTabContentsState(
const GURL& requesting_frame,
ContentSetting expected_content_setting) {
TabSpecificContentSettings* content_settings =
TabSpecificContentSettings::FromWebContents(web_contents());
const GeolocationSettingsState::StateMap& state_map =
content_settings->geolocation_settings_state().state_map();
EXPECT_EQ(1U, state_map.count(requesting_frame.GetOrigin()));
EXPECT_EQ(0U, state_map.count(requesting_frame));
GeolocationSettingsState::StateMap::const_iterator settings =
state_map.find(requesting_frame.GetOrigin());
ASSERT_FALSE(settings == state_map.end())
<< "geolocation state not found " << requesting_frame;
EXPECT_EQ(expected_content_setting, settings->second);
}
void GeolocationPermissionContextTests::SetUp() {
ChromeRenderViewHostTestHarness::SetUp();
// Set up required helpers, and make this be as "tabby" as the code requires.
extensions::SetViewType(web_contents(), extensions::VIEW_TYPE_TAB_CONTENTS);
InfoBarService::CreateForWebContents(web_contents());
TabSpecificContentSettings::CreateForWebContents(web_contents());
#if defined(OS_ANDROID)
MockGoogleLocationSettingsHelper::SetLocationStatus(true, true);
#endif
geolocation_permission_context_ =
ChromeGeolocationPermissionContextFactory::GetForProfile(profile());
}
void GeolocationPermissionContextTests::TearDown() {
extra_tabs_.clear();
ChromeRenderViewHostTestHarness::TearDown();
}
// Tests ----------------------------------------------------------------------
TEST_F(GeolocationPermissionContextTests, SinglePermission) {
GURL requesting_frame("http://www.example.com/geolocation");
NavigateAndCommit(requesting_frame);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
ASSERT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate);
infobar_delegate->Cancel();
infobar_service()->RemoveInfoBar(infobar_delegate);
EXPECT_EQ(1U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate));
delete infobar_delegate;
}
#if defined(OS_ANDROID)
TEST_F(GeolocationPermissionContextTests, GeolocationEnabledDisabled) {
GURL requesting_frame("http://www.example.com/geolocation");
NavigateAndCommit(requesting_frame);
MockGoogleLocationSettingsHelper::SetLocationStatus(true, true);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
EXPECT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate_0 =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_0);
string16 text_0 = infobar_delegate_0->GetButtonLabel(
ConfirmInfoBarDelegate::BUTTON_OK);
NavigateAndCommit(requesting_frame);
MockGoogleLocationSettingsHelper::SetLocationStatus(true, false);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
EXPECT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate_1 =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_1);
string16 text_1 = infobar_delegate_1->GetButtonLabel(
ConfirmInfoBarDelegate::BUTTON_OK);
EXPECT_NE(text_0, text_1);
NavigateAndCommit(requesting_frame);
MockGoogleLocationSettingsHelper::SetLocationStatus(false, false);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
EXPECT_EQ(0U, infobar_service()->infobar_count());
}
TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsEnabled) {
GURL requesting_frame("http://www.example.com/geolocation");
NavigateAndCommit(requesting_frame);
MockGoogleLocationSettingsHelper::SetLocationStatus(true, true);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
EXPECT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate);
infobar_delegate->Accept();
CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
CheckPermissionMessageSent(0, true);
}
TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsDisabled) {
GURL requesting_frame("http://www.example.com/geolocation");
NavigateAndCommit(requesting_frame);
MockGoogleLocationSettingsHelper::SetLocationStatus(true, false);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
EXPECT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate);
infobar_delegate->Accept();
EXPECT_TRUE(
MockGoogleLocationSettingsHelper::WasGoogleLocationSettingsCalled());
}
#endif
TEST_F(GeolocationPermissionContextTests, QueuedPermission) {
GURL requesting_frame_0("http://www.example.com/geolocation");
GURL requesting_frame_1("http://www.example-2.com/geolocation");
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_0, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_1, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
NavigateAndCommit(requesting_frame_0);
EXPECT_EQ(0U, infobar_service()->infobar_count());
// Request permission for two frames.
RequestGeolocationPermission(RequestID(0), requesting_frame_0);
RequestGeolocationPermission(RequestID(1), requesting_frame_1);
// Ensure only one infobar is created.
ASSERT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate_0 =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_0);
string16 text_0 = infobar_delegate_0->GetMessageText();
// Accept the first frame.
infobar_delegate_0->Accept();
CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW);
CheckPermissionMessageSent(0, true);
infobar_service()->RemoveInfoBar(infobar_delegate_0);
EXPECT_EQ(1U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_0));
closed_infobar_tracker_.Clear();
delete infobar_delegate_0;
// Now we should have a new infobar for the second frame.
ASSERT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate_1 =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_1);
string16 text_1 = infobar_delegate_1->GetMessageText();
EXPECT_NE(text_0, text_1);
// Cancel (block) this frame.
infobar_delegate_1->Cancel();
CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_BLOCK);
CheckPermissionMessageSent(1, false);
infobar_service()->RemoveInfoBar(infobar_delegate_1);
EXPECT_EQ(1U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_1));
delete infobar_delegate_1;
EXPECT_EQ(0U, infobar_service()->infobar_count());
// Ensure the persisted permissions are ok.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_0, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_1, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
}
TEST_F(GeolocationPermissionContextTests, PermissionForFileScheme) {
GURL requesting_frame("file://example/geolocation.html");
NavigateAndCommit(requesting_frame);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
EXPECT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate);
// Accept the frame
infobar_delegate->Accept();
CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
CheckPermissionMessageSent(0, true);
infobar_service()->RemoveInfoBar(infobar_delegate);
delete infobar_delegate;
// Make sure the setting is not stored.
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame,
requesting_frame,
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string()));
}
TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) {
GURL requesting_frame_0("http://www.example.com/geolocation");
GURL requesting_frame_1("http://www.example-2.com/geolocation");
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_0, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_1, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
NavigateAndCommit(requesting_frame_0);
EXPECT_EQ(0U, infobar_service()->infobar_count());
// Request permission for two frames.
RequestGeolocationPermission(RequestID(0), requesting_frame_0);
RequestGeolocationPermission(RequestID(1), requesting_frame_1);
ASSERT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate_0 =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_0);
string16 text_0 = infobar_delegate_0->GetMessageText();
// Simulate the frame going away, ensure the infobar for this frame
// is removed and the next pending infobar is created.
CancelGeolocationPermissionRequest(RequestID(0), requesting_frame_0);
EXPECT_EQ(1U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_0));
closed_infobar_tracker_.Clear();
delete infobar_delegate_0;
ASSERT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate_1 =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_1);
string16 text_1 = infobar_delegate_1->GetMessageText();
EXPECT_NE(text_0, text_1);
// Allow this frame.
infobar_delegate_1->Accept();
CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW);
CheckPermissionMessageSent(1, true);
infobar_service()->RemoveInfoBar(infobar_delegate_1);
EXPECT_EQ(1U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_1));
delete infobar_delegate_1;
EXPECT_EQ(0U, infobar_service()->infobar_count());
// Ensure the persisted permissions are ok.
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_0, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_1, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
}
TEST_F(GeolocationPermissionContextTests, InvalidURL) {
GURL invalid_embedder("about:blank");
GURL requesting_frame;
NavigateAndCommit(invalid_embedder);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), requesting_frame);
EXPECT_EQ(0U, infobar_service()->infobar_count());
CheckPermissionMessageSent(0, false);
}
TEST_F(GeolocationPermissionContextTests, SameOriginMultipleTabs) {
GURL url_a("http://www.example.com/geolocation");
GURL url_b("http://www.example-2.com/geolocation");
NavigateAndCommit(url_a);
AddNewTab(url_b);
AddNewTab(url_a);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), url_a);
ASSERT_EQ(1U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestIDForTab(0, 0), url_b);
EXPECT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
RequestGeolocationPermission(RequestIDForTab(1, 0), url_a);
ASSERT_EQ(1U, infobar_service_for_tab(1)->infobar_count());
ConfirmInfoBarDelegate* removed_infobar =
infobar_service_for_tab(1)->infobar_at(0)->AsConfirmInfoBarDelegate();
// Accept the first tab.
ConfirmInfoBarDelegate* infobar_delegate_0 =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_0);
infobar_delegate_0->Accept();
CheckPermissionMessageSent(0, true);
infobar_service()->RemoveInfoBar(infobar_delegate_0);
EXPECT_EQ(2U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_0));
delete infobar_delegate_0;
// Now the infobar for the tab with the same origin should have gone.
EXPECT_EQ(0U, infobar_service_for_tab(1)->infobar_count());
CheckPermissionMessageSentForTab(1, 0, true);
EXPECT_TRUE(closed_infobar_tracker_.Contains(removed_infobar));
closed_infobar_tracker_.Clear();
// Destroy the infobar that has just been removed.
delete removed_infobar;
// But the other tab should still have the info bar...
ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
ConfirmInfoBarDelegate* infobar_delegate_1 =
infobar_service_for_tab(0)->infobar_at(0)->AsConfirmInfoBarDelegate();
infobar_delegate_1->Cancel();
infobar_service_for_tab(0)->RemoveInfoBar(infobar_delegate_1);
EXPECT_EQ(1U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_1));
delete infobar_delegate_1;
}
TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) {
GURL url_a("http://www.example.com/geolocation");
GURL url_b("http://www.example-2.com/geolocation");
NavigateAndCommit(url_a);
AddNewTab(url_a);
EXPECT_EQ(0U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestID(0), url_a);
ASSERT_EQ(1U, infobar_service()->infobar_count());
RequestGeolocationPermission(RequestIDForTab(0, 0), url_a);
EXPECT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
RequestGeolocationPermission(RequestIDForTab(0, 1), url_b);
ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
ConfirmInfoBarDelegate* removed_infobar =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
// Accept the second tab.
ConfirmInfoBarDelegate* infobar_delegate_0 =
infobar_service_for_tab(0)->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_0);
infobar_delegate_0->Accept();
CheckPermissionMessageSentForTab(0, 0, true);
infobar_service_for_tab(0)->RemoveInfoBar(infobar_delegate_0);
EXPECT_EQ(2U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_0));
delete infobar_delegate_0;
// Now the infobar for the tab with the same origin should have gone.
EXPECT_EQ(0U, infobar_service()->infobar_count());
CheckPermissionMessageSent(0, true);
EXPECT_TRUE(closed_infobar_tracker_.Contains(removed_infobar));
closed_infobar_tracker_.Clear();
delete removed_infobar;
// And we should have the queued infobar displayed now.
ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
// Accept the second infobar.
ConfirmInfoBarDelegate* infobar_delegate_1 =
infobar_service_for_tab(0)->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar_delegate_1);
infobar_delegate_1->Accept();
CheckPermissionMessageSentForTab(0, 1, true);
infobar_service_for_tab(0)->RemoveInfoBar(infobar_delegate_1);
EXPECT_EQ(1U, closed_infobar_tracker_.size());
EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_delegate_1));
delete infobar_delegate_1;
}
TEST_F(GeolocationPermissionContextTests, TabDestroyed) {
GURL requesting_frame_0("http://www.example.com/geolocation");
GURL requesting_frame_1("http://www.example-2.com/geolocation");
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_0, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
profile()->GetHostContentSettingsMap()->GetContentSetting(
requesting_frame_1, requesting_frame_0,
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
NavigateAndCommit(requesting_frame_0);
EXPECT_EQ(0U, infobar_service()->infobar_count());
// Request permission for two frames.
RequestGeolocationPermission(RequestID(0), requesting_frame_0);
RequestGeolocationPermission(RequestID(1), requesting_frame_1);
// Ensure only one infobar is created.
ASSERT_EQ(1U, infobar_service()->infobar_count());
ConfirmInfoBarDelegate* infobar =
infobar_service()->infobar_at(0)->AsConfirmInfoBarDelegate();
ASSERT_TRUE(infobar);
// Delete the tab contents.
DeleteContents();
delete infobar;
// During contents destruction, the infobar will have been closed, and the
// pending request should have been cleared without an infobar being created.
ASSERT_EQ(1U, closed_infobar_tracker_.size());
ASSERT_TRUE(closed_infobar_tracker_.Contains(infobar));
}
TEST_F(GeolocationPermissionContextTests, InfoBarUsesCommittedEntry) {
GURL requesting_frame_0("http://www.example.com/geolocation");
GURL requesting_frame_1("http://www.example-2.com/geolocation");
NavigateAndCommit(requesting_frame_0);
NavigateAndCommit(requesting_frame_1);
EXPECT_EQ(0U, infobar_service()->infobar_count());
// Go back: navigate to a pending entry before requesting geolocation
// permission.
web_contents()->GetController().GoBack();
// Request permission for the committed frame (not the pending one).
RequestGeolocationPermission(RequestID(0), requesting_frame_1);
// Ensure the infobar is created.
ASSERT_EQ(1U, infobar_service()->infobar_count());
InfoBarDelegate* infobar_delegate = infobar_service()->infobar_at(0);
ASSERT_TRUE(infobar_delegate);
// Ensure the infobar wouldn't expire for a navigation to the committed entry.
content::LoadCommittedDetails details;
details.entry = web_contents()->GetController().GetLastCommittedEntry();
EXPECT_FALSE(infobar_delegate->ShouldExpire(details));
// Ensure the infobar will expire when we commit the pending navigation.
details.entry = web_contents()->GetController().GetActiveEntry();
EXPECT_TRUE(infobar_delegate->ShouldExpire(details));
// Delete the tab contents.
DeleteContents();
delete infobar_delegate;
}
|