chromium/content/public/test/network_connection_change_simulator.cc

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/test/network_connection_change_simulator.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/network_service_util.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/network_change_notifier.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/network_service_test.mojom.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "net/base/network_change_notifier_passive.h"
#endif

namespace content {

// SetConnectionType will block until the network connection changes, and
// unblocking it involves posting a task (see
// NetworkConnectionTracker::OnNetworkChanged). If SetConnectionType is ever
// called downstream of a task run within another RunLoop::Run call, this
// class's RunLoop::Run will deadlock because the task needed to unblock it
// won't be run. To stop this, this class uses RunLoops that allow nested tasks.
constexpr base::RunLoop::Type kRunLoopType =;

NetworkConnectionChangeSimulator::NetworkConnectionChangeSimulator() = default;
NetworkConnectionChangeSimulator::~NetworkConnectionChangeSimulator() = default;

#if BUILDFLAG(IS_CHROMEOS)
void NetworkConnectionChangeSimulator::InitializeChromeosConnectionType() {
  // Manually set the connection type since ChromeOS's NetworkChangeNotifier
  // implementation relies on some other class controlling it (normally
  // NetworkChangeManagerClient), which isn't used on content/.
  net::NetworkChangeNotifierPassive* network_change_notifier =
      static_cast<net::NetworkChangeNotifierPassive*>(
          content::GetNetworkChangeNotifier());
  network_change_notifier->OnConnectionChanged(
      net::NetworkChangeNotifier::CONNECTION_ETHERNET);
  // If the network service is enabled, set the connection type for its
  // NetworkChangeNotifier instance as well.
  if (IsOutOfProcessNetworkService()) {
    mojo::Remote<network::mojom::NetworkChangeManager> manager;
    GetNetworkService()->GetNetworkChangeManager(
        manager.BindNewPipeAndPassReceiver());
    manager->OnNetworkChanged(
        /*dns_changed=*/false, /*ip_address_changed=*/false,
        /*connection_type_changed=*/true,
        network::mojom::ConnectionType::CONNECTION_ETHERNET,
        /*connection_subtype_changed=*/false,
        network::mojom::ConnectionSubtype::SUBTYPE_UNKNOWN);
  }
}
#endif

void NetworkConnectionChangeSimulator::SetConnectionType(
    network::mojom::ConnectionType type) {}

// static
void NetworkConnectionChangeSimulator::SimulateNetworkChange(
    network::mojom::ConnectionType type) {}

void NetworkConnectionChangeSimulator::OnConnectionChanged(
    network::mojom::ConnectionType connection_type) {}

}  // namespace content