blob: da15039de75b52a13c7866e2c5f1ed1880ff2811 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/cancelable_callback.h"
#include "chromecast/net/connectivity_checker.h"
#include "net/base/network_change_notifier.h"
#include "net/url_request/url_request.h"
class GURL;
namespace base {
class SingleThreadTaskRunner;
namespace net {
class SSLInfo;
class URLRequest;
class URLRequestContext;
namespace chromecast {
// Simple class to check network connectivity by sending a HEAD http request
// to given url.
class ConnectivityCheckerImpl
: public ConnectivityChecker,
public net::URLRequest::Delegate,
public net::NetworkChangeNotifier::NetworkChangeObserver {
explicit ConnectivityCheckerImpl(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
// ConnectivityChecker implementation:
bool Connected() const override;
void Check() override;
~ConnectivityCheckerImpl() override;
// UrlRequest::Delegate implementation:
void OnResponseStarted(net::URLRequest* request) override;
void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
void OnSSLCertificateError(net::URLRequest* request,
const net::SSLInfo& ssl_info,
bool fatal) override;
// Initializes ConnectivityChecker
void Initialize();
// net::NetworkChangeNotifier::NetworkChangeObserver implementation:
void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) override;
// Cancels current connectivity checking in progress.
void Cancel();
// Sets connectivity and alerts observers if it has changed
void SetConnected(bool connected);
// Called when URL request failed.
void OnUrlRequestError();
// Called when URL request timed out.
void OnUrlRequestTimeout();
scoped_ptr<GURL> connectivity_check_url_;
scoped_ptr<net::URLRequestContext> url_request_context_;
scoped_ptr<net::URLRequest> url_request_;
const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
bool connected_;
// Number of connectivity check errors.
unsigned int check_errors_;
base::CancelableCallback<void()> timeout_;
} // namespace chromecast