Commit f115bd90 authored by noamsml@chromium.org's avatar noamsml@chromium.org

Added cache flush on network change to ServiceDiscoveryHostClient

ServiceDiscoveryHostClient now listens for IP address changes (and, by
extension, sleep/wake) and flushes the cache and resets the utility process.

BUG=284673

Review URL: https://chromiumcodereview.appspot.com/23851008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221914 0039d316-1c4b-4281-b951-d872f2087c98
parent 4a4ab03f
...@@ -50,6 +50,7 @@ class PrivetDeviceLister { ...@@ -50,6 +50,7 @@ class PrivetDeviceLister {
const std::string& name, const std::string& name,
const DeviceDescription& description) = 0; const DeviceDescription& description) = 0;
virtual void DeviceRemoved(const std::string& name) = 0; virtual void DeviceRemoved(const std::string& name) = 0;
virtual void DeviceCacheFlushed() = 0;
}; };
......
...@@ -37,12 +37,7 @@ PrivetDeviceListerImpl::~PrivetDeviceListerImpl() { ...@@ -37,12 +37,7 @@ PrivetDeviceListerImpl::~PrivetDeviceListerImpl() {
} }
void PrivetDeviceListerImpl::Start() { void PrivetDeviceListerImpl::Start() {
service_watcher_ = CreateServiceWatcher();
service_discovery_client_->CreateServiceWatcher(
service_type_,
base::Bind(&PrivetDeviceListerImpl::OnServiceUpdated,
base::Unretained(this)));
service_watcher_->Start();
} }
void PrivetDeviceListerImpl::DiscoverNewDevices(bool force_update) { void PrivetDeviceListerImpl::DiscoverNewDevices(bool force_update) {
...@@ -52,6 +47,14 @@ void PrivetDeviceListerImpl::DiscoverNewDevices(bool force_update) { ...@@ -52,6 +47,14 @@ void PrivetDeviceListerImpl::DiscoverNewDevices(bool force_update) {
void PrivetDeviceListerImpl::OnServiceUpdated( void PrivetDeviceListerImpl::OnServiceUpdated(
ServiceWatcher::UpdateType update, ServiceWatcher::UpdateType update,
const std::string& service_name) { const std::string& service_name) {
if (update == ServiceWatcher::UPDATE_INVALIDATED) {
resolvers_.clear();
CreateServiceWatcher();
delegate_->DeviceCacheFlushed();
return;
}
if (update != ServiceWatcher::UPDATE_REMOVED) { if (update != ServiceWatcher::UPDATE_REMOVED) {
bool added = (update == ServiceWatcher::UPDATE_ADDED); bool added = (update == ServiceWatcher::UPDATE_ADDED);
std::pair<ServiceResolverMap::iterator, bool> insert_result = std::pair<ServiceResolverMap::iterator, bool> insert_result =
...@@ -143,4 +146,14 @@ PrivetDeviceListerImpl::ConnectionStateFromString(const std::string& str) { ...@@ -143,4 +146,14 @@ PrivetDeviceListerImpl::ConnectionStateFromString(const std::string& str) {
return DeviceDescription::UNKNOWN; return DeviceDescription::UNKNOWN;
} }
void PrivetDeviceListerImpl::CreateServiceWatcher() {
service_watcher_ =
service_discovery_client_->CreateServiceWatcher(
service_type_,
base::Bind(&PrivetDeviceListerImpl::OnServiceUpdated,
base::Unretained(this)));
service_watcher_->Start();
}
} // namespace local_discovery } // namespace local_discovery
...@@ -51,6 +51,9 @@ class PrivetDeviceListerImpl : public PrivetDeviceLister { ...@@ -51,6 +51,9 @@ class PrivetDeviceListerImpl : public PrivetDeviceLister {
DeviceDescription::ConnectionState ConnectionStateFromString( DeviceDescription::ConnectionState ConnectionStateFromString(
const std::string& str); const std::string& str);
// Create or recreate the service watcher
void CreateServiceWatcher();
PrivetDeviceLister::Delegate* delegate_; PrivetDeviceLister::Delegate* delegate_;
ServiceDiscoveryClient* service_discovery_client_; ServiceDiscoveryClient* service_discovery_client_;
......
...@@ -163,6 +163,8 @@ class MockDeviceListerDelegate : public PrivetDeviceLister::Delegate { ...@@ -163,6 +163,8 @@ class MockDeviceListerDelegate : public PrivetDeviceLister::Delegate {
const DeviceDescription& description)); const DeviceDescription& description));
MOCK_METHOD1(DeviceRemoved, void(const std::string& name)); MOCK_METHOD1(DeviceRemoved, void(const std::string& name));
MOCK_METHOD0(DeviceCacheFlushed, void());
}; };
class PrivetDeviceListerTest : public testing::Test { class PrivetDeviceListerTest : public testing::Test {
......
...@@ -125,6 +125,20 @@ void PrivetNotificationsListener::DeviceRemoved(const std::string& name) { ...@@ -125,6 +125,20 @@ void PrivetNotificationsListener::DeviceRemoved(const std::string& name) {
delegate_->PrivetRemoveNotification(name); delegate_->PrivetRemoveNotification(name);
} }
void PrivetNotificationsListener::DeviceCacheFlushed() {
for (DeviceContextMap::iterator i = devices_seen_.begin();
i != devices_seen_.end(); ++i) {
DeviceContext* device = i->second.get();
device->info_operation.reset();
device->privet_http_resolution.reset();
if (device->notification_may_be_active) {
device->notification_may_be_active = false;
delegate_->PrivetRemoveNotification(i->first);
}
}
}
PrivetNotificationsListener::DeviceContext::DeviceContext() { PrivetNotificationsListener::DeviceContext::DeviceContext() {
} }
...@@ -156,6 +170,10 @@ void PrivetNotificationService::DeviceRemoved(const std::string& name) { ...@@ -156,6 +170,10 @@ void PrivetNotificationService::DeviceRemoved(const std::string& name) {
privet_notifications_listener_->DeviceRemoved(name); privet_notifications_listener_->DeviceRemoved(name);
} }
void PrivetNotificationService::DeviceCacheFlushed() {
privet_notifications_listener_->DeviceCacheFlushed();
}
void PrivetNotificationService::PrivetNotify( void PrivetNotificationService::PrivetNotify(
const std::string& device_name, const std::string& device_name,
const std::string& human_readable_name, const std::string& human_readable_name,
......
...@@ -54,6 +54,7 @@ class PrivetNotificationsListener : public PrivetInfoOperation::Delegate { ...@@ -54,6 +54,7 @@ class PrivetNotificationsListener : public PrivetInfoOperation::Delegate {
const std::string& name, const std::string& name,
const DeviceDescription& description); const DeviceDescription& description);
void DeviceRemoved(const std::string& name); void DeviceRemoved(const std::string& name);
virtual void DeviceCacheFlushed();
// PrivetInfoOperation::Delegate implementation. // PrivetInfoOperation::Delegate implementation.
virtual void OnPrivetInfoDone( virtual void OnPrivetInfoDone(
...@@ -108,6 +109,8 @@ class PrivetNotificationService ...@@ -108,6 +109,8 @@ class PrivetNotificationService
virtual void PrivetRemoveNotification( virtual void PrivetRemoveNotification(
const std::string& device_name) OVERRIDE; const std::string& device_name) OVERRIDE;
virtual void DeviceCacheFlushed() OVERRIDE;
private: private:
void Start(); void Start();
......
...@@ -192,25 +192,23 @@ uint64 ServiceDiscoveryHostClient::RegisterLocalDomainResolverCallback( ...@@ -192,25 +192,23 @@ uint64 ServiceDiscoveryHostClient::RegisterLocalDomainResolverCallback(
void ServiceDiscoveryHostClient::UnregisterWatcherCallback(uint64 id) { void ServiceDiscoveryHostClient::UnregisterWatcherCallback(uint64 id) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
DCHECK(ContainsKey(service_watcher_callbacks_, id));
service_watcher_callbacks_.erase(id); service_watcher_callbacks_.erase(id);
} }
void ServiceDiscoveryHostClient::UnregisterResolverCallback(uint64 id) { void ServiceDiscoveryHostClient::UnregisterResolverCallback(uint64 id) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
DCHECK(ContainsKey(service_resolver_callbacks_, id));
service_resolver_callbacks_.erase(id); service_resolver_callbacks_.erase(id);
} }
void ServiceDiscoveryHostClient::UnregisterLocalDomainResolverCallback( void ServiceDiscoveryHostClient::UnregisterLocalDomainResolverCallback(
uint64 id) { uint64 id) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
DCHECK(ContainsKey(domain_resolver_callbacks_, id));
domain_resolver_callbacks_.erase(id); domain_resolver_callbacks_.erase(id);
} }
void ServiceDiscoveryHostClient::Start() { void ServiceDiscoveryHostClient::Start() {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
net::NetworkChangeNotifier::AddIPAddressObserver(this);
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::IO, BrowserThread::IO,
FROM_HERE, FROM_HERE,
...@@ -218,6 +216,7 @@ void ServiceDiscoveryHostClient::Start() { ...@@ -218,6 +216,7 @@ void ServiceDiscoveryHostClient::Start() {
} }
void ServiceDiscoveryHostClient::Shutdown() { void ServiceDiscoveryHostClient::Shutdown() {
net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::IO, BrowserThread::IO,
...@@ -259,6 +258,13 @@ void ServiceDiscoveryHostClient::ShutdownOnIOThread() { ...@@ -259,6 +258,13 @@ void ServiceDiscoveryHostClient::ShutdownOnIOThread() {
} }
} }
void ServiceDiscoveryHostClient::RestartOnIOThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
ShutdownOnIOThread();
StartOnIOThread();
}
void ServiceDiscoveryHostClient::Send(IPC::Message* msg) { void ServiceDiscoveryHostClient::Send(IPC::Message* msg) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
BrowserThread::PostTask( BrowserThread::PostTask(
...@@ -273,6 +279,23 @@ void ServiceDiscoveryHostClient::SendOnIOThread(IPC::Message* msg) { ...@@ -273,6 +279,23 @@ void ServiceDiscoveryHostClient::SendOnIOThread(IPC::Message* msg) {
utility_host_->Send(msg); utility_host_->Send(msg);
} }
void ServiceDiscoveryHostClient::OnIPAddressChanged() {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&ServiceDiscoveryHostClient::RestartOnIOThread, this));
WatcherCallbacks service_watcher_callbacks;
service_watcher_callbacks_.swap(service_watcher_callbacks);
for (WatcherCallbacks::iterator i = service_watcher_callbacks.begin();
i != service_watcher_callbacks.end(); i++) {
if (!i->second.is_null()) {
i->second.Run(ServiceWatcher::UPDATE_INVALIDATED, "");
}
}
}
bool ServiceDiscoveryHostClient::OnMessageReceived( bool ServiceDiscoveryHostClient::OnMessageReceived(
const IPC::Message& message) { const IPC::Message& message) {
bool handled = true; bool handled = true;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/threading/non_thread_safe.h" #include "base/threading/non_thread_safe.h"
#include "chrome/common/local_discovery/service_discovery_client.h" #include "chrome/common/local_discovery/service_discovery_client.h"
#include "content/public/browser/utility_process_host_client.h" #include "content/public/browser/utility_process_host_client.h"
#include "net/base/network_change_notifier.h"
namespace base { namespace base {
class TaskRunner; class TaskRunner;
...@@ -25,9 +26,11 @@ namespace local_discovery { ...@@ -25,9 +26,11 @@ namespace local_discovery {
// Implementation of ServiceDiscoveryClient that delegates all functionality to // Implementation of ServiceDiscoveryClient that delegates all functionality to
// utility process. // utility process.
class ServiceDiscoveryHostClient : public base::NonThreadSafe, class ServiceDiscoveryHostClient
public ServiceDiscoveryClient, : public base::NonThreadSafe,
public content::UtilityProcessHostClient { public ServiceDiscoveryClient,
public content::UtilityProcessHostClient,
public net::NetworkChangeNotifier::IPAddressObserver {
public: public:
ServiceDiscoveryHostClient(); ServiceDiscoveryHostClient();
...@@ -52,6 +55,9 @@ class ServiceDiscoveryHostClient : public base::NonThreadSafe, ...@@ -52,6 +55,9 @@ class ServiceDiscoveryHostClient : public base::NonThreadSafe,
// UtilityProcessHostClient implementation. // UtilityProcessHostClient implementation.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
// net::NetworkChangeNotifier::IPAddressObserver implementation.
virtual void OnIPAddressChanged() OVERRIDE;
protected: protected:
virtual ~ServiceDiscoveryHostClient(); virtual ~ServiceDiscoveryHostClient();
...@@ -68,6 +74,7 @@ class ServiceDiscoveryHostClient : public base::NonThreadSafe, ...@@ -68,6 +74,7 @@ class ServiceDiscoveryHostClient : public base::NonThreadSafe,
void StartOnIOThread(); void StartOnIOThread();
void ShutdownOnIOThread(); void ShutdownOnIOThread();
void RestartOnIOThread();
void Send(IPC::Message* msg); void Send(IPC::Message* msg);
void SendOnIOThread(IPC::Message* msg); void SendOnIOThread(IPC::Message* msg);
......
...@@ -209,17 +209,7 @@ cr.define('local_discovery', function() { ...@@ -209,17 +209,7 @@ cr.define('local_discovery', function() {
} }
} }
var numberPrinters = $('register-device-list').children.length; updateUIToReflectNumberOfLocalDevices();
$('printer-num').textContent = generateNumberPrintersAvailableText(
numberPrinters);
if (numberPrinters == 0) {
$('register-message').textContent = loadTimeData.getString(
'noPrintersOnNetworkExplanation');
} else {
$('register-message').textContent = loadTimeData.getString(
'registerConfirmMessage');
}
} }
/** /**
...@@ -250,6 +240,29 @@ cr.define('local_discovery', function() { ...@@ -250,6 +240,29 @@ cr.define('local_discovery', function() {
} }
} }
function onDeviceCacheFlushed() {
for (var deviceName in devices) {
devices[deviceName].removeDevice();
delete devices[deviceName];
}
updateUIToReflectNumberOfLocalDevices();
}
function updateUIToReflectNumberOfLocalDevices() {
var numberPrinters = $('register-device-list').children.length;
$('printer-num').textContent = generateNumberPrintersAvailableText(
numberPrinters);
if (numberPrinters == 0) {
$('register-message').textContent = loadTimeData.getString(
'noPrintersOnNetworkExplanation');
} else {
$('register-message').textContent = loadTimeData.getString(
'registerConfirmMessage');
}
}
/** /**
* Announce that a registration succeeeded. * Announce that a registration succeeeded.
...@@ -352,6 +365,7 @@ cr.define('local_discovery', function() { ...@@ -352,6 +365,7 @@ cr.define('local_discovery', function() {
onRegistrationFailed: onRegistrationFailed, onRegistrationFailed: onRegistrationFailed,
onUnregisteredDeviceUpdate: onUnregisteredDeviceUpdate, onUnregisteredDeviceUpdate: onUnregisteredDeviceUpdate,
onRegistrationConfirmedOnPrinter: onRegistrationConfirmedOnPrinter, onRegistrationConfirmedOnPrinter: onRegistrationConfirmedOnPrinter,
onCloudDeviceListAvailable: onCloudDeviceListAvailable onCloudDeviceListAvailable: onCloudDeviceListAvailable,
onDeviceCacheFlushed: onDeviceCacheFlushed
}; };
}); });
...@@ -42,7 +42,7 @@ LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() : is_visible_(false) { ...@@ -42,7 +42,7 @@ LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() : is_visible_(false) {
} }
LocalDiscoveryUIHandler::LocalDiscoveryUIHandler( LocalDiscoveryUIHandler::LocalDiscoveryUIHandler(
scoped_ptr<PrivetDeviceLister> privet_lister) { scoped_ptr<PrivetDeviceLister> privet_lister) : is_visible_(false) {
privet_lister.swap(privet_lister_); privet_lister.swap(privet_lister_);
} }
...@@ -312,6 +312,11 @@ void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { ...@@ -312,6 +312,11 @@ void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) {
name_value, *null_value); name_value, *null_value);
} }
void LocalDiscoveryUIHandler::DeviceCacheFlushed() {
web_ui()->CallJavascriptFunction("local_discovery.onDeviceCacheFlushed");
privet_lister_->DiscoverNewDevices(false);
}
void LocalDiscoveryUIHandler::OnCloudPrintPrinterListReady() { void LocalDiscoveryUIHandler::OnCloudPrintPrinterListReady() {
base::ListValue printer_object_list; base::ListValue printer_object_list;
std::set<std::string> local_ids; std::set<std::string> local_ids;
......
...@@ -74,8 +74,11 @@ class LocalDiscoveryUIHandler : public content::WebUIMessageHandler, ...@@ -74,8 +74,11 @@ class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
bool added, bool added,
const std::string& name, const std::string& name,
const DeviceDescription& description) OVERRIDE; const DeviceDescription& description) OVERRIDE;
virtual void DeviceRemoved(const std::string& name) OVERRIDE; virtual void DeviceRemoved(const std::string& name) OVERRIDE;
virtual void DeviceCacheFlushed() OVERRIDE;
// CloudPrintPrinterList::Delegate implementation. // CloudPrintPrinterList::Delegate implementation.
virtual void OnCloudPrintPrinterListReady() OVERRIDE; virtual void OnCloudPrintPrinterListReady() OVERRIDE;
......
...@@ -51,7 +51,8 @@ class ServiceWatcher { ...@@ -51,7 +51,8 @@ class ServiceWatcher {
enum UpdateType { enum UpdateType {
UPDATE_ADDED, UPDATE_ADDED,
UPDATE_CHANGED, UPDATE_CHANGED,
UPDATE_REMOVED UPDATE_REMOVED,
UPDATE_INVALIDATED
}; };
// Called when a service has been added or removed for a certain service name. // Called when a service has been added or removed for a certain service name.
......
...@@ -291,7 +291,6 @@ void ServiceDiscoveryMessageHandler::DiscoverServices(uint64 id, ...@@ -291,7 +291,6 @@ void ServiceDiscoveryMessageHandler::DiscoverServices(uint64 id,
void ServiceDiscoveryMessageHandler::DestroyWatcher(uint64 id) { void ServiceDiscoveryMessageHandler::DestroyWatcher(uint64 id) {
if (!service_discovery_client_) if (!service_discovery_client_)
return; return;
DCHECK(ContainsKey(service_watchers_, id));
service_watchers_.erase(id); service_watchers_.erase(id);
} }
...@@ -313,7 +312,6 @@ void ServiceDiscoveryMessageHandler::ResolveService( ...@@ -313,7 +312,6 @@ void ServiceDiscoveryMessageHandler::ResolveService(
void ServiceDiscoveryMessageHandler::DestroyResolver(uint64 id) { void ServiceDiscoveryMessageHandler::DestroyResolver(uint64 id) {
if (!service_discovery_client_) if (!service_discovery_client_)
return; return;
DCHECK(ContainsKey(service_resolvers_, id));
service_resolvers_.erase(id); service_resolvers_.erase(id);
} }
...@@ -336,7 +334,6 @@ void ServiceDiscoveryMessageHandler::ResolveLocalDomain( ...@@ -336,7 +334,6 @@ void ServiceDiscoveryMessageHandler::ResolveLocalDomain(
void ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver(uint64 id) { void ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver(uint64 id) {
if (!service_discovery_client_) if (!service_discovery_client_)
return; return;
DCHECK(ContainsKey(local_domain_resolvers_, id));
local_domain_resolvers_.erase(id); local_domain_resolvers_.erase(id);
} }
......
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