FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
gecko-dev/netwerk/base/nsIOService.cpp at master · mozilla/gecko-dev · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Jul 9, 2025. It is now read-only.
mozilla
/
gecko-dev
Public archive
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
3.7k
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
gecko-dev
/
netwerk
/
base
/
nsIOService.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
2491 lines (2116 loc) · 76.8 KB
Breadcrumbs
gecko-dev
/
netwerk
/
base
/
nsIOService.cpp
Copy path
File metadata and controls
2491 lines (2116 loc) · 76.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
-*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*/
/*
vim:set ts=4 sw=2 cindent et:
*/
/*
This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#
include
"
mozilla/DebugOnly.h
"
#
include
"
nsIOService.h
"
#
include
"
nsIProtocolHandler.h
"
#
include
"
nsIFileProtocolHandler.h
"
#
include
"
nscore.h
"
#
include
"
nsIURI.h
"
#
include
"
prprf.h
"
#
include
"
netCore.h
"
#
include
"
nsIObserverService.h
"
#
include
"
nsXPCOM.h
"
#
include
"
nsIProxiedProtocolHandler.h
"
#
include
"
nsIProxyInfo.h
"
#
include
"
nsDNSService2.h
"
#
include
"
nsEscape.h
"
#
include
"
nsNetUtil.h
"
#
include
"
nsNetCID.h
"
#
include
"
nsCRT.h
"
#
include
"
nsSimpleNestedURI.h
"
#
include
"
nsSocketTransport2.h
"
#
include
"
nsTArray.h
"
#
include
"
nsIConsoleService.h
"
#
include
"
nsIUploadChannel2.h
"
#
include
"
nsXULAppAPI.h
"
#
include
"
nsIProtocolProxyCallback.h
"
#
include
"
nsICancelable.h
"
#
include
"
nsINetworkLinkService.h
"
#
include
"
nsAsyncRedirectVerifyHelper.h
"
#
include
"
nsURLHelper.h
"
#
include
"
nsIProtocolProxyService2.h
"
#
include
"
MainThreadUtils.h
"
#
include
"
nsINode.h
"
#
include
"
nsIWebTransport.h
"
#
include
"
nsIWidget.h
"
#
include
"
nsThreadUtils.h
"
#
include
"
WebTransportSessionProxy.h
"
#
include
"
mozilla/AppShutdown.h
"
#
include
"
mozilla/Components.h
"
#
include
"
mozilla/LoadInfo.h
"
#
include
"
mozilla/net/NeckoCommon.h
"
#
include
"
mozilla/Services.h
"
#
include
"
mozilla/net/DNS.h
"
#
include
"
mozilla/ipc/URIUtils.h
"
#
include
"
mozilla/net/NeckoChild.h
"
#
include
"
mozilla/net/NeckoParent.h
"
#
include
"
mozilla/dom/ClientInfo.h
"
#
include
"
mozilla/dom/ContentParent.h
"
#
include
"
mozilla/dom/nsHTTPSOnlyUtils.h
"
#
include
"
mozilla/dom/ServiceWorkerDescriptor.h
"
#
include
"
mozilla/net/CaptivePortalService.h
"
#
include
"
mozilla/net/NetworkConnectivityService.h
"
#
include
"
mozilla/net/SocketProcessHost.h
"
#
include
"
mozilla/net/SocketProcessParent.h
"
#
include
"
mozilla/net/SSLTokensCache.h
"
#
include
"
mozilla/StoragePrincipalHelper.h
"
#
include
"
mozilla/Unused.h
"
#
include
"
nsContentSecurityManager.h
"
#
include
"
nsContentUtils.h
"
#
include
"
mozilla/StaticPrefs_network.h
"
#
include
"
mozilla/StaticPrefs_security.h
"
#
include
"
mozilla/glean/NetwerkMetrics.h
"
#
include
"
nsNSSComponent.h
"
#
include
"
IPv4Parser.h
"
#
include
"
ssl.h
"
#
include
"
StaticComponents.h
"
#
include
"
SuspendableChannelWrapper.h
"
#
ifdef
MOZ_WIDGET_ANDROID
#
include
<
regex
>
#
include
"
AndroidBridge.h
"
#
include
"
mozilla/java/GeckoAppShellWrappers.h
"
#
include
"
mozilla/jni/Utils.h
"
#
endif
namespace
mozilla
{
namespace
net
{
using
mozilla::Maybe;
using
mozilla::dom::ClientInfo;
using
mozilla::dom::ServiceWorkerDescriptor;
#
define
PORT_PREF_PREFIX
"
network.security.ports.
"
#
define
PORT_PREF
(
x
)
PORT_PREF_PREFIX
x
#
define
MANAGE_OFFLINE_STATUS_PREF
"
network.manage-offline-status
"
//
Nb: these have been misnomers since bug 715770 removed the buffer cache.
//
"network.segment.count" and "network.segment.size" would be better names,
//
but the old names are still used to preserve backward compatibility.
#
define
NECKO_BUFFER_CACHE_COUNT_PREF
"
network.buffer.cache.count
"
#
define
NECKO_BUFFER_CACHE_SIZE_PREF
"
network.buffer.cache.size
"
#
define
NETWORK_CAPTIVE_PORTAL_PREF
"
network.captive-portal-service.enabled
"
#
define
WEBRTC_PREF_PREFIX
"
media.peerconnection.
"
#
define
NETWORK_DNS_PREF
"
network.dns.
"
#
define
FORCE_EXTERNAL_PREF_PREFIX
"
network.protocol-handler.external.
"
//
prefs for overriding IPAddress->IpAddressSpace mapping
#
define
PREF_LNA_IP_ADDR_SPACE_PUBLIC
\
"
network.lna.address_space.public.override
"
#
define
PREF_LNA_IP_ADDR_SPACE_PRIVATE
\
"
network.lna.address_space.private.override
"
#
define
PREF_LNA_IP_ADDR_SPACE_LOCAL
"
network.lna.address_space.local.override
"
nsIOService*
gIOService
;
static
bool
gHasWarnedUploadChannel2
;
static
bool
gCaptivePortalEnabled
=
false
;
static
LazyLogModule
gIOServiceLog
(
"
nsIOService
"
);
#
undef
LOG
#
define
LOG
(
args
)
MOZ_LOG
(
gIOServiceLog
, LogLevel::Debug, args)
//
A general port blacklist. Connections to these ports will not be allowed
//
unless the protocol overrides.
//
//
This list is to be kept in sync with "bad ports" as defined in the
//
WHATWG Fetch standard at <https://fetch.spec.whatwg.org/#port-blocking>
int16_t
gBadPortList
[] = {
1
,
//
tcpmux
7
,
//
echo
9
,
//
discard
11
,
//
systat
13
,
//
daytime
15
,
//
netstat
17
,
//
qotd
19
,
//
chargen
20
,
//
ftp-data
21
,
//
ftp
22
,
//
ssh
23
,
//
telnet
25
,
//
smtp
37
,
//
time
42
,
//
name
43
,
//
nicname
53
,
//
domain
69
,
//
tftp
77
,
//
priv-rjs
79
,
//
finger
87
,
//
ttylink
95
,
//
supdup
101
,
//
hostriame
102
,
//
iso-tsap
103
,
//
gppitnp
104
,
//
acr-nema
109
,
//
pop2
110
,
//
pop3
111
,
//
sunrpc
113
,
//
auth
115
,
//
sftp
117
,
//
uucp-path
119
,
//
nntp
123
,
//
ntp
135
,
//
loc-srv / epmap
137
,
//
netbios
139
,
//
netbios
143
,
//
imap2
161
,
//
snmp
179
,
//
bgp
389
,
//
ldap
427
,
//
afp (alternate)
465
,
//
smtp (alternate)
512
,
//
print / exec
513
,
//
login
514
,
//
shell
515
,
//
printer
526
,
//
tempo
530
,
//
courier
531
,
//
chat
532
,
//
netnews
540
,
//
uucp
548
,
//
afp
554
,
//
rtsp
556
,
//
remotefs
563
,
//
nntp+ssl
587
,
//
smtp (outgoing)
601
,
//
syslog-conn
636
,
//
ldap+ssl
989
,
//
ftps-data
990
,
//
ftps
993
,
//
imap+ssl
995
,
//
pop3+ssl
1719
,
//
h323gatestat
1720
,
//
h323hostcall
1723
,
//
pptp
2049
,
//
nfs
3659
,
//
apple-sasl
4045
,
//
lockd
4190
,
//
sieve
5060
,
//
sip
5061
,
//
sips
6000
,
//
x11
6566
,
//
sane-port
6665
,
//
irc (alternate)
6666
,
//
irc (alternate)
6667
,
//
irc (default)
6668
,
//
irc (alternate)
6669
,
//
irc (alternate)
6679
,
//
osaut
6697
,
//
irc+tls
10080
,
//
amanda
0
,
//
Sentinel value: This MUST be zero
};
static
const
char
kProfileChangeNetTeardownTopic
[] =
"
profile-change-net-teardown
"
;
static
const
char
kProfileChangeNetRestoreTopic
[] =
"
profile-change-net-restore
"
;
static
const
char
kProfileDoChange
[] =
"
profile-do-change
"
;
//
Necko buffer defaults
uint32_t
nsIOService::
gDefaultSegmentSize
=
4096
;
uint32_t
nsIOService::
gDefaultSegmentCount
=
24
;
uint32_t
nsIOService::
sSocketProcessCrashedCount
=
0
;
//
//////////////////////////////////////////////////////////////////////////////
nsIOService::nsIOService
()
:
mLastOfflineStateChange
(PR_IntervalNow()),
mLastConnectivityChange
(PR_IntervalNow()),
mLastNetworkLinkChange(PR_IntervalNow()) {}
static
const
char
*
gCallbackPrefs
[] = {
PORT_PREF_PREFIX
,
MANAGE_OFFLINE_STATUS_PREF
,
NECKO_BUFFER_CACHE_COUNT_PREF
,
NECKO_BUFFER_CACHE_SIZE_PREF
,
NETWORK_CAPTIVE_PORTAL_PREF
,
FORCE_EXTERNAL_PREF_PREFIX
,
SIMPLE_URI_SCHEMES_PREF
,
PREF_LNA_IP_ADDR_SPACE_PUBLIC
,
PREF_LNA_IP_ADDR_SPACE_PRIVATE
,
PREF_LNA_IP_ADDR_SPACE_LOCAL
,
nullptr
,
};
static
const
char
*
gCallbackPrefsForSocketProcess
[] = {
WEBRTC_PREF_PREFIX
,
NETWORK_DNS_PREF
,
"
network.send_ODA_to_content_directly
"
,
"
network.trr.
"
,
"
doh-rollout.
"
,
"
network.dns.disableIPv6
"
,
"
network.offline-mirrors-connectivity
"
,
"
network.disable-localhost-when-offline
"
,
"
network.proxy.parse_pac_on_socket_process
"
,
"
network.proxy.allow_hijacking_localhost
"
,
"
network.connectivity-service.
"
,
"
network.captive-portal-service.testMode
"
,
"
network.socket.ip_addr_any.disabled
"
,
"
network.socket.attach_mock_network_layer
"
,
"
network.lna.enabled
"
,
"
network.lna.blocking
"
,
nullptr
,
};
static
const
char
*
gCallbackSecurityPrefs
[] = {
//
Note the prefs listed below should be in sync with the code in
//
HandleTLSPrefChange().
"
security.tls.version.min
"
,
"
security.tls.version.max
"
,
"
security.tls.version.enable-deprecated
"
,
"
security.tls.hello_downgrade_check
"
,
"
security.ssl.require_safe_negotiation
"
,
"
security.ssl.enable_false_start
"
,
"
security.ssl.enable_alpn
"
,
"
security.tls.enable_0rtt_data
"
,
"
security.ssl.disable_session_identifiers
"
,
"
security.tls.enable_post_handshake_auth
"
,
"
security.tls.enable_delegated_credentials
"
,
nullptr
,
};
nsresult
nsIOService::Init
() {
SSLTokensCache::Init
();
InitializeCaptivePortalService
();
//
setup our bad port list stuff
for
(
int
i =
0
;
gBadPortList
[i]; i++) {
//
We can't be accessed by another thread yet
MOZ_PUSH_IGNORE_THREAD_SAFETY
mRestrictedPortList
.
AppendElement
(
gBadPortList
[i]);
MOZ_POP_THREAD_SAFETY
}
//
Further modifications to the port list come from prefs
Preferences::RegisterPrefixCallbacks
(nsIOService::PrefsChanged,
gCallbackPrefs
,
this
);
PrefsChanged
();
mSocketProcessTopicBlockedList
.
Insert
(
nsLiteralCString
(
NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID
));
mSocketProcessTopicBlockedList
.
Insert
(
nsLiteralCString
(
NS_XPCOM_SHUTDOWN_OBSERVER_ID
));
mSocketProcessTopicBlockedList
.
Insert
(
"
xpcom-shutdown-threads
"
_ns);
mSocketProcessTopicBlockedList
.
Insert
(
"
profile-do-change
"
_ns);
mSocketProcessTopicBlockedList
.
Insert
(
"
network:socket-process-crashed
"
_ns);
//
Register for profile change notifications
mObserverService
=
services::GetObserverService
();
AddObserver
(
this
,
kProfileChangeNetTeardownTopic
,
true
);
AddObserver
(
this
,
kProfileChangeNetRestoreTopic
,
true
);
AddObserver
(
this
,
kProfileDoChange
,
true
);
AddObserver
(
this
,
NS_XPCOM_SHUTDOWN_OBSERVER_ID
,
true
);
AddObserver
(
this
,
NS_NETWORK_LINK_TOPIC
,
true
);
AddObserver
(
this
,
NS_NETWORK_ID_CHANGED_TOPIC
,
true
);
AddObserver
(
this
,
NS_WIDGET_WAKE_OBSERVER_TOPIC
,
true
);
//
Register observers for sending notifications to nsSocketTransportService
if
(
XRE_IsParentProcess
()) {
AddObserver
(
this
,
"
profile-initial-state
"
,
true
);
AddObserver
(
this
,
NS_WIDGET_SLEEP_OBSERVER_TOPIC
,
true
);
}
if
(
IsSocketProcessChild
()) {
Preferences::RegisterCallbacks
(nsIOService::OnTLSPrefChange,
gCallbackSecurityPrefs
,
this
);
}
gIOService
=
this
;
InitializeNetworkLinkService
();
InitializeProtocolProxyService
();
SetOffline
(
false
);
return
NS_OK
;
}
NS_IMETHODIMP
nsIOService::AddObserver
(nsIObserver* aObserver,
const
char
* aTopic,
bool
aOwnsWeak) {
if
(!
mObserverService
) {
return
NS_ERROR_FAILURE
;
}
//
Register for the origional observer.
nsresult rv =
mObserverService
->
AddObserver
(aObserver, aTopic, aOwnsWeak);
if
(
NS_FAILED
(rv)) {
return
rv;
}
if
(!
XRE_IsParentProcess
()) {
return
NS_OK
;
}
nsAutoCString
topic
(aTopic);
//
This happens when AddObserver() is called by nsIOService::Init(). We don't
//
want to add nsIOService again.
if
(
SameCOMIdentity
(aObserver,
static_cast
<nsIObserver*>(
this
))) {
mIOServiceTopicList
.
Insert
(topic);
return
NS_OK
;
}
if
(!
UseSocketProcess
()) {
return
NS_OK
;
}
if
(
mSocketProcessTopicBlockedList
.
Contains
(topic)) {
return
NS_ERROR_FAILURE
;
}
//
Avoid registering duplicate topics.
if
(
mObserverTopicForSocketProcess
.
Contains
(topic)) {
return
NS_ERROR_FAILURE
;
}
mObserverTopicForSocketProcess
.
Insert
(topic);
//
Avoid registering duplicate topics.
if
(
mIOServiceTopicList
.
Contains
(topic)) {
return
NS_ERROR_FAILURE
;
}
return
mObserverService
->
AddObserver
(
this
, aTopic,
true
);
}
NS_IMETHODIMP
nsIOService::RemoveObserver
(nsIObserver* aObserver,
const
char
* aTopic) {
return
NS_ERROR_NOT_IMPLEMENTED
;
}
NS_IMETHODIMP
nsIOService::EnumerateObservers
(
const
char
* aTopic,
nsISimpleEnumerator** anEnumerator) {
return
NS_ERROR_NOT_IMPLEMENTED
;
}
NS_IMETHODIMP
nsIOService::NotifyObservers
(nsISupports* aSubject,
const
char
* aTopic,
const
char16_t
* aSomeData) {
return
NS_ERROR_NOT_IMPLEMENTED
;
}
nsIOService::~nsIOService
() {
if
(
gIOService
) {
MOZ_ASSERT
(
gIOService
==
this
);
gIOService
=
nullptr
;
}
}
#
ifdef
MOZ_WIDGET_ANDROID
bool
nsIOService::ShouldAddAdditionalSearchHeaders
(nsIURI* aURI,
bool
* aHeaderVal) {
if
(!(
mozilla::AndroidBridge::Bridge
())) {
return
false
;
}
if
(!aURI->
SchemeIs
(
"
https
"
)) {
return
false
;
}
//
We need to improve below logic for matching google domains
//
See Bug 1894642
//
Is URI same as google ^https://www\\.google\\..+
nsAutoCString host;
aURI->
GetHost
(host);
LOG
((
"
nsIOService::ShouldAddAdditionalSearchHeaders() checking host %s
\n
"
,
PromiseFlatCString
(host).
get
()));
std::regex
pattern
(
"
^www
\\
.google
\\
..+
"
);
if
(
std::regex_match
(host.
get
(), pattern)) {
LOG
((
"
Google domain detected for host %s
\n
"
,
PromiseFlatCString
(host).
get
()));
static
bool
ramAboveThreshold =
java::GeckoAppShell::IsDeviceRamThresholdOkay
();
*aHeaderVal = ramAboveThreshold;
return
true
;
}
return
false
;
}
#
endif
//
static
void
nsIOService::OnTLSPrefChange
(
const
char
* aPref,
void
* aSelf) {
MOZ_ASSERT
(
IsSocketProcessChild
());
if
(!
EnsureNSSInitializedChromeOrContent
()) {
LOG
((
"
NSS not initialized.
"
));
return
;
}
nsAutoCString
pref
(aPref);
//
The preferences listed in gCallbackSecurityPrefs need to be in sync with
//
the code in HandleTLSPrefChange().
if
(
HandleTLSPrefChange
(pref)) {
LOG
((
"
HandleTLSPrefChange done
"
));
}
}
nsresult
nsIOService::InitializeCaptivePortalService
() {
if
(
XRE_GetProcessType
() != GeckoProcessType_Default) {
//
We only initalize a captive portal service in the main process
return
NS_OK
;
}
mCaptivePortalService
=
mozilla::components::CaptivePortal::Service
();
if
(
mCaptivePortalService
) {
static_cast
<CaptivePortalService*>(
mCaptivePortalService
.
get
())
->
Initialize
();
}
//
Instantiate and initialize the service
RefPtr<NetworkConnectivityService> ncs =
NetworkConnectivityService::GetSingleton
();
return
NS_OK
;
}
nsresult
nsIOService::InitializeSocketTransportService
() {
nsresult rv =
NS_OK
;
if
(
AppShutdown::IsInOrBeyond
(ShutdownPhase::AppShutdownConfirmed)) {
LOG
(
(
"
nsIOService aborting InitializeSocketTransportService because of app
"
"
shutdown
"
));
return
NS_ERROR_ILLEGAL_DURING_SHUTDOWN
;
}
if
(!
mSocketTransportService
) {
mSocketTransportService
=
mozilla::components::SocketTransport::Service
(&rv);
if
(
NS_FAILED
(rv)) {
NS_WARNING
(
"
failed to get socket transport service
"
);
}
}
if
(
mSocketTransportService
) {
rv =
mSocketTransportService
->
Init
();
NS_ASSERTION
(
NS_SUCCEEDED
(rv), "socket transport service init failed");
mSocketTransportService
->
SetOffline
(
false
);
}
return
rv;
}
nsresult
nsIOService::InitializeNetworkLinkService
() {
nsresult rv =
NS_OK
;
if
(
mNetworkLinkServiceInitialized
)
return
rv;
if
(!NS_IsMainThread()) {
NS_WARNING
(
"
Network link service should be created on main thread
"
);
return
NS_ERROR_FAILURE
;
}
//
go into managed mode if we can, and chrome process
if
(!
XRE_IsParentProcess
()) {
return
NS_ERROR_NOT_AVAILABLE
;
}
mNetworkLinkService
=
do_GetService
(
NS_NETWORK_LINK_SERVICE_CONTRACTID
, &rv);
if
(
mNetworkLinkService
) {
mNetworkLinkServiceInitialized
=
true
;
}
//
After initializing the networkLinkService, query the connectivity state
OnNetworkLinkEvent
(
NS_NETWORK_LINK_DATA_UNKNOWN
);
return
rv;
}
nsresult
nsIOService::InitializeProtocolProxyService
() {
nsresult rv =
NS_OK
;
if
(
XRE_IsParentProcess
()) {
//
for early-initialization
Unused <<
mozilla::components::ProtocolProxy::Service
(&rv);
}
return
rv;
}
already_AddRefed<nsIOService>
nsIOService::GetInstance
() {
if
(!
gIOService
) {
RefPtr<nsIOService> ios =
new
nsIOService
();
if
(
NS_SUCCEEDED
(ios->
Init
())) {
MOZ_ASSERT
(
gIOService
== ios.
get
());
return
ios.
forget
();
}
}
return
do_AddRef
(
gIOService
);
}
class
SocketProcessListenerProxy
:
public
SocketProcessHost
::Listener {
public:
SocketProcessListenerProxy
() =
default
;
void
OnProcessLaunchComplete
(SocketProcessHost* aHost,
bool
aSucceeded) {
if
(!
gIOService
) {
return
;
}
gIOService
->
OnProcessLaunchComplete
(aHost, aSucceeded);
}
void
OnProcessUnexpectedShutdown
(SocketProcessHost* aHost) {
if
(!
gIOService
) {
return
;
}
gIOService
->
OnProcessUnexpectedShutdown
(aHost);
}
};
//
static
bool
nsIOService::TooManySocketProcessCrash
() {
return
sSocketProcessCrashedCount
>=
StaticPrefs::network_max_socket_process_failed_count
();
}
//
static
void
nsIOService::IncreaseSocketProcessCrashCount
() {
MOZ_ASSERT
(
IsNeckoChild
());
sSocketProcessCrashedCount
++;
}
nsresult
nsIOService::LaunchSocketProcess
() {
MOZ_ASSERT
(NS_IsMainThread());
if
(
XRE_GetProcessType
() != GeckoProcessType_Default) {
return
NS_OK
;
}
//
We shouldn't launch socket prcess when shutdown begins.
if
(
AppShutdown::IsInOrBeyond
(ShutdownPhase::AppShutdownConfirmed)) {
return
NS_OK
;
}
if
(
mSocketProcess
) {
return
NS_OK
;
}
if
(
PR_GetEnv
(
"
MOZ_DISABLE_SOCKET_PROCESS
"
)) {
LOG
((
"
nsIOService skipping LaunchSocketProcess because of the env
"
));
return
NS_OK
;
}
if
(!
StaticPrefs::network_process_enabled
()) {
LOG
((
"
nsIOService skipping LaunchSocketProcess because of the pref
"
));
return
NS_OK
;
}
Preferences::RegisterPrefixCallbacks
(
nsIOService::NotifySocketProcessPrefsChanged,
gCallbackPrefsForSocketProcess
,
this
);
//
The subprocess is launched asynchronously, so we wait for a callback to
//
acquire the IPDL actor.
mSocketProcess
=
new
SocketProcessHost
(
new
SocketProcessListenerProxy
());
LOG
((
"
nsIOService::LaunchSocketProcess
"
));
if
(!
mSocketProcess
->
Launch
()) {
NS_WARNING
(
"
Failed to launch socket process!!
"
);
DestroySocketProcess
();
return
NS_ERROR_FAILURE
;
}
return
NS_OK
;
}
void
nsIOService::DestroySocketProcess
() {
LOG
((
"
nsIOService::DestroySocketProcess
"
));
MOZ_ASSERT
(NS_IsMainThread());
if
(
XRE_GetProcessType
() != GeckoProcessType_Default || !
mSocketProcess
) {
return
;
}
Preferences::UnregisterPrefixCallbacks
(
nsIOService::NotifySocketProcessPrefsChanged,
gCallbackPrefsForSocketProcess
,
this
);
mSocketProcess
->
Shutdown
();
mSocketProcess
=
nullptr
;
}
bool
nsIOService::SocketProcessReady
() {
return
mSocketProcess
&&
mSocketProcess
->
IsConnected
();
}
static
bool
sUseSocketProcess
=
false
;
static
bool
sUseSocketProcessChecked
=
false
;
//
static
bool
nsIOService::UseSocketProcess
(
bool
aCheckAgain) {
if
(
sUseSocketProcessChecked
&& !aCheckAgain) {
return
sUseSocketProcess
;
}
sUseSocketProcessChecked
=
true
;
sUseSocketProcess
=
false
;
if
(
PR_GetEnv
(
"
MOZ_DISABLE_SOCKET_PROCESS
"
)) {
return
sUseSocketProcess
;
}
if
(
TooManySocketProcessCrash
()) {
LOG
((
"
TooManySocketProcessCrash
"
));
return
sUseSocketProcess
;
}
if
(
PR_GetEnv
(
"
MOZ_FORCE_USE_SOCKET_PROCESS
"
)) {
sUseSocketProcess
=
true
;
return
sUseSocketProcess
;
}
if
(
StaticPrefs::network_process_enabled
()) {
sUseSocketProcess
=
StaticPrefs::network_http_network_access_on_socket_process_enabled
();
}
return
sUseSocketProcess
;
}
//
static
void
nsIOService::NotifySocketProcessPrefsChanged
(
const
char
* aName,
void
* aSelf) {
static_cast
<nsIOService*>(aSelf)->
NotifySocketProcessPrefsChanged
(aName);
}
void
nsIOService::NotifySocketProcessPrefsChanged
(
const
char
* aName) {
MOZ_ASSERT
(NS_IsMainThread());
if
(!
XRE_IsParentProcess
()) {
return
;
}
if
(!
StaticPrefs::network_process_enabled
()) {
return
;
}
dom::Pref
pref
(
nsCString
(aName),
/*
isLocked
*/
false
,
/*
isSanitized
*/
false
,
Nothing
(),
Nothing
());
Preferences::GetPreference
(&pref, GeckoProcessType_Socket,
/*
remoteType
*/
"
"
_ns);
auto
sendPrefUpdate = [pref]() {
Unused <<
gIOService
->
mSocketProcess
->
GetActor
()->
SendPreferenceUpdate
(
pref);
};
CallOrWaitForSocketProcess
(sendPrefUpdate);
}
void
nsIOService::OnProcessLaunchComplete
(SocketProcessHost* aHost,
bool
aSucceeded) {
MOZ_ASSERT
(NS_IsMainThread());
LOG
((
"
nsIOService::OnProcessLaunchComplete aSucceeded=%d
\n
"
, aSucceeded));
mSocketProcessLaunchComplete
= aSucceeded;
if
(
mShutdown
|| !
SocketProcessReady
() || !aSucceeded) {
mPendingEvents
.
Clear
();
return
;
}
if
(!
mPendingEvents
.
IsEmpty
()) {
nsTArray<std::function<
void
()>> pendingEvents =
std::move
(
mPendingEvents
);
for
(
auto
& func : pendingEvents) {
func
();
}
}
}
void
nsIOService::CallOrWaitForSocketProcess
(
const
std::function<
void
()>& aFunc) {
MOZ_ASSERT
(NS_IsMainThread());
if
(
IsSocketProcessLaunchComplete
() &&
SocketProcessReady
()) {
aFunc
();
}
else
{
mPendingEvents
.
AppendElement
(aFunc);
//
infallible
LaunchSocketProcess
();
}
}
int32_t
nsIOService::SocketProcessPid
() {
if
(!
mSocketProcess
) {
return
0
;
}
if
(SocketProcessParent* actor =
mSocketProcess
->
GetActor
()) {
return
(
int32_t
)actor->
OtherPid
();
}
return
0
;
}
bool
nsIOService::IsSocketProcessLaunchComplete
() {
MOZ_ASSERT
(NS_IsMainThread());
return
mSocketProcessLaunchComplete
;
}
void
nsIOService::OnProcessUnexpectedShutdown
(SocketProcessHost* aHost) {
MOZ_ASSERT
(NS_IsMainThread());
LOG
((
"
nsIOService::OnProcessUnexpectedShutdown
\n
"
));
DestroySocketProcess
();
mPendingEvents
.
Clear
();
//
Nothing to do if socket process was not used before.
if
(!
UseSocketProcess
()) {
return
;
}
sSocketProcessCrashedCount
++;
if
(
TooManySocketProcessCrash
()) {
sUseSocketProcessChecked
=
false
;
DNSServiceWrapper::SwitchToBackupDNSService
();
}
nsCOMPtr<nsIObserverService> observerService =
services::GetObserverService
();
if
(observerService) {
(
void
)observerService->
NotifyObservers
(
nullptr
,
"
network:socket-process-crashed
"
,
nullptr
);
}
//
UseSocketProcess() could return false if we have too many crashes, so we
//
should call it again.
if
(
UseSocketProcess
()) {
MOZ_ALWAYS_SUCCEEDS
(NS_DispatchToMainThread(
NewRunnableMethod
(
"
nsIOService::LaunchSocketProcess
"
,
this
,
&nsIOService::LaunchSocketProcess)));
}
}
RefPtr<MemoryReportingProcess>
nsIOService::GetSocketProcessMemoryReporter
() {
//
Check the prefs here again, since we don't want to create
//
SocketProcessMemoryReporter for some tests.
if
(!
StaticPrefs::network_process_enabled
() || !
SocketProcessReady
()) {
return
nullptr
;
}
return
new
SocketProcessMemoryReporter
();
}
NS_IMETHODIMP
nsIOService::SocketProcessTelemetryPing
() {
CallOrWaitForSocketProcess
([]() {
Unused <<
gIOService
->
mSocketProcess
->
GetActor
()
->
SendSocketProcessTelemetryPing
();
});
return
NS_OK
;
}
NS_IMPL_ISUPPORTS
(nsIOService, nsIIOService, nsINetUtil, nsISpeculativeConnect,
nsIObserver, nsIIOServiceInternal, nsISupportsWeakReference,
nsIObserverService)
//
//////////////////////////////////////////////////////////////////////////////
nsresult
nsIOService::RecheckCaptivePortal
() {
MOZ_ASSERT
(NS_IsMainThread(),
"
Must be called on the main thread
"
);
if
(!
mCaptivePortalService
) {
return
NS_OK
;
}
nsCOMPtr<nsIRunnable> task =
NewRunnableMethod
(
"
nsIOService::RecheckCaptivePortal
"
,
mCaptivePortalService
,
&nsICaptivePortalService::RecheckCaptivePortal);
return
NS_DispatchToMainThread(task);
}
nsresult
nsIOService::RecheckCaptivePortalIfLocalRedirect
(nsIChannel* newChan) {
nsresult rv;
if
(!
mCaptivePortalService
) {
return
NS_OK
;
}
nsCOMPtr<nsIURI> uri;
rv = newChan->
GetURI
(
getter_AddRefs
(uri));
if
(
NS_FAILED
(rv)) {
return
rv;
}
nsCString host;
rv = uri->
GetHost
(host);
if
(
NS_FAILED
(rv)) {
return
rv;
}
NetAddr addr;
//
If the redirect wasn't to an IP literal, so there's probably no need
//
to trigger the captive portal detection right now. It can wait.
if
(
NS_SUCCEEDED
(addr.
InitFromString
(host)) && addr.
IsIPAddrLocal
()) {
RecheckCaptivePortal
();
}
return
NS_OK
;
}
nsresult
nsIOService::AsyncOnChannelRedirect
(
nsIChannel* oldChan, nsIChannel* newChan,
uint32_t
flags,
nsAsyncRedirectVerifyHelper* helper) {
//
If a redirect to a local network address occurs, then chances are we
//
are in a captive portal, so we trigger a recheck.
RecheckCaptivePortalIfLocalRedirect
(newChan);
//
This is silly. I wish there was a simpler way to get at the global
//
reference of the contentSecurityManager. But it lives in the XPCOM
//
service registry.
nsCOMPtr<nsIChannelEventSink> sink;
sink =
mozilla::components::ContentSecurityManager::Service
();
if
(sink) {
nsresult rv =
helper->
DelegateOnChannelRedirect
(sink, oldChan, newChan, flags);
if
(
NS_FAILED
(rv))
return
rv;
}
//
Finally, our category
nsCOMArray<nsIChannelEventSink> entries;
mChannelEventSinks
.
GetEntries
(entries);
int32_t
len = entries.
Count
();
for
(
int32_t
i =
0
; i < len; ++i) {
nsresult rv =
helper->
DelegateOnChannelRedirect
(entries[i], oldChan, newChan, flags);
if
(
NS_FAILED
(rv))
return
rv;
}
nsCOMPtr<nsIHttpChannel>
httpChan
(
do_QueryInterface
(oldChan));
//
Collect the redirection from HTTP(S) only.
if
(httpChan) {
MOZ_ASSERT
(NS_IsMainThread());
nsCOMPtr<nsIURI> newURI;
newChan->
GetURI
(
getter_AddRefs
(newURI));
MOZ_ASSERT
(newURI);
nsAutoCString scheme;
newURI->
GetScheme
(scheme);
MOZ_ASSERT
(!scheme.
IsEmpty
());
if
(oldChan->
IsDocument
()) {
mozilla::glean::networking::http_redirect_to_scheme_top_level.
Get
(scheme)
.
Add
(
1
);
}
else
{
mozilla::glean::networking::http_redirect_to_scheme_subresource
.
Get
(scheme)
.
Add
(
1
);
}
}
return
NS_OK
;
}
bool
nsIOService::UsesExternalProtocolHandler
(
const
nsACString& aScheme) {
if
(aScheme ==
"
file
"
_ns || aScheme ==
"
chrome
"
_ns ||
aScheme ==
"
resource
"
_ns || aScheme ==
"
moz-src
"
_ns) {
//
Don't allow file:, chrome: or resource: URIs to be handled with
//
nsExternalProtocolHandler, since internally we rely on being able to
//
use and read from these URIs.
return
false
;
}
if
(aScheme ==
"
place
"
_ns || aScheme ==
"
fake-favicon-uri
"
_ns ||
aScheme ==
"
favicon
"
_ns || aScheme ==
"
moz-nullprincipal
"
_ns) {
//
Force place: fake-favicon-uri: favicon: and moz-nullprincipal: URIs to be
//
handled with nsExternalProtocolHandler, and not with a dynamically
//
registered handler.
return
true
;
}
//
If prefs configure the URI to be handled externally, do so.
for
(
const
auto
& scheme :
mForceExternalSchemes
) {
if
(aScheme == scheme) {
return
true
;
}
}
return
false
;
}
ProtocolHandlerInfo
nsIOService::LookupProtocolHandler
(
const
nsACString& aScheme) {
//
Look-ups are ASCII-case-insensitive, so lower-case the string before
//
continuing.
nsAutoCString
scheme
(aScheme);
ToLowerCase
(scheme);
//
NOTE: If we could get rid of mForceExternalSchemes (or prevent them from
//
disabling static protocols), we could avoid locking mLock until we need to
//
check `mRuntimeProtocolHandlers.
AutoReadLock
lock
(
mLock
);
if
(!
UsesExternalProtocolHandler
(scheme)) {
//
Try the static protocol handler first - they cannot be overridden by
//
dynamic protocols.
if
(
const
xpcom::StaticProtocolHandler* handler =
xpcom::StaticProtocolHandler::Lookup
(scheme)) {
return
ProtocolHandlerInfo
(*handler);
}
if
(
auto
handler =
mRuntimeProtocolHandlers
.
Lookup
(scheme)) {
return
ProtocolHandlerInfo
(handler.
Data
());
}
}
return
ProtocolHandlerInfo
(
xpcom::StaticProtocolHandler::Default
());
}
NS_IMETHODIMP
nsIOService::GetProtocolHandler
(
const
char
* scheme,
nsIProtocolHandler** result) {
AssertIsOnMainThread
();
NS_ENSURE_ARG_POINTER
(scheme);
*result =
LookupProtocolHandler
(
nsDependentCString
(scheme)).
Handler
().
take
();
return
*result ?
NS_OK
:
NS_ERROR_UNKNOWN_PROTOCOL
;
}
NS_IMETHODIMP
nsIOService::ExtractScheme
(
const
nsACString& inURI, nsACString& scheme) {
return
net_ExtractURLScheme
(inURI, scheme);
}
NS_IMETHODIMP
nsIOService::HostnameIsLocalIPAddress
(nsIURI* aURI,
bool
* aResult) {
NS_ENSURE_ARG_POINTER
(aURI);
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
NS_ENSURE_ARG_POINTER
(innerURI);
nsAutoCString host;
nsresult rv = innerURI->
GetAsciiHost
(host);
if
(
NS_FAILED
(rv)) {
return
rv;
}
*aResult =
false
;
NetAddr addr;
if
(
NS_SUCCEEDED
(addr.
InitFromString
(host)) && addr.
IsIPAddrLocal
()) {
*aResult =
true
;
}
return
NS_OK
;
}
NS_IMETHODIMP
nsIOService::HostnameIsIPAddressAny
(nsIURI* aURI,
bool
* aResult) {
NS_ENSURE_ARG_POINTER
(aURI);
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
NS_ENSURE_ARG_POINTER
(innerURI);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL