Tickler.h (3641B)
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/. */ 5 6 #ifndef mozilla_net_Tickler_h 7 #define mozilla_net_Tickler_h 8 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). 20 // 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. 26 // 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. 29 30 #if defined(ANDROID) && !defined(MOZ_PROXY_BYPASS_PROTECTION) 31 # define MOZ_USE_WIFI_TICKLER 32 #endif 33 34 #include "mozilla/Attributes.h" 35 #include "nsISupports.h" 36 #include <stdint.h> 37 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" 46 47 class nsIPrefBranch; 48 #endif 49 50 namespace mozilla { 51 namespace net { 52 53 #ifdef MOZ_USE_WIFI_TICKLER 54 55 // 8f769ed6-207c-4af9-9f7e-9e832da3754e 56 # define NS_TICKLER_IID \ 57 {0x8f769ed6, \ 58 0x207c, \ 59 0x4af9, \ 60 {0x9f, 0x7e, 0x9e, 0x83, 0x2d, 0xa3, 0x75, 0x4e}} 61 62 class Tickler final : public nsSupportsWeakReference { 63 public: 64 NS_DECL_THREADSAFE_ISUPPORTS 65 NS_INLINE_DECL_STATIC_IID(NS_TICKLER_IID) 66 67 // These methods are main thread only 68 Tickler(); 69 void Cancel(); 70 nsresult Init(); 71 void SetIPV4Address(uint32_t address); 72 void SetIPV4Port(uint16_t port); 73 74 // Tickle the tickler to (re-)start the activity. 75 // May call from any thread 76 void Tickle(); 77 78 private: 79 ~Tickler(); 80 81 friend class TicklerTimer; 82 Mutex mLock MOZ_UNANNOTATED; 83 nsCOMPtr<nsIThread> mThread; 84 nsCOMPtr<nsITimer> mTimer; 85 nsCOMPtr<nsIPrefBranch> mPrefs; 86 87 bool mActive; 88 bool mCanceled; 89 bool mEnabled; 90 uint32_t mDelay; 91 TimeDuration mDuration; 92 PRFileDesc* mFD; 93 94 TimeStamp mLastTickle; 95 PRNetAddr mAddr; 96 97 // These functions may be called from any thread 98 void PostCheckTickler(); 99 void MaybeStartTickler(); 100 void MaybeStartTicklerUnlocked(); 101 102 // Tickler thread only 103 void CheckTickler(); 104 void StartTickler(); 105 void StopTickler(); 106 }; 107 108 #else // not defined MOZ_USE_WIFI_TICKLER 109 110 class Tickler final : public nsISupports { 111 ~Tickler() = default; 112 113 public: 114 NS_DECL_THREADSAFE_ISUPPORTS 115 116 Tickler() = default; 117 nsresult Init() { return NS_ERROR_NOT_IMPLEMENTED; } 118 void Cancel() {} 119 void SetIPV4Address(uint32_t) {}; 120 void SetIPV4Port(uint16_t) {} 121 void Tickle() {} 122 }; 123 124 #endif // defined MOZ_USE_WIFI_TICKLER 125 126 } // namespace net 127 } // namespace mozilla 128 129 #endif // mozilla_net_Tickler_h