| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_ | 5 #ifndef CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_ |
| 6 #define CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_ | 6 #define CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_ |
| 7 | 7 |
| 8 #include "base/cancelable_callback.h" | 8 #include "base/cancelable_callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "chromecast/net/connectivity_checker.h" | 10 #include "chromecast/net/connectivity_checker.h" |
| 11 #include "net/base/network_change_notifier.h" | 11 #include "net/base/network_change_notifier.h" |
| 12 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
| 13 | 13 |
| 14 class GURL; | 14 class GURL; |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class SingleThreadTaskRunner; | 17 class SingleThreadTaskRunner; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 class SSLInfo; | 21 class SSLInfo; |
| 22 class URLRequest; | 22 class URLRequest; |
| 23 class URLRequestContext; | 23 class URLRequestContext; |
| 24 class URLRequestContextGetter; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace chromecast { | 27 namespace chromecast { |
| 27 | 28 |
| 28 // Simple class to check network connectivity by sending a HEAD http request | 29 // Simple class to check network connectivity by sending a HEAD http request |
| 29 // to given url. | 30 // to given url. |
| 30 class ConnectivityCheckerImpl | 31 class ConnectivityCheckerImpl |
| 31 : public ConnectivityChecker, | 32 : public ConnectivityChecker, |
| 32 public net::URLRequest::Delegate, | 33 public net::URLRequest::Delegate, |
| 33 public net::NetworkChangeNotifier::NetworkChangeObserver { | 34 public net::NetworkChangeNotifier::NetworkChangeObserver { |
| 34 public: | 35 public: |
| 35 // Connectivity checking and initialization will run on task_runner. | 36 // Connectivity checking and initialization will run on task_runner. |
| 36 explicit ConnectivityCheckerImpl( | 37 explicit ConnectivityCheckerImpl( |
| 37 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 39 net::URLRequestContextGetter* url_request_context_getter); |
| 38 | 40 |
| 39 // ConnectivityChecker implementation: | 41 // ConnectivityChecker implementation: |
| 40 bool Connected() const override; | 42 bool Connected() const override; |
| 41 void Check() override; | 43 void Check() override; |
| 42 | 44 |
| 43 protected: | 45 protected: |
| 44 ~ConnectivityCheckerImpl() override; | 46 ~ConnectivityCheckerImpl() override; |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 // UrlRequest::Delegate implementation: | 49 // UrlRequest::Delegate implementation: |
| 48 void OnResponseStarted(net::URLRequest* request) override; | 50 void OnResponseStarted(net::URLRequest* request) override; |
| 49 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | 51 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| 50 void OnSSLCertificateError(net::URLRequest* request, | 52 void OnSSLCertificateError(net::URLRequest* request, |
| 51 const net::SSLInfo& ssl_info, | 53 const net::SSLInfo& ssl_info, |
| 52 bool fatal) override; | 54 bool fatal) override; |
| 53 | 55 |
| 54 // Initializes ConnectivityChecker | 56 // Initializes ConnectivityChecker |
| 55 void Initialize(); | 57 void Initialize(net::URLRequestContextGetter* url_request_context_getter); |
| 56 | 58 |
| 57 // net::NetworkChangeNotifier::NetworkChangeObserver implementation: | 59 // net::NetworkChangeNotifier::NetworkChangeObserver implementation: |
| 58 void OnNetworkChanged( | 60 void OnNetworkChanged( |
| 59 net::NetworkChangeNotifier::ConnectionType type) override; | 61 net::NetworkChangeNotifier::ConnectionType type) override; |
| 60 | 62 |
| 61 void OnNetworkChangedInternal(); | 63 void OnNetworkChangedInternal(); |
| 62 | 64 |
| 63 // Cancels current connectivity checking in progress. | 65 // Cancels current connectivity checking in progress. |
| 64 void Cancel(); | 66 void Cancel(); |
| 65 | 67 |
| 66 // Sets connectivity and alerts observers if it has changed | 68 // Sets connectivity and alerts observers if it has changed |
| 67 void SetConnected(bool connected); | 69 void SetConnected(bool connected); |
| 68 | 70 |
| 69 enum class ErrorType { | 71 enum class ErrorType { |
| 70 BAD_HTTP_STATUS = 1, | 72 BAD_HTTP_STATUS = 1, |
| 71 SSL_CERTIFICATE_ERROR = 2, | 73 SSL_CERTIFICATE_ERROR = 2, |
| 72 REQUEST_TIMEOUT = 3, | 74 REQUEST_TIMEOUT = 3, |
| 73 }; | 75 }; |
| 74 | 76 |
| 75 // Called when URL request failed. | 77 // Called when URL request failed. |
| 76 void OnUrlRequestError(ErrorType type); | 78 void OnUrlRequestError(ErrorType type); |
| 77 | 79 |
| 78 // Called when URL request timed out. | 80 // Called when URL request timed out. |
| 79 void OnUrlRequestTimeout(); | 81 void OnUrlRequestTimeout(); |
| 80 | 82 |
| 81 void CheckInternal(); | 83 void CheckInternal(); |
| 82 | 84 |
| 83 std::unique_ptr<GURL> connectivity_check_url_; | 85 std::unique_ptr<GURL> connectivity_check_url_; |
| 84 std::unique_ptr<net::URLRequestContext> url_request_context_; | 86 net::URLRequestContext* url_request_context_; |
| 85 std::unique_ptr<net::URLRequest> url_request_; | 87 std::unique_ptr<net::URLRequest> url_request_; |
| 86 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 88 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 87 | 89 |
| 88 // connected_lock_ protects access to connected_ which is shared across | 90 // connected_lock_ protects access to connected_ which is shared across |
| 89 // threads. | 91 // threads. |
| 90 mutable base::Lock connected_lock_; | 92 mutable base::Lock connected_lock_; |
| 91 bool connected_; | 93 bool connected_; |
| 92 | 94 |
| 93 net::NetworkChangeNotifier::ConnectionType connection_type_; | 95 net::NetworkChangeNotifier::ConnectionType connection_type_; |
| 94 // Number of connectivity check errors. | 96 // Number of connectivity check errors. |
| 95 unsigned int check_errors_; | 97 unsigned int check_errors_; |
| 96 bool network_changed_pending_; | 98 bool network_changed_pending_; |
| 97 // Timeout handler for connectivity checks. | 99 // Timeout handler for connectivity checks. |
| 98 // Note: Cancelling this timeout can cause the destructor for this class to be | 100 // Note: Cancelling this timeout can cause the destructor for this class to be |
| 99 // called. | 101 // called. |
| 100 base::CancelableCallback<void()> timeout_; | 102 base::CancelableCallback<void()> timeout_; |
| 101 | 103 |
| 102 DISALLOW_COPY_AND_ASSIGN(ConnectivityCheckerImpl); | 104 DISALLOW_COPY_AND_ASSIGN(ConnectivityCheckerImpl); |
| 103 }; | 105 }; |
| 104 | 106 |
| 105 } // namespace chromecast | 107 } // namespace chromecast |
| 106 | 108 |
| 107 #endif // CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_ | 109 #endif // CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_ |
| OLD | NEW |