michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsPACMan_h__ michael@0: #define nsPACMan_h__ michael@0: michael@0: #include "nsIStreamLoader.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: #include "nsIChannelEventSink.h" michael@0: #include "ProxyAutoConfig.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsIURI.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsString.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/LinkedList.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "mozilla/TimeStamp.h" michael@0: #include "prlog.h" michael@0: michael@0: class nsPACMan; michael@0: class nsISystemProxySettings; michael@0: class nsIThread; michael@0: class WaitForThreadShutdown; michael@0: michael@0: /** michael@0: * This class defines a callback interface used by AsyncGetProxyForChannel. michael@0: */ michael@0: class NS_NO_VTABLE nsPACManCallback : public nsISupports michael@0: { michael@0: public: michael@0: /** michael@0: * This method is invoked on the same thread that called AsyncGetProxyForChannel. michael@0: * michael@0: * @param status michael@0: * This parameter indicates whether or not the PAC query succeeded. michael@0: * @param pacString michael@0: * This parameter holds the value of the PAC string. It is empty when michael@0: * status is a failure code. michael@0: * @param newPACURL michael@0: * This parameter holds the URL of a new PAC file that should be loaded michael@0: * before the query is evaluated again. At least one of pacString and michael@0: * newPACURL should be 0 length. michael@0: */ michael@0: virtual void OnQueryComplete(nsresult status, michael@0: const nsCString &pacString, michael@0: const nsCString &newPACURL) = 0; michael@0: }; michael@0: michael@0: class PendingPACQuery MOZ_FINAL : public nsRunnable, michael@0: public mozilla::LinkedListElement michael@0: { michael@0: public: michael@0: PendingPACQuery(nsPACMan *pacMan, nsIURI *uri, michael@0: nsPACManCallback *callback, bool mainThreadResponse); michael@0: michael@0: // can be called from either thread michael@0: void Complete(nsresult status, const nsCString &pacString); michael@0: void UseAlternatePACFile(const nsCString &pacURL); michael@0: michael@0: nsCString mSpec; michael@0: nsCString mScheme; michael@0: nsCString mHost; michael@0: int32_t mPort; michael@0: michael@0: NS_IMETHOD Run(void); /* nsRunnable */ michael@0: michael@0: private: michael@0: nsPACMan *mPACMan; // weak reference michael@0: nsRefPtr mCallback; michael@0: bool mOnMainThreadOnly; michael@0: }; michael@0: michael@0: /** michael@0: * This class provides an abstraction layer above the PAC thread. The methods michael@0: * defined on this class are intended to be called on the main thread only. michael@0: */ michael@0: michael@0: class nsPACMan MOZ_FINAL : public nsIStreamLoaderObserver michael@0: , public nsIInterfaceRequestor michael@0: , public nsIChannelEventSink michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: nsPACMan(); michael@0: michael@0: /** michael@0: * This method may be called to shutdown the PAC manager. Any async queries michael@0: * that have not yet completed will either finish normally or be canceled by michael@0: * the time this method returns. michael@0: */ michael@0: void Shutdown(); michael@0: michael@0: /** michael@0: * This method queries a PAC result asynchronously. The callback runs on the michael@0: * calling thread. If the PAC file has not yet been loaded, then this method michael@0: * will queue up the request, and complete it once the PAC file has been michael@0: * loaded. michael@0: * michael@0: * @param channel michael@0: * The channel to query. michael@0: * @param callback michael@0: * The callback to run once the PAC result is available. michael@0: * @param mustCallbackOnMainThread michael@0: * If set to false the callback can be made from the PAC thread michael@0: */ michael@0: nsresult AsyncGetProxyForChannel(nsIChannel *channel, nsPACManCallback *callback, michael@0: bool mustCallbackOnMainThread); michael@0: michael@0: /** michael@0: * This method may be called to reload the PAC file. While we are loading michael@0: * the PAC file, any asynchronous PAC queries will be queued up to be michael@0: * processed once the PAC file finishes loading. michael@0: * michael@0: * @param pacSpec michael@0: * The non normalized uri spec of this URI used for comparison with michael@0: * system proxy settings to determine if the PAC uri has changed. michael@0: */ michael@0: nsresult LoadPACFromURI(const nsCString &pacSpec); michael@0: michael@0: /** michael@0: * Returns true if we are currently loading the PAC file. michael@0: */ michael@0: bool IsLoading() { return mLoader != nullptr; } michael@0: michael@0: /** michael@0: * Returns true if the given URI matches the URI of our PAC file or the michael@0: * URI it has been redirected to. In the case of a chain of redirections michael@0: * only the current one being followed and the original are considered michael@0: * becuase this information is used, respectively, to determine if we michael@0: * should bypass the proxy (to fetch the pac file) or if the pac michael@0: * configuration has changed (and we should reload the pac file) michael@0: */ michael@0: bool IsPACURI(const nsACString &spec) michael@0: { michael@0: return mPACURISpec.Equals(spec) || mPACURIRedirectSpec.Equals(spec) || michael@0: mNormalPACURISpec.Equals(spec); michael@0: } michael@0: michael@0: bool IsPACURI(nsIURI *uri) { michael@0: if (mPACURISpec.IsEmpty() && mPACURIRedirectSpec.IsEmpty()) michael@0: return false; michael@0: michael@0: nsAutoCString tmp; michael@0: uri->GetSpec(tmp); michael@0: return IsPACURI(tmp); michael@0: } michael@0: michael@0: NS_HIDDEN_(nsresult) Init(nsISystemProxySettings *); michael@0: static nsPACMan *sInstance; michael@0: michael@0: // PAC thread operations only michael@0: void ProcessPendingQ(); michael@0: void CancelPendingQ(nsresult); michael@0: michael@0: private: michael@0: NS_DECL_NSISTREAMLOADEROBSERVER michael@0: NS_DECL_NSIINTERFACEREQUESTOR michael@0: NS_DECL_NSICHANNELEVENTSINK michael@0: michael@0: friend class PendingPACQuery; michael@0: friend class PACLoadComplete; michael@0: friend class ExecutePACThreadAction; michael@0: friend class WaitForThreadShutdown; michael@0: michael@0: ~nsPACMan(); michael@0: michael@0: /** michael@0: * Cancel any existing load if any. michael@0: */ michael@0: void CancelExistingLoad(); michael@0: michael@0: /** michael@0: * Start loading the PAC file. michael@0: */ michael@0: void StartLoading(); michael@0: michael@0: /** michael@0: * Reload the PAC file if there is reason to. michael@0: */ michael@0: void MaybeReloadPAC(); michael@0: michael@0: /** michael@0: * Called when we fail to load the PAC file. michael@0: */ michael@0: void OnLoadFailure(); michael@0: michael@0: /** michael@0: * PostQuery() only runs on the PAC thread and it is used to michael@0: * place a pendingPACQuery into the queue and potentially michael@0: * execute the queue if it was otherwise empty michael@0: */ michael@0: nsresult PostQuery(PendingPACQuery *query); michael@0: michael@0: // PAC thread operations only michael@0: void PostProcessPendingQ(); michael@0: void PostCancelPendingQ(nsresult); michael@0: bool ProcessPending(); michael@0: void NamePACThread(); michael@0: michael@0: private: michael@0: mozilla::net::ProxyAutoConfig mPAC; michael@0: nsCOMPtr mPACThread; michael@0: nsCOMPtr mSystemProxySettings; michael@0: michael@0: mozilla::LinkedList mPendingQ; /* pac thread only */ michael@0: michael@0: // These specs are not nsIURI so that they can be used off the main thread. michael@0: // The non-normalized versions are directly from the configuration, the michael@0: // normalized version has been extracted from an nsIURI michael@0: nsCString mPACURISpec; michael@0: nsCString mPACURIRedirectSpec; michael@0: nsCString mNormalPACURISpec; michael@0: michael@0: nsCOMPtr mLoader; michael@0: bool mLoadPending; michael@0: bool mShutdown; michael@0: mozilla::TimeStamp mScheduledReload; michael@0: uint32_t mLoadFailureCount; michael@0: michael@0: bool mInProgress; michael@0: }; michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: PRLogModuleInfo* GetProxyLog(); michael@0: } michael@0: } michael@0: michael@0: #endif // nsPACMan_h__