Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / netwerk / base / Tickler.h
blob264ecefebb1d64d7625daa53c405d3ac8b3665ae
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
6 #ifndef mozilla_net_Tickler_h
7 #define mozilla_net_Tickler_h
9 // The tickler sends a regular 0 byte UDP heartbeat out to a
10 // particular address for a short time after it has been touched. This
11 // is used on some mobile wifi chipsets to mitigate Power Save Polling
12 // (PSP) Mode when we are anticipating a response packet
13 // soon. Typically PSP adds 100ms of latency to a read event because
14 // the packet delivery is not triggered until the 802.11 beacon is
15 // delivered to the host (100ms is the standard Access Point
16 // configuration for the beacon interval.) Requesting a frequent
17 // transmission and getting a CTS frame from the AP at least that
18 // frequently allows for low latency receives when we have reason to
19 // expect them (e.g a SYN-ACK).
21 // The tickler is used to allow RTT based phases of web transport to
22 // complete quickly when on wifi - ARP, DNS, TCP handshake, SSL
23 // handshake, HTTP headers, and the TCP slow start phase. The
24 // transaction is given up to 400 miliseconds by default to get
25 // through those phases before the tickler is disabled.
27 // The tickler only applies to wifi on mobile right now. Hopefully it
28 // can also be restricted to particular handset models in the future.
30 #if defined(ANDROID) && !defined(MOZ_PROXY_BYPASS_PROTECTION)
31 # define MOZ_USE_WIFI_TICKLER
32 #endif
34 #include "mozilla/Attributes.h"
35 #include "nsISupports.h"
36 #include <stdint.h>
38 #ifdef MOZ_USE_WIFI_TICKLER
39 # include "mozilla/Mutex.h"
40 # include "mozilla/TimeStamp.h"
41 # include "nsISupports.h"
42 # include "nsIThread.h"
43 # include "nsITimer.h"
44 # include "nsWeakReference.h"
45 # include "prio.h"
47 class nsIPrefBranch;
48 #endif
50 namespace mozilla {
51 namespace net {
53 #ifdef MOZ_USE_WIFI_TICKLER
55 // 8f769ed6-207c-4af9-9f7e-9e832da3754e
56 # define NS_TICKLER_IID \
57 { \
58 0x8f769ed6, 0x207c, 0x4af9, { \
59 0x9f, 0x7e, 0x9e, 0x83, 0x2d, 0xa3, 0x75, 0x4e \
60 } \
63 class Tickler final : public nsSupportsWeakReference {
64 public:
65 NS_DECL_THREADSAFE_ISUPPORTS
66 NS_DECLARE_STATIC_IID_ACCESSOR(NS_TICKLER_IID)
68 // These methods are main thread only
69 Tickler();
70 void Cancel();
71 nsresult Init();
72 void SetIPV4Address(uint32_t address);
73 void SetIPV4Port(uint16_t port);
75 // Tickle the tickler to (re-)start the activity.
76 // May call from any thread
77 void Tickle();
79 private:
80 ~Tickler();
82 friend class TicklerTimer;
83 Mutex mLock MOZ_UNANNOTATED;
84 nsCOMPtr<nsIThread> mThread;
85 nsCOMPtr<nsITimer> mTimer;
86 nsCOMPtr<nsIPrefBranch> mPrefs;
88 bool mActive;
89 bool mCanceled;
90 bool mEnabled;
91 uint32_t mDelay;
92 TimeDuration mDuration;
93 PRFileDesc* mFD;
95 TimeStamp mLastTickle;
96 PRNetAddr mAddr;
98 // These functions may be called from any thread
99 void PostCheckTickler();
100 void MaybeStartTickler();
101 void MaybeStartTicklerUnlocked();
103 // Tickler thread only
104 void CheckTickler();
105 void StartTickler();
106 void StopTickler();
109 NS_DEFINE_STATIC_IID_ACCESSOR(Tickler, NS_TICKLER_IID)
111 #else // not defined MOZ_USE_WIFI_TICKLER
113 class Tickler final : public nsISupports {
114 ~Tickler() = default;
116 public:
117 NS_DECL_THREADSAFE_ISUPPORTS
119 Tickler() = default;
120 nsresult Init() { return NS_ERROR_NOT_IMPLEMENTED; }
121 void Cancel() {}
122 void SetIPV4Address(uint32_t){};
123 void SetIPV4Port(uint16_t) {}
124 void Tickle() {}
127 #endif // defined MOZ_USE_WIFI_TICKLER
129 } // namespace net
130 } // namespace mozilla
132 #endif // mozilla_net_Tickler_h