OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 26 matching lines...) Expand all Loading... |
37 namespace blink { | 37 namespace blink { |
38 | 38 |
39 class ExecutionContext; | 39 class ExecutionContext; |
40 | 40 |
41 class CORE_EXPORT NetworkStateNotifier { | 41 class CORE_EXPORT NetworkStateNotifier { |
42 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED(NetworkS
tateNotifier); | 42 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED(NetworkS
tateNotifier); |
43 public: | 43 public: |
44 class NetworkStateObserver { | 44 class NetworkStateObserver { |
45 public: | 45 public: |
46 // Will be called on the thread of the context passed in addObserver. | 46 // Will be called on the thread of the context passed in addObserver. |
47 virtual void connectionTypeChange(WebConnectionType) = 0; | 47 virtual void connectionChange(WebConnectionType, double maxBandwidthMbps
) = 0; |
48 }; | 48 }; |
49 | 49 |
50 NetworkStateNotifier() | 50 NetworkStateNotifier() |
51 : m_isOnLine(true) | 51 : m_isOnLine(true) |
52 , m_type(ConnectionTypeOther) | 52 , m_type(ConnectionTypeOther) |
| 53 , m_maxBandwidthMbps(std::numeric_limits<double>::infinity()) |
53 , m_testUpdatesOnly(false) | 54 , m_testUpdatesOnly(false) |
54 { | 55 { |
55 } | 56 } |
56 | 57 |
| 58 // Can be called on any thread. |
57 bool onLine() const | 59 bool onLine() const |
58 { | 60 { |
59 MutexLocker locker(m_mutex); | 61 MutexLocker locker(m_mutex); |
60 return m_isOnLine; | 62 return m_isOnLine; |
61 } | 63 } |
62 | 64 |
63 void setOnLine(bool); | 65 void setOnLine(bool); |
64 | 66 |
| 67 // Can be called on any thread. |
65 WebConnectionType connectionType() const | 68 WebConnectionType connectionType() const |
66 { | 69 { |
67 MutexLocker locker(m_mutex); | 70 MutexLocker locker(m_mutex); |
68 return m_type; | 71 return m_type; |
69 } | 72 } |
70 | 73 |
71 void setWebConnectionType(WebConnectionType); | 74 // Can be called on any thread. |
| 75 double maxBandwidth() const |
| 76 { |
| 77 MutexLocker locker(m_mutex); |
| 78 return m_maxBandwidthMbps; |
| 79 } |
| 80 |
| 81 void setWebConnection(WebConnectionType, double maxBandwidthMbps); |
72 | 82 |
73 // Must be called on the context's thread. An added observer must be removed | 83 // Must be called on the context's thread. An added observer must be removed |
74 // before its ExecutionContext is deleted. It's possible for an observer to | 84 // before its ExecutionContext is deleted. It's possible for an observer to |
75 // be called twice for the same event if it is first removed and then added | 85 // be called twice for the same event if it is first removed and then added |
76 // during notification. | 86 // during notification. |
77 void addObserver(NetworkStateObserver*, ExecutionContext*); | 87 void addObserver(NetworkStateObserver*, ExecutionContext*); |
78 void removeObserver(NetworkStateObserver*, ExecutionContext*); | 88 void removeObserver(NetworkStateObserver*, ExecutionContext*); |
79 | 89 |
80 // The following functions are for testing purposes. | 90 // The following functions are for testing purposes. |
81 | 91 |
82 // When true, setWebConnectionType calls are ignored and only setWebConnecti
onTypeForTest | 92 // When true, setWebConnectionType calls are ignored and only setWebConnecti
onTypeForTest |
83 // can update the connection type. This is used for layout tests (see crbug.
com/377736). | 93 // can update the connection type. This is used for layout tests (see crbug.
com/377736). |
84 void setTestUpdatesOnly(bool); | 94 void setTestUpdatesOnly(bool); |
85 // Tests should call this as it will change the type regardless of the value
of m_testUpdatesOnly. | 95 // Tests should call this as it will change the type regardless of the value
of m_testUpdatesOnly. |
86 void setWebConnectionTypeForTest(WebConnectionType); | 96 void setWebConnectionForTest(WebConnectionType, double maxBandwidthMbps); |
87 | 97 |
88 private: | 98 private: |
89 struct ObserverList { | 99 struct ObserverList { |
90 ObserverList() | 100 ObserverList() |
91 : iterating(false) | 101 : iterating(false) |
92 { | 102 { |
93 } | 103 } |
94 bool iterating; | 104 bool iterating; |
95 Vector<NetworkStateObserver*> observers; | 105 Vector<NetworkStateObserver*> observers; |
96 Vector<size_t> zeroedObservers; // Indices in observers that are 0. | 106 Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
97 }; | 107 }; |
98 | 108 |
99 void setWebConnectionTypeImpl(WebConnectionType); | 109 void setWebConnectionImpl(WebConnectionType, double maxBandwidthMbps); |
| 110 void setMaxBandwidthImpl(double maxBandwidthMbps); |
100 | 111 |
101 using ObserverListMap = HashMap<ExecutionContext*, OwnPtr<ObserverList>>; | 112 using ObserverListMap = HashMap<ExecutionContext*, OwnPtr<ObserverList>>; |
102 | 113 |
103 void notifyObserversOnContext(WebConnectionType, ExecutionContext*); | 114 void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double ma
xBandwidthMbps, ExecutionContext*); |
104 | 115 |
105 ObserverList* lockAndFindObserverList(ExecutionContext*); | 116 ObserverList* lockAndFindObserverList(ExecutionContext*); |
106 | 117 |
107 // Removed observers are nulled out in the list in case the list is being | 118 // Removed observers are nulled out in the list in case the list is being |
108 // iterated over. Once done iterating, call this to clean up nulled | 119 // iterated over. Once done iterating, call this to clean up nulled |
109 // observers. | 120 // observers. |
110 void collectZeroedObservers(ObserverList*, ExecutionContext*); | 121 void collectZeroedObservers(ObserverList*, ExecutionContext*); |
111 | 122 |
112 mutable Mutex m_mutex; | 123 mutable Mutex m_mutex; |
113 bool m_isOnLine; | 124 bool m_isOnLine; |
114 WebConnectionType m_type; | 125 WebConnectionType m_type; |
| 126 double m_maxBandwidthMbps; |
115 ObserverListMap m_observers; | 127 ObserverListMap m_observers; |
116 bool m_testUpdatesOnly; | 128 bool m_testUpdatesOnly; |
117 }; | 129 }; |
118 | 130 |
119 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); | 131 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); |
120 | 132 |
121 } // namespace blink | 133 } // namespace blink |
122 | 134 |
123 #endif // NetworkStateNotifier_h | 135 #endif // NetworkStateNotifier_h |
OLD | NEW |