Commit b20a4341 authored by Justin Carlson's avatar Justin Carlson Committed by Commit Bot

Refactor ServiceDiscoveryDeviceLister.

This change does two things.  First, it splits out the implementation
of ServiceDiscoveryDeviceLister from the interface, which will allow
users of the API to mock/fake it for testing.

Second, it adds a service_type parameter to
ServiceDiscoveryDeviceLister::Delegate::OnDeviceCacheFlushed.  This
allows users of the class that want to manage multiple service types
with a single Delegate to disambiguate which service is being flushed.
The other Delegate functions already pass in the service description
so don't need any additional information for this use.

Also fixup callsites for all users of this API.

Bug: 808157
Change-Id: I9cf4b23692f54c66e0224e06df1769f750f846b3
Reviewed-on: https://chromium-review.googlesource.com/898184
Commit-Queue: Justin Carlson <justincarlson@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Reviewed-by: default avatarGene Gutnik <gene@chromium.org>
Reviewed-by: default avatarmark a. foltz <mfoltz@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#534146}
parent 6ff23b06
...@@ -226,9 +226,8 @@ class ZeroconfPrinterDetectorImpl ...@@ -226,9 +226,8 @@ class ZeroconfPrinterDetectorImpl
// Since we start the discoverers immediately, this must come last in the // Since we start the discoverers immediately, this must come last in the
// constructor. // constructor.
for (const char* service : services) { for (const char* service : services) {
device_listers_.emplace_back( device_listers_.emplace_back(ServiceDiscoveryDeviceLister::Create(
std::make_unique<ServiceDiscoveryDeviceLister>( this, discovery_client_.get(), service));
this, discovery_client_.get(), service));
device_listers_.back()->Start(); device_listers_.back()->Start();
device_listers_.back()->DiscoverNewDevices(); device_listers_.back()->DiscoverNewDevices();
} }
...@@ -250,7 +249,8 @@ class ZeroconfPrinterDetectorImpl ...@@ -250,7 +249,8 @@ class ZeroconfPrinterDetectorImpl
} }
// ServiceDiscoveryDeviceLister::Delegate implementation // ServiceDiscoveryDeviceLister::Delegate implementation
void OnDeviceChanged(bool added, void OnDeviceChanged(const std::string& service_type,
bool added,
const ServiceDescription& service_description) override { const ServiceDescription& service_description) override {
// We don't care if it was added or not; we generate an update either way. // We don't care if it was added or not; we generate an update either way.
ParsedMetadata metadata(service_description); ParsedMetadata metadata(service_description);
...@@ -270,7 +270,8 @@ class ZeroconfPrinterDetectorImpl ...@@ -270,7 +270,8 @@ class ZeroconfPrinterDetectorImpl
} }
} }
void OnDeviceRemoved(const std::string& service_name) override { void OnDeviceRemoved(const std::string& service_type,
const std::string& service_name) override {
// Leverage ServiceDescription parsing to pull out the instance name. // Leverage ServiceDescription parsing to pull out the instance name.
ServiceDescription service_description; ServiceDescription service_description;
service_description.service_name = service_name; service_description.service_name = service_name;
...@@ -285,7 +286,7 @@ class ZeroconfPrinterDetectorImpl ...@@ -285,7 +286,7 @@ class ZeroconfPrinterDetectorImpl
} }
// Don't need to do anything here. // Don't need to do anything here.
void OnDeviceCacheFlushed() override {} void OnDeviceCacheFlushed(const std::string& service_type) override {}
private: private:
// Requires that printers_lock_ be held. // Requires that printers_lock_ be held.
......
...@@ -102,30 +102,33 @@ class CastDeviceProvider::DeviceListerDelegate ...@@ -102,30 +102,33 @@ class CastDeviceProvider::DeviceListerDelegate
if (device_lister_) if (device_lister_)
return; return;
service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance(); service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance();
device_lister_.reset(new ServiceDiscoveryDeviceLister( device_lister_ = ServiceDiscoveryDeviceLister::Create(
this, service_discovery_client_.get(), kCastServiceType)); this, service_discovery_client_.get(), kCastServiceType);
device_lister_->Start(); device_lister_->Start();
device_lister_->DiscoverNewDevices(); device_lister_->DiscoverNewDevices();
} }
// ServiceDiscoveryDeviceLister::Delegate implementation: // ServiceDiscoveryDeviceLister::Delegate implementation:
void OnDeviceChanged(bool added, void OnDeviceChanged(const std::string& service_type,
bool added,
const ServiceDescription& service_description) override { const ServiceDescription& service_description) override {
runner_->PostTask(FROM_HERE, runner_->PostTask(
base::BindOnce(&CastDeviceProvider::OnDeviceChanged, FROM_HERE,
provider_, added, service_description)); base::BindOnce(&CastDeviceProvider::OnDeviceChanged, provider_,
service_type, added, service_description));
} }
void OnDeviceRemoved(const std::string& service_name) override { void OnDeviceRemoved(const std::string& service_type,
const std::string& service_name) override {
runner_->PostTask(FROM_HERE, runner_->PostTask(FROM_HERE,
base::BindOnce(&CastDeviceProvider::OnDeviceRemoved, base::BindOnce(&CastDeviceProvider::OnDeviceRemoved,
provider_, service_name)); provider_, service_type, service_name));
} }
void OnDeviceCacheFlushed() override { void OnDeviceCacheFlushed(const std::string& service_type) override {
runner_->PostTask( runner_->PostTask(FROM_HERE,
FROM_HERE, base::BindOnce(&CastDeviceProvider::OnDeviceCacheFlushed,
base::BindOnce(&CastDeviceProvider::OnDeviceCacheFlushed, provider_)); provider_, service_type));
} }
private: private:
...@@ -173,6 +176,7 @@ void CastDeviceProvider::OpenSocket(const std::string& serial, ...@@ -173,6 +176,7 @@ void CastDeviceProvider::OpenSocket(const std::string& serial,
} }
void CastDeviceProvider::OnDeviceChanged( void CastDeviceProvider::OnDeviceChanged(
const std::string& service_type,
bool added, bool added,
const ServiceDescription& service_description) { const ServiceDescription& service_description) {
VLOG(1) << "Device " << (added ? "added: " : "changed: ") VLOG(1) << "Device " << (added ? "added: " : "changed: ")
...@@ -190,7 +194,8 @@ void CastDeviceProvider::OnDeviceChanged( ...@@ -190,7 +194,8 @@ void CastDeviceProvider::OnDeviceChanged(
device_info_map_[host] = ServiceDescriptionToDeviceInfo(service_description); device_info_map_[host] = ServiceDescriptionToDeviceInfo(service_description);
} }
void CastDeviceProvider::OnDeviceRemoved(const std::string& service_name) { void CastDeviceProvider::OnDeviceRemoved(const std::string& service_type,
const std::string& service_name) {
VLOG(1) << "Device removed: " << service_name; VLOG(1) << "Device removed: " << service_name;
auto it = service_hostname_map_.find(service_name); auto it = service_hostname_map_.find(service_name);
if (it == service_hostname_map_.end()) if (it == service_hostname_map_.end())
...@@ -200,7 +205,7 @@ void CastDeviceProvider::OnDeviceRemoved(const std::string& service_name) { ...@@ -200,7 +205,7 @@ void CastDeviceProvider::OnDeviceRemoved(const std::string& service_name) {
service_hostname_map_.erase(it); service_hostname_map_.erase(it);
} }
void CastDeviceProvider::OnDeviceCacheFlushed() { void CastDeviceProvider::OnDeviceCacheFlushed(const std::string& service_type) {
VLOG(1) << "Device cache flushed"; VLOG(1) << "Device cache flushed";
service_hostname_map_.clear(); service_hostname_map_.clear();
device_info_map_.clear(); device_info_map_.clear();
......
...@@ -35,10 +35,12 @@ class CastDeviceProvider ...@@ -35,10 +35,12 @@ class CastDeviceProvider
// ServiceDiscoveryDeviceLister::Delegate implementation: // ServiceDiscoveryDeviceLister::Delegate implementation:
void OnDeviceChanged( void OnDeviceChanged(
const std::string& service_type,
bool added, bool added,
const local_discovery::ServiceDescription& service_description) override; const local_discovery::ServiceDescription& service_description) override;
void OnDeviceRemoved(const std::string& service_name) override; void OnDeviceRemoved(const std::string& service_type,
void OnDeviceCacheFlushed() override; const std::string& service_name) override;
void OnDeviceCacheFlushed(const std::string& service_type) override;
private: private:
class DeviceListerDelegate; class DeviceListerDelegate;
......
...@@ -52,7 +52,7 @@ TEST(CastDeviceProviderTest, ServiceDiscovery) { ...@@ -52,7 +52,7 @@ TEST(CastDeviceProviderTest, ServiceDiscovery) {
cast_service.metadata.push_back("md=" + cast_service_model); cast_service.metadata.push_back("md=" + cast_service_model);
ASSERT_TRUE(cast_service.ip_address.AssignFromIPLiteral("192.168.1.101")); ASSERT_TRUE(cast_service.ip_address.AssignFromIPLiteral("192.168.1.101"));
device_provider_->OnDeviceChanged(true, cast_service); device_provider_->OnDeviceChanged(cast_service_type, true, cast_service);
BrowserInfo exp_browser_info; BrowserInfo exp_browser_info;
exp_browser_info.socket_name = "9222"; exp_browser_info.socket_name = "9222";
...@@ -91,7 +91,7 @@ TEST(CastDeviceProviderTest, ServiceDiscovery) { ...@@ -91,7 +91,7 @@ TEST(CastDeviceProviderTest, ServiceDiscovery) {
base::Bind(&DummyCallback, &was_run)); base::Bind(&DummyCallback, &was_run));
ASSERT_FALSE(was_run); ASSERT_FALSE(was_run);
device_provider_->OnDeviceChanged(true, other_service); device_provider_->OnDeviceChanged(cast_service_type, true, other_service);
// Callback should not be run, since non-cast services are not discovered by // Callback should not be run, since non-cast services are not discovered by
// this device provider. // this device provider.
...@@ -100,7 +100,8 @@ TEST(CastDeviceProviderTest, ServiceDiscovery) { ...@@ -100,7 +100,8 @@ TEST(CastDeviceProviderTest, ServiceDiscovery) {
ASSERT_FALSE(was_run); ASSERT_FALSE(was_run);
// Remove the cast service. // Remove the cast service.
device_provider_->OnDeviceRemoved(cast_service.service_name); device_provider_->OnDeviceRemoved(cast_service_type,
cast_service.service_name);
// Callback should not be run, since the cast service has been removed. // Callback should not be run, since the cast service has been removed.
device_provider_->QueryDeviceInfo(cast_service.address.host(), device_provider_->QueryDeviceInfo(cast_service.address.host(),
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/location.h" #include "base/location.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
...@@ -20,103 +21,127 @@ namespace { ...@@ -20,103 +21,127 @@ namespace {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
const int kMacServiceResolvingIntervalSecs = 60; const int kMacServiceResolvingIntervalSecs = 60;
#endif #endif
} // namespace
ServiceDiscoveryDeviceLister::ServiceDiscoveryDeviceLister(
Delegate* delegate,
ServiceDiscoveryClient* service_discovery_client,
const std::string& service_type)
: delegate_(delegate),
service_discovery_client_(service_discovery_client),
service_type_(service_type),
weak_factory_(this) {
}
ServiceDiscoveryDeviceLister::~ServiceDiscoveryDeviceLister() {
}
void ServiceDiscoveryDeviceLister::Start() {
VLOG(1) << "DeviceListerStart: service_type: " << service_type_;
CreateServiceWatcher();
}
void ServiceDiscoveryDeviceLister::DiscoverNewDevices() { class ServiceDiscoveryDeviceListerImpl : public ServiceDiscoveryDeviceLister {
VLOG(1) << "DiscoverNewDevices: service_type: " << service_type_; public:
service_watcher_->DiscoverNewServices(); ServiceDiscoveryDeviceListerImpl(
} Delegate* delegate,
ServiceDiscoveryClient* service_discovery_client,
void ServiceDiscoveryDeviceLister::OnServiceUpdated( const std::string& service_type)
ServiceWatcher::UpdateType update, : delegate_(delegate),
const std::string& service_name) { service_discovery_client_(service_discovery_client),
VLOG(1) << "OnServiceUpdated: service_type: " << service_type_ service_type_(service_type),
<< ", service_name: " << service_name weak_factory_(this) {}
<< ", update: " << update;
if (update == ServiceWatcher::UPDATE_INVALIDATED) { ~ServiceDiscoveryDeviceListerImpl() override = default;
resolvers_.clear();
// ServiceDiscoveryDeviceLister implementation.
void Start() override {
VLOG(1) << "DeviceListerStart: service_type: " << service_type_;
CreateServiceWatcher(); CreateServiceWatcher();
delegate_->OnDeviceCacheFlushed();
return;
} }
if (update == ServiceWatcher::UPDATE_REMOVED) { // ServiceDiscoveryDeviceLister implementation.
delegate_->OnDeviceRemoved(service_name); void DiscoverNewDevices() override {
return; VLOG(1) << "DiscoverNewDevices: service_type: " << service_type_;
service_watcher_->DiscoverNewServices();
} }
// If there is already a resolver working on this service, don't add one. const std::string& service_type() const override { return service_type_; }
if (base::ContainsKey(resolvers_, service_name)) {
VLOG(1) << "Resolver already exists, service_name: " << service_name; private:
return; using ServiceResolverMap =
std::map<std::string, std::unique_ptr<ServiceResolver>>;
void OnServiceUpdated(ServiceWatcher::UpdateType update,
const std::string& service_name) {
VLOG(1) << "OnServiceUpdated: service_type: " << service_type_
<< ", service_name: " << service_name << ", update: " << update;
if (update == ServiceWatcher::UPDATE_INVALIDATED) {
resolvers_.clear();
CreateServiceWatcher();
delegate_->OnDeviceCacheFlushed(service_type_);
return;
}
if (update == ServiceWatcher::UPDATE_REMOVED) {
delegate_->OnDeviceRemoved(service_type_, service_name);
return;
}
// If there is already a resolver working on this service, don't add one.
if (base::ContainsKey(resolvers_, service_name)) {
VLOG(1) << "Resolver already exists, service_name: " << service_name;
return;
}
VLOG(1) << "Adding resolver for service_name: " << service_name;
bool added = (update == ServiceWatcher::UPDATE_ADDED);
std::unique_ptr<ServiceResolver> resolver =
service_discovery_client_->CreateServiceResolver(
service_name,
base::Bind(&ServiceDiscoveryDeviceListerImpl::OnResolveComplete,
weak_factory_.GetWeakPtr(), added, service_name));
resolver->StartResolving();
resolvers_[service_name] = std::move(resolver);
} }
VLOG(1) << "Adding resolver for service_name: " << service_name; // TODO(noamsml): Update ServiceDiscoveryClient interface to match this.
bool added = (update == ServiceWatcher::UPDATE_ADDED); void OnResolveComplete(bool added,
std::unique_ptr<ServiceResolver> resolver = std::string service_name,
service_discovery_client_->CreateServiceResolver( ServiceResolver::RequestStatus status,
service_name, const ServiceDescription& service_description) {
base::Bind(&ServiceDiscoveryDeviceLister::OnResolveComplete, VLOG(1) << "OnResolveComplete: service_type: " << service_type_
weak_factory_.GetWeakPtr(), added, service_name)); << ", service_name: " << service_name << ", status: " << status;
resolver->StartResolving(); if (status == ServiceResolver::STATUS_SUCCESS) {
resolvers_[service_name] = std::move(resolver); delegate_->OnDeviceChanged(service_type_, added, service_description);
}
// TODO(noamsml): Update ServiceDiscoveryClient interface to match this.
void ServiceDiscoveryDeviceLister::OnResolveComplete(
bool added,
std::string service_name,
ServiceResolver::RequestStatus status,
const ServiceDescription& service_description) {
VLOG(1) << "OnResolveComplete: service_type: " << service_type_
<< ", service_name: " << service_name
<< ", status: " << status;
if (status == ServiceResolver::STATUS_SUCCESS) {
delegate_->OnDeviceChanged(added, service_description);
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// On Mac, the Bonjour service does not seem to ever evict a service if a // On Mac, the Bonjour service does not seem to ever evict a service if a
// device is unplugged, so we need to continuously try to resolve the // device is unplugged, so we need to continuously try to resolve the
// service to detect non-graceful shutdowns. // service to detect non-graceful shutdowns.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, FROM_HERE,
base::Bind(&ServiceDiscoveryDeviceLister::OnServiceUpdated, base::Bind(&ServiceDiscoveryDeviceListerImpl::OnServiceUpdated,
weak_factory_.GetWeakPtr(), ServiceWatcher::UPDATE_CHANGED, weak_factory_.GetWeakPtr(), ServiceWatcher::UPDATE_CHANGED,
service_description.service_name), service_description.service_name),
base::TimeDelta::FromSeconds(kMacServiceResolvingIntervalSecs)); base::TimeDelta::FromSeconds(kMacServiceResolvingIntervalSecs));
#endif #endif
} else { } else {
// TODO(noamsml): Add retry logic. // TODO(noamsml): Add retry logic.
}
resolvers_.erase(service_name);
} }
resolvers_.erase(service_name);
}
void ServiceDiscoveryDeviceLister::CreateServiceWatcher() { // Create or recreate the service watcher
service_watcher_ = void CreateServiceWatcher() {
service_discovery_client_->CreateServiceWatcher( service_watcher_ = service_discovery_client_->CreateServiceWatcher(
service_type_, service_type_,
base::Bind(&ServiceDiscoveryDeviceLister::OnServiceUpdated, base::Bind(&ServiceDiscoveryDeviceListerImpl::OnServiceUpdated,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
service_watcher_->Start(); service_watcher_->Start();
}
Delegate* const delegate_;
ServiceDiscoveryClient* const service_discovery_client_;
const std::string service_type_;
std::unique_ptr<ServiceWatcher> service_watcher_;
ServiceResolverMap resolvers_;
base::WeakPtrFactory<ServiceDiscoveryDeviceListerImpl> weak_factory_;
};
} // namespace
// static
std::unique_ptr<ServiceDiscoveryDeviceLister>
ServiceDiscoveryDeviceLister::Create(
Delegate* delegate,
ServiceDiscoveryClient* service_discovery_client,
const std::string& service_type) {
return std::make_unique<ServiceDiscoveryDeviceListerImpl>(
delegate, service_discovery_client, service_type);
} }
} // namespace local_discovery } // namespace local_discovery
...@@ -9,60 +9,44 @@ ...@@ -9,60 +9,44 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/local_discovery/service_discovery_client.h" #include "chrome/browser/local_discovery/service_discovery_client.h"
namespace local_discovery { namespace local_discovery {
// ServiceDiscoveryDeviceLister provides a way to get and maintain a list of all
// discoverable devices of a given service type (e.g. _ipp._tcp).
class ServiceDiscoveryDeviceLister { class ServiceDiscoveryDeviceLister {
public: public:
// Delegate interface to be implemented by recipients of list updates.
class Delegate { class Delegate {
public: public:
virtual void OnDeviceChanged( virtual void OnDeviceChanged(
const std::string& service_type,
bool added, bool added,
const ServiceDescription& service_description) = 0; const ServiceDescription& service_description) = 0;
// Not guaranteed to be called after OnDeviceChanged. // Not guaranteed to be called after OnDeviceChanged.
virtual void OnDeviceRemoved(const std::string& service_name) = 0; virtual void OnDeviceRemoved(const std::string& service_type,
virtual void OnDeviceCacheFlushed() = 0; const std::string& service_name) = 0;
virtual void OnDeviceCacheFlushed(const std::string& service_type) = 0;
}; };
ServiceDiscoveryDeviceLister(Delegate* delegate, virtual ~ServiceDiscoveryDeviceLister() = default;
ServiceDiscoveryClient* service_discovery_client,
const std::string& service_type);
virtual ~ServiceDiscoveryDeviceLister();
void Start(); virtual void Start() = 0;
void DiscoverNewDevices(); virtual void DiscoverNewDevices() = 0;
const std::string& service_type() const { return service_type_; } virtual const std::string& service_type() const = 0;
private: // Factory function, create and return a default-implementation DeviceLister
using ServiceResolverMap = // that lists devices of type |service_type|. |delegate| will receive
std::map<std::string, std::unique_ptr<ServiceResolver>>; // callbacks, and |service_discovery_client| will be used as the source of the
// the underlying discovery traffic.
void OnServiceUpdated(ServiceWatcher::UpdateType update, //
const std::string& service_name); // Typically, the ServiceDiscoverySharedClient is used as the client.
static std::unique_ptr<ServiceDiscoveryDeviceLister> Create(
void OnResolveComplete( Delegate* delegate,
bool added, ServiceDiscoveryClient* service_discovery_client,
std::string service_name, const std::string& service_type);
ServiceResolver::RequestStatus status,
const ServiceDescription& description);
// Create or recreate the service watcher
void CreateServiceWatcher();
Delegate* const delegate_;
ServiceDiscoveryClient* const service_discovery_client_;
const std::string service_type_;
std::unique_ptr<ServiceWatcher> service_watcher_;
ServiceResolverMap resolvers_;
base::WeakPtrFactory<ServiceDiscoveryDeviceLister> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryDeviceLister);
}; };
} // namespace local_discovery } // namespace local_discovery
......
...@@ -31,24 +31,28 @@ DnsSdDeviceLister::DnsSdDeviceLister( ...@@ -31,24 +31,28 @@ DnsSdDeviceLister::DnsSdDeviceLister(
DnsSdDelegate* delegate, DnsSdDelegate* delegate,
const std::string& service_type) const std::string& service_type)
: delegate_(delegate), : delegate_(delegate),
device_lister_(this, service_discovery_client, service_type), device_lister_(local_discovery::ServiceDiscoveryDeviceLister::Create(
this,
service_discovery_client,
service_type)),
started_(false) {} started_(false) {}
DnsSdDeviceLister::~DnsSdDeviceLister() {} DnsSdDeviceLister::~DnsSdDeviceLister() {}
void DnsSdDeviceLister::Discover() { void DnsSdDeviceLister::Discover() {
if (!started_) { if (!started_) {
device_lister_.Start(); device_lister_->Start();
started_ = true; started_ = true;
VLOG(1) << "Started device lister for service type " VLOG(1) << "Started device lister for service type "
<< device_lister_.service_type(); << device_lister_->service_type();
} }
device_lister_.DiscoverNewDevices(); device_lister_->DiscoverNewDevices();
VLOG(1) << "Discovery new devices for service type " VLOG(1) << "Discovery new devices for service type "
<< device_lister_.service_type(); << device_lister_->service_type();
} }
void DnsSdDeviceLister::OnDeviceChanged( void DnsSdDeviceLister::OnDeviceChanged(
const std::string& service_type,
bool added, bool added,
const ServiceDescription& service_description) { const ServiceDescription& service_description) {
DnsSdService service; DnsSdService service;
...@@ -56,22 +60,23 @@ void DnsSdDeviceLister::OnDeviceChanged( ...@@ -56,22 +60,23 @@ void DnsSdDeviceLister::OnDeviceChanged(
VLOG(1) << "OnDeviceChanged: " VLOG(1) << "OnDeviceChanged: "
<< "service_name: " << service.service_name << ", " << "service_name: " << service.service_name << ", "
<< "added: " << added << ", " << "added: " << added << ", "
<< "service_type: " << device_lister_.service_type(); << "service_type: " << device_lister_->service_type();
delegate_->ServiceChanged(device_lister_.service_type(), added, service); delegate_->ServiceChanged(device_lister_->service_type(), added, service);
} }
void DnsSdDeviceLister::OnDeviceRemoved(const std::string& service_name) { void DnsSdDeviceLister::OnDeviceRemoved(const std::string& service_type,
const std::string& service_name) {
VLOG(1) << "OnDeviceRemoved: " VLOG(1) << "OnDeviceRemoved: "
<< "service_name: " << service_name << ", " << "service_name: " << service_name << ", "
<< "service_type: " << device_lister_.service_type(); << "service_type: " << service_type;
delegate_->ServiceRemoved(device_lister_.service_type(), service_name); delegate_->ServiceRemoved(service_type, service_name);
} }
void DnsSdDeviceLister::OnDeviceCacheFlushed() { void DnsSdDeviceLister::OnDeviceCacheFlushed(const std::string& service_type) {
VLOG(1) << "OnDeviceCacheFlushed: " VLOG(1) << "OnDeviceCacheFlushed: "
<< "service_type: " << device_lister_.service_type(); << "service_type: " << device_lister_->service_type();
delegate_->ServicesFlushed(device_lister_.service_type()); delegate_->ServicesFlushed(device_lister_->service_type());
device_lister_.DiscoverNewDevices(); device_lister_->DiscoverNewDevices();
} }
} // namespace media_router } // namespace media_router
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_DNS_SD_DEVICE_LISTER_H_ #ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_DNS_SD_DEVICE_LISTER_H_
#define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_DNS_SD_DEVICE_LISTER_H_ #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_DNS_SD_DEVICE_LISTER_H_
#include <memory>
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
...@@ -32,15 +33,17 @@ class DnsSdDeviceLister ...@@ -32,15 +33,17 @@ class DnsSdDeviceLister
protected: protected:
void OnDeviceChanged( void OnDeviceChanged(
const std::string& service_type,
bool added, bool added,
const local_discovery::ServiceDescription& service_description) override; const local_discovery::ServiceDescription& service_description) override;
void OnDeviceRemoved(const std::string& service_name) override; void OnDeviceRemoved(const std::string& service_type,
void OnDeviceCacheFlushed() override; const std::string& service_name) override;
void OnDeviceCacheFlushed(const std::string& service_type) override;
private: private:
// The delegate to notify of changes to services. // The delegate to notify of changes to services.
DnsSdDelegate* const delegate_; DnsSdDelegate* const delegate_;
local_discovery::ServiceDiscoveryDeviceLister device_lister_; std::unique_ptr<local_discovery::ServiceDiscoveryDeviceLister> device_lister_;
bool started_; bool started_;
DISALLOW_COPY_AND_ASSIGN(DnsSdDeviceLister); DISALLOW_COPY_AND_ASSIGN(DnsSdDeviceLister);
......
...@@ -14,21 +14,24 @@ PrivetDeviceListerImpl::PrivetDeviceListerImpl( ...@@ -14,21 +14,24 @@ PrivetDeviceListerImpl::PrivetDeviceListerImpl(
local_discovery::ServiceDiscoveryClient* service_discovery_client, local_discovery::ServiceDiscoveryClient* service_discovery_client,
PrivetDeviceLister::Delegate* delegate) PrivetDeviceLister::Delegate* delegate)
: delegate_(delegate), : delegate_(delegate),
device_lister_(this, service_discovery_client, kPrivetDefaultDeviceType) { device_lister_(local_discovery::ServiceDiscoveryDeviceLister::Create(
} this,
service_discovery_client,
kPrivetDefaultDeviceType)) {}
PrivetDeviceListerImpl::~PrivetDeviceListerImpl() { PrivetDeviceListerImpl::~PrivetDeviceListerImpl() {
} }
void PrivetDeviceListerImpl::Start() { void PrivetDeviceListerImpl::Start() {
device_lister_.Start(); device_lister_->Start();
} }
void PrivetDeviceListerImpl::DiscoverNewDevices() { void PrivetDeviceListerImpl::DiscoverNewDevices() {
device_lister_.DiscoverNewDevices(); device_lister_->DiscoverNewDevices();
} }
void PrivetDeviceListerImpl::OnDeviceChanged( void PrivetDeviceListerImpl::OnDeviceChanged(
const std::string& service_type,
bool added, bool added,
const local_discovery::ServiceDescription& service_description) { const local_discovery::ServiceDescription& service_description) {
if (!delegate_) if (!delegate_)
...@@ -38,12 +41,14 @@ void PrivetDeviceListerImpl::OnDeviceChanged( ...@@ -38,12 +41,14 @@ void PrivetDeviceListerImpl::OnDeviceChanged(
DeviceDescription(service_description)); DeviceDescription(service_description));
} }
void PrivetDeviceListerImpl::OnDeviceRemoved(const std::string& service_name) { void PrivetDeviceListerImpl::OnDeviceRemoved(const std::string& service_type,
const std::string& service_name) {
if (delegate_) if (delegate_)
delegate_->DeviceRemoved(service_name); delegate_->DeviceRemoved(service_name);
} }
void PrivetDeviceListerImpl::OnDeviceCacheFlushed() { void PrivetDeviceListerImpl::OnDeviceCacheFlushed(
const std::string& service_type) {
if (delegate_) if (delegate_)
delegate_->DeviceCacheFlushed(); delegate_->DeviceCacheFlushed();
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_PRINTING_CLOUD_PRINT_PRIVET_DEVICE_LISTER_IMPL_H_ #ifndef CHROME_BROWSER_PRINTING_CLOUD_PRINT_PRIVET_DEVICE_LISTER_IMPL_H_
#define CHROME_BROWSER_PRINTING_CLOUD_PRINT_PRIVET_DEVICE_LISTER_IMPL_H_ #define CHROME_BROWSER_PRINTING_CLOUD_PRINT_PRIVET_DEVICE_LISTER_IMPL_H_
#include <memory>
#include <string> #include <string>
#include "chrome/browser/local_discovery/service_discovery_device_lister.h" #include "chrome/browser/local_discovery/service_discovery_device_lister.h"
...@@ -32,14 +33,16 @@ class PrivetDeviceListerImpl ...@@ -32,14 +33,16 @@ class PrivetDeviceListerImpl
protected: protected:
// ServiceDiscoveryDeviceLister: // ServiceDiscoveryDeviceLister:
void OnDeviceChanged( void OnDeviceChanged(
const std::string& service_type,
bool added, bool added,
const local_discovery::ServiceDescription& service_description) override; const local_discovery::ServiceDescription& service_description) override;
void OnDeviceRemoved(const std::string& service_name) override; void OnDeviceRemoved(const std::string& service_type,
void OnDeviceCacheFlushed() override; const std::string& service_name) override;
void OnDeviceCacheFlushed(const std::string& service_type) override;
private: private:
PrivetDeviceLister::Delegate* const delegate_; PrivetDeviceLister::Delegate* const delegate_;
local_discovery::ServiceDiscoveryDeviceLister device_lister_; std::unique_ptr<local_discovery::ServiceDiscoveryDeviceLister> device_lister_;
}; };
} // namespace cloud_print } // namespace cloud_print
......
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