forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest_tracker_impl.mm
890 lines (780 loc) · 30.7 KB
/
request_tracker_impl.mm
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
// Copyright 2014 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.
#import "ios/web/net/request_tracker_impl.h"
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include "base/bind.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/sys_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/task/post_task.h"
#include "ios/web/history_state_util.h"
#include "ios/web/public/browser_state.h"
#include "ios/web/public/certificate_policy_cache.h"
#include "ios/web/public/url_util.h"
#include "ios/web/public/web_task_traits.h"
#include "ios/web/public/web_thread.h"
#import "net/base/mac/url_conversions.h"
#include "net/url_request/url_request.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// A map of all RequestTrackerImpls for tabs that are:
// * Currently open
// * Recently closed waiting for all their network operations to finish.
// The code accesses this variable from two threads: the consumer is expected to
// always access it from the main thread, the provider is accessing it from the
// WebThread, a thread created by the UIWebView/CFURL. For this reason access to
// this variable must always gated by |g_trackers_lock|.
typedef base::hash_map<std::string, web::RequestTrackerImpl*> TrackerMap;
TrackerMap* g_trackers = NULL;
base::Lock* g_trackers_lock = NULL;
pthread_once_t g_once_control = PTHREAD_ONCE_INIT;
// Flag, lock, and function to implement BlockUntilTrackersShutdown().
// |g_waiting_on_io_thread| is guarded by |g_waiting_on_io_thread_lock|;
// it is set to true when the shutdown wait starts, then a call to
// StopIOThreadWaiting is posted to the IO thread (enqueued after any pending
// request terminations) while the posting method loops over a check on the
// |g_waiting_on_io_thread|.
static bool g_waiting_on_io_thread = false;
base::Lock* g_waiting_on_io_thread_lock = NULL;
void StopIOThreadWaiting() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
base::AutoLock scoped_lock(*g_waiting_on_io_thread_lock);
g_waiting_on_io_thread = false;
}
// Initialize global state. Calls to this should be conditional on
// |g_once_control| (that is, this should only be called once, across all
// threads).
void InitializeGlobals() {
g_trackers = new TrackerMap;
g_trackers_lock = new base::Lock;
g_waiting_on_io_thread_lock = new base::Lock;
}
// Each request tracker get a unique increasing number, used anywhere an
// identifier is needed for tracker (e.g. storing certs).
int g_next_request_tracker_id = 0;
// Add |tracker| to |g_trackers| under |key|.
static void RegisterTracker(web::RequestTrackerImpl* tracker, NSString* key) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
pthread_once(&g_once_control, &InitializeGlobals);
{
std::string scoped_key = base::SysNSStringToUTF8(key);
base::AutoLock scoped_lock(*g_trackers_lock);
DCHECK(!g_trackers->count(scoped_key));
(*g_trackers)[scoped_key] = tracker;
}
}
} // namespace
// The structure used to gather the information about the resources loaded.
struct TrackerCounts {
public:
TrackerCounts(const GURL& tracked_url, const net::URLRequest* tracked_request)
: url(tracked_url),
site_for_cookies_origin(
tracked_request->site_for_cookies().GetOrigin()),
request(tracked_request),
ssl_info(net::SSLInfo()),
ssl_judgment(web::CertPolicy::ALLOWED),
allowed_by_user(false),
expected_length(0),
processed(0),
done(false) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
is_subrequest =
tracked_request->site_for_cookies().is_valid() &&
tracked_request->url() != tracked_request->site_for_cookies();
};
// The resource url.
const GURL url;
// The origin of the url of the top level document of the resource. This is
// used to ignore request coming from an old document when detecting mixed
// content.
const GURL site_for_cookies_origin;
// The request associated with this struct. As a void* to prevent access from
// the wrong thread.
const void* request;
// SSLInfo for the request.
net::SSLInfo ssl_info;
// Is the SSL request blocked waiting for user choice.
web::CertPolicy::Judgment ssl_judgment;
// True if |ssl_judgment| is ALLOWED as the result of a user choice.
bool allowed_by_user;
// block to call to cancel or authorize a blocked request.
net::RequestTracker::SSLCallback ssl_callback;
// If known, the expected length of the resource in bytes.
uint64_t expected_length;
// Number of bytes loaded so far.
uint64_t processed;
// Set to true is the resource is fully loaded.
bool done;
// Set to true if the request has a main request set.
bool is_subrequest;
NSString* Description() {
NSString* spec = base::SysUTF8ToNSString(url.spec());
NSString* status = nil;
if (done) {
status = [NSString stringWithFormat:@"\t-- Done -- (%04qu) bytes",
processed];
} else if (!expected_length) {
status = [NSString stringWithFormat:@"\t>> Loading (%04qu) bytes",
processed];
} else {
status = [NSString stringWithFormat:@"\t>> Loading (%04qu/%04qu)",
processed, expected_length];
}
NSString* ssl = @"";
if (ssl_info.is_valid()) {
NSString* subject = base::SysUTF8ToNSString(
ssl_info.cert.get()->subject().GetDisplayName());
NSString* issuer = base::SysUTF8ToNSString(
ssl_info.cert.get()->issuer().GetDisplayName());
ssl = [NSString stringWithFormat:
@"\n\t\tcert for '%@' issued by '%@'", subject, issuer];
if (!net::IsCertStatusMinorError(ssl_info.cert_status)) {
ssl = [NSString stringWithFormat:@"%@ (status: %0xd)",
ssl, ssl_info.cert_status];
}
}
return [NSString stringWithFormat:@"%@\n\t\t%@%@", status, spec, ssl];
}
DISALLOW_COPY_AND_ASSIGN(TrackerCounts);
};
namespace web {
#pragma mark Consumer API
// static
scoped_refptr<RequestTrackerImpl>
RequestTrackerImpl::CreateTrackerForRequestGroupID(
NSString* request_group_id,
BrowserState* browser_state,
net::URLRequestContextGetter* context_getter) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
DCHECK(request_group_id);
scoped_refptr<RequestTrackerImpl> tracker =
new RequestTrackerImpl(request_group_id, context_getter);
scoped_refptr<CertificatePolicyCache> policy_cache =
BrowserState::GetCertificatePolicyCache(browser_state);
DCHECK(policy_cache);
// Take care of the IO-thread init.
base::PostTaskWithTraits(
FROM_HERE, {web::WebThread::IO},
base::Bind(&RequestTrackerImpl::InitOnIOThread, tracker, policy_cache));
RegisterTracker(tracker.get(), request_group_id);
return tracker;
}
void RequestTrackerImpl::StartPageLoad(const GURL& url, id user_info) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
id scoped_user_info = user_info;
base::PostTaskWithTraits(
FROM_HERE, {web::WebThread::IO},
base::Bind(&RequestTrackerImpl::TrimToURL, this, url, scoped_user_info));
}
void RequestTrackerImpl::FinishPageLoad(const GURL& url, bool load_success) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {web::WebThread::IO},
base::Bind(&RequestTrackerImpl::StopPageLoad, this, url, load_success));
}
void RequestTrackerImpl::HistoryStateChange(const GURL& url) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {web::WebThread::IO},
base::Bind(&RequestTrackerImpl::HistoryStateChangeToURL, this, url));
}
// Close is called when an owning object (a Tab or something that acts like
// it) is done with the RequestTrackerImpl. There may still be queued calls on
// the UI thread that will make use of the fields being cleaned-up here; they
// must ensure they they operate without crashing with the cleaned-up values.
void RequestTrackerImpl::Close() {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
// Mark the tracker as closing on the IO thread. Note that because the local
// scoped_refptr here retains |this|, we a are guaranteed that destruiction
// won't begin until the block completes, and thus |is_closing_| will always
// be set before destruction begins.
base::PostTaskWithTraits(
FROM_HERE, {web::WebThread::IO},
base::Bind(
[](RequestTrackerImpl* tracker) { tracker->is_closing_ = true; },
base::RetainedRef(this)));
// The user_info is no longer needed.
user_info_ = nil;
}
// static
void RequestTrackerImpl::RunAfterRequestsCancel(const base::Closure& callback) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
// Post a no-op to the IO thread, and after that has executed, run |callback|.
// This ensures that |callback| runs after anything elese queued on the IO
// thread, in particular CancelRequest() calls made from closing trackers.
base::PostTaskWithTraitsAndReply(FROM_HERE, {web::WebThread::IO},
base::DoNothing(), callback);
}
// static
void RequestTrackerImpl::BlockUntilTrackersShutdown() {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
// Initialize the globals as part of the shutdown to prevent a crash when
// trying to acquire the lock if it was never initialised. It can happen
// if the application is terminated when no RequestTracker has been created
// (which can happen if no UIWebView has been created). See crbug.com/684611
// for information on such a crash.
//
// As RequestTracker are deprecated and are only used in Sign-In workflow
// it is simpler to just do the initialisation here than tracking whether
// the method should be called or not by client code.
pthread_once(&g_once_control, &InitializeGlobals);
{
base::AutoLock scoped_lock(*g_waiting_on_io_thread_lock);
g_waiting_on_io_thread = true;
}
base::PostTaskWithTraits(FROM_HERE, {web::WebThread::IO},
base::Bind(&StopIOThreadWaiting));
// Poll endlessly until the wait flag is unset on the IO thread by
// StopIOThreadWaiting().
// (Consider instead having a hard time cap, like 100ms or so, after which
// we stop blocking. In that case this method would return a boolean
// indicating if the wait completed or not).
while (1) {
base::AutoLock scoped_lock(*g_waiting_on_io_thread_lock);
if (!g_waiting_on_io_thread)
return;
// Ensure that other threads have a chance to run even on a single-core
// devices.
pthread_yield_np();
}
}
#pragma mark Provider API
// static
RequestTrackerImpl* RequestTrackerImpl::GetTrackerForRequestGroupID(
NSString* request_group_id) {
DCHECK(request_group_id);
DCHECK_CURRENTLY_ON(web::WebThread::IO);
RequestTrackerImpl* tracker = nullptr;
TrackerMap::iterator map_it;
pthread_once(&g_once_control, &InitializeGlobals);
{
base::AutoLock scoped_lock(*g_trackers_lock);
map_it = g_trackers->find(base::SysNSStringToUTF8(request_group_id));
if (map_it != g_trackers->end())
tracker = map_it->second;
}
return tracker;
}
net::URLRequestContext* RequestTrackerImpl::GetRequestContext() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
return request_context_getter_->GetURLRequestContext();
}
void RequestTrackerImpl::StartRequest(net::URLRequest* request) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
DCHECK(!counts_by_request_.count(request));
DCHECK(!request->url().SchemeIsFile());
if (new_estimate_round_) {
// Starting a new estimate round. Ignore the previous requests for the
// calculation.
counts_by_request_.clear();
estimate_start_index_ = counts_.size();
new_estimate_round_ = false;
}
const GURL& url = request->original_url();
auto counts =
std::make_unique<TrackerCounts>(GURLByRemovingRefFromGURL(url), request);
counts_by_request_[request] = counts.get();
counts_.push_back(std::move(counts));
if (page_url_.SchemeIsCryptographic() && !url.SchemeIsCryptographic())
has_mixed_content_ = true;
Notify();
}
void RequestTrackerImpl::CaptureExpectedLength(const net::URLRequest* request,
uint64_t length) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (counts_by_request_.count(request)) {
TrackerCounts* counts = counts_by_request_[request];
DCHECK(!counts->done);
if (length < counts->processed) {
// Something is wrong with the estimate. Ignore it.
counts->expected_length = 0;
} else {
counts->expected_length = length;
}
Notify();
}
}
void RequestTrackerImpl::CaptureReceivedBytes(const net::URLRequest* request,
uint64_t byte_count) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (counts_by_request_.count(request)) {
TrackerCounts* counts = counts_by_request_[request];
DCHECK(!counts->done);
const net::SSLInfo& ssl_info = request->ssl_info();
if (ssl_info.is_valid())
counts->ssl_info = ssl_info;
counts->processed += byte_count;
if (counts->expected_length > 0 &&
counts->expected_length < counts->processed) {
// Something is wrong with the estimate, it is too low. Ignore it.
counts->expected_length = 0;
}
Notify();
}
}
void RequestTrackerImpl::StopRequest(net::URLRequest* request) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (counts_by_request_.count(request)) {
StopRedirectedRequest(request);
Notify();
}
}
void RequestTrackerImpl::StopRedirectedRequest(net::URLRequest* request) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (counts_by_request_.count(request)) {
TrackerCounts* counts = counts_by_request_[request];
DCHECK(!counts->done);
const net::SSLInfo& ssl_info = request->ssl_info();
if (ssl_info.is_valid())
counts->ssl_info = ssl_info;
counts->done = true;
counts_by_request_.erase(request);
}
}
void RequestTrackerImpl::CaptureCertificatePolicyCache(
const net::URLRequest* request,
const RequestTracker::SSLCallback& should_continue) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
std::string host = request->url().host();
CertPolicy::Judgment judgment = policy_cache_->QueryPolicy(
request->ssl_info().cert.get(), host, request->ssl_info().cert_status);
if (judgment == CertPolicy::UNKNOWN) {
// The request comes from the cache, and has been loaded even though the
// policy is UNKNOWN. Display the interstitial page now.
OnSSLCertificateError(request, request->ssl_info(), true, should_continue);
return;
}
// Notify the delegate that a judgment has been used.
DCHECK(judgment == CertPolicy::ALLOWED);
should_continue.Run(true);
}
void RequestTrackerImpl::OnSSLCertificateError(
const net::URLRequest* request,
const net::SSLInfo& ssl_info,
bool recoverable,
const RequestTracker::SSLCallback& should_continue) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
DCHECK(ssl_info.is_valid());
if (counts_by_request_.count(request)) {
TrackerCounts* counts = counts_by_request_[request];
DCHECK(!counts->done);
// Store the ssl error.
counts->ssl_info = ssl_info;
counts->ssl_callback = should_continue;
counts->ssl_judgment =
recoverable ? CertPolicy::UNKNOWN : CertPolicy::DENIED;
ReevaluateCallbacksForAllCounts();
}
}
#pragma mark Client utility methods.
void RequestTrackerImpl::PostUITaskIfOpen(const base::Closure& task) {
PostTask(task, web::WebThread::UI);
}
// static
void RequestTrackerImpl::PostUITaskIfOpen(
const base::WeakPtr<RequestTracker> tracker,
const base::Closure& task) {
if (!tracker)
return;
RequestTrackerImpl* tracker_impl =
static_cast<RequestTrackerImpl*>(tracker.get());
tracker_impl->PostUITaskIfOpen(task);
}
void RequestTrackerImpl::PostIOTask(const base::Closure& task) {
PostTask(task, web::WebThread::IO);
}
void RequestTrackerImpl::ScheduleIOTask(const base::Closure& task) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
base::PostTaskWithTraits(FROM_HERE, {web::WebThread::IO}, task);
}
#pragma mark Private Object Lifecycle API
RequestTrackerImpl::RequestTrackerImpl(
NSString* request_group_id,
net::URLRequestContextGetter* context_getter)
: previous_estimate_(0.0f), // Not active by default.
estimate_start_index_(0),
notification_depth_(0),
has_mixed_content_(false),
is_loading_(false),
new_estimate_round_(true),
request_context_getter_(context_getter),
identifier_(++g_next_request_tracker_id),
request_group_id_([request_group_id copy]),
is_closing_(false) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
}
void RequestTrackerImpl::InitOnIOThread(
const scoped_refptr<CertificatePolicyCache>& policy_cache) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
Init();
DCHECK(policy_cache);
policy_cache_ = policy_cache;
}
RequestTrackerImpl::~RequestTrackerImpl() {
}
void RequestTrackerImplTraits::Destruct(const RequestTrackerImpl* t) {
// RefCountedThreadSafe assumes we can do all the destruct tasks with a
// const pointer, but we actually can't.
RequestTrackerImpl* inconstant_t = const_cast<RequestTrackerImpl*>(t);
if (web::WebThread::CurrentlyOn(web::WebThread::IO)) {
inconstant_t->Destruct();
} else {
// Use BindBlock rather than Bind to avoid creating another scoped_refpter
// to |this|. |inconstant_t| isn't retained by the block, but since this
// method is the mechanism by which all RequestTrackerImpl instances are
// destroyed, the object inconstant_t points to won't be deleted while
// the block is executing (and Destruct() itself will do the deleting).
base::PostTaskWithTraits(FROM_HERE, {web::WebThread::IO}, base::BindOnce(^{
inconstant_t->Destruct();
}));
}
}
void RequestTrackerImpl::Destruct() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
DCHECK(is_closing_);
pthread_once(&g_once_control, &InitializeGlobals);
{
base::AutoLock scoped_lock(*g_trackers_lock);
g_trackers->erase(base::SysNSStringToUTF8(request_group_id_));
}
InvalidateWeakPtrs();
// Delete on the UI thread.
base::PostTaskWithTraits(FROM_HERE, {web::WebThread::UI}, base::BindOnce(^{
delete this;
}));
}
#pragma mark Other private methods
void RequestTrackerImpl::Notify() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (is_closing_)
return;
// Notify() is called asynchronously, it runs later on the same
// thread. This is used to collate notifications together, avoiding
// blanketing the UI with a stream of information.
notification_depth_ += 1;
base::PostTaskWithTraits(
FROM_HERE, {web::WebThread::IO},
base::Bind(&RequestTrackerImpl::StackNotification, this));
}
void RequestTrackerImpl::StackNotification() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (is_closing_)
return;
// There is no point in sending the notification if there is another one
// already queued. This queue is processing very lightweight changes and
// should be exhausted very easily.
--notification_depth_;
if (notification_depth_)
return;
}
void RequestTrackerImpl::EvaluateSSLCallbackForCounts(TrackerCounts* counts) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
DCHECK(policy_cache_);
// Ignore non-SSL requests.
if (!counts->ssl_info.is_valid())
return;
CertPolicy::Judgment judgment =
policy_cache_->QueryPolicy(counts->ssl_info.cert.get(),
counts->url.host(),
counts->ssl_info.cert_status);
if (judgment != CertPolicy::ALLOWED) {
// Apply some fine tuning.
// TODO(droger): This logic is duplicated from SSLPolicy. Sharing the code
// would be better.
switch (net::MapCertStatusToNetError(counts->ssl_info.cert_status)) {
case net::ERR_CERT_NO_REVOCATION_MECHANISM:
// Ignore this error.
judgment = CertPolicy::ALLOWED;
break;
case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION:
// We ignore this error but it will show a warning status in the
// location bar.
judgment = CertPolicy::ALLOWED;
break;
case net::ERR_CERT_CONTAINS_ERRORS:
case net::ERR_CERT_REVOKED:
case net::ERR_CERT_INVALID:
case net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY:
case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN:
judgment = CertPolicy::DENIED;
break;
case net::ERR_CERT_COMMON_NAME_INVALID:
case net::ERR_CERT_DATE_INVALID:
case net::ERR_CERT_AUTHORITY_INVALID:
case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
case net::ERR_CERT_WEAK_KEY:
case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION:
case net::ERR_CERT_VALIDITY_TOO_LONG:
// Nothing. If DENIED it will stay denied. If UNKNOWN it will be
// shown to the user for decision.
break;
default:
NOTREACHED();
judgment = CertPolicy::DENIED;
break;
}
}
counts->ssl_judgment = judgment;
switch (judgment) {
case CertPolicy::UNKNOWN:
case CertPolicy::DENIED:
// Simply cancel the request.
CancelRequestForCounts(counts);
break;
case CertPolicy::ALLOWED:
counts->ssl_callback.Run(YES);
counts->ssl_callback = base::DoNothing();
break;
default:
NOTREACHED();
// For now simply cancel the request.
CancelRequestForCounts(counts);
break;
}
}
void RequestTrackerImpl::ReevaluateCallbacksForAllCounts() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (is_closing_)
return;
for (const auto& tracker_count : counts_) {
// Check if the value hasn't changed via a user action.
if (tracker_count->ssl_judgment == CertPolicy::UNKNOWN)
EvaluateSSLCallbackForCounts(tracker_count.get());
}
}
void RequestTrackerImpl::CancelRequestForCounts(TrackerCounts* counts) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
// Cancel the request.
counts->done = true;
counts_by_request_.erase(counts->request);
counts->ssl_callback.Run(NO);
counts->ssl_callback = base::DoNothing();
Notify();
}
PageCounts RequestTrackerImpl::pageCounts() {
DCHECK_GE(counts_.size(), estimate_start_index_);
PageCounts page_counts;
for (const auto& tracker_count : counts_) {
if (tracker_count->done) {
uint64_t size = tracker_count->processed;
page_counts.finished += 1;
page_counts.finished_bytes += size;
if (page_counts.largest_byte_size_known < size) {
page_counts.largest_byte_size_known = size;
}
} else {
page_counts.unfinished += 1;
if (tracker_count->expected_length) {
uint64_t size = tracker_count->expected_length;
page_counts.unfinished_estimate_bytes_done += tracker_count->processed;
page_counts.unfinished_estimated_bytes_left += size;
if (page_counts.largest_byte_size_known < size) {
page_counts.largest_byte_size_known = size;
}
} else {
page_counts.unfinished_no_estimate += 1;
page_counts.unfinished_no_estimate_bytes_done +=
tracker_count->processed;
}
}
}
return page_counts;
}
float RequestTrackerImpl::EstimatedProgress() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
const PageCounts page_counts = pageCounts();
// Nothing in progress and the last time was the same.
if (!page_counts.unfinished && previous_estimate_ == 0.0f)
return -1.0f;
// First request.
if (previous_estimate_ == 0.0f) {
// start low.
previous_estimate_ = 0.1f;
return previous_estimate_; // Return the just started status.
}
// The very simple case where everything is probably done and dusted.
if (!page_counts.unfinished) {
// Add 60%, and return. Another task is going to finish this.
float bump = (1.0f - previous_estimate_) * 0.6f;
previous_estimate_ += bump;
return previous_estimate_;
}
// Calculate some ratios.
// First the ratio of the finished vs the unfinished counts of resources
// loaded.
float unfinishedRatio =
static_cast<float>(page_counts.finished) /
static_cast<float>(page_counts.unfinished + page_counts.finished);
// The ratio of bytes left vs bytes already downloaded for the resources where
// no estimates of final size are known. For this ratio it is assumed the size
// of a resource not downloaded yet is the maximum size of all the resources
// seen so far.
float noEstimateRatio = (!page_counts.unfinished_no_estimate_bytes_done) ?
0.0f :
static_cast<float>(page_counts.unfinished_no_estimate *
page_counts.largest_byte_size_known) /
static_cast<float>(page_counts.finished_bytes +
page_counts.unfinished_no_estimate_bytes_done);
// The ratio of bytes left vs bytes already downloaded for the resources with
// available estimated size.
float estimateRatio = (!page_counts.unfinished_estimated_bytes_left) ?
noEstimateRatio :
static_cast<float>(page_counts.unfinished_estimate_bytes_done) /
static_cast<float>(page_counts.unfinished_estimate_bytes_done +
page_counts.unfinished_estimated_bytes_left);
// Reassemble all of this.
float total =
0.1f + // Minimum value.
unfinishedRatio * 0.6f +
estimateRatio * 0.3f;
if (previous_estimate_ >= total)
return -1.0f;
// 10% of what's left.
float maxBump = (1.0f - previous_estimate_) / 10.0f;
// total is greater than previous estimate, need to bump the estimate up.
if ((previous_estimate_ + maxBump) > total) {
// Less than a 10% bump, bump to the new value.
previous_estimate_ = total;
} else {
// Just bump by 10% toward the total.
previous_estimate_ += maxBump;
}
return previous_estimate_;
}
void RequestTrackerImpl::RecomputeMixedContent(
const TrackerCounts* split_position) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
// Check if the mixed content before trimming was correct.
if (page_url_.SchemeIsCryptographic() && has_mixed_content_) {
bool old_url_has_mixed_content = false;
const GURL origin = page_url_.GetOrigin();
auto it = counts_.begin();
while (it != counts_.end() && it->get() != split_position) {
if (!(*it)->url.SchemeIsCryptographic() &&
origin == (*it)->site_for_cookies_origin) {
old_url_has_mixed_content = true;
break;
}
++it;
}
if (!old_url_has_mixed_content) {
// We marked the previous page with incorrect data about its mixed
// content. Turns out that the elements that triggered that condition
// where in fact in a subsequent page. Duh.
// Resend a notification for the |page_url_| informing the upper layer
// that the mixed content was a red herring.
has_mixed_content_ = false;
}
}
}
void RequestTrackerImpl::HistoryStateChangeToURL(const GURL& full_url) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
GURL url = GURLByRemovingRefFromGURL(full_url);
if (is_loading_ &&
web::history_state_util::IsHistoryStateChangeValid(url, page_url_)) {
page_url_ = url;
}
}
void RequestTrackerImpl::TrimToURL(const GURL& full_url, id user_info) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
GURL url = GURLByRemovingRefFromGURL(full_url);
// Locate the request with this url, if present.
bool new_url_has_mixed_content = false;
bool url_scheme_is_secure = url.SchemeIsCryptographic();
auto rit = counts_.rbegin();
while (rit != counts_.rend() && (*rit)->url != url) {
if (url_scheme_is_secure && !(*rit)->url.SchemeIsCryptographic() &&
(*rit)->site_for_cookies_origin == url.GetOrigin()) {
new_url_has_mixed_content = true;
}
++rit;
}
// |split_position| will be set to the count for the passed url if it exists.
TrackerCounts* split_position = NULL;
if (rit != counts_.rend()) {
split_position = rit->get();
} else {
// The URL was not found, everything will be trimmed. The mixed content
// calculation is invalid.
new_url_has_mixed_content = false;
// In the case of a page loaded via a HTML5 manifest there is no page
// boundary to be found. However the latest count is a request for a
// manifest. This tries to detect this peculiar case.
// This is important as if this request for the manifest is on the same
// domain as the page itself this will allow retrieval of the SSL
// information.
if (url_scheme_is_secure && counts_.size()) {
TrackerCounts* back = counts_.back().get();
const GURL& back_url = back->url;
if (back_url.SchemeIsCryptographic() &&
back_url.GetOrigin() == url.GetOrigin() && !back->is_subrequest) {
split_position = back;
}
}
}
RecomputeMixedContent(split_position);
// Trim up to that element.
auto it = counts_.begin();
while (it != counts_.end() && it->get() != split_position) {
if (!(*it)->done) {
// This is for an unfinished request on a previous page. We do not care
// about those anymore. Cancel the request.
if ((*it)->ssl_judgment == CertPolicy::UNKNOWN)
CancelRequestForCounts(it->get());
counts_by_request_.erase((*it)->request);
}
it = counts_.erase(it);
}
has_mixed_content_ = new_url_has_mixed_content;
page_url_ = url;
user_info_ = user_info;
estimate_start_index_ = 0;
is_loading_ = true;
previous_estimate_ = 0.0f;
new_estimate_round_ = true;
ReevaluateCallbacksForAllCounts();
Notify();
}
void RequestTrackerImpl::StopPageLoad(const GURL& url, bool load_success) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
DCHECK(page_url_ == GURLByRemovingRefFromGURL(url));
is_loading_ = false;
}
#pragma mark Internal utilities for task posting
void RequestTrackerImpl::PostTask(const base::Closure& task,
web::WebThread::ID thread) {
// Absolute sanity test: |thread| is one of {UI, IO}
DCHECK(thread == web::WebThread::UI || thread == web::WebThread::IO);
// Check that we're on the counterpart thread to the one we're posting to.
DCHECK_CURRENTLY_ON(thread == web::WebThread::IO ? web::WebThread::UI
: web::WebThread::IO);
// Don't post if the tracker is closing and we're on the IO thread.
// (there should be no way to call anything from the UI thread if
// the tracker is closing).
if (is_closing_ && web::WebThread::CurrentlyOn(web::WebThread::IO))
return;
base::PostTaskWithTraits(FROM_HERE, {thread}, task);
}
#pragma mark Other internal methods.
NSString* RequestTrackerImpl::UnsafeDescription() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
NSMutableArray* urls = [NSMutableArray array];
for (const auto& tracker_count : counts_)
[urls addObject:tracker_count->Description()];
return [NSString stringWithFormat:@"RequestGroupID %@\n%@\n%@",
request_group_id_,
net::NSURLWithGURL(page_url_),
[urls componentsJoinedByString:@"\n"]];
}
void RequestTrackerImpl::SetCertificatePolicyCacheForTest(
web::CertificatePolicyCache* cache) {
policy_cache_ = cache;
}
} // namespace web