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 {
const std::string& name,
const DeviceDescription& description) = 0;
virtual void DeviceRemoved(const std::string& name) = 0;
virtual void DeviceCacheFlushed() = 0;
};
......
......@@ -37,12 +37,7 @@ PrivetDeviceListerImpl::~PrivetDeviceListerImpl() {
}
void PrivetDeviceListerImpl::Start() {
service_watcher_ =
service_discovery_client_->CreateServiceWatcher(
service_type_,
base::Bind(&PrivetDeviceListerImpl::OnServiceUpdated,
base::Unretained(this)));
service_watcher_->Start();
CreateServiceWatcher();
}
void PrivetDeviceListerImpl::DiscoverNewDevices(bool force_update) {
......@@ -52,6 +47,14 @@ void PrivetDeviceListerImpl::DiscoverNewDevices(bool force_update) {
void PrivetDeviceListerImpl::OnServiceUpdated(
ServiceWatcher::UpdateType update,
const std::string& service_name) {
if (update == ServiceWatcher::UPDATE_INVALIDATED) {
resolvers_.clear();
CreateServiceWatcher();
delegate_->DeviceCacheFlushed();
return;
}
if (update != ServiceWatcher::UPDATE_REMOVED) {
bool added = (update == ServiceWatcher::UPDATE_ADDED);
std::pair<ServiceResolverMap::iterator, bool> insert_result =
......@@ -143,4 +146,14 @@ PrivetDeviceListerImpl::ConnectionStateFromString(const std::string& str) {
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
......@@ -51,6 +51,9 @@ class PrivetDeviceListerImpl : public PrivetDeviceLister {
DeviceDescription::ConnectionState ConnectionStateFromString(
const std::string& str);
// Create or recreate the service watcher
void CreateServiceWatcher();
PrivetDeviceLister::Delegate* delegate_;
ServiceDiscoveryClient* service_discovery_client_;
......
......@@ -163,6 +163,8 @@ class MockDeviceListerDelegate : public PrivetDeviceLister::Delegate {
const DeviceDescription& description));
MOCK_METHOD1(DeviceRemoved, void(const std::string& name));
MOCK_METHOD0(DeviceCacheFlushed, void());
};
class PrivetDeviceListerTest : public testing::Test {
......
......@@ -125,6 +125,20 @@ void PrivetNotificationsListener::DeviceRemoved(const std::string& 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() {
}
......@@ -156,6 +170,10 @@ void PrivetNotificationService::DeviceRemoved(const std::string& name) {
privet_notifications_listener_->DeviceRemoved(name);
}
void PrivetNotificationService::DeviceCacheFlushed() {
privet_notifications_listener_->DeviceCacheFlushed();
}
void PrivetNotificationService::PrivetNotify(
const std::string& device_name,
const std::string& human_readable_name,
......
......@@ -54,6 +54,7 @@ class PrivetNotificationsListener : public PrivetInfoOperation::Delegate {
const std::string& name,
const DeviceDescription& description);
void DeviceRemoved(const std::string& name);
virtual void DeviceCacheFlushed();
// PrivetInfoOperation::Delegate implementation.
virtual void OnPrivetInfoDone(
......@@ -108,6 +109,8 @@ class PrivetNotificationService
virtual void PrivetRemoveNotification(
const std::string& device_name) OVERRIDE;
virtual void DeviceCacheFlushed() OVERRIDE;
private:
void Start();
......
......@@ -192,25 +192,23 @@ uint64 ServiceDiscoveryHostClient::RegisterLocalDomainResolverCallback(
void ServiceDiscoveryHostClient::UnregisterWatcherCallback(uint64 id) {
DCHECK(CalledOnValidThread());
DCHECK(ContainsKey(service_watcher_callbacks_, id));
service_watcher_callbacks_.erase(id);
}
void ServiceDiscoveryHostClient::UnregisterResolverCallback(uint64 id) {
DCHECK(CalledOnValidThread());
DCHECK(ContainsKey(service_resolver_callbacks_, id));
service_resolver_callbacks_.erase(id);
}
void ServiceDiscoveryHostClient::UnregisterLocalDomainResolverCallback(
uint64 id) {
DCHECK(CalledOnValidThread());
DCHECK(ContainsKey(domain_resolver_callbacks_, id));
domain_resolver_callbacks_.erase(id);
}
void ServiceDiscoveryHostClient::Start() {
DCHECK(CalledOnValidThread());
net::NetworkChangeNotifier::AddIPAddressObserver(this);
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
......@@ -218,6 +216,7 @@ void ServiceDiscoveryHostClient::Start() {
}
void ServiceDiscoveryHostClient::Shutdown() {
net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
DCHECK(CalledOnValidThread());
BrowserThread::PostTask(
BrowserThread::IO,
......@@ -259,6 +258,13 @@ void ServiceDiscoveryHostClient::ShutdownOnIOThread() {
}
}
void ServiceDiscoveryHostClient::RestartOnIOThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
ShutdownOnIOThread();
StartOnIOThread();
}
void ServiceDiscoveryHostClient::Send(IPC::Message* msg) {
DCHECK(CalledOnValidThread());
BrowserThread::PostTask(
......@@ -273,6 +279,23 @@ void ServiceDiscoveryHostClient::SendOnIOThread(IPC::Message* 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(
const IPC::Message& message) {
bool handled = true;
......
......@@ -12,6 +12,7 @@
#include "base/threading/non_thread_safe.h"
#include "chrome/common/local_discovery/service_discovery_client.h"
#include "content/public/browser/utility_process_host_client.h"
#include "net/base/network_change_notifier.h"
namespace base {
class TaskRunner;
......@@ -25,9 +26,11 @@ namespace local_discovery {
// Implementation of ServiceDiscoveryClient that delegates all functionality to
// utility process.
class ServiceDiscoveryHostClient : public base::NonThreadSafe,
public ServiceDiscoveryClient,
public content::UtilityProcessHostClient {
class ServiceDiscoveryHostClient
: public base::NonThreadSafe,
public ServiceDiscoveryClient,
public content::UtilityProcessHostClient,
public net::NetworkChangeNotifier::IPAddressObserver {
public:
ServiceDiscoveryHostClient();
......@@ -52,6 +55,9 @@ class ServiceDiscoveryHostClient : public base::NonThreadSafe,
// UtilityProcessHostClient implementation.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
// net::NetworkChangeNotifier::IPAddressObserver implementation.
virtual void OnIPAddressChanged() OVERRIDE;
protected:
virtual ~ServiceDiscoveryHostClient();
......@@ -68,6 +74,7 @@ class ServiceDiscoveryHostClient : public base::NonThreadSafe,
void StartOnIOThread();
void ShutdownOnIOThread();
void RestartOnIOThread();
void Send(IPC::Message* msg);
void SendOnIOThread(IPC::Message* msg);
......
......@@ -209,17 +209,7 @@ cr.define('local_discovery', function() {
}
}
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');
}
updateUIToReflectNumberOfLocalDevices();
}
/**
......@@ -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.
......@@ -352,6 +365,7 @@ cr.define('local_discovery', function() {
onRegistrationFailed: onRegistrationFailed,
onUnregisteredDeviceUpdate: onUnregisteredDeviceUpdate,
onRegistrationConfirmedOnPrinter: onRegistrationConfirmedOnPrinter,
onCloudDeviceListAvailable: onCloudDeviceListAvailable
onCloudDeviceListAvailable: onCloudDeviceListAvailable,
onDeviceCacheFlushed: onDeviceCacheFlushed
};
});
......@@ -42,7 +42,7 @@ LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() : is_visible_(false) {
}
LocalDiscoveryUIHandler::LocalDiscoveryUIHandler(
scoped_ptr<PrivetDeviceLister> privet_lister) {
scoped_ptr<PrivetDeviceLister> privet_lister) : is_visible_(false) {
privet_lister.swap(privet_lister_);
}
......@@ -312,6 +312,11 @@ void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) {
name_value, *null_value);
}
void LocalDiscoveryUIHandler::DeviceCacheFlushed() {
web_ui()->CallJavascriptFunction("local_discovery.onDeviceCacheFlushed");
privet_lister_->DiscoverNewDevices(false);
}
void LocalDiscoveryUIHandler::OnCloudPrintPrinterListReady() {
base::ListValue printer_object_list;
std::set<std::string> local_ids;
......
......@@ -74,8 +74,11 @@ class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
bool added,
const std::string& name,
const DeviceDescription& description) OVERRIDE;
virtual void DeviceRemoved(const std::string& name) OVERRIDE;
virtual void DeviceCacheFlushed() OVERRIDE;
// CloudPrintPrinterList::Delegate implementation.
virtual void OnCloudPrintPrinterListReady() OVERRIDE;
......
......@@ -51,7 +51,8 @@ class ServiceWatcher {
enum UpdateType {
UPDATE_ADDED,
UPDATE_CHANGED,
UPDATE_REMOVED
UPDATE_REMOVED,
UPDATE_INVALIDATED
};
// Called when a service has been added or removed for a certain service name.
......
......@@ -291,7 +291,6 @@ void ServiceDiscoveryMessageHandler::DiscoverServices(uint64 id,
void ServiceDiscoveryMessageHandler::DestroyWatcher(uint64 id) {
if (!service_discovery_client_)
return;
DCHECK(ContainsKey(service_watchers_, id));
service_watchers_.erase(id);
}
......@@ -313,7 +312,6 @@ void ServiceDiscoveryMessageHandler::ResolveService(
void ServiceDiscoveryMessageHandler::DestroyResolver(uint64 id) {
if (!service_discovery_client_)
return;
DCHECK(ContainsKey(service_resolvers_, id));
service_resolvers_.erase(id);
}
......@@ -336,7 +334,6 @@ void ServiceDiscoveryMessageHandler::ResolveLocalDomain(
void ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver(uint64 id) {
if (!service_discovery_client_)
return;
DCHECK(ContainsKey(local_domain_resolvers_, 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