Index: chrome/browser/extensions/api/dial/dial_api.cc |
diff --git a/chrome/browser/extensions/api/dial/dial_api.cc b/chrome/browser/extensions/api/dial/dial_api.cc |
index e97f90fa0b260780ad189b0ce32a5c585dd4c248..02f4cf77d03c8c99660baf46f838bf94ec3ac396 100644 |
--- a/chrome/browser/extensions/api/dial/dial_api.cc |
+++ b/chrome/browser/extensions/api/dial/dial_api.cc |
@@ -46,8 +46,10 @@ DialAPI::DialAPI(Profile* profile) |
: RefcountedKeyedService( |
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), |
profile_(profile) { |
- EventRouter::Get(profile)->RegisterObserver( |
- this, api::dial::OnDeviceList::kEventName); |
+ auto* event_router = EventRouter::Get(profile); |
+ event_router->RegisterObserver(this, api::dial::OnDeviceList::kEventName); |
+ event_router->RegisterObserver(this, |
+ api::dial::OnNetworkIdChanged::kEventName); |
} |
DialAPI::~DialAPI() {} |
@@ -68,16 +70,40 @@ DialRegistry* DialAPI::dial_registry() { |
void DialAPI::OnListenerAdded(const EventListenerInfo& details) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
- BrowserThread::PostTask( |
- BrowserThread::IO, FROM_HERE, |
- base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); |
+ if (details.event_name == api::dial::OnNetworkIdChanged::kEventName) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&DialAPI::NetworkIdListenerAddedOnIOThread, this)); |
+ } else { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); |
+ } |
} |
void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
- BrowserThread::PostTask( |
- BrowserThread::IO, FROM_HERE, |
- base::Bind(&DialAPI::NotifyListenerRemovedOnIOThread, this)); |
+ if (details.event_name == api::dial::OnNetworkIdChanged::kEventName) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&DialAPI::NetworkIdListenerRemovedOnIOThread, this)); |
mark a. foltz
2017/04/26 22:25:11
What if multiple event listeners are added? Do yo
btolsch
2017/05/03 22:03:08
From testing and looking at the code it wasn't cle
|
+ } else { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&DialAPI::NotifyListenerRemovedOnIOThread, this)); |
+ } |
+} |
+ |
+void DialAPI::NetworkIdListenerAddedOnIOThread() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ VLOG(2) << "Network Id event listener added."; |
+ DiscoveryNetworkMonitor::GetInstance()->AddObserver(this); |
+} |
+ |
+void DialAPI::NetworkIdListenerRemovedOnIOThread() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ VLOG(2) << "Network Id event listener removed."; |
+ DiscoveryNetworkMonitor::GetInstance()->RemoveObserver(this); |
} |
void DialAPI::NotifyListenerAddedOnIOThread() { |
@@ -104,6 +130,13 @@ void DialAPI::OnDialError(const DialRegistry::DialErrorCode code) { |
base::Bind(&DialAPI::SendErrorOnUIThread, this, code)); |
} |
+void DialAPI::OnNetworksChanged(const DiscoveryNetworkMonitor& manager) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ base::Bind(&DialAPI::SendNetworkIdChangedOnUIThread, |
+ this, manager.GetNetworkId())); |
+} |
+ |
void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
@@ -154,6 +187,17 @@ void DialAPI::SendErrorOnUIThread(const DialRegistry::DialErrorCode code) { |
EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); |
} |
+void DialAPI::SendNetworkIdChangedOnUIThread(const std::string& network_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ std::unique_ptr<base::ListValue> results = |
+ api::dial::OnNetworkIdChanged::Create(network_id); |
+ std::unique_ptr<Event> event( |
+ new Event(events::DIAL_ON_NETWORK_ID_CHANGED, |
+ api::dial::OnNetworkIdChanged::kEventName, std::move(results))); |
+ EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); |
+} |
+ |
void DialAPI::ShutdownOnUIThread() {} |
void DialAPI::SetDeviceForTest( |
@@ -261,4 +305,34 @@ void DialFetchDeviceDescriptionFunction::OnFetchError( |
SendResponse(false); |
} |
+DialGetNetworkIdFunction::DialGetNetworkIdFunction() {} |
+ |
+DialGetNetworkIdFunction::~DialGetNetworkIdFunction() {} |
+ |
+bool DialGetNetworkIdFunction::RunAsync() { |
+ auto* network_monitor = DiscoveryNetworkMonitor::GetInstance(); |
+ // If we are the first to call the DiscoveryNetworkMonitor, it will have no |
+ // network state. Force a refresh to be sure the network ID is populated. |
+ network_monitor->Refresh( |
+ base::Bind(&DialGetNetworkIdFunction::NetworkInfoReadyCallback, this)); |
+ return true; |
+} |
+ |
+void DialGetNetworkIdFunction::NetworkInfoReadyCallback() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&DialGetNetworkIdFunction::GetNetworkIdOnIOThread, this)); |
+} |
+ |
+void DialGetNetworkIdFunction::GetNetworkIdOnIOThread() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ const auto* network_monitor = DiscoveryNetworkMonitor::GetInstance(); |
+ std::string network_id(network_monitor->GetNetworkId()); |
+ |
+ SetResult(base::MakeUnique<base::Value>( |
+ network_id.size() == 0 ? std::string("unknown") : network_id)); |
mark a. foltz
2017/04/26 22:25:11
Can the NetworkMonitor return "unknown" in place o
btolsch
2017/05/03 22:03:08
Yes, it already doesn't return an empty string so
|
+ SendResponse(true); |
+} |
+ |
} // namespace extensions |