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( ...@@ -209,6 +209,11 @@ PrivetNotificationService::PrivetNotificationService(
} }
PrivetNotificationService::~PrivetNotificationService() { PrivetNotificationService::~PrivetNotificationService() {
#if BUILDFLAG(ENABLE_MDNS)
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (traffic_detector_)
traffic_detector_->Stop();
#endif
} }
void PrivetNotificationService::DeviceChanged( void PrivetNotificationService::DeviceChanged(
......
...@@ -14,8 +14,10 @@ ...@@ -14,8 +14,10 @@
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/browser_process.h"
#include "net/base/ip_address.h" #include "net/base/ip_address.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_interfaces.h" #include "net/base/network_interfaces.h"
#include "net/dns/dns_protocol.h" #include "net/dns/dns_protocol.h"
#include "net/dns/dns_response.h" #include "net/dns/dns_response.h"
...@@ -74,30 +76,42 @@ PrivetTrafficDetector::PrivetTrafficDetector( ...@@ -74,30 +76,42 @@ PrivetTrafficDetector::PrivetTrafficDetector(
restart_attempts_(kMaxRestartAttempts), restart_attempts_(kMaxRestartAttempts),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {}
PrivetTrafficDetector::~PrivetTrafficDetector() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
}
void PrivetTrafficDetector::Start() { void PrivetTrafficDetector::Start() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
g_browser_process->network_connection_tracker()->AddNetworkConnectionObserver(
this);
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&PrivetTrafficDetector::StartOnIOThread, base::BindOnce(&PrivetTrafficDetector::ScheduleRestart,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
PrivetTrafficDetector::~PrivetTrafficDetector() { void PrivetTrafficDetector::Stop() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); g_browser_process->network_connection_tracker()
->RemoveNetworkConnectionObserver(this);
} }
void PrivetTrafficDetector::StartOnIOThread() { void PrivetTrafficDetector::OnConnectionChanged(
DCHECK_CURRENTLY_ON(content::BrowserThread::IO); network::mojom::ConnectionType type) {
net::NetworkChangeNotifier::AddNetworkChangeObserver(this); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
ScheduleRestart(); content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&PrivetTrafficDetector::HandleConnectionChanged,
weak_ptr_factory_.GetWeakPtr(), type));
} }
void PrivetTrafficDetector::OnNetworkChanged( void PrivetTrafficDetector::HandleConnectionChanged(
net::NetworkChangeNotifier::ConnectionType type) { network::mojom::ConnectionType type) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO); DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
restart_attempts_ = kMaxRestartAttempts; restart_attempts_ = kMaxRestartAttempts;
if (type != net::NetworkChangeNotifier::CONNECTION_NONE) if (type != network::mojom::ConnectionType::CONNECTION_NONE) {
ScheduleRestart(); ScheduleRestart();
}
} }
void PrivetTrafficDetector::ScheduleRestart() { void PrivetTrafficDetector::ScheduleRestart() {
......
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
#include "base/cancelable_callback.h" #include "base/cancelable_callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "content/public/browser/browser_thread.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/address_family.h"
#include "net/base/ip_endpoint.h" #include "net/base/ip_endpoint.h"
#include "net/base/network_change_notifier.h"
namespace net { namespace net {
class DatagramServerSocket; class DatagramServerSocket;
...@@ -28,13 +28,15 @@ namespace cloud_print { ...@@ -28,13 +28,15 @@ namespace cloud_print {
// When traffic is detected, class fires callback and shutdowns itself. // When traffic is detected, class fires callback and shutdowns itself.
class PrivetTrafficDetector class PrivetTrafficDetector
: public base::RefCountedThreadSafe< : public base::RefCountedThreadSafe<
PrivetTrafficDetector, content::BrowserThread::DeleteOnIOThread>, PrivetTrafficDetector,
private net::NetworkChangeNotifier::NetworkChangeObserver { content::BrowserThread::DeleteOnIOThread>,
private content::NetworkConnectionTracker::NetworkConnectionObserver {
public: public:
PrivetTrafficDetector(net::AddressFamily address_family, PrivetTrafficDetector(net::AddressFamily address_family,
const base::Closure& on_traffic_detected); const base::Closure& on_traffic_detected);
void Start(); void Start();
void Stop();
private: private:
friend struct content::BrowserThread::DeleteOnThread< friend struct content::BrowserThread::DeleteOnThread<
...@@ -42,11 +44,10 @@ class PrivetTrafficDetector ...@@ -42,11 +44,10 @@ class PrivetTrafficDetector
friend class base::DeleteHelper<PrivetTrafficDetector>; friend class base::DeleteHelper<PrivetTrafficDetector>;
~PrivetTrafficDetector() override; ~PrivetTrafficDetector() override;
// net::NetworkChangeNotifier::NetworkChangeObserver implementation. // content::NetworkConnectionTracker::NetworkConnectionObserver:
void OnNetworkChanged( void OnConnectionChanged(network::mojom::ConnectionType type) override;
net::NetworkChangeNotifier::ConnectionType type) override;
void StartOnIOThread(); void HandleConnectionChanged(network::mojom::ConnectionType type);
void ScheduleRestart(); void ScheduleRestart();
void Restart(const net::NetworkInterfaceList& networks); void Restart(const net::NetworkInterfaceList& networks);
int Bind(); 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