Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set sw=2 ts=4 sts=2 et cin: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "mozilla/DebugOnly.h"
8
9
#include "nsLoadGroup.h"
10
11
#include "nsArrayEnumerator.h"
12
#include "nsCOMArray.h"
13
#include "nsCOMPtr.h"
14
#include "mozilla/Logging.h"
15
#include "nsString.h"
16
#include "nsTArray.h"
17
#include "mozilla/Telemetry.h"
18
#include "nsITimedChannel.h"
19
#include "nsIInterfaceRequestor.h"
20
#include "nsIRequestObserver.h"
21
#include "nsIRequestContext.h"
22
#include "CacheObserver.h"
23
#include "MainThreadUtils.h"
24
#include "RequestContextService.h"
25
#include "mozilla/Unused.h"
26
27
namespace mozilla {
28
namespace net {
29
30
//
31
// Log module for nsILoadGroup logging...
32
//
33
// To enable logging (see prlog.h for full details):
34
//
35
// set MOZ_LOG=LoadGroup:5
36
// set MOZ_LOG_FILE=network.log
37
//
38
// This enables LogLevel::Debug level information and places all output in
39
// the file network.log.
40
//
41
static LazyLogModule gLoadGroupLog("LoadGroup");
42
#undef LOG
43
#define LOG(args) MOZ_LOG(gLoadGroupLog, mozilla::LogLevel::Debug, args)
44
45
////////////////////////////////////////////////////////////////////////////////
46
47
class RequestMapEntry : public PLDHashEntryHdr {
48
public:
49
explicit RequestMapEntry(nsIRequest* aRequest) : mKey(aRequest) {}
50
51
nsCOMPtr<nsIRequest> mKey;
52
};
53
54
static bool RequestHashMatchEntry(const PLDHashEntryHdr* entry,
55
const void* key) {
56
const RequestMapEntry* e = static_cast<const RequestMapEntry*>(entry);
57
const nsIRequest* request = static_cast<const nsIRequest*>(key);
58
59
return e->mKey == request;
60
}
61
62
static void RequestHashClearEntry(PLDHashTable* table, PLDHashEntryHdr* entry) {
63
RequestMapEntry* e = static_cast<RequestMapEntry*>(entry);
64
65
// An entry is being cleared, let the entry do its own cleanup.
66
e->~RequestMapEntry();
67
}
68
69
static void RequestHashInitEntry(PLDHashEntryHdr* entry, const void* key) {
70
const nsIRequest* const_request = static_cast<const nsIRequest*>(key);
71
nsIRequest* request = const_cast<nsIRequest*>(const_request);
72
73
// Initialize the entry with placement new
74
new (entry) RequestMapEntry(request);
75
}
76
77
static const PLDHashTableOps sRequestHashOps = {
78
PLDHashTable::HashVoidPtrKeyStub, RequestHashMatchEntry,
79
PLDHashTable::MoveEntryStub, RequestHashClearEntry, RequestHashInitEntry};
80
81
static void RescheduleRequest(nsIRequest* aRequest, int32_t delta) {
82
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(aRequest);
83
if (p) p->AdjustPriority(delta);
84
}
85
86
nsLoadGroup::nsLoadGroup()
87
: mForegroundCount(0),
88
mLoadFlags(LOAD_NORMAL),
89
mDefaultLoadFlags(0),
90
mPriority(PRIORITY_NORMAL),
91
mRequests(&sRequestHashOps, sizeof(RequestMapEntry)),
92
mStatus(NS_OK),
93
mIsCanceling(false),
94
mDefaultLoadIsTimed(false),
95
mTimedRequests(0),
96
mCachedRequests(0) {
97
LOG(("LOADGROUP [%p]: Created.\n", this));
98
}
99
100
nsLoadGroup::~nsLoadGroup() {
101
DebugOnly<nsresult> rv = Cancel(NS_BINDING_ABORTED);
102
NS_ASSERTION(NS_SUCCEEDED(rv), "Cancel failed");
103
104
mDefaultLoadRequest = nullptr;
105
106
if (mRequestContext) {
107
mRequestContextService->RemoveRequestContext(mRequestContext->GetID());
108
}
109
110
LOG(("LOADGROUP [%p]: Destroyed.\n", this));
111
}
112
113
////////////////////////////////////////////////////////////////////////////////
114
// nsISupports methods:
115
116
NS_IMPL_ISUPPORTS(nsLoadGroup, nsILoadGroup, nsILoadGroupChild, nsIRequest,
117
nsISupportsPriority, nsISupportsWeakReference)
118
119
////////////////////////////////////////////////////////////////////////////////
120
// nsIRequest methods:
121
122
NS_IMETHODIMP
123
nsLoadGroup::GetName(nsACString& result) {
124
// XXX is this the right "name" for a load group?
125
126
if (!mDefaultLoadRequest) {
127
result.Truncate();
128
return NS_OK;
129
}
130
131
return mDefaultLoadRequest->GetName(result);
132
}
133
134
NS_IMETHODIMP
135
nsLoadGroup::IsPending(bool* aResult) {
136
*aResult = (mForegroundCount > 0) ? true : false;
137
return NS_OK;
138
}
139
140
NS_IMETHODIMP
141
nsLoadGroup::GetStatus(nsresult* status) {
142
if (NS_SUCCEEDED(mStatus) && mDefaultLoadRequest)
143
return mDefaultLoadRequest->GetStatus(status);
144
145
*status = mStatus;
146
return NS_OK;
147
}
148
149
static bool AppendRequestsToArray(PLDHashTable* aTable,
150
nsTArray<nsIRequest*>* aArray) {
151
for (auto iter = aTable->Iter(); !iter.Done(); iter.Next()) {
152
auto e = static_cast<RequestMapEntry*>(iter.Get());
153
nsIRequest* request = e->mKey;
154
NS_ASSERTION(request, "What? Null key in PLDHashTable entry?");
155
156
bool ok = !!aArray->AppendElement(request);
157
if (!ok) {
158
break;
159
}
160
NS_ADDREF(request);
161
}
162
163
if (aArray->Length() != aTable->EntryCount()) {
164
for (uint32_t i = 0, len = aArray->Length(); i < len; ++i) {
165
NS_RELEASE((*aArray)[i]);
166
}
167
return false;
168
}
169
return true;
170
}
171
172
NS_IMETHODIMP
173
nsLoadGroup::Cancel(nsresult status) {
174
MOZ_ASSERT(NS_IsMainThread());
175
176
NS_ASSERTION(NS_FAILED(status), "shouldn't cancel with a success code");
177
nsresult rv;
178
uint32_t count = mRequests.EntryCount();
179
180
AutoTArray<nsIRequest*, 8> requests;
181
182
if (!AppendRequestsToArray(&mRequests, &requests)) {
183
return NS_ERROR_OUT_OF_MEMORY;
184
}
185
186
// set the load group status to our cancel status while we cancel
187
// all our requests...once the cancel is done, we'll reset it...
188
//
189
mStatus = status;
190
191
// Set the flag indicating that the loadgroup is being canceled... This
192
// prevents any new channels from being added during the operation.
193
//
194
mIsCanceling = true;
195
196
nsresult firstError = NS_OK;
197
198
while (count > 0) {
199
nsCOMPtr<nsIRequest> request = dont_AddRef(requests.ElementAt(--count));
200
201
NS_ASSERTION(request, "NULL request found in list.");
202
203
if (!mRequests.Search(request)) {
204
// |request| was removed already
205
continue;
206
}
207
208
if (MOZ_LOG_TEST(gLoadGroupLog, LogLevel::Debug)) {
209
nsAutoCString nameStr;
210
request->GetName(nameStr);
211
LOG(("LOADGROUP [%p]: Canceling request %p %s.\n", this, request.get(),
212
nameStr.get()));
213
}
214
215
//
216
// Remove the request from the load group... This may cause
217
// the OnStopRequest notification to fire...
218
//
219
// XXX: What should the context be?
220
//
221
(void)RemoveRequest(request, nullptr, status);
222
223
// Cancel the request...
224
rv = request->Cancel(status);
225
226
// Remember the first failure and return it...
227
if (NS_FAILED(rv) && NS_SUCCEEDED(firstError)) firstError = rv;
228
}
229
230
if (mRequestContext) {
231
Unused << mRequestContext->CancelTailPendingRequests(status);
232
}
233
234
#if defined(DEBUG)
235
NS_ASSERTION(mRequests.EntryCount() == 0, "Request list is not empty.");
236
NS_ASSERTION(mForegroundCount == 0, "Foreground URLs are active.");
237
#endif
238
239
mStatus = NS_OK;
240
mIsCanceling = false;
241
242
return firstError;
243
}
244
245
NS_IMETHODIMP
246
nsLoadGroup::Suspend() {
247
nsresult rv, firstError;
248
uint32_t count = mRequests.EntryCount();
249
250
AutoTArray<nsIRequest*, 8> requests;
251
252
if (!AppendRequestsToArray(&mRequests, &requests)) {
253
return NS_ERROR_OUT_OF_MEMORY;
254
}
255
256
firstError = NS_OK;
257
//
258
// Operate the elements from back to front so that if items get
259
// get removed from the list it won't affect our iteration
260
//
261
while (count > 0) {
262
nsCOMPtr<nsIRequest> request = dont_AddRef(requests.ElementAt(--count));
263
264
NS_ASSERTION(request, "NULL request found in list.");
265
if (!request) continue;
266
267
if (MOZ_LOG_TEST(gLoadGroupLog, LogLevel::Debug)) {
268
nsAutoCString nameStr;
269
request->GetName(nameStr);
270
LOG(("LOADGROUP [%p]: Suspending request %p %s.\n", this, request.get(),
271
nameStr.get()));
272
}
273
274
// Suspend the request...
275
rv = request->Suspend();
276
277
// Remember the first failure and return it...
278
if (NS_FAILED(rv) && NS_SUCCEEDED(firstError)) firstError = rv;
279
}
280
281
return firstError;
282
}
283
284
NS_IMETHODIMP
285
nsLoadGroup::Resume() {
286
nsresult rv, firstError;
287
uint32_t count = mRequests.EntryCount();
288
289
AutoTArray<nsIRequest*, 8> requests;
290
291
if (!AppendRequestsToArray(&mRequests, &requests)) {
292
return NS_ERROR_OUT_OF_MEMORY;
293
}
294
295
firstError = NS_OK;
296
//
297
// Operate the elements from back to front so that if items get
298
// get removed from the list it won't affect our iteration
299
//
300
while (count > 0) {
301
nsCOMPtr<nsIRequest> request = dont_AddRef(requests.ElementAt(--count));
302
303
NS_ASSERTION(request, "NULL request found in list.");
304
if (!request) continue;
305
306
if (MOZ_LOG_TEST(gLoadGroupLog, LogLevel::Debug)) {
307
nsAutoCString nameStr;
308
request->GetName(nameStr);
309
LOG(("LOADGROUP [%p]: Resuming request %p %s.\n", this, request.get(),
310
nameStr.get()));
311
}
312
313
// Resume the request...
314
rv = request->Resume();
315
316
// Remember the first failure and return it...
317
if (NS_FAILED(rv) && NS_SUCCEEDED(firstError)) firstError = rv;
318
}
319
320
return firstError;
321
}
322
323
NS_IMETHODIMP
324
nsLoadGroup::GetLoadFlags(uint32_t* aLoadFlags) {
325
*aLoadFlags = mLoadFlags;
326
return NS_OK;
327
}
328
329
NS_IMETHODIMP
330
nsLoadGroup::SetLoadFlags(uint32_t aLoadFlags) {
331
mLoadFlags = aLoadFlags;
332
return NS_OK;
333
}
334
335
NS_IMETHODIMP
336
nsLoadGroup::GetLoadGroup(nsILoadGroup** loadGroup) {
337
nsCOMPtr<nsILoadGroup> result = mLoadGroup;
338
result.forget(loadGroup);
339
return NS_OK;
340
}
341
342
NS_IMETHODIMP
343
nsLoadGroup::SetLoadGroup(nsILoadGroup* loadGroup) {
344
mLoadGroup = loadGroup;
345
return NS_OK;
346
}
347
348
////////////////////////////////////////////////////////////////////////////////
349
// nsILoadGroup methods:
350
351
NS_IMETHODIMP
352
nsLoadGroup::GetDefaultLoadRequest(nsIRequest** aRequest) {
353
nsCOMPtr<nsIRequest> result = mDefaultLoadRequest;
354
result.forget(aRequest);
355
return NS_OK;
356
}
357
358
NS_IMETHODIMP
359
nsLoadGroup::SetDefaultLoadRequest(nsIRequest* aRequest) {
360
LOG(("nsLoadGroup::SetDefaultLoadRequest this=%p default-request=%p", this,
361
aRequest));
362
363
mDefaultLoadRequest = aRequest;
364
// Inherit the group load flags from the default load request
365
if (mDefaultLoadRequest) {
366
mDefaultLoadRequest->GetLoadFlags(&mLoadFlags);
367
//
368
// Mask off any bits that are not part of the nsIRequest flags.
369
// in particular, nsIChannel::LOAD_DOCUMENT_URI...
370
//
371
mLoadFlags &= nsIRequest::LOAD_REQUESTMASK;
372
373
nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(aRequest);
374
mDefaultLoadIsTimed = timedChannel != nullptr;
375
if (mDefaultLoadIsTimed) {
376
timedChannel->GetChannelCreation(&mDefaultRequestCreationTime);
377
timedChannel->SetTimingEnabled(true);
378
}
379
}
380
// Else, do not change the group's load flags (see bug 95981)
381
return NS_OK;
382
}
383
384
NS_IMETHODIMP
385
nsLoadGroup::AddRequest(nsIRequest* request, nsISupports* ctxt) {
386
nsresult rv;
387
388
if (MOZ_LOG_TEST(gLoadGroupLog, LogLevel::Debug)) {
389
nsAutoCString nameStr;
390
request->GetName(nameStr);
391
LOG(("LOADGROUP [%p]: Adding request %p %s (count=%d).\n", this, request,
392
nameStr.get(), mRequests.EntryCount()));
393
}
394
395
NS_ASSERTION(!mRequests.Search(request),
396
"Entry added to loadgroup twice, don't do that");
397
398
//
399
// Do not add the channel, if the loadgroup is being canceled...
400
//
401
if (mIsCanceling) {
402
LOG(
403
("LOADGROUP [%p]: AddChannel() ABORTED because LoadGroup is"
404
" being canceled!!\n",
405
this));
406
407
return NS_BINDING_ABORTED;
408
}
409
410
nsLoadFlags flags;
411
// if the request is the default load request or if the default load
412
// request is null, then the load group should inherit its load flags from
413
// the request, but also we need to enforce defaultLoadFlags.
414
if (mDefaultLoadRequest == request || !mDefaultLoadRequest) {
415
rv = MergeDefaultLoadFlags(request, flags);
416
} else {
417
rv = MergeLoadFlags(request, flags);
418
}
419
if (NS_FAILED(rv)) return rv;
420
421
//
422
// Add the request to the list of active requests...
423
//
424
425
auto entry = static_cast<RequestMapEntry*>(mRequests.Add(request, fallible));
426
if (!entry) {
427
return NS_ERROR_OUT_OF_MEMORY;
428
}
429
430
if (mPriority != 0) RescheduleRequest(request, mPriority);
431
432
nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(request);
433
if (timedChannel) timedChannel->SetTimingEnabled(true);
434
435
if (!(flags & nsIRequest::LOAD_BACKGROUND)) {
436
// Update the count of foreground URIs..
437
mForegroundCount += 1;
438
439
//
440
// Fire the OnStartRequest notification out to the observer...
441
//
442
// If the notification fails then DO NOT add the request to
443
// the load group.
444
//
445
nsCOMPtr<nsIRequestObserver> observer = do_QueryReferent(mObserver);
446
if (observer) {
447
LOG(
448
("LOADGROUP [%p]: Firing OnStartRequest for request %p."
449
"(foreground count=%d).\n",
450
this, request, mForegroundCount));
451
452
rv = observer->OnStartRequest(request);
453
if (NS_FAILED(rv)) {
454
LOG(("LOADGROUP [%p]: OnStartRequest for request %p FAILED.\n", this,
455
request));
456
//
457
// The URI load has been canceled by the observer. Clean up
458
// the damage...
459
//
460
461
mRequests.Remove(request);
462
463
rv = NS_OK;
464
465
mForegroundCount -= 1;
466
}
467
}
468
469
// Ensure that we're part of our loadgroup while pending
470
if (mForegroundCount == 1 && mLoadGroup) {
471
mLoadGroup->AddRequest(this, nullptr);
472
}
473
}
474
475
return rv;
476
}
477
478
NS_IMETHODIMP
479
nsLoadGroup::RemoveRequest(nsIRequest* request, nsISupports* ctxt,
480
nsresult aStatus) {
481
NS_ENSURE_ARG_POINTER(request);
482
nsresult rv;
483
484
if (MOZ_LOG_TEST(gLoadGroupLog, LogLevel::Debug)) {
485
nsAutoCString nameStr;
486
request->GetName(nameStr);
487
LOG(("LOADGROUP [%p]: Removing request %p %s status %" PRIx32
488
" (count=%d).\n",
489
this, request, nameStr.get(), static_cast<uint32_t>(aStatus),
490
mRequests.EntryCount() - 1));
491
}
492
493
// Make sure we have a owning reference to the request we're about
494
// to remove.
495
496
nsCOMPtr<nsIRequest> kungFuDeathGrip(request);
497
498
//
499
// Remove the request from the group. If this fails, it means that
500
// the request was *not* in the group so do not update the foreground
501
// count or it will get messed up...
502
//
503
auto entry = static_cast<RequestMapEntry*>(mRequests.Search(request));
504
505
if (!entry) {
506
LOG(("LOADGROUP [%p]: Unable to remove request %p. Not in group!\n", this,
507
request));
508
509
return NS_ERROR_FAILURE;
510
}
511
512
mRequests.RemoveEntry(entry);
513
514
// Collect telemetry stats only when default request is a timed channel.
515
// Don't include failed requests in the timing statistics.
516
if (mDefaultLoadIsTimed && NS_SUCCEEDED(aStatus)) {
517
nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(request);
518
if (timedChannel) {
519
// Figure out if this request was served from the cache
520
++mTimedRequests;
521
TimeStamp timeStamp;
522
rv = timedChannel->GetCacheReadStart(&timeStamp);
523
if (NS_SUCCEEDED(rv) && !timeStamp.IsNull()) {
524
++mCachedRequests;
525
}
526
527
rv = timedChannel->GetAsyncOpen(&timeStamp);
528
if (NS_SUCCEEDED(rv) && !timeStamp.IsNull()) {
529
Telemetry::AccumulateTimeDelta(
530
Telemetry::HTTP_SUBITEM_OPEN_LATENCY_TIME,
531
mDefaultRequestCreationTime, timeStamp);
532
}
533
534
rv = timedChannel->GetResponseStart(&timeStamp);
535
if (NS_SUCCEEDED(rv) && !timeStamp.IsNull()) {
536
Telemetry::AccumulateTimeDelta(
537
Telemetry::HTTP_SUBITEM_FIRST_BYTE_LATENCY_TIME,
538
mDefaultRequestCreationTime, timeStamp);
539
}
540
541
TelemetryReportChannel(timedChannel, false);
542
}
543
}
544
545
if (mRequests.EntryCount() == 0) {
546
TelemetryReport();
547
}
548
549
// Undo any group priority delta...
550
if (mPriority != 0) RescheduleRequest(request, -mPriority);
551
552
nsLoadFlags flags;
553
rv = request->GetLoadFlags(&flags);
554
if (NS_FAILED(rv)) return rv;
555
556
if (!(flags & nsIRequest::LOAD_BACKGROUND)) {
557
NS_ASSERTION(mForegroundCount > 0, "ForegroundCount messed up");
558
mForegroundCount -= 1;
559
560
// Fire the OnStopRequest out to the observer...
561
nsCOMPtr<nsIRequestObserver> observer = do_QueryReferent(mObserver);
562
if (observer) {
563
LOG(
564
("LOADGROUP [%p]: Firing OnStopRequest for request %p."
565
"(foreground count=%d).\n",
566
this, request, mForegroundCount));
567
568
rv = observer->OnStopRequest(request, aStatus);
569
570
if (NS_FAILED(rv)) {
571
LOG(("LOADGROUP [%p]: OnStopRequest for request %p FAILED.\n", this,
572
request));
573
}
574
}
575
576
// If that was the last request -> remove ourselves from loadgroup
577
if (mForegroundCount == 0 && mLoadGroup) {
578
mLoadGroup->RemoveRequest(this, nullptr, aStatus);
579
}
580
}
581
582
return rv;
583
}
584
585
NS_IMETHODIMP
586
nsLoadGroup::GetRequests(nsISimpleEnumerator** aRequests) {
587
nsCOMArray<nsIRequest> requests;
588
requests.SetCapacity(mRequests.EntryCount());
589
590
for (auto iter = mRequests.Iter(); !iter.Done(); iter.Next()) {
591
auto e = static_cast<RequestMapEntry*>(iter.Get());
592
requests.AppendObject(e->mKey);
593
}
594
595
return NS_NewArrayEnumerator(aRequests, requests, NS_GET_IID(nsIRequest));
596
}
597
598
NS_IMETHODIMP
599
nsLoadGroup::SetGroupObserver(nsIRequestObserver* aObserver) {
600
mObserver = do_GetWeakReference(aObserver);
601
return NS_OK;
602
}
603
604
NS_IMETHODIMP
605
nsLoadGroup::GetGroupObserver(nsIRequestObserver** aResult) {
606
nsCOMPtr<nsIRequestObserver> observer = do_QueryReferent(mObserver);
607
observer.forget(aResult);
608
return NS_OK;
609
}
610
611
NS_IMETHODIMP
612
nsLoadGroup::GetActiveCount(uint32_t* aResult) {
613
*aResult = mForegroundCount;
614
return NS_OK;
615
}
616
617
NS_IMETHODIMP
618
nsLoadGroup::GetNotificationCallbacks(nsIInterfaceRequestor** aCallbacks) {
619
NS_ENSURE_ARG_POINTER(aCallbacks);
620
nsCOMPtr<nsIInterfaceRequestor> callbacks = mCallbacks;
621
callbacks.forget(aCallbacks);
622
return NS_OK;
623
}
624
625
NS_IMETHODIMP
626
nsLoadGroup::SetNotificationCallbacks(nsIInterfaceRequestor* aCallbacks) {
627
mCallbacks = aCallbacks;
628
return NS_OK;
629
}
630
631
NS_IMETHODIMP
632
nsLoadGroup::GetRequestContextID(uint64_t* aRCID) {
633
if (!mRequestContext) {
634
return NS_ERROR_NOT_AVAILABLE;
635
}
636
*aRCID = mRequestContext->GetID();
637
return NS_OK;
638
}
639
640
////////////////////////////////////////////////////////////////////////////////
641
// nsILoadGroupChild methods:
642
643
NS_IMETHODIMP
644
nsLoadGroup::GetParentLoadGroup(nsILoadGroup** aParentLoadGroup) {
645
*aParentLoadGroup = nullptr;
646
nsCOMPtr<nsILoadGroup> parent = do_QueryReferent(mParentLoadGroup);
647
if (!parent) return NS_OK;
648
parent.forget(aParentLoadGroup);
649
return NS_OK;
650
}
651
652
NS_IMETHODIMP
653
nsLoadGroup::SetParentLoadGroup(nsILoadGroup* aParentLoadGroup) {
654
mParentLoadGroup = do_GetWeakReference(aParentLoadGroup);
655
return NS_OK;
656
}
657
658
NS_IMETHODIMP
659
nsLoadGroup::GetChildLoadGroup(nsILoadGroup** aChildLoadGroup) {
660
NS_ADDREF(*aChildLoadGroup = this);
661
return NS_OK;
662
}
663
664
NS_IMETHODIMP
665
nsLoadGroup::GetRootLoadGroup(nsILoadGroup** aRootLoadGroup) {
666
// first recursively try the root load group of our parent
667
nsCOMPtr<nsILoadGroupChild> ancestor = do_QueryReferent(mParentLoadGroup);
668
if (ancestor) return ancestor->GetRootLoadGroup(aRootLoadGroup);
669
670
// next recursively try the root load group of our own load grop
671
ancestor = do_QueryInterface(mLoadGroup);
672
if (ancestor) return ancestor->GetRootLoadGroup(aRootLoadGroup);
673
674
// finally just return this
675
NS_ADDREF(*aRootLoadGroup = this);
676
return NS_OK;
677
}
678
679
////////////////////////////////////////////////////////////////////////////////
680
// nsISupportsPriority methods:
681
682
NS_IMETHODIMP
683
nsLoadGroup::GetPriority(int32_t* aValue) {
684
*aValue = mPriority;
685
return NS_OK;
686
}
687
688
NS_IMETHODIMP
689
nsLoadGroup::SetPriority(int32_t aValue) {
690
return AdjustPriority(aValue - mPriority);
691
}
692
693
NS_IMETHODIMP
694
nsLoadGroup::AdjustPriority(int32_t aDelta) {
695
// Update the priority for each request that supports nsISupportsPriority
696
if (aDelta != 0) {
697
mPriority += aDelta;
698
for (auto iter = mRequests.Iter(); !iter.Done(); iter.Next()) {
699
auto e = static_cast<RequestMapEntry*>(iter.Get());
700
RescheduleRequest(e->mKey, aDelta);
701
}
702
}
703
return NS_OK;
704
}
705
706
NS_IMETHODIMP
707
nsLoadGroup::GetDefaultLoadFlags(uint32_t* aFlags) {
708
*aFlags = mDefaultLoadFlags;
709
return NS_OK;
710
}
711
712
NS_IMETHODIMP
713
nsLoadGroup::SetDefaultLoadFlags(uint32_t aFlags) {
714
mDefaultLoadFlags = aFlags;
715
return NS_OK;
716
}
717
718
NS_IMETHODIMP
719
nsLoadGroup::GetUserAgentOverrideCache(nsACString& aUserAgentOverrideCache) {
720
aUserAgentOverrideCache = mUserAgentOverrideCache;
721
return NS_OK;
722
}
723
724
NS_IMETHODIMP
725
nsLoadGroup::SetUserAgentOverrideCache(
726
const nsACString& aUserAgentOverrideCache) {
727
mUserAgentOverrideCache = aUserAgentOverrideCache;
728
return NS_OK;
729
}
730
731
////////////////////////////////////////////////////////////////////////////////
732
733
void nsLoadGroup::TelemetryReport() {
734
nsresult defaultStatus = NS_ERROR_INVALID_ARG;
735
// We should only report HTTP_PAGE_* telemetry if the defaultRequest was
736
// actually successful.
737
if (mDefaultLoadRequest) {
738
mDefaultLoadRequest->GetStatus(&defaultStatus);
739
}
740
if (mDefaultLoadIsTimed && NS_SUCCEEDED(defaultStatus)) {
741
Telemetry::Accumulate(Telemetry::HTTP_REQUEST_PER_PAGE, mTimedRequests);
742
if (mTimedRequests) {
743
Telemetry::Accumulate(Telemetry::HTTP_REQUEST_PER_PAGE_FROM_CACHE,
744
mCachedRequests * 100 / mTimedRequests);
745
}
746
747
nsCOMPtr<nsITimedChannel> timedChannel =
748
do_QueryInterface(mDefaultLoadRequest);
749
if (timedChannel) TelemetryReportChannel(timedChannel, true);
750
}
751
752
mTimedRequests = 0;
753
mCachedRequests = 0;
754
mDefaultLoadIsTimed = false;
755
}
756
757
void nsLoadGroup::TelemetryReportChannel(nsITimedChannel* aTimedChannel,
758
bool aDefaultRequest) {
759
nsresult rv;
760
bool timingEnabled;
761
rv = aTimedChannel->GetTimingEnabled(&timingEnabled);
762
if (NS_FAILED(rv) || !timingEnabled) return;
763
764
TimeStamp asyncOpen;
765
rv = aTimedChannel->GetAsyncOpen(&asyncOpen);
766
// We do not check !asyncOpen.IsNull() bellow, prevent ASSERTIONs this way
767
if (NS_FAILED(rv) || asyncOpen.IsNull()) return;
768
769
TimeStamp cacheReadStart;
770
rv = aTimedChannel->GetCacheReadStart(&cacheReadStart);
771
if (NS_FAILED(rv)) return;
772
773
TimeStamp cacheReadEnd;
774
rv = aTimedChannel->GetCacheReadEnd(&cacheReadEnd);
775
if (NS_FAILED(rv)) return;
776
777
TimeStamp domainLookupStart;
778
rv = aTimedChannel->GetDomainLookupStart(&domainLookupStart);
779
if (NS_FAILED(rv)) return;
780
781
TimeStamp domainLookupEnd;
782
rv = aTimedChannel->GetDomainLookupEnd(&domainLookupEnd);
783
if (NS_FAILED(rv)) return;
784
785
TimeStamp connectStart;
786
rv = aTimedChannel->GetConnectStart(&connectStart);
787
if (NS_FAILED(rv)) return;
788
789
TimeStamp secureConnectionStart;
790
rv = aTimedChannel->GetSecureConnectionStart(&secureConnectionStart);
791
if (NS_FAILED(rv)) return;
792
793
TimeStamp connectEnd;
794
rv = aTimedChannel->GetConnectEnd(&connectEnd);
795
if (NS_FAILED(rv)) return;
796
797
TimeStamp requestStart;
798
rv = aTimedChannel->GetRequestStart(&requestStart);
799
if (NS_FAILED(rv)) return;
800
801
TimeStamp responseStart;
802
rv = aTimedChannel->GetResponseStart(&responseStart);
803
if (NS_FAILED(rv)) return;
804
805
TimeStamp responseEnd;
806
rv = aTimedChannel->GetResponseEnd(&responseEnd);
807
if (NS_FAILED(rv)) return;
808
809
#define HTTP_REQUEST_HISTOGRAMS(prefix) \
810
if (!domainLookupStart.IsNull()) { \
811
Telemetry::AccumulateTimeDelta(Telemetry::HTTP_##prefix##_DNS_ISSUE_TIME, \
812
asyncOpen, domainLookupStart); \
813
} \
814
\
815
if (!domainLookupStart.IsNull() && !domainLookupEnd.IsNull()) { \
816
Telemetry::AccumulateTimeDelta(Telemetry::HTTP_##prefix##_DNS_LOOKUP_TIME, \
817
domainLookupStart, domainLookupEnd); \
818
} \
819
\
820
if (!secureConnectionStart.IsNull() && !connectEnd.IsNull()) { \
821
Telemetry::AccumulateTimeDelta(Telemetry::HTTP_##prefix##_TLS_HANDSHAKE, \
822
secureConnectionStart, connectEnd); \
823
} \
824
\
825
if (!connectStart.IsNull() && !connectEnd.IsNull()) { \
826
Telemetry::AccumulateTimeDelta( \
827
Telemetry::HTTP_##prefix##_TCP_CONNECTION_2, connectStart, \
828
connectEnd); \
829
} \
830
\
831
if (!requestStart.IsNull() && !responseEnd.IsNull()) { \
832
Telemetry::AccumulateTimeDelta( \
833
Telemetry::HTTP_##prefix##_OPEN_TO_FIRST_SENT, asyncOpen, \
834
requestStart); \
835
\
836
Telemetry::AccumulateTimeDelta( \
837
Telemetry::HTTP_##prefix##_FIRST_SENT_TO_LAST_RECEIVED, requestStart, \
838
responseEnd); \
839
\
840
if (cacheReadStart.IsNull() && !responseStart.IsNull()) { \
841
Telemetry::AccumulateTimeDelta( \
842
Telemetry::HTTP_##prefix##_OPEN_TO_FIRST_RECEIVED, asyncOpen, \
843
responseStart); \
844
} \
845
} \
846
\
847
if (!cacheReadStart.IsNull() && !cacheReadEnd.IsNull()) { \
848
Telemetry::AccumulateTimeDelta( \
849
Telemetry::HTTP_##prefix##_OPEN_TO_FIRST_FROM_CACHE_V2, asyncOpen, \
850
cacheReadStart); \
851
\
852
Telemetry::AccumulateTimeDelta( \
853
Telemetry::HTTP_##prefix##_CACHE_READ_TIME_V2, cacheReadStart, \
854
cacheReadEnd); \
855
\
856
if (!requestStart.IsNull() && !responseEnd.IsNull()) { \
857
Telemetry::AccumulateTimeDelta(Telemetry::HTTP_##prefix##_REVALIDATION, \
858
requestStart, responseEnd); \
859
} \
860
} \
861
\
862
if (!cacheReadEnd.IsNull()) { \
863
Telemetry::AccumulateTimeDelta( \
864
Telemetry::HTTP_##prefix##_COMPLETE_LOAD_V2, asyncOpen, cacheReadEnd); \
865
Telemetry::AccumulateTimeDelta( \
866
Telemetry::HTTP_##prefix##_COMPLETE_LOAD_CACHED_V2, asyncOpen, \
867
cacheReadEnd); \
868
} else if (!responseEnd.IsNull()) { \
869
Telemetry::AccumulateTimeDelta( \
870
Telemetry::HTTP_##prefix##_COMPLETE_LOAD_V2, asyncOpen, responseEnd); \
871
Telemetry::AccumulateTimeDelta( \
872
Telemetry::HTTP_##prefix##_COMPLETE_LOAD_NET_V2, asyncOpen, \
873
responseEnd); \
874
}
875
876
if (aDefaultRequest) {
877
HTTP_REQUEST_HISTOGRAMS(PAGE)
878
} else {
879
HTTP_REQUEST_HISTOGRAMS(SUB)
880
}
881
#undef HTTP_REQUEST_HISTOGRAMS
882
}
883
884
nsresult nsLoadGroup::MergeLoadFlags(nsIRequest* aRequest,
885
nsLoadFlags& outFlags) {
886
nsresult rv;
887
nsLoadFlags flags, oldFlags;
888
889
rv = aRequest->GetLoadFlags(&flags);
890
if (NS_FAILED(rv)) {
891
return rv;
892
}
893
894
oldFlags = flags;
895
896
// Inherit the following bits...
897
flags |= (mLoadFlags &
898
(LOAD_BACKGROUND | LOAD_BYPASS_CACHE | LOAD_FROM_CACHE |
899
VALIDATE_ALWAYS | VALIDATE_ONCE_PER_SESSION | VALIDATE_NEVER));
900
901
// ... and force the default flags.
902
flags |= mDefaultLoadFlags;
903
904
if (flags != oldFlags) {
905
rv = aRequest->SetLoadFlags(flags);
906
}
907
908
outFlags = flags;
909
return rv;
910
}
911
912
nsresult nsLoadGroup::MergeDefaultLoadFlags(nsIRequest* aRequest,
913
nsLoadFlags& outFlags) {
914
nsresult rv;
915
nsLoadFlags flags, oldFlags;
916
917
rv = aRequest->GetLoadFlags(&flags);
918
if (NS_FAILED(rv)) {
919
return rv;
920
}
921
922
oldFlags = flags;
923
// ... and force the default flags.
924
flags |= mDefaultLoadFlags;
925
926
if (flags != oldFlags) {
927
rv = aRequest->SetLoadFlags(flags);
928
}
929
outFlags = flags;
930
return rv;
931
}
932
933
nsresult nsLoadGroup::Init() {
934
mRequestContextService = RequestContextService::GetOrCreate();
935
if (mRequestContextService) {
936
Unused << mRequestContextService->NewRequestContext(
937
getter_AddRefs(mRequestContext));
938
}
939
940
return NS_OK;
941
}
942
943
} // namespace net
944
} // namespace mozilla
945
946
#undef LOG