Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/dial/dial_api.h" | 5 #include "chrome/browser/extensions/api/dial/dial_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 | 39 |
| 40 // The maximum number of devices retained at once in the registry. | 40 // The maximum number of devices retained at once in the registry. |
| 41 const size_t kDialMaxDevices = 256; | 41 const size_t kDialMaxDevices = 256; |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 DialAPI::DialAPI(Profile* profile) | 45 DialAPI::DialAPI(Profile* profile) |
| 46 : RefcountedKeyedService( | 46 : RefcountedKeyedService( |
| 47 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), | 47 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), |
| 48 profile_(profile) { | 48 profile_(profile) { |
| 49 EventRouter::Get(profile)->RegisterObserver( | 49 auto* event_router = EventRouter::Get(profile); |
| 50 this, api::dial::OnDeviceList::kEventName); | 50 event_router->RegisterObserver(this, api::dial::OnDeviceList::kEventName); |
| 51 event_router->RegisterObserver(this, | |
| 52 api::dial::OnNetworkIdChanged::kEventName); | |
| 51 } | 53 } |
| 52 | 54 |
| 53 DialAPI::~DialAPI() {} | 55 DialAPI::~DialAPI() {} |
| 54 | 56 |
| 55 DialRegistry* DialAPI::dial_registry() { | 57 DialRegistry* DialAPI::dial_registry() { |
| 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 58 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 57 if (!dial_registry_.get()) { | 59 if (!dial_registry_.get()) { |
| 58 dial_registry_.reset(new DialRegistry( | 60 dial_registry_.reset(new DialRegistry( |
| 59 TimeDelta::FromSeconds(kDialRefreshIntervalSecs), | 61 TimeDelta::FromSeconds(kDialRefreshIntervalSecs), |
| 60 TimeDelta::FromSeconds(kDialExpirationSecs), kDialMaxDevices)); | 62 TimeDelta::FromSeconds(kDialExpirationSecs), kDialMaxDevices)); |
| 61 dial_registry_->RegisterObserver(this); | 63 dial_registry_->RegisterObserver(this); |
| 62 if (test_device_data_) { | 64 if (test_device_data_) { |
| 63 dial_registry_->AddDeviceForTest(*test_device_data_); | 65 dial_registry_->AddDeviceForTest(*test_device_data_); |
| 64 } | 66 } |
| 65 } | 67 } |
| 66 return dial_registry_.get(); | 68 return dial_registry_.get(); |
| 67 } | 69 } |
| 68 | 70 |
| 69 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { | 71 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 72 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 71 BrowserThread::PostTask( | 73 if (details.event_name == api::dial::OnNetworkIdChanged::kEventName) { |
| 72 BrowserThread::IO, FROM_HERE, | 74 BrowserThread::PostTask( |
| 73 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); | 75 BrowserThread::IO, FROM_HERE, |
| 76 base::Bind(&DialAPI::NetworkIdListenerAddedOnIOThread, this)); | |
| 77 } else { | |
| 78 BrowserThread::PostTask( | |
| 79 BrowserThread::IO, FROM_HERE, | |
| 80 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); | |
| 81 } | |
| 74 } | 82 } |
| 75 | 83 |
| 76 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { | 84 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 85 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 78 BrowserThread::PostTask( | 86 if (details.event_name == api::dial::OnNetworkIdChanged::kEventName) { |
| 79 BrowserThread::IO, FROM_HERE, | 87 BrowserThread::PostTask( |
| 80 base::Bind(&DialAPI::NotifyListenerRemovedOnIOThread, this)); | 88 BrowserThread::IO, FROM_HERE, |
| 89 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
| |
| 90 } else { | |
| 91 BrowserThread::PostTask( | |
| 92 BrowserThread::IO, FROM_HERE, | |
| 93 base::Bind(&DialAPI::NotifyListenerRemovedOnIOThread, this)); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void DialAPI::NetworkIdListenerAddedOnIOThread() { | |
| 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 99 VLOG(2) << "Network Id event listener added."; | |
| 100 DiscoveryNetworkMonitor::GetInstance()->AddObserver(this); | |
| 101 } | |
| 102 | |
| 103 void DialAPI::NetworkIdListenerRemovedOnIOThread() { | |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 105 VLOG(2) << "Network Id event listener removed."; | |
| 106 DiscoveryNetworkMonitor::GetInstance()->RemoveObserver(this); | |
| 81 } | 107 } |
| 82 | 108 |
| 83 void DialAPI::NotifyListenerAddedOnIOThread() { | 109 void DialAPI::NotifyListenerAddedOnIOThread() { |
| 84 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 85 VLOG(2) << "DIAL device event listener added."; | 111 VLOG(2) << "DIAL device event listener added."; |
| 86 dial_registry()->OnListenerAdded(); | 112 dial_registry()->OnListenerAdded(); |
| 87 } | 113 } |
| 88 | 114 |
| 89 void DialAPI::NotifyListenerRemovedOnIOThread() { | 115 void DialAPI::NotifyListenerRemovedOnIOThread() { |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 91 VLOG(2) << "DIAL device event listener removed"; | 117 VLOG(2) << "DIAL device event listener removed"; |
| 92 dial_registry()->OnListenerRemoved(); | 118 dial_registry()->OnListenerRemoved(); |
| 93 } | 119 } |
| 94 | 120 |
| 95 void DialAPI::OnDialDeviceEvent(const DialRegistry::DeviceList& devices) { | 121 void DialAPI::OnDialDeviceEvent(const DialRegistry::DeviceList& devices) { |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 122 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 97 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 123 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 98 base::Bind(&DialAPI::SendEventOnUIThread, this, devices)); | 124 base::Bind(&DialAPI::SendEventOnUIThread, this, devices)); |
| 99 } | 125 } |
| 100 | 126 |
| 101 void DialAPI::OnDialError(const DialRegistry::DialErrorCode code) { | 127 void DialAPI::OnDialError(const DialRegistry::DialErrorCode code) { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 128 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 103 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 129 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 104 base::Bind(&DialAPI::SendErrorOnUIThread, this, code)); | 130 base::Bind(&DialAPI::SendErrorOnUIThread, this, code)); |
| 105 } | 131 } |
| 106 | 132 |
| 133 void DialAPI::OnNetworksChanged(const DiscoveryNetworkMonitor& manager) { | |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 135 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 136 base::Bind(&DialAPI::SendNetworkIdChangedOnUIThread, | |
| 137 this, manager.GetNetworkId())); | |
| 138 } | |
| 139 | |
| 107 void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { | 140 void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 141 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 109 | 142 |
| 110 std::vector<api::dial::DialDevice> args; | 143 std::vector<api::dial::DialDevice> args; |
| 111 for (const DialDeviceData& device : devices) { | 144 for (const DialDeviceData& device : devices) { |
| 112 api::dial::DialDevice api_device; | 145 api::dial::DialDevice api_device; |
| 113 device.FillDialDevice(&api_device); | 146 device.FillDialDevice(&api_device); |
| 114 args.push_back(std::move(api_device)); | 147 args.push_back(std::move(api_device)); |
| 115 } | 148 } |
| 116 std::unique_ptr<base::ListValue> results = | 149 std::unique_ptr<base::ListValue> results = |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 147 } | 180 } |
| 148 | 181 |
| 149 std::unique_ptr<base::ListValue> results = | 182 std::unique_ptr<base::ListValue> results = |
| 150 api::dial::OnError::Create(dial_error); | 183 api::dial::OnError::Create(dial_error); |
| 151 std::unique_ptr<Event> event(new Event(events::DIAL_ON_ERROR, | 184 std::unique_ptr<Event> event(new Event(events::DIAL_ON_ERROR, |
| 152 api::dial::OnError::kEventName, | 185 api::dial::OnError::kEventName, |
| 153 std::move(results))); | 186 std::move(results))); |
| 154 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); | 187 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); |
| 155 } | 188 } |
| 156 | 189 |
| 190 void DialAPI::SendNetworkIdChangedOnUIThread(const std::string& network_id) { | |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 192 | |
| 193 std::unique_ptr<base::ListValue> results = | |
| 194 api::dial::OnNetworkIdChanged::Create(network_id); | |
| 195 std::unique_ptr<Event> event( | |
| 196 new Event(events::DIAL_ON_NETWORK_ID_CHANGED, | |
| 197 api::dial::OnNetworkIdChanged::kEventName, std::move(results))); | |
| 198 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); | |
| 199 } | |
| 200 | |
| 157 void DialAPI::ShutdownOnUIThread() {} | 201 void DialAPI::ShutdownOnUIThread() {} |
| 158 | 202 |
| 159 void DialAPI::SetDeviceForTest( | 203 void DialAPI::SetDeviceForTest( |
| 160 const api::dial::DialDeviceData& device_data, | 204 const api::dial::DialDeviceData& device_data, |
| 161 const api::dial::DialDeviceDescriptionData& device_description) { | 205 const api::dial::DialDeviceDescriptionData& device_description) { |
| 162 test_device_data_ = base::MakeUnique<DialDeviceData>(device_data); | 206 test_device_data_ = base::MakeUnique<DialDeviceData>(device_data); |
| 163 test_device_description_ = | 207 test_device_description_ = |
| 164 base::MakeUnique<DialDeviceDescriptionData>(device_description); | 208 base::MakeUnique<DialDeviceDescriptionData>(device_description); |
| 165 } | 209 } |
| 166 | 210 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 | 298 |
| 255 void DialFetchDeviceDescriptionFunction::OnFetchError( | 299 void DialFetchDeviceDescriptionFunction::OnFetchError( |
| 256 const std::string& message) { | 300 const std::string& message) { |
| 257 // Destroy the DeviceDescriptionFetcher since it still contains a reference | 301 // Destroy the DeviceDescriptionFetcher since it still contains a reference |
| 258 // to |this| in its un-invoked callback. | 302 // to |this| in its un-invoked callback. |
| 259 device_description_fetcher_.reset(); | 303 device_description_fetcher_.reset(); |
| 260 SetError(message); | 304 SetError(message); |
| 261 SendResponse(false); | 305 SendResponse(false); |
| 262 } | 306 } |
| 263 | 307 |
| 308 DialGetNetworkIdFunction::DialGetNetworkIdFunction() {} | |
| 309 | |
| 310 DialGetNetworkIdFunction::~DialGetNetworkIdFunction() {} | |
| 311 | |
| 312 bool DialGetNetworkIdFunction::RunAsync() { | |
| 313 auto* network_monitor = DiscoveryNetworkMonitor::GetInstance(); | |
| 314 // If we are the first to call the DiscoveryNetworkMonitor, it will have no | |
| 315 // network state. Force a refresh to be sure the network ID is populated. | |
| 316 network_monitor->Refresh( | |
| 317 base::Bind(&DialGetNetworkIdFunction::NetworkInfoReadyCallback, this)); | |
| 318 return true; | |
| 319 } | |
| 320 | |
| 321 void DialGetNetworkIdFunction::NetworkInfoReadyCallback() { | |
| 322 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 323 BrowserThread::PostTask( | |
| 324 BrowserThread::IO, FROM_HERE, | |
| 325 base::Bind(&DialGetNetworkIdFunction::GetNetworkIdOnIOThread, this)); | |
| 326 } | |
| 327 | |
| 328 void DialGetNetworkIdFunction::GetNetworkIdOnIOThread() { | |
| 329 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 330 const auto* network_monitor = DiscoveryNetworkMonitor::GetInstance(); | |
| 331 std::string network_id(network_monitor->GetNetworkId()); | |
| 332 | |
| 333 SetResult(base::MakeUnique<base::Value>( | |
| 334 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
| |
| 335 SendResponse(true); | |
| 336 } | |
| 337 | |
| 264 } // namespace extensions | 338 } // namespace extensions |
| OLD | NEW |