OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_SERVICE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // Starts a new round of discovery. Returns |true| if discovery was started | 84 // Starts a new round of discovery. Returns |true| if discovery was started |
85 // successfully or there is already one active. Returns |false| on error. | 85 // successfully or there is already one active. Returns |false| on error. |
86 virtual bool Discover() = 0; | 86 virtual bool Discover() = 0; |
87 | 87 |
88 // Called by listeners to this service to add/remove themselves as observers. | 88 // Called by listeners to this service to add/remove themselves as observers. |
89 virtual void AddObserver(Observer* observer) = 0; | 89 virtual void AddObserver(Observer* observer) = 0; |
90 virtual void RemoveObserver(Observer* observer) = 0; | 90 virtual void RemoveObserver(Observer* observer) = 0; |
91 virtual bool HasObserver(Observer* observer) = 0; | 91 virtual bool HasObserver(Observer* observer) = 0; |
92 }; | 92 }; |
93 | 93 |
| 94 // Implements DialService. |
| 95 // |
| 96 // NOTE(mfoltz): It would make this class cleaner to refactor most of the state |
| 97 // associated with a single discovery cycle into its own |DiscoveryOperation| |
| 98 // object. This would also simplify lifetime of the object w.r.t. DialRegistry; |
| 99 // the Registry would not need to create/destroy the Service on demand. |
94 class DialServiceImpl : public DialService, | 100 class DialServiceImpl : public DialService, |
95 public base::SupportsWeakPtr<DialServiceImpl> { | 101 public base::SupportsWeakPtr<DialServiceImpl> { |
96 public: | 102 public: |
97 explicit DialServiceImpl(net::NetLog* net_log); | 103 explicit DialServiceImpl(net::NetLog* net_log); |
98 virtual ~DialServiceImpl(); | 104 virtual ~DialServiceImpl(); |
99 | 105 |
100 // DialService implementation | 106 // DialService implementation |
101 virtual bool Discover() OVERRIDE; | 107 virtual bool Discover() OVERRIDE; |
102 virtual void AddObserver(Observer* observer) OVERRIDE; | 108 virtual void AddObserver(Observer* observer) OVERRIDE; |
103 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 109 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
104 virtual bool HasObserver(Observer* observer) OVERRIDE; | 110 virtual bool HasObserver(Observer* observer) OVERRIDE; |
105 | 111 |
106 private: | 112 private: |
107 // Starts the flow to construct and send a discovery request. | 113 // Starts the control flow for one discovery cycle. |
108 void StartRequest(); | 114 void StartDiscovery(); |
109 | 115 |
110 // Establishes the UDP socket that is used for requests and responses, then | 116 // Establishes the UDP socket that is used for requests and responses, |
111 // sends a discovery request on the bound socket. Returns |true| if | 117 // establishes a read callback on the socket, and sends the first discovery |
112 // successful. | 118 // request. Returns true if successful. |
113 bool BindAndWriteSocket(const net::NetworkInterface& bind_interface); | 119 bool BindSocketAndSendRequest(const net::IPAddressNumber& bind_ip_address); |
| 120 |
| 121 // Sends a single discovery request over the socket. |
| 122 void SendOneRequest(); |
114 | 123 |
115 // Callback invoked for socket writes. | 124 // Callback invoked for socket writes. |
116 void OnSocketWrite(int result); | 125 void OnSocketWrite(int result); |
117 | 126 |
118 // Send the network list to IO thread. | 127 // Send the network list to IO thread. |
119 void SendNetworkList(const net::NetworkInterfaceList& list); | 128 void SendNetworkList(const net::NetworkInterfaceList& list); |
120 | 129 |
121 // Establishes the callback to read from the socket. Returns true if | 130 // Establishes the callback to read from the socket. Returns true if |
122 // successful. | 131 // successful. |
123 bool ReadSocket(); | 132 bool ReadSocket(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 183 |
175 // Marks whether there is an active read callback. | 184 // Marks whether there is an active read callback. |
176 bool is_reading_; | 185 bool is_reading_; |
177 | 186 |
178 // True when we are currently doing discovery. | 187 // True when we are currently doing discovery. |
179 bool discovery_active_; | 188 bool discovery_active_; |
180 | 189 |
181 // The number of requests that have been sent in the current discovery. | 190 // The number of requests that have been sent in the current discovery. |
182 int num_requests_sent_; | 191 int num_requests_sent_; |
183 | 192 |
| 193 // The maximum number of requests to send per discovery cycle. |
| 194 int max_requests_; |
| 195 |
184 // Timer for finishing discovery. | 196 // Timer for finishing discovery. |
185 base::OneShotTimer<DialServiceImpl> finish_timer_; | 197 base::OneShotTimer<DialServiceImpl> finish_timer_; |
186 | 198 |
187 // The delay for |finish_timer_|; how long to wait for discovery to finish. | 199 // The delay for |finish_timer_|; how long to wait for discovery to finish. |
| 200 // Setting this to zero disables the timer. |
188 base::TimeDelta finish_delay_; | 201 base::TimeDelta finish_delay_; |
189 | 202 |
| 203 // Timer for sending multiple requests at fixed intervals. |
| 204 base::RepeatingTimer<DialServiceImpl> request_timer_; |
| 205 |
| 206 // The delay for |request_timer_|; how long to wait between successive |
| 207 // requests. |
| 208 base::TimeDelta request_interval_; |
| 209 |
190 // List of observers. | 210 // List of observers. |
191 ObserverList<Observer> observer_list_; | 211 ObserverList<Observer> observer_list_; |
192 | 212 |
193 // Thread checker. | 213 // Thread checker. |
194 base::ThreadChecker thread_checker_; | 214 base::ThreadChecker thread_checker_; |
195 | 215 |
| 216 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestSendMultipleRequests); |
196 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestOnDeviceDiscovered); | 217 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestOnDeviceDiscovered); |
197 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestOnDiscoveryFinished); | 218 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestOnDiscoveryFinished); |
198 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestOnDiscoveryRequest); | 219 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestOnDiscoveryRequest); |
199 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestResponseParsing); | 220 FRIEND_TEST_ALL_PREFIXES(DialServiceTest, TestResponseParsing); |
200 DISALLOW_COPY_AND_ASSIGN(DialServiceImpl); | 221 DISALLOW_COPY_AND_ASSIGN(DialServiceImpl); |
201 }; | 222 }; |
202 | 223 |
203 } // namespace extensions | 224 } // namespace extensions |
204 | 225 |
205 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_SERVICE_H_ | 226 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_SERVICE_H_ |
OLD | NEW |