1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 
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 mozilla_net_NullHttpTransaction_h
8 #define mozilla_net_NullHttpTransaction_h
9 
10 #include "nsAHttpTransaction.h"
11 #include "mozilla/Attributes.h"
12 #include "TimingStruct.h"
13 
14 // This is the minimal nsAHttpTransaction implementation. A NullHttpTransaction
15 // can be used to drive connection level semantics (such as SSL handshakes
16 // tunnels) so that a nsHttpConnection becomes fully established in
17 // anticipation of a real transaction needing to use it soon.
18 
19 class nsIHttpActivityObserver;
20 
21 namespace mozilla {
22 namespace net {
23 
24 class nsAHttpConnection;
25 class nsHttpConnectionInfo;
26 class nsHttpRequestHead;
27 
28 // 6c445340-3b82-4345-8efa-4902c3b8805a
29 #define NS_NULLHTTPTRANSACTION_IID                   \
30   {                                                  \
31     0x6c445340, 0x3b82, 0x4345, {                    \
32       0x8e, 0xfa, 0x49, 0x02, 0xc3, 0xb8, 0x80, 0x5a \
33     }                                                \
34   }
35 
36 class NullHttpTransaction : public nsAHttpTransaction {
37  public:
38   NS_DECLARE_STATIC_IID_ACCESSOR(NS_NULLHTTPTRANSACTION_IID)
39   NS_DECL_THREADSAFE_ISUPPORTS
40   NS_DECL_NSAHTTPTRANSACTION
41 
42   NullHttpTransaction(nsHttpConnectionInfo *ci,
43                       nsIInterfaceRequestor *callbacks, uint32_t caps);
44 
45   MOZ_MUST_USE bool Claim();
46   void Unclaim();
47 
48   // Overload of nsAHttpTransaction methods
IsNullTransaction()49   bool IsNullTransaction() final { return true; }
QueryNullTransaction()50   NullHttpTransaction *QueryNullTransaction() final { return this; }
ResponseTimeoutEnabled()51   bool ResponseTimeoutEnabled() const final { return true; }
ResponseTimeout()52   PRIntervalTime ResponseTimeout() final { return PR_SecondsToInterval(15); }
53 
54   // We have to override this function because |mTransaction| in
55   // nsHalfOpenSocket could be either nsHttpTransaction or NullHttpTransaction.
56   // NullHttpTransaction will be activated on the connection immediately after
57   // creation and be never put in a pending queue, so it's OK to just return 0.
TopLevelOuterContentWindowId()58   uint64_t TopLevelOuterContentWindowId() override { return 0; }
59 
Timings()60   TimingStruct Timings() { return mTimings; }
61 
GetTcpConnectEnd()62   mozilla::TimeStamp GetTcpConnectEnd() { return mTimings.tcpConnectEnd; }
GetSecureConnectionStart()63   mozilla::TimeStamp GetSecureConnectionStart() {
64     return mTimings.secureConnectionStart;
65   }
66 
SetFastOpenStatus(uint8_t aStatus)67   void SetFastOpenStatus(uint8_t aStatus) override {
68     mFastOpenStatus = aStatus;
69   }
70 
71  protected:
72   virtual ~NullHttpTransaction();
73 
74  private:
75   nsresult mStatus;
76 
77  protected:
78   uint32_t mCaps;
79   nsHttpRequestHead *mRequestHead;
80 
81  private:
82   // mCapsToClear holds flags that should be cleared in mCaps, e.g. unset
83   // NS_HTTP_REFRESH_DNS when DNS refresh request has completed to avoid
84   // redundant requests on the network. The member itself is atomic, but
85   // access to it from the networking thread may happen either before or
86   // after the main thread modifies it. To deal with raciness, only unsetting
87   // bitfields should be allowed: 'lost races' will thus err on the
88   // conservative side, e.g. by going ahead with a 2nd DNS refresh.
89   Atomic<uint32_t> mCapsToClear;
90   bool mIsDone;
91   bool mClaimed;
92   TimingStruct mTimings;
93   uint8_t mFastOpenStatus;
94 
95  protected:
96   RefPtr<nsAHttpConnection> mConnection;
97   nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
98   RefPtr<nsHttpConnectionInfo> mConnectionInfo;
99   nsCOMPtr<nsIHttpActivityObserver> mActivityDistributor;
100 };
101 
102 NS_DEFINE_STATIC_IID_ACCESSOR(NullHttpTransaction, NS_NULLHTTPTRANSACTION_IID)
103 
104 }  // namespace net
105 }  // namespace mozilla
106 
107 #endif  // mozilla_net_NullHttpTransaction_h
108