blob: c448665e469a52dee1ac810581eb02a0e04b96c2 [file] [log] [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_
#define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_
#include <netlistmgr.h>
#include <ocidl.h>
#include <windows.h>
#include <wrl.h>
#include <wrl/client.h>
#include <memory>
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/thread_annotations.h"
#include "base/timer/timer.h"
#include "base/win/object_watcher.h"
#include "net/base/net_export.h"
#include "net/base/network_change_notifier.h"
namespace base {
class SequencedTaskRunner;
} // namespace base
namespace net {
class NetworkCostManagerEventSink;
// NetworkChangeNotifierWin uses a SequenceChecker, as all its internal
// notification code must be called on the sequence it is created and destroyed
// on. All the NetworkChangeNotifier methods it implements are threadsafe.
class NET_EXPORT_PRIVATE NetworkChangeNotifierWin
: public NetworkChangeNotifier,
public base::win::ObjectWatcher::Delegate {
public:
NetworkChangeNotifierWin();
NetworkChangeNotifierWin(const NetworkChangeNotifierWin&) = delete;
NetworkChangeNotifierWin& operator=(const NetworkChangeNotifierWin&) = delete;
~NetworkChangeNotifierWin() override;
// Begins listening for a single subsequent address change. If it fails to
// start watching, it retries on a timer. Must be called only once, on the
// sequence |this| was created on. This cannot be called in the constructor,
// as WatchForAddressChangeInternal is mocked out in unit tests.
// TODO(mmenke): Consider making this function a part of the
// NetworkChangeNotifier interface, so other subclasses can be
// unit tested in similar fashion, as needed.
void WatchForAddressChange();
protected:
// For unit tests only.
bool is_watching() const { return is_watching_; }
void set_is_watching(bool is_watching) { is_watching_ = is_watching; }
int sequential_failures() const { return sequential_failures_; }
private:
friend class NetworkChangeNotifierWinTest;
friend class TestNetworkChangeNotifierWin;
// NetworkChangeNotifier methods:
ConnectionCost GetCurrentConnectionCost() override;
ConnectionType GetCurrentConnectionType() const override;
// ObjectWatcher::Delegate methods:
// Must only be called on the sequence |this| was created on.
void OnObjectSignaled(HANDLE object) override;
// Does the actual work to determine the current connection type.
// It is not thread safe, see crbug.com/324913.
static ConnectionType RecomputeCurrentConnectionType();
// Calls RecomputeCurrentConnectionTypeImpl on the DNS sequence and runs
// |reply_callback| with the type on the calling sequence.
virtual void RecomputeCurrentConnectionTypeOnBlockingSequence(
base::OnceCallback<void(ConnectionType)> reply_callback) const;
void SetCurrentConnectionType(ConnectionType connection_type);
// Notifies IP address change observers of a change immediately, and notifies
// network state change observers on a delay. Must only be called on the
// sequence |this| was created on.
void NotifyObservers(ConnectionType connection_type);
// Forwards connection type notifications to parent class.
void NotifyParentOfConnectionTypeChange();
void NotifyParentOfConnectionTypeChangeImpl(ConnectionType connection_type);
// Tries to start listening for a single subsequent address change. Returns
// false on failure. The caller is responsible for updating |is_watching_|.
// Virtual for unit tests. Must only be called on the sequence |this| was
// created on.
virtual bool WatchForAddressChangeInternal();
static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsWin();
// Gets the current network connection cost (if possible) and caches it.
void InitializeConnectionCost();
// Does the work of initializing for thread safety.
bool InitializeConnectionCostOnce();
// Retrieves the current network connection cost from the OS's Cost Manager.
HRESULT UpdateConnectionCostFromCostManager();
// Converts the OS enum values to the enum values used in our code.
static ConnectionCost ConnectionCostFromNlmCost(NLM_CONNECTION_COST cost);
// Sets the cached network connection cost value.
void SetCurrentConnectionCost(ConnectionCost connection_cost);
// Callback method for the notification event sink.
void OnCostChanged();
// Tells this class that an observer was added and therefore this class needs
// to register for notifications.
void ConnectionCostObserverAdded() override;
// Since ConnectionCostObserverAdded() can be called on any thread and we
// don't want to do a bunch of work on an arbitrary thread, this method used
// to post task to do the work.
void OnConnectionCostObserverAdded();
// All member variables may only be accessed on the sequence |this| was
// created on.
// False when not currently watching for network change events. This only
// happens on initialization and when WatchForAddressChangeInternal fails and
// there is a pending task to try again. Needed for safe cleanup.
bool is_watching_ = false;
base::win::ObjectWatcher addr_watcher_;
OVERLAPPED addr_overlapped_;
base::OneShotTimer timer_;
// Number of times WatchForAddressChange has failed in a row.
int sequential_failures_ = 0;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
mutable base::Lock last_computed_connection_type_lock_;
ConnectionType last_computed_connection_type_;
std::atomic<ConnectionCost> last_computed_connection_cost_ =
ConnectionCost::CONNECTION_COST_UNKNOWN;
// Result of IsOffline() when NotifyObserversOfConnectionTypeChange()
// was last called.
bool last_announced_offline_;
// Number of times polled to check if still offline.
int offline_polls_;
Microsoft::WRL::ComPtr<INetworkCostManager> network_cost_manager_;
Microsoft::WRL::ComPtr<NetworkCostManagerEventSink>
network_cost_manager_event_sink_;
// Used to ensure that all registration actions are properly sequenced on the
// same thread regardless of which thread was used to call into the
// NetworkChangeNotifier API.
scoped_refptr<base::SequencedTaskRunner> sequence_runner_for_registration_;
SEQUENCE_CHECKER(sequence_checker_);
// Used for calling WatchForAddressChange again on failure.
base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_{this};
};
} // namespace net
#endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_