Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_net_Tickler_h |
michael@0 | 7 | #define mozilla_net_Tickler_h |
michael@0 | 8 | |
michael@0 | 9 | // The tickler sends a regular 0 byte UDP heartbeat out to a |
michael@0 | 10 | // particular address for a short time after it has been touched. This |
michael@0 | 11 | // is used on some mobile wifi chipsets to mitigate Power Save Polling |
michael@0 | 12 | // (PSP) Mode when we are anticipating a response packet |
michael@0 | 13 | // soon. Typically PSP adds 100ms of latency to a read event because |
michael@0 | 14 | // the packet delivery is not triggered until the 802.11 beacon is |
michael@0 | 15 | // delivered to the host (100ms is the standard Access Point |
michael@0 | 16 | // configuration for the beacon interval.) Requesting a frequent |
michael@0 | 17 | // transmission and getting a CTS frame from the AP at least that |
michael@0 | 18 | // frequently allows for low latency receives when we have reason to |
michael@0 | 19 | // expect them (e.g a SYN-ACK). |
michael@0 | 20 | // |
michael@0 | 21 | // The tickler is used to allow RTT based phases of web transport to |
michael@0 | 22 | // complete quickly when on wifi - ARP, DNS, TCP handshake, SSL |
michael@0 | 23 | // handshake, HTTP headers, and the TCP slow start phase. The |
michael@0 | 24 | // transaction is given up to 400 miliseconds by default to get |
michael@0 | 25 | // through those phases before the tickler is disabled. |
michael@0 | 26 | // |
michael@0 | 27 | // The tickler only applies to wifi on mobile right now. Hopefully it |
michael@0 | 28 | // can also be restricted to particular handset models in the future. |
michael@0 | 29 | |
michael@0 | 30 | #if defined(ANDROID) && !defined(MOZ_B2G) |
michael@0 | 31 | #define MOZ_USE_WIFI_TICKLER |
michael@0 | 32 | #endif |
michael@0 | 33 | |
michael@0 | 34 | #include "mozilla/Attributes.h" |
michael@0 | 35 | #include "nsISupports.h" |
michael@0 | 36 | #include <stdint.h> |
michael@0 | 37 | |
michael@0 | 38 | #ifdef MOZ_USE_WIFI_TICKLER |
michael@0 | 39 | #include "mozilla/Mutex.h" |
michael@0 | 40 | #include "mozilla/TimeStamp.h" |
michael@0 | 41 | #include "nsAutoPtr.h" |
michael@0 | 42 | #include "nsISupports.h" |
michael@0 | 43 | #include "nsIThread.h" |
michael@0 | 44 | #include "nsITimer.h" |
michael@0 | 45 | #include "nsWeakReference.h" |
michael@0 | 46 | #include "prio.h" |
michael@0 | 47 | |
michael@0 | 48 | class nsIPrefBranch; |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | namespace mozilla { |
michael@0 | 52 | namespace net { |
michael@0 | 53 | |
michael@0 | 54 | #ifdef MOZ_USE_WIFI_TICKLER |
michael@0 | 55 | |
michael@0 | 56 | // 8f769ed6-207c-4af9-9f7e-9e832da3754e |
michael@0 | 57 | #define NS_TICKLER_IID \ |
michael@0 | 58 | { 0x8f769ed6, 0x207c, 0x4af9, \ |
michael@0 | 59 | { 0x9f, 0x7e, 0x9e, 0x83, 0x2d, 0xa3, 0x75, 0x4e } } |
michael@0 | 60 | |
michael@0 | 61 | class Tickler MOZ_FINAL : public nsSupportsWeakReference |
michael@0 | 62 | { |
michael@0 | 63 | public: |
michael@0 | 64 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 65 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_TICKLER_IID) |
michael@0 | 66 | |
michael@0 | 67 | // These methods are main thread only |
michael@0 | 68 | Tickler(); |
michael@0 | 69 | ~Tickler(); |
michael@0 | 70 | void Cancel(); |
michael@0 | 71 | nsresult Init(); |
michael@0 | 72 | void SetIPV4Address(uint32_t address); |
michael@0 | 73 | void SetIPV4Port(uint16_t port); |
michael@0 | 74 | |
michael@0 | 75 | // Tickle the tickler to (re-)start the activity. |
michael@0 | 76 | // May call from any thread |
michael@0 | 77 | void Tickle(); |
michael@0 | 78 | |
michael@0 | 79 | private: |
michael@0 | 80 | friend class TicklerTimer; |
michael@0 | 81 | Mutex mLock; |
michael@0 | 82 | nsCOMPtr<nsIThread> mThread; |
michael@0 | 83 | nsCOMPtr<nsITimer> mTimer; |
michael@0 | 84 | nsCOMPtr<nsIPrefBranch> mPrefs; |
michael@0 | 85 | |
michael@0 | 86 | bool mActive; |
michael@0 | 87 | bool mCanceled; |
michael@0 | 88 | bool mEnabled; |
michael@0 | 89 | uint32_t mDelay; |
michael@0 | 90 | TimeDuration mDuration; |
michael@0 | 91 | PRFileDesc* mFD; |
michael@0 | 92 | |
michael@0 | 93 | TimeStamp mLastTickle; |
michael@0 | 94 | PRNetAddr mAddr; |
michael@0 | 95 | |
michael@0 | 96 | // These functions may be called from any thread |
michael@0 | 97 | void PostCheckTickler(); |
michael@0 | 98 | void MaybeStartTickler(); |
michael@0 | 99 | void MaybeStartTicklerUnlocked(); |
michael@0 | 100 | |
michael@0 | 101 | // Tickler thread only |
michael@0 | 102 | void CheckTickler(); |
michael@0 | 103 | void StartTickler(); |
michael@0 | 104 | void StopTickler(); |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | NS_DEFINE_STATIC_IID_ACCESSOR(Tickler, NS_TICKLER_IID) |
michael@0 | 108 | |
michael@0 | 109 | #else // not defined MOZ_USE_WIFI_TICKLER |
michael@0 | 110 | |
michael@0 | 111 | class Tickler MOZ_FINAL : public nsISupports |
michael@0 | 112 | { |
michael@0 | 113 | public: |
michael@0 | 114 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 115 | |
michael@0 | 116 | Tickler() { } |
michael@0 | 117 | ~Tickler() { } |
michael@0 | 118 | nsresult Init() { return NS_ERROR_NOT_IMPLEMENTED; } |
michael@0 | 119 | void Cancel() { } |
michael@0 | 120 | void SetIPV4Address(uint32_t) { }; |
michael@0 | 121 | void SetIPV4Port(uint16_t) { } |
michael@0 | 122 | void Tickle() { } |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | #endif // defined MOZ_USE_WIFI_TICKLER |
michael@0 | 126 | |
michael@0 | 127 | } // namespace mozilla::net |
michael@0 | 128 | } // namespace mozilla |
michael@0 | 129 | |
michael@0 | 130 | #endif // mozilla_net_Tickler_h |