| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/extensions/api/dial/device_description_fetcher.h" | |
| 15 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" | 14 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" |
| 15 #include "chrome/browser/media/router/discovery/dial/device_description_fetcher.
h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/extensions/api/dial.h" | 17 #include "chrome/common/extensions/api/dial.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "extensions/browser/event_router.h" | 19 #include "extensions/browser/event_router.h" |
| 20 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 | 22 |
| 23 using base::TimeDelta; | 23 using base::TimeDelta; |
| 24 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 using extensions::api::dial::DeviceDescriptionFetcher; | 25 using media_router::DeviceDescriptionFetcher; |
| 26 using extensions::api::dial::DialDeviceData; | 26 using media_router::DialDeviceData; |
| 27 using extensions::api::dial::DialDeviceDescriptionData; | 27 using media_router::DialDeviceDescriptionData; |
| 28 using extensions::api::dial::DialRegistry; | 28 using media_router::DialRegistry; |
| 29 | 29 |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // How often to poll for devices. | 34 // How often to poll for devices. |
| 35 const int kDialRefreshIntervalSecs = 120; | 35 const int kDialRefreshIntervalSecs = 120; |
| 36 | 36 |
| 37 // We prune a device if it does not respond after this time. | 37 // We prune a device if it does not respond after this time. |
| 38 const int kDialExpirationSecs = 240; | 38 const int kDialExpirationSecs = 240; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 VLOG(2) << "DIAL device event listener added."; | 85 VLOG(2) << "DIAL device event listener added."; |
| 86 dial_registry()->OnListenerAdded(); | 86 dial_registry()->OnListenerAdded(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void DialAPI::NotifyListenerRemovedOnIOThread() { | 89 void DialAPI::NotifyListenerRemovedOnIOThread() { |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 91 VLOG(2) << "DIAL device event listener removed"; | 91 VLOG(2) << "DIAL device event listener removed"; |
| 92 dial_registry()->OnListenerRemoved(); | 92 dial_registry()->OnListenerRemoved(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void DialAPI::FillDialDevice(const media_router::DialDeviceData& device_data, |
| 96 api::dial::DialDevice* device) const { |
| 97 DCHECK(!device_data.device_id().empty()); |
| 98 DCHECK(media_router::DialDeviceData::IsDeviceDescriptionUrl( |
| 99 device_data.device_description_url())); |
| 100 device->device_label = device_data.label(); |
| 101 device->device_description_url = device_data.device_description_url().spec(); |
| 102 if (device_data.has_config_id()) |
| 103 device->config_id.reset(new int(device_data.config_id())); |
| 104 } |
| 105 |
| 95 void DialAPI::OnDialDeviceEvent(const DialRegistry::DeviceList& devices) { | 106 void DialAPI::OnDialDeviceEvent(const DialRegistry::DeviceList& devices) { |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 107 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 97 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 108 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 98 base::Bind(&DialAPI::SendEventOnUIThread, this, devices)); | 109 base::Bind(&DialAPI::SendEventOnUIThread, this, devices)); |
| 99 } | 110 } |
| 100 | 111 |
| 101 void DialAPI::OnDialError(const DialRegistry::DialErrorCode code) { | 112 void DialAPI::OnDialError(const DialRegistry::DialErrorCode code) { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 113 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 103 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 114 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 104 base::Bind(&DialAPI::SendErrorOnUIThread, this, code)); | 115 base::Bind(&DialAPI::SendErrorOnUIThread, this, code)); |
| 105 } | 116 } |
| 106 | 117 |
| 107 void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { | 118 void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 119 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 109 | 120 |
| 110 std::vector<api::dial::DialDevice> args; | 121 std::vector<api::dial::DialDevice> args; |
| 111 for (const DialDeviceData& device : devices) { | 122 for (const DialDeviceData& device : devices) { |
| 112 api::dial::DialDevice api_device; | 123 api::dial::DialDevice api_device; |
| 113 device.FillDialDevice(&api_device); | 124 FillDialDevice(device, &api_device); |
| 114 args.push_back(std::move(api_device)); | 125 args.push_back(std::move(api_device)); |
| 115 } | 126 } |
| 116 std::unique_ptr<base::ListValue> results = | 127 std::unique_ptr<base::ListValue> results = |
| 117 api::dial::OnDeviceList::Create(args); | 128 api::dial::OnDeviceList::Create(args); |
| 118 std::unique_ptr<Event> event(new Event(events::DIAL_ON_DEVICE_LIST, | 129 std::unique_ptr<Event> event(new Event(events::DIAL_ON_DEVICE_LIST, |
| 119 api::dial::OnDeviceList::kEventName, | 130 api::dial::OnDeviceList::kEventName, |
| 120 std::move(results))); | 131 std::move(results))); |
| 121 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); | 132 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); |
| 122 } | 133 } |
| 123 | 134 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 150 api::dial::OnError::Create(dial_error); | 161 api::dial::OnError::Create(dial_error); |
| 151 std::unique_ptr<Event> event(new Event(events::DIAL_ON_ERROR, | 162 std::unique_ptr<Event> event(new Event(events::DIAL_ON_ERROR, |
| 152 api::dial::OnError::kEventName, | 163 api::dial::OnError::kEventName, |
| 153 std::move(results))); | 164 std::move(results))); |
| 154 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); | 165 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); |
| 155 } | 166 } |
| 156 | 167 |
| 157 void DialAPI::ShutdownOnUIThread() {} | 168 void DialAPI::ShutdownOnUIThread() {} |
| 158 | 169 |
| 159 void DialAPI::SetDeviceForTest( | 170 void DialAPI::SetDeviceForTest( |
| 160 const api::dial::DialDeviceData& device_data, | 171 const media_router::DialDeviceData& device_data, |
| 161 const api::dial::DialDeviceDescriptionData& device_description) { | 172 const media_router::DialDeviceDescriptionData& device_description) { |
| 162 test_device_data_ = base::MakeUnique<DialDeviceData>(device_data); | 173 test_device_data_ = base::MakeUnique<DialDeviceData>(device_data); |
| 163 test_device_description_ = | 174 test_device_description_ = |
| 164 base::MakeUnique<DialDeviceDescriptionData>(device_description); | 175 base::MakeUnique<DialDeviceDescriptionData>(device_description); |
| 165 } | 176 } |
| 166 | 177 |
| 167 DialDiscoverNowFunction::DialDiscoverNowFunction() | 178 DialDiscoverNowFunction::DialDiscoverNowFunction() |
| 168 : dial_(NULL), result_(false) { | 179 : dial_(NULL), result_(false) { |
| 169 } | 180 } |
| 170 | 181 |
| 171 bool DialDiscoverNowFunction::Prepare() { | 182 bool DialDiscoverNowFunction::Prepare() { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 device_description_fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>( | 244 device_description_fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>( |
| 234 url, Profile::FromBrowserContext(browser_context())->GetRequestContext(), | 245 url, Profile::FromBrowserContext(browser_context())->GetRequestContext(), |
| 235 base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchComplete, | 246 base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchComplete, |
| 236 this), | 247 this), |
| 237 base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchError, this)); | 248 base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchError, this)); |
| 238 | 249 |
| 239 device_description_fetcher_->Start(); | 250 device_description_fetcher_->Start(); |
| 240 } | 251 } |
| 241 | 252 |
| 242 void DialFetchDeviceDescriptionFunction::OnFetchComplete( | 253 void DialFetchDeviceDescriptionFunction::OnFetchComplete( |
| 243 const api::dial::DialDeviceDescriptionData& result) { | 254 const media_router::DialDeviceDescriptionData& result) { |
| 244 // Destroy the DeviceDescriptionFetcher since it still contains a reference | 255 // Destroy the DeviceDescriptionFetcher since it still contains a reference |
| 245 // to |this| in its un-invoked callback. | 256 // to |this| in its un-invoked callback. |
| 246 device_description_fetcher_.reset(); | 257 device_description_fetcher_.reset(); |
| 247 api::dial::DialDeviceDescription device_description; | 258 api::dial::DialDeviceDescription device_description; |
| 248 device_description.device_label = params_->device_label; | 259 device_description.device_label = params_->device_label; |
| 249 device_description.app_url = result.app_url.spec(); | 260 device_description.app_url = result.app_url.spec(); |
| 250 device_description.device_description = result.device_description; | 261 device_description.device_description = result.device_description; |
| 251 SetResult(device_description.ToValue()); | 262 SetResult(device_description.ToValue()); |
| 252 SendResponse(true); | 263 SendResponse(true); |
| 253 } | 264 } |
| 254 | 265 |
| 255 void DialFetchDeviceDescriptionFunction::OnFetchError( | 266 void DialFetchDeviceDescriptionFunction::OnFetchError( |
| 256 const std::string& message) { | 267 const std::string& message) { |
| 257 // Destroy the DeviceDescriptionFetcher since it still contains a reference | 268 // Destroy the DeviceDescriptionFetcher since it still contains a reference |
| 258 // to |this| in its un-invoked callback. | 269 // to |this| in its un-invoked callback. |
| 259 device_description_fetcher_.reset(); | 270 device_description_fetcher_.reset(); |
| 260 SetError(message); | 271 SetError(message); |
| 261 SendResponse(false); | 272 SendResponse(false); |
| 262 } | 273 } |
| 263 | 274 |
| 264 } // namespace extensions | 275 } // namespace extensions |
| OLD | NEW |