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/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/extensions/api/dial/device_description_fetcher.h" |
13 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" | 15 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/extensions/api/dial.h" | 17 #include "chrome/common/extensions/api/dial.h" |
16 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
17 #include "extensions/browser/event_router.h" | 19 #include "extensions/browser/event_router.h" |
18 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
| 21 #include "url/gurl.h" |
19 | 22 |
20 using base::TimeDelta; | 23 using base::TimeDelta; |
21 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 using extensions::api::dial::DeviceDescriptionFetcher; |
22 using extensions::api::dial::DialDeviceData; | 26 using extensions::api::dial::DialDeviceData; |
| 27 using extensions::api::dial::DialDeviceDescriptionData; |
23 using extensions::api::dial::DialRegistry; | 28 using extensions::api::dial::DialRegistry; |
24 | 29 |
25 namespace extensions { | 30 namespace extensions { |
26 | 31 |
27 namespace { | 32 namespace { |
28 | 33 |
29 // How often to poll for devices. | 34 // How often to poll for devices. |
30 const int kDialRefreshIntervalSecs = 120; | 35 const int kDialRefreshIntervalSecs = 120; |
31 | 36 |
32 // 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. |
(...skipping 10 matching lines...) Expand all Loading... |
43 profile_(profile) { | 48 profile_(profile) { |
44 EventRouter::Get(profile)->RegisterObserver( | 49 EventRouter::Get(profile)->RegisterObserver( |
45 this, api::dial::OnDeviceList::kEventName); | 50 this, api::dial::OnDeviceList::kEventName); |
46 } | 51 } |
47 | 52 |
48 DialAPI::~DialAPI() {} | 53 DialAPI::~DialAPI() {} |
49 | 54 |
50 DialRegistry* DialAPI::dial_registry() { | 55 DialRegistry* DialAPI::dial_registry() { |
51 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
52 if (!dial_registry_.get()) { | 57 if (!dial_registry_.get()) { |
53 dial_registry_.reset(new DialRegistry(this, | 58 dial_registry_.reset(new DialRegistry( |
54 TimeDelta::FromSeconds(kDialRefreshIntervalSecs), | 59 this, TimeDelta::FromSeconds(kDialRefreshIntervalSecs), |
55 TimeDelta::FromSeconds(kDialExpirationSecs), | 60 TimeDelta::FromSeconds(kDialExpirationSecs), kDialMaxDevices)); |
56 kDialMaxDevices)); | 61 if (test_device_data_) { |
| 62 dial_registry_->AddDeviceForTest(*test_device_data_); |
| 63 } |
57 } | 64 } |
58 return dial_registry_.get(); | 65 return dial_registry_.get(); |
59 } | 66 } |
60 | 67 |
61 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { | 68 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { |
62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
63 BrowserThread::PostTask( | 70 BrowserThread::PostTask( |
64 BrowserThread::IO, FROM_HERE, | 71 BrowserThread::IO, FROM_HERE, |
65 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); | 72 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); |
66 } | 73 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 std::unique_ptr<base::ListValue> results = | 148 std::unique_ptr<base::ListValue> results = |
142 api::dial::OnError::Create(dial_error); | 149 api::dial::OnError::Create(dial_error); |
143 std::unique_ptr<Event> event(new Event(events::DIAL_ON_ERROR, | 150 std::unique_ptr<Event> event(new Event(events::DIAL_ON_ERROR, |
144 api::dial::OnError::kEventName, | 151 api::dial::OnError::kEventName, |
145 std::move(results))); | 152 std::move(results))); |
146 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); | 153 EventRouter::Get(profile_)->BroadcastEvent(std::move(event)); |
147 } | 154 } |
148 | 155 |
149 void DialAPI::ShutdownOnUIThread() {} | 156 void DialAPI::ShutdownOnUIThread() {} |
150 | 157 |
| 158 void DialAPI::SetDeviceForTest( |
| 159 const api::dial::DialDeviceData& device_data, |
| 160 const api::dial::DialDeviceDescriptionData& device_description) { |
| 161 test_device_data_ = base::MakeUnique<DialDeviceData>(device_data); |
| 162 test_device_description_ = |
| 163 base::MakeUnique<DialDeviceDescriptionData>(device_description); |
| 164 } |
| 165 |
151 DialDiscoverNowFunction::DialDiscoverNowFunction() | 166 DialDiscoverNowFunction::DialDiscoverNowFunction() |
152 : dial_(NULL), result_(false) { | 167 : dial_(NULL), result_(false) { |
153 } | 168 } |
154 | 169 |
155 bool DialDiscoverNowFunction::Prepare() { | 170 bool DialDiscoverNowFunction::Prepare() { |
156 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
157 DCHECK(browser_context()); | 172 DCHECK(browser_context()); |
158 dial_ = DialAPIFactory::GetForBrowserContext(browser_context()).get(); | 173 dial_ = DialAPIFactory::GetForBrowserContext(browser_context()).get(); |
159 return true; | 174 return true; |
160 } | 175 } |
161 | 176 |
162 void DialDiscoverNowFunction::Work() { | 177 void DialDiscoverNowFunction::Work() { |
163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
164 result_ = dial_->dial_registry()->DiscoverNow(); | 179 result_ = dial_->dial_registry()->DiscoverNow(); |
165 } | 180 } |
166 | 181 |
167 bool DialDiscoverNowFunction::Respond() { | 182 bool DialDiscoverNowFunction::Respond() { |
168 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 183 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
169 SetResult(base::MakeUnique<base::FundamentalValue>(result_)); | 184 SetResult(base::MakeUnique<base::FundamentalValue>(result_)); |
170 return true; | 185 return true; |
171 } | 186 } |
172 | 187 |
| 188 DialFetchDeviceDescriptionFunction::DialFetchDeviceDescriptionFunction() |
| 189 : dial_(nullptr) {} |
| 190 |
| 191 DialFetchDeviceDescriptionFunction::~DialFetchDeviceDescriptionFunction() {} |
| 192 |
| 193 bool DialFetchDeviceDescriptionFunction::RunAsync() { |
| 194 dial_ = DialAPIFactory::GetForBrowserContext(browser_context()).get(); |
| 195 params_ = api::dial::FetchDeviceDescription::Params::Create(*args_); |
| 196 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 197 |
| 198 // DialRegistry lives on the IO thread. Hop on over there to get the URL to |
| 199 // fetch. |
| 200 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 201 base::Bind(&DialFetchDeviceDescriptionFunction:: |
| 202 GetDeviceDescriptionUrlOnIOThread, |
| 203 this, params_->device_label)); |
| 204 return true; |
| 205 } |
| 206 |
| 207 void DialFetchDeviceDescriptionFunction::GetDeviceDescriptionUrlOnIOThread( |
| 208 const std::string& label) { |
| 209 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 210 const GURL& device_description_url = |
| 211 dial_->dial_registry()->GetDeviceDescriptionURL(label); |
| 212 BrowserThread::PostTask( |
| 213 BrowserThread::UI, FROM_HERE, |
| 214 base::Bind(&DialFetchDeviceDescriptionFunction::MaybeStartFetch, this, |
| 215 device_description_url)); |
| 216 } |
| 217 |
| 218 void DialFetchDeviceDescriptionFunction::MaybeStartFetch(const GURL& url) { |
| 219 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 220 if (url.is_empty()) { |
| 221 SetError("Device not found"); |
| 222 SendResponse(false); |
| 223 return; |
| 224 } |
| 225 |
| 226 if (dial_->test_device_data_ && dial_->test_device_description_ && |
| 227 dial_->test_device_data_->device_description_url() == url) { |
| 228 OnFetchComplete(*(dial_->test_device_description_)); |
| 229 return; |
| 230 } |
| 231 |
| 232 device_description_fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>( |
| 233 url, Profile::FromBrowserContext(browser_context()), |
| 234 base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchComplete, |
| 235 this), |
| 236 base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchError, this)); |
| 237 |
| 238 device_description_fetcher_->Start(); |
| 239 } |
| 240 |
| 241 void DialFetchDeviceDescriptionFunction::OnFetchComplete( |
| 242 const api::dial::DialDeviceDescriptionData& result) { |
| 243 api::dial::DialDeviceDescription device_description; |
| 244 device_description.device_label = params_->device_label; |
| 245 device_description.app_url = result.app_url.spec(); |
| 246 device_description.device_description = result.device_description; |
| 247 SetResult(device_description.ToValue()); |
| 248 SendResponse(true); |
| 249 } |
| 250 |
| 251 void DialFetchDeviceDescriptionFunction::OnFetchError( |
| 252 const std::string& message) { |
| 253 SetError(message); |
| 254 SendResponse(false); |
| 255 } |
| 256 |
173 } // namespace extensions | 257 } // namespace extensions |
OLD | NEW |