Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef nsIOService_h__
7 : #define nsIOService_h__
8 :
9 : #include "nsStringFwd.h"
10 : #include "nsIIOService2.h"
11 : #include "nsTArray.h"
12 : #include "nsCOMPtr.h"
13 : #include "nsWeakPtr.h"
14 : #include "nsIObserver.h"
15 : #include "nsWeakReference.h"
16 : #include "nsINetUtil.h"
17 : #include "nsIChannelEventSink.h"
18 : #include "nsCategoryCache.h"
19 : #include "nsISpeculativeConnect.h"
20 : #include "nsDataHashtable.h"
21 : #include "mozilla/Atomics.h"
22 : #include "mozilla/Attributes.h"
23 : #include "prtime.h"
24 : #include "nsICaptivePortalService.h"
25 :
26 : #define NS_N(x) (sizeof(x)/sizeof(*x))
27 :
28 : // We don't want to expose this observer topic.
29 : // Intended internal use only for remoting offline/inline events.
30 : // See Bug 552829
31 : #define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline"
32 : #define NS_IPC_IOSERVICE_SET_CONNECTIVITY_TOPIC "ipc:network:set-connectivity"
33 :
34 : static const char gScheme[][sizeof("moz-safe-about")] =
35 : {"chrome", "file", "http", "https", "jar", "data", "about", "moz-safe-about", "resource"};
36 :
37 : class nsINetworkLinkService;
38 : class nsIPrefBranch;
39 : class nsIProtocolProxyService2;
40 : class nsIProxyInfo;
41 : class nsPIDNSService;
42 : class nsPISocketTransportService;
43 :
44 : namespace mozilla {
45 : namespace net {
46 : class NeckoChild;
47 : class nsAsyncRedirectVerifyHelper;
48 :
49 : class nsIOService final : public nsIIOService2
50 : , public nsIObserver
51 : , public nsINetUtil
52 : , public nsISpeculativeConnect
53 : , public nsSupportsWeakReference
54 : , public nsIIOServiceInternal
55 : {
56 : public:
57 : NS_DECL_THREADSAFE_ISUPPORTS
58 : NS_DECL_NSIIOSERVICE
59 : NS_DECL_NSIIOSERVICE2
60 : NS_DECL_NSIOBSERVER
61 : NS_DECL_NSINETUTIL
62 : NS_DECL_NSISPECULATIVECONNECT
63 : NS_DECL_NSIIOSERVICEINTERNAL
64 :
65 : // Gets the singleton instance of the IO Service, creating it as needed
66 : // Returns nullptr on out of memory or failure to initialize.
67 : // Returns an addrefed pointer.
68 : static nsIOService* GetInstance();
69 :
70 : nsresult Init();
71 : nsresult NewURI(const char* aSpec, nsIURI* aBaseURI,
72 : nsIURI* *result,
73 : nsIProtocolHandler* *hdlrResult);
74 :
75 : // Called by channels before a redirect happens. This notifies the global
76 : // redirect observers.
77 : nsresult AsyncOnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan,
78 : uint32_t flags,
79 : nsAsyncRedirectVerifyHelper *helper);
80 :
81 15 : bool IsOffline() { return mOffline; }
82 0 : PRIntervalTime LastOfflineStateChange() { return mLastOfflineStateChange; }
83 0 : PRIntervalTime LastConnectivityChange() { return mLastConnectivityChange; }
84 0 : PRIntervalTime LastNetworkLinkChange() { return mLastNetworkLinkChange; }
85 138 : bool IsNetTearingDown() { return mShutdown || mOfflineForProfileChange ||
86 138 : mHttpHandlerAlreadyShutingDown; }
87 0 : PRIntervalTime NetTearingDownStarted() { return mNetTearingDownStarted; }
88 :
89 : // nsHttpHandler is going to call this function to inform nsIOService that network
90 : // is in process of tearing down. Moving nsHttpConnectionMgr::Shutdown to nsIOService
91 : // caused problems (bug 1242755) so we doing it in this way.
92 : // As soon as nsIOService gets notification that it is shutdown it is going to
93 : // reset mHttpHandlerAlreadyShutingDown.
94 : void SetHttpHandlerAlreadyShutingDown();
95 :
96 : bool IsLinkUp();
97 :
98 : static bool IsDataURIUniqueOpaqueOrigin();
99 :
100 : // Used to count the total number of HTTP requests made
101 0 : void IncrementRequestNumber() { mTotalRequests++; }
102 0 : uint32_t GetTotalRequestNumber() { return mTotalRequests; }
103 : // Used to keep "race cache with network" stats
104 0 : void IncrementCacheWonRequestNumber() { mCacheWon++; }
105 0 : uint32_t GetCacheWonRequestNumber() { return mCacheWon; }
106 0 : void IncrementNetWonRequestNumber() { mNetWon++; }
107 0 : uint32_t GetNetWonRequestNumber() { return mNetWon; }
108 :
109 : // Used to trigger a recheck of the captive portal status
110 : nsresult RecheckCaptivePortal();
111 : private:
112 : // These shouldn't be called directly:
113 : // - construct using GetInstance
114 : // - destroy using Release
115 : nsIOService();
116 : ~nsIOService();
117 : nsresult SetConnectivityInternal(bool aConnectivity);
118 :
119 : nsresult OnNetworkLinkEvent(const char *data);
120 :
121 : nsresult GetCachedProtocolHandler(const char *scheme,
122 : nsIProtocolHandler* *hdlrResult,
123 : uint32_t start=0,
124 : uint32_t end=0);
125 : nsresult CacheProtocolHandler(const char *scheme,
126 : nsIProtocolHandler* hdlr);
127 :
128 : nsresult InitializeCaptivePortalService();
129 : nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan);
130 :
131 : // Prefs wrangling
132 : void PrefsChanged(nsIPrefBranch *prefs, const char *pref = nullptr);
133 : void GetPrefBranch(nsIPrefBranch **);
134 : void ParsePortList(nsIPrefBranch *prefBranch, const char *pref, bool remove);
135 :
136 : nsresult InitializeSocketTransportService();
137 : nsresult InitializeNetworkLinkService();
138 :
139 : // consolidated helper function
140 : void LookupProxyInfo(nsIURI *aURI, nsIURI *aProxyURI, uint32_t aProxyFlags,
141 : nsCString *aScheme, nsIProxyInfo **outPI);
142 :
143 : nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI,
144 : nsIURI* aProxyURI,
145 : uint32_t aProxyFlags,
146 : nsILoadInfo* aLoadInfo,
147 : nsIChannel** result);
148 :
149 : nsresult SpeculativeConnectInternal(nsIURI *aURI,
150 : nsIPrincipal *aPrincipal,
151 : nsIInterfaceRequestor *aCallbacks,
152 : bool aAnonymous);
153 :
154 : private:
155 : bool mOffline;
156 : mozilla::Atomic<bool, mozilla::Relaxed> mOfflineForProfileChange;
157 : bool mManageLinkStatus;
158 : bool mConnectivity;
159 : // If true, the connectivity state will be mirrored by IOService.offline
160 : // meaning if !mConnectivity, GetOffline() will return true
161 : bool mOfflineMirrorsConnectivity;
162 :
163 : // Used to handle SetOffline() reentrancy. See the comment in
164 : // SetOffline() for more details.
165 : bool mSettingOffline;
166 : bool mSetOfflineValue;
167 :
168 : mozilla::Atomic<bool, mozilla::Relaxed> mShutdown;
169 : mozilla::Atomic<bool, mozilla::Relaxed> mHttpHandlerAlreadyShutingDown;
170 :
171 : nsCOMPtr<nsPISocketTransportService> mSocketTransportService;
172 : nsCOMPtr<nsPIDNSService> mDNSService;
173 : nsCOMPtr<nsIProtocolProxyService2> mProxyService;
174 : nsCOMPtr<nsICaptivePortalService> mCaptivePortalService;
175 : nsCOMPtr<nsINetworkLinkService> mNetworkLinkService;
176 : bool mNetworkLinkServiceInitialized;
177 :
178 : // Cached protocol handlers, only accessed on the main thread
179 : nsWeakPtr mWeakHandler[NS_N(gScheme)];
180 :
181 : // cached categories
182 : nsCategoryCache<nsIChannelEventSink> mChannelEventSinks;
183 :
184 : nsTArray<int32_t> mRestrictedPortList;
185 :
186 : bool mNetworkNotifyChanged;
187 :
188 : static bool sIsDataURIUniqueOpaqueOrigin;
189 :
190 : uint32_t mTotalRequests;
191 : uint32_t mCacheWon;
192 : uint32_t mNetWon;
193 :
194 : // These timestamps are needed for collecting telemetry on PR_Connect,
195 : // PR_ConnectContinue and PR_Close blocking time. If we spend very long
196 : // time in any of these functions we want to know if and what network
197 : // change has happened shortly before.
198 : mozilla::Atomic<PRIntervalTime> mLastOfflineStateChange;
199 : mozilla::Atomic<PRIntervalTime> mLastConnectivityChange;
200 : mozilla::Atomic<PRIntervalTime> mLastNetworkLinkChange;
201 :
202 : // Time a network tearing down started.
203 : mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted;
204 : public:
205 : // Used for all default buffer sizes that necko allocates.
206 : static uint32_t gDefaultSegmentSize;
207 : static uint32_t gDefaultSegmentCount;
208 : };
209 :
210 : /**
211 : * Reference to the IO service singleton. May be null.
212 : */
213 : extern nsIOService* gIOService;
214 :
215 : } // namespace net
216 : } // namespace mozilla
217 :
218 : #endif // nsIOService_h__
|