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
3 // found in the LICENSE file.
5 #ifndef CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_
6 #define CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_
8 #include "base/cancelable_callback.h"
9 #include "chromecast/net/connectivity_checker.h"
10 #include "net/base/network_change_notifier.h"
11 #include "net/url_request/url_request.h"
16 class SingleThreadTaskRunner
;
22 class URLRequestContext
;
25 namespace chromecast
{
27 // Simple class to check network connectivity by sending a HEAD http request
29 class ConnectivityCheckerImpl
30 : public ConnectivityChecker
,
31 public net::URLRequest::Delegate
,
32 public net::NetworkChangeNotifier::NetworkChangeObserver
{
34 explicit ConnectivityCheckerImpl(
35 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
);
37 // ConnectivityChecker implementation:
38 bool Connected() const override
;
39 void Check() override
;
42 ~ConnectivityCheckerImpl() override
;
45 // UrlRequest::Delegate implementation:
46 void OnResponseStarted(net::URLRequest
* request
) override
;
47 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
48 void OnSSLCertificateError(net::URLRequest
* request
,
49 const net::SSLInfo
& ssl_info
,
52 // Initializes ConnectivityChecker
55 // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
56 void OnNetworkChanged(
57 net::NetworkChangeNotifier::ConnectionType type
) override
;
59 // Cancels current connectivity checking in progress.
62 // Sets connectivity and alerts observers if it has changed
63 void SetConnected(bool connected
);
65 // Called when URL request failed.
66 void OnUrlRequestError();
68 // Called when URL request timed out.
69 void OnUrlRequestTimeout();
71 scoped_ptr
<GURL
> connectivity_check_url_
;
72 scoped_ptr
<net::URLRequestContext
> url_request_context_
;
73 scoped_ptr
<net::URLRequest
> url_request_
;
74 const scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
76 // Number of connectivity check errors.
77 unsigned int check_errors_
;
78 base::CancelableCallback
<void()> timeout_
;
80 DISALLOW_COPY_AND_ASSIGN(ConnectivityCheckerImpl
);
83 } // namespace chromecast
85 #endif // CHROMECAST_NET_CONNECTIVITY_CHECKER_IMPL_H_