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