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 nsFtpProtocolHandler_h__
7 #define nsFtpProtocolHandler_h__
8 
9 #include "nsFtpControlConnection.h"
10 #include "nsIProxiedProtocolHandler.h"
11 #include "nsTArray.h"
12 #include "nsITimer.h"
13 #include "nsIObserver.h"
14 #include "nsWeakReference.h"
15 
16 //-----------------------------------------------------------------------------
17 
18 class nsFtpProtocolHandler final : public nsIProxiedProtocolHandler,
19                                    public nsIObserver,
20                                    public nsSupportsWeakReference {
21  public:
22   NS_DECL_THREADSAFE_ISUPPORTS
23   NS_DECL_NSIPROTOCOLHANDLER
24   NS_DECL_NSIPROXIEDPROTOCOLHANDLER
25   NS_DECL_NSIOBSERVER
26 
27   nsFtpProtocolHandler();
28 
29   nsresult Init();
30 
31   // FTP Connection list access
32   nsresult InsertConnection(nsIURI *aKey, nsFtpControlConnection *aConn);
33   nsresult RemoveConnection(nsIURI *aKey, nsFtpControlConnection **aConn);
GetSessionId()34   uint32_t GetSessionId() { return mSessionId; }
35 
GetDataQoSBits()36   uint8_t GetDataQoSBits() { return mDataQoSBits; }
GetControlQoSBits()37   uint8_t GetControlQoSBits() { return mControlQoSBits; }
38 
39  private:
40   virtual ~nsFtpProtocolHandler();
41 
42   // Stuff for the timer callback function
43   struct timerStruct {
44     nsCOMPtr<nsITimer> timer;
45     RefPtr<nsFtpControlConnection> conn;
46     char *key;
47 
timerStructtimerStruct48     timerStruct() : key(nullptr) {}
49 
~timerStructtimerStruct50     ~timerStruct() {
51       if (timer) timer->Cancel();
52       if (key) free(key);
53       if (conn) {
54         conn->Disconnect(NS_ERROR_ABORT);
55       }
56     }
57   };
58 
59   static void Timeout(nsITimer *aTimer, void *aClosure);
60   void ClearAllConnections();
61 
62   nsTArray<timerStruct *> mRootConnectionList;
63 
64   int32_t mIdleTimeout;
65   bool mEnabled;
66 
67   // When "clear active logins" is performed, all idle connection are dropped
68   // and mSessionId is incremented. When nsFtpState wants to insert idle
69   // connection we refuse to cache if its mSessionId is different (i.e.
70   // control connection had been created before last "clear active logins" was
71   // performed.
72   uint32_t mSessionId;
73 
74   uint8_t mControlQoSBits;
75   uint8_t mDataQoSBits;
76 };
77 
78 //-----------------------------------------------------------------------------
79 
80 extern nsFtpProtocolHandler *gFtpHandler;
81 
82 #include "mozilla/Logging.h"
83 extern mozilla::LazyLogModule gFTPLog;
84 
85 #endif  // !nsFtpProtocolHandler_h__
86