Commit 77922074 authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

Reland "Migrate PrivetTrafficDetector to use NetworkConnectionTracker."

This is a reland of 9b785217 with test
fixes. The failing unit tests weren't being run when I first landed
this, but were enabled afterwards and started breaking, so I rolled
back.

Specifically, I PostTask'd (un)registering with the
NetworkConnectionTracker to the right threads, and added a
PrivetTrafficDetector::Stop method to clean up the registration and
call it from PrivetNotificationService.

Original change's description:
> Migrate PrivetTrafficDetector to use NetworkConnectionTracker.
>
> This migrates PrivetTrafficDetector from using
> net::NetworkChangeNotifier to content::NetworkConnectionTracker, which
> works with the network service enabled.
>
> Bug: 859134
> Change-Id: I06f2ecbe142bbed9a8772f69ab109b0c0dc32581
> Reviewed-on: https://chromium-review.googlesource.com/1123681
> Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
> Reviewed-by: Lei Zhang <thestig@chromium.org>
> Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#574700}

Bug: 859134
Change-Id: I19bc5bf0abdbd41cfbd3da1c0881f122adca6c2b
Reviewed-on: https://chromium-review.googlesource.com/1137411Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarHelen Li <xunjieli@chromium.org>
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576278}
parent c915f656
......@@ -209,6 +209,11 @@ PrivetNotificationService::PrivetNotificationService(
}
PrivetNotificationService::~PrivetNotificationService() {
#if BUILDFLAG(ENABLE_MDNS)
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (traffic_detector_)
traffic_detector_->Stop();
#endif
}
void PrivetNotificationService::DeviceChanged(
......
......@@ -14,8 +14,10 @@
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/browser_process.h"
#include "net/base/ip_address.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_interfaces.h"
#include "net/dns/dns_protocol.h"
#include "net/dns/dns_response.h"
......@@ -74,30 +76,42 @@ PrivetTrafficDetector::PrivetTrafficDetector(
restart_attempts_(kMaxRestartAttempts),
weak_ptr_factory_(this) {}
PrivetTrafficDetector::~PrivetTrafficDetector() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
}
void PrivetTrafficDetector::Start() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
g_browser_process->network_connection_tracker()->AddNetworkConnectionObserver(
this);
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&PrivetTrafficDetector::StartOnIOThread,
base::BindOnce(&PrivetTrafficDetector::ScheduleRestart,
weak_ptr_factory_.GetWeakPtr()));
}
PrivetTrafficDetector::~PrivetTrafficDetector() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
void PrivetTrafficDetector::Stop() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
g_browser_process->network_connection_tracker()
->RemoveNetworkConnectionObserver(this);
}
void PrivetTrafficDetector::StartOnIOThread() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
ScheduleRestart();
void PrivetTrafficDetector::OnConnectionChanged(
network::mojom::ConnectionType type) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&PrivetTrafficDetector::HandleConnectionChanged,
weak_ptr_factory_.GetWeakPtr(), type));
}
void PrivetTrafficDetector::OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) {
void PrivetTrafficDetector::HandleConnectionChanged(
network::mojom::ConnectionType type) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
restart_attempts_ = kMaxRestartAttempts;
if (type != net::NetworkChangeNotifier::CONNECTION_NONE)
if (type != network::mojom::ConnectionType::CONNECTION_NONE) {
ScheduleRestart();
}
}
void PrivetTrafficDetector::ScheduleRestart() {
......
......@@ -11,9 +11,9 @@
#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_connection_tracker.h"
#include "net/base/address_family.h"
#include "net/base/ip_endpoint.h"
#include "net/base/network_change_notifier.h"
namespace net {
class DatagramServerSocket;
......@@ -28,13 +28,15 @@ namespace cloud_print {
// When traffic is detected, class fires callback and shutdowns itself.
class PrivetTrafficDetector
: public base::RefCountedThreadSafe<
PrivetTrafficDetector, content::BrowserThread::DeleteOnIOThread>,
private net::NetworkChangeNotifier::NetworkChangeObserver {
PrivetTrafficDetector,
content::BrowserThread::DeleteOnIOThread>,
private content::NetworkConnectionTracker::NetworkConnectionObserver {
public:
PrivetTrafficDetector(net::AddressFamily address_family,
const base::Closure& on_traffic_detected);
void Start();
void Stop();
private:
friend struct content::BrowserThread::DeleteOnThread<
......@@ -42,11 +44,10 @@ class PrivetTrafficDetector
friend class base::DeleteHelper<PrivetTrafficDetector>;
~PrivetTrafficDetector() override;
// net::NetworkChangeNotifier::NetworkChangeObserver implementation.
void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) override;
// content::NetworkConnectionTracker::NetworkConnectionObserver:
void OnConnectionChanged(network::mojom::ConnectionType type) override;
void StartOnIOThread();
void HandleConnectionChanged(network::mojom::ConnectionType type);
void ScheduleRestart();
void Restart(const net::NetworkInterfaceList& networks);
int Bind();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment