Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / chromecast / net / connectivity_checker_impl.h
blobda15039de75b52a13c7866e2c5f1ed1880ff2811
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"
13 class GURL;
15 namespace base {
16 class SingleThreadTaskRunner;
19 namespace net {
20 class SSLInfo;
21 class URLRequest;
22 class URLRequestContext;
25 namespace chromecast {
27 // Simple class to check network connectivity by sending a HEAD http request
28 // to given url.
29 class ConnectivityCheckerImpl
30 : public ConnectivityChecker,
31 public net::URLRequest::Delegate,
32 public net::NetworkChangeNotifier::NetworkChangeObserver {
33 public:
34 explicit ConnectivityCheckerImpl(
35 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
37 // ConnectivityChecker implementation:
38 bool Connected() const override;
39 void Check() override;
41 protected:
42 ~ConnectivityCheckerImpl() override;
44 private:
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,
50 bool fatal) override;
52 // Initializes ConnectivityChecker
53 void Initialize();
55 // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
56 void OnNetworkChanged(
57 net::NetworkChangeNotifier::ConnectionType type) override;
59 // Cancels current connectivity checking in progress.
60 void Cancel();
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_;
75 bool connected_;
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_