Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
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
#ifndef nsPACMan_h__
8
#define nsPACMan_h__
9
10
#include "mozilla/Atomics.h"
11
#include "mozilla/Attributes.h"
12
#include "mozilla/LinkedList.h"
13
#include "mozilla/Logging.h"
14
#include "mozilla/net/NeckoTargetHolder.h"
15
#include "mozilla/TimeStamp.h"
16
#include "nsAutoPtr.h"
17
#include "nsCOMPtr.h"
18
#include "nsIChannelEventSink.h"
19
#include "nsIInterfaceRequestor.h"
20
#include "nsIStreamLoader.h"
21
#include "nsThreadUtils.h"
22
#include "nsIURI.h"
23
#include "nsString.h"
24
#include "ProxyAutoConfig.h"
25
26
class nsISystemProxySettings;
27
class nsIDHCPClient;
28
class nsIThread;
29
30
namespace mozilla {
31
namespace net {
32
33
class nsPACMan;
34
class WaitForThreadShutdown;
35
36
/**
37
* This class defines a callback interface used by AsyncGetProxyForURI.
38
*/
39
class NS_NO_VTABLE nsPACManCallback : public nsISupports {
40
public:
41
/**
42
* This method is invoked on the same thread that called AsyncGetProxyForURI.
43
*
44
* @param status
45
* This parameter indicates whether or not the PAC query succeeded.
46
* @param pacString
47
* This parameter holds the value of the PAC string. It is empty when
48
* status is a failure code.
49
* @param newPACURL
50
* This parameter holds the URL of a new PAC file that should be loaded
51
* before the query is evaluated again. At least one of pacString and
52
* newPACURL should be 0 length.
53
*/
54
virtual void OnQueryComplete(nsresult status, const nsACString& pacString,
55
const nsACString& newPACURL) = 0;
56
};
57
58
class PendingPACQuery final : public Runnable,
59
public LinkedListElement<PendingPACQuery> {
60
public:
61
PendingPACQuery(nsPACMan* pacMan, nsIURI* uri, nsPACManCallback* callback,
62
bool mainThreadResponse);
63
64
// can be called from either thread
65
void Complete(nsresult status, const nsACString& pacString);
66
void UseAlternatePACFile(const nsACString& pacURL);
67
68
nsCString mSpec;
69
nsCString mScheme;
70
nsCString mHost;
71
int32_t mPort;
72
73
NS_IMETHOD Run(void) override; /* Runnable */
74
75
private:
76
nsPACMan* mPACMan; // weak reference
77
78
private:
79
RefPtr<nsPACManCallback> mCallback;
80
bool mOnMainThreadOnly;
81
};
82
83
/**
84
* This class provides an abstraction layer above the PAC thread. The methods
85
* defined on this class are intended to be called on the main thread only.
86
*/
87
88
class nsPACMan final : public nsIStreamLoaderObserver,
89
public nsIInterfaceRequestor,
90
public nsIChannelEventSink,
91
public NeckoTargetHolder {
92
public:
93
NS_DECL_THREADSAFE_ISUPPORTS
94
95
explicit nsPACMan(nsIEventTarget* mainThreadEventTarget);
96
97
/**
98
* This method may be called to shutdown the PAC manager. Any async queries
99
* that have not yet completed will either finish normally or be canceled by
100
* the time this method returns.
101
*/
102
void Shutdown();
103
104
/**
105
* This method queries a PAC result asynchronously. The callback runs on the
106
* calling thread. If the PAC file has not yet been loaded, then this method
107
* will queue up the request, and complete it once the PAC file has been
108
* loaded.
109
*
110
* @param uri
111
* The URI to query.
112
* @param callback
113
* The callback to run once the PAC result is available.
114
* @param mustCallbackOnMainThread
115
* If set to false the callback can be made from the PAC thread
116
*/
117
nsresult AsyncGetProxyForURI(nsIURI* uri, nsPACManCallback* callback,
118
bool mustCallbackOnMainThread);
119
120
/**
121
* This method may be called to reload the PAC file. While we are loading
122
* the PAC file, any asynchronous PAC queries will be queued up to be
123
* processed once the PAC file finishes loading.
124
*
125
* @param aSpec
126
* The non normalized uri spec of this URI used for comparison with
127
* system proxy settings to determine if the PAC uri has changed.
128
*/
129
nsresult LoadPACFromURI(const nsACString& aSpec);
130
131
/**
132
* Returns true if we are currently loading the PAC file.
133
*/
134
bool IsLoading() { return mLoader != nullptr; }
135
136
/**
137
* Returns true if the given URI matches the URI of our PAC file or the
138
* URI it has been redirected to. In the case of a chain of redirections
139
* only the current one being followed and the original are considered
140
* becuase this information is used, respectively, to determine if we
141
* should bypass the proxy (to fetch the pac file) or if the pac
142
* configuration has changed (and we should reload the pac file)
143
*/
144
bool IsPACURI(const nsACString& spec) {
145
return mPACURISpec.Equals(spec) || mPACURIRedirectSpec.Equals(spec) ||
146
mNormalPACURISpec.Equals(spec);
147
}
148
149
bool IsPACURI(nsIURI* uri) {
150
if (mPACURISpec.IsEmpty() && mPACURIRedirectSpec.IsEmpty()) {
151
return false;
152
}
153
154
nsAutoCString tmp;
155
nsresult rv = uri->GetSpec(tmp);
156
if (NS_FAILED(rv)) {
157
return false;
158
}
159
160
return IsPACURI(tmp);
161
}
162
163
bool IsUsingWPAD() { return mAutoDetect; }
164
165
nsresult Init(nsISystemProxySettings*);
166
static nsPACMan* sInstance;
167
168
// PAC thread operations only
169
void ProcessPendingQ();
170
void CancelPendingQ(nsresult, bool aShutdown);
171
172
void SetWPADOverDHCPEnabled(bool aValue) { mWPADOverDHCPEnabled = aValue; }
173
174
private:
175
NS_DECL_NSISTREAMLOADEROBSERVER
176
NS_DECL_NSIINTERFACEREQUESTOR
177
NS_DECL_NSICHANNELEVENTSINK
178
179
friend class PendingPACQuery;
180
friend class PACLoadComplete;
181
friend class ConfigureWPADComplete;
182
friend class ExecutePACThreadAction;
183
friend class WaitForThreadShutdown;
184
friend class TestPACMan;
185
186
~nsPACMan();
187
188
/**
189
* Cancel any existing load if any.
190
*/
191
void CancelExistingLoad();
192
193
/**
194
* Start loading the PAC file.
195
*/
196
void StartLoading();
197
198
/**
199
* Continue loading the PAC file.
200
*/
201
void ContinueLoadingAfterPACUriKnown();
202
203
/**
204
* This method may be called to reload the PAC file. While we are loading
205
* the PAC file, any asynchronous PAC queries will be queued up to be
206
* processed once the PAC file finishes loading.
207
*
208
* @param aSpec
209
* The non normalized uri spec of this URI used for comparison with
210
* system proxy settings to determine if the PAC uri has changed.
211
* @param aResetLoadFailureCount
212
* A flag saying whether the exponential back-off for attempting to
213
* reload the PAC should be reset.
214
*/
215
nsresult LoadPACFromURI(const nsACString& aSpec, bool aResetLoadFailureCount);
216
217
/**
218
* Reload the PAC file if there is reason to.
219
*/
220
void MaybeReloadPAC();
221
222
/**
223
* Called when we fail to load the PAC file.
224
*/
225
void OnLoadFailure();
226
227
/**
228
* PostQuery() only runs on the PAC thread and it is used to
229
* place a pendingPACQuery into the queue and potentially
230
* execute the queue if it was otherwise empty
231
*/
232
nsresult PostQuery(PendingPACQuery* query);
233
234
// Having found the PAC URI on the PAC thread, copy it to a string which
235
// can be altered on the main thread.
236
void AssignPACURISpec(const nsACString& aSpec);
237
238
// PAC thread operations only
239
void PostProcessPendingQ();
240
void PostCancelPendingQ(nsresult, bool aShutdown = false);
241
bool ProcessPending();
242
nsresult GetPACFromDHCP(nsACString& aSpec);
243
nsresult ConfigureWPAD(nsACString& aSpec);
244
245
private:
246
/**
247
* Dispatches a runnable to the PAC processing thread. Handles lazy
248
* instantiation of the thread.
249
*
250
* @param aEvent The event to disptach.
251
* @param aSync Whether or not this should be synchronous dispatch.
252
*/
253
nsresult DispatchToPAC(already_AddRefed<nsIRunnable> aEvent,
254
bool aSync = false);
255
256
ProxyAutoConfig mPAC;
257
nsCOMPtr<nsIThread> mPACThread;
258
nsCOMPtr<nsISystemProxySettings> mSystemProxySettings;
259
nsCOMPtr<nsIDHCPClient> mDHCPClient;
260
261
LinkedList<PendingPACQuery> mPendingQ; /* pac thread only */
262
263
// These specs are not nsIURI so that they can be used off the main thread.
264
// The non-normalized versions are directly from the configuration, the
265
// normalized version has been extracted from an nsIURI
266
nsCString mPACURISpec;
267
nsCString mPACURIRedirectSpec;
268
nsCString mNormalPACURISpec;
269
270
nsCOMPtr<nsIStreamLoader> mLoader;
271
bool mLoadPending;
272
Atomic<bool, Relaxed> mShutdown;
273
TimeStamp mScheduledReload;
274
uint32_t mLoadFailureCount;
275
276
bool mInProgress;
277
bool mIncludePath;
278
bool mAutoDetect;
279
bool mWPADOverDHCPEnabled;
280
int32_t mProxyConfigType;
281
};
282
283
extern LazyLogModule gProxyLog;
284
285
} // namespace net
286
} // namespace mozilla
287
288
#endif // nsPACMan_h__