| 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_NET_NETWORK_STATS_H_ | 5 #ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| 6 #define CHROME_BROWSER_NET_NETWORK_STATS_H_ | 6 #define CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "chrome/browser/io_thread.h" | 16 #include "chrome/browser/io_thread.h" |
| 17 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
| 18 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
| 19 #include "net/base/host_port_pair.h" | 19 #include "net/base/host_port_pair.h" |
| 20 #include "net/base/host_resolver.h" | 20 #include "net/base/host_resolver.h" |
| 21 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
| 22 #include "net/base/ip_endpoint.h" | 22 #include "net/base/ip_endpoint.h" |
| 23 #include "net/base/test_data_stream.h" | 23 #include "net/base/test_data_stream.h" |
| 24 #include "net/proxy/proxy_info.h" |
| 24 #include "net/socket/socket.h" | 25 #include "net/socket/socket.h" |
| 25 namespace chrome_browser_net { | 26 namespace chrome_browser_net { |
| 26 | 27 |
| 27 // This class is used for live experiment of network connectivity (either TCP or | 28 // This class is used for live experiment of network connectivity (either TCP or |
| 28 // UDP) metrics. A small percentage of users participate in this experiment. All | 29 // UDP) metrics. A small percentage of users participate in this experiment. All |
| 29 // users (who are in the experiment) must have enabled "UMA upload". | 30 // users (who are in the experiment) must have enabled "UMA upload". |
| 30 // | 31 // |
| 31 // This class collects the following stats from users who have opted in. | 32 // This class collects the following stats from users who have opted in. |
| 32 // a) What percentage of users can get a message end-to-end to a UDP server? | 33 // a) What percentage of users can get a message end-to-end to a UDP server? |
| 33 // b) What percentage of users can get a message end-to-end to a TCP server? | 34 // b) What percentage of users can get a message end-to-end to a TCP server? |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 // Starts the client, connecting to |server|. | 95 // Starts the client, connecting to |server|. |
| 95 // Client will send |bytes_to_send| bytes to |server|. | 96 // Client will send |bytes_to_send| bytes to |server|. |
| 96 // When client has received all echoed bytes from the server, or | 97 // When client has received all echoed bytes from the server, or |
| 97 // when an error occurs causing the client to stop, |Finish| will be | 98 // when an error occurs causing the client to stop, |Finish| will be |
| 98 // called with a net status code. | 99 // called with a net status code. |
| 99 // |Finish| will collect histogram stats. | 100 // |Finish| will collect histogram stats. |
| 100 // Returns true if successful in starting the client. | 101 // Returns true if successful in starting the client. |
| 101 bool Start(net::HostResolver* host_resolver, | 102 bool Start(net::HostResolver* host_resolver, |
| 102 const net::HostPortPair& server, | 103 const net::HostPortPair& server, |
| 103 HistogramPortSelector histogram_port, | 104 HistogramPortSelector histogram_port, |
| 105 bool has_proxy_server, |
| 104 uint32 bytes_to_send, | 106 uint32 bytes_to_send, |
| 105 uint32 packets_to_send, | 107 uint32 packets_to_send, |
| 106 const net::CompletionCallback& callback); | 108 const net::CompletionCallback& callback); |
| 107 | 109 |
| 108 protected: | 110 protected: |
| 109 // Constructs an NetworkStats object that collects metrics for network | 111 // Constructs an NetworkStats object that collects metrics for network |
| 110 // connectivity (either TCP or UDP). | 112 // connectivity (either TCP or UDP). |
| 111 NetworkStats(); | 113 NetworkStats(); |
| 114 // NetworkStats is deleted when Finish() is called. |
| 112 virtual ~NetworkStats(); | 115 virtual ~NetworkStats(); |
| 113 | 116 |
| 114 // Initializes |finished_callback_| and the number of bytes to send to the | 117 // Initializes |finished_callback_| and the number of bytes to send to the |
| 115 // server. |finished_callback| is called when we are done with the test. | 118 // server. |finished_callback| is called when we are done with the test. |
| 116 // |finished_callback| is mainly useful for unittests. | 119 // |finished_callback| is mainly useful for unittests. |
| 117 void Initialize(uint32 bytes_to_send, | 120 void Initialize(uint32 bytes_to_send, |
| 118 HistogramPortSelector histogram_port, | 121 HistogramPortSelector histogram_port, |
| 122 bool has_proxy_server, |
| 119 uint32 packets_to_send, | 123 uint32 packets_to_send, |
| 120 const net::CompletionCallback& finished_callback); | 124 const net::CompletionCallback& finished_callback); |
| 121 | 125 |
| 122 // Called after host is resolved. UDPStatsClient and TCPStatsClient implement | 126 // Called after host is resolved. UDPStatsClient and TCPStatsClient implement |
| 123 // this method. They create the socket and connect to the server. | 127 // this method. They create the socket and connect to the server. |
| 124 virtual bool DoConnect(int result) = 0; | 128 virtual bool DoConnect(int result) = 0; |
| 125 | 129 |
| 126 // This method is called after socket connection is completed. It will start | 130 // This method is called after socket connection is completed. It will start |
| 127 // the process of sending packets to |server| by calling SendPacket(). Returns | 131 // the process of sending packets to |server| by calling SendPacket(). Returns |
| 128 // false if connection is not established (result is less than 0). | 132 // false if connection is not established (result is less than 0). |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 std::string encoded_message_; | 268 std::string encoded_message_; |
| 265 | 269 |
| 266 // |stream_| is used to generate data to be sent to the server and it is also | 270 // |stream_| is used to generate data to be sent to the server and it is also |
| 267 // used to verify the data received from the server. | 271 // used to verify the data received from the server. |
| 268 net::TestDataStream stream_; | 272 net::TestDataStream stream_; |
| 269 | 273 |
| 270 // |histogram_port_| specifies the port for which we are testing the network | 274 // |histogram_port_| specifies the port for which we are testing the network |
| 271 // connectivity. | 275 // connectivity. |
| 272 HistogramPortSelector histogram_port_; | 276 HistogramPortSelector histogram_port_; |
| 273 | 277 |
| 278 // |has_proxy_server_| specifies if there is a proxy server or not. |
| 279 bool has_proxy_server_; |
| 280 |
| 274 // HostResolver fills out the |addresses_| after host resolution is completed. | 281 // HostResolver fills out the |addresses_| after host resolution is completed. |
| 275 net::AddressList addresses_; | 282 net::AddressList addresses_; |
| 276 | 283 |
| 277 // Callback to call when echo protocol is successefully finished or whenever | 284 // Callback to call when echo protocol is successefully finished or whenever |
| 278 // there is an error (this allows unittests to wait until echo protocol's | 285 // there is an error (this allows unittests to wait until echo protocol's |
| 279 // round trip is finished). | 286 // round trip is finished). |
| 280 net::CompletionCallback finished_callback_; | 287 net::CompletionCallback finished_callback_; |
| 281 | 288 |
| 282 // The time when the session was started. | 289 // The time when the session was started. |
| 283 base::TimeTicks start_time_; | 290 base::TimeTicks start_time_; |
| 284 | 291 |
| 285 // Data to track number of packets to send to the server and the packets we | 292 // Data to track number of packets to send to the server and the packets we |
| 286 // have received from the server. | 293 // have received from the server. |
| 287 uint32 packets_to_send_; | 294 uint32 packets_to_send_; |
| 288 uint32 packets_sent_; | 295 uint32 packets_sent_; |
| 289 uint32 base_packet_number_; | 296 uint32 base_packet_number_; |
| 290 uint32 packets_received_mask_; | 297 uint32 packets_received_mask_; |
| 291 | 298 |
| 292 // We use this factory to create timeout tasks for socket's ReadData. | 299 // We use this factory to create timeout tasks for socket's ReadData. |
| 293 base::WeakPtrFactory<NetworkStats> weak_factory_; | 300 base::WeakPtrFactory<NetworkStats> weak_factory_; |
| 294 }; | 301 }; |
| 295 | 302 |
| 296 class UDPStatsClient : public NetworkStats { | 303 class UDPStatsClient : public NetworkStats { |
| 297 public: | 304 public: |
| 298 // Constructs an UDPStatsClient object that collects metrics for UDP | 305 // Constructs an UDPStatsClient object that collects metrics for UDP |
| 299 // connectivity. | 306 // connectivity. |
| 300 UDPStatsClient(); | 307 UDPStatsClient(); |
| 308 // UDPStatsClient is deleted when Finish() is called. |
| 301 virtual ~UDPStatsClient(); | 309 virtual ~UDPStatsClient(); |
| 302 | 310 |
| 303 protected: | 311 protected: |
| 304 // Allow tests to access our innards for testing purposes. | 312 // Allow tests to access our innards for testing purposes. |
| 305 friend class NetworkStatsTestUDP; | 313 friend class NetworkStatsTestUDP; |
| 306 | 314 |
| 307 // Called after host is resolved. Creates UDClientSocket and connects to the | 315 // Called after host is resolved. Creates UDClientSocket and connects to the |
| 308 // server. If successfully connected, then calls ConnectComplete() to start | 316 // server. If successfully connected, then calls ConnectComplete() to start |
| 309 // the echo protocol. Returns |false| if there is any error. | 317 // the echo protocol. Returns |false| if there is any error. |
| 310 virtual bool DoConnect(int result) OVERRIDE; | 318 virtual bool DoConnect(int result) OVERRIDE; |
| 311 | 319 |
| 312 // This method calls NetworkStats::ReadComplete() to verify the data and calls | 320 // This method calls NetworkStats::ReadComplete() to verify the data and calls |
| 313 // Finish() if there is an error or if read callback didn't return any data | 321 // Finish() if there is an error or if read callback didn't return any data |
| 314 // (|result| is less than or equal to 0). | 322 // (|result| is less than or equal to 0). |
| 315 virtual bool ReadComplete(int result) OVERRIDE; | 323 virtual bool ReadComplete(int result) OVERRIDE; |
| 316 | 324 |
| 317 // Collects stats for UDP connectivity. This is called when all the data from | 325 // Collects stats for UDP connectivity. This is called when all the data from |
| 318 // server is read or when there is a failure during connect/read/write. | 326 // server is read or when there is a failure during connect/read/write. This |
| 327 // object is deleted at the end of this method. |
| 319 virtual void Finish(Status status, int result) OVERRIDE; | 328 virtual void Finish(Status status, int result) OVERRIDE; |
| 320 }; | 329 }; |
| 321 | 330 |
| 322 class TCPStatsClient : public NetworkStats { | 331 class TCPStatsClient : public NetworkStats { |
| 323 public: | 332 public: |
| 324 // Constructs a TCPStatsClient object that collects metrics for TCP | 333 // Constructs a TCPStatsClient object that collects metrics for TCP |
| 325 // connectivity. | 334 // connectivity. |
| 326 TCPStatsClient(); | 335 TCPStatsClient(); |
| 336 // TCPStatsClient is deleted when Finish() is called. |
| 327 virtual ~TCPStatsClient(); | 337 virtual ~TCPStatsClient(); |
| 328 | 338 |
| 329 protected: | 339 protected: |
| 330 // Allow tests to access our innards for testing purposes. | 340 // Allow tests to access our innards for testing purposes. |
| 331 friend class NetworkStatsTestTCP; | 341 friend class NetworkStatsTestTCP; |
| 332 | 342 |
| 333 // Called after host is resolved. Creates TCPClientSocket and connects to the | 343 // Called after host is resolved. Creates TCPClientSocket and connects to the |
| 334 // server. | 344 // server. |
| 335 virtual bool DoConnect(int result) OVERRIDE; | 345 virtual bool DoConnect(int result) OVERRIDE; |
| 336 | 346 |
| 337 // This method calls NetworkStats::ReadComplete() to verify the data and calls | 347 // This method calls NetworkStats::ReadComplete() to verify the data and calls |
| 338 // Finish() if there is an error (|result| is less than 0). | 348 // Finish() if there is an error (|result| is less than 0). |
| 339 virtual bool ReadComplete(int result) OVERRIDE; | 349 virtual bool ReadComplete(int result) OVERRIDE; |
| 340 | 350 |
| 341 // Collects stats for TCP connectivity. This is called when all the data from | 351 // Collects stats for TCP connectivity. This is called when all the data from |
| 342 // server is read or when there is a failure during connect/read/write. | 352 // server is read or when there is a failure during connect/read/write. This |
| 353 // object is deleted at the end of this method. |
| 343 virtual void Finish(Status status, int result) OVERRIDE; | 354 virtual void Finish(Status status, int result) OVERRIDE; |
| 344 | 355 |
| 345 private: | 356 private: |
| 346 // Callback that is called when connect is completed and calls | 357 // Callback that is called when connect is completed and calls |
| 347 // ConnectComplete() to start the echo protocol. | 358 // ConnectComplete() to start the echo protocol. |
| 348 void OnConnectComplete(int result); | 359 void OnConnectComplete(int result); |
| 349 }; | 360 }; |
| 350 | 361 |
| 362 class ProxyDetector { |
| 363 public: |
| 364 // Used for the callback that is called from |OnResolveProxyComplete|. |
| 365 typedef base::Callback<void(bool)> OnResolvedCallback; |
| 366 |
| 367 // Constructs a ProxyDetector object that finds out if access to |
| 368 // |server_address| goes through a proxy server or not. Calls the |callback| |
| 369 // after proxy resolution is completed by currying the proxy resolution |
| 370 // status. |
| 371 ProxyDetector(net::ProxyService* proxy_service, |
| 372 const net::HostPortPair& server_address, |
| 373 OnResolvedCallback callback); |
| 374 |
| 375 // This method uses |proxy_service_| to resolve the proxy for |
| 376 // |server_address_|. |
| 377 void StartResolveProxy(); |
| 378 |
| 379 private: |
| 380 // This object is deleted from |OnResolveProxyComplete|. |
| 381 ~ProxyDetector(); |
| 382 |
| 383 // Calls the |callback_| by currying the proxy resolution status. |
| 384 void OnResolveProxyComplete(int result); |
| 385 |
| 386 // |proxy_service_| specifies the proxy service that is to be used to find |
| 387 // if access to |server_address_| goes through proxy server or not. |
| 388 net::ProxyService* proxy_service_; |
| 389 |
| 390 // |server_address_| specifies the server host and port pair for which we are |
| 391 // trying to see if access to it, goes through proxy or not. |
| 392 net::HostPortPair server_address_; |
| 393 |
| 394 // |callback_| will be called after proxy resolution is completed. |
| 395 OnResolvedCallback callback_; |
| 396 |
| 397 // |proxy_info_| holds proxy information returned by ResolveProxy. |
| 398 net::ProxyInfo proxy_info_; |
| 399 |
| 400 // Indicates if there is a pending a proxy resolution. We use this to assert |
| 401 // that there is no in-progress proxy resolution request. |
| 402 bool has_pending_proxy_resolution_; |
| 403 }; |
| 404 |
| 351 // This collects the network connectivity stats for UDP and TCP for small | 405 // This collects the network connectivity stats for UDP and TCP for small |
| 352 // percentage of users who are participating in the experiment. All users must | 406 // percentage of users who are participating in the experiment. All users must |
| 353 // have enabled "UMA upload". This method gets called only if UMA upload to the | 407 // have enabled "UMA upload". This method gets called only if UMA upload to the |
| 354 // server has succeeded. | 408 // server has succeeded. |
| 355 void CollectNetworkStats(const std::string& network_stats_server_url, | 409 void CollectNetworkStats(const std::string& network_stats_server_url, |
| 356 IOThread* io_thread); | 410 IOThread* io_thread); |
| 357 | 411 |
| 412 // This starts a test randomly selected among "TCP test with small packet size", |
| 413 // "TCP test with large packet size", "UDP test with small packet size", "UDP |
| 414 // test with large packet size" and "UDP multi packet loss" tests to collect the |
| 415 // network connectivity stats. |
| 416 void StartNetworkStatsTest(net::HostResolver* host_resolver, |
| 417 const net::HostPortPair& server_address, |
| 418 NetworkStats::HistogramPortSelector histogram_port, |
| 419 bool has_proxy_server); |
| 420 |
| 358 } // namespace chrome_browser_net | 421 } // namespace chrome_browser_net |
| 359 | 422 |
| 360 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | 423 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| OLD | NEW |