Chromium Code Reviews| 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 6f43b17aaf0ade8c3e4e533e98cdbe6a0cfa6358..a67117204369ecdfb7834159c23a89e9f910b6e2 100644 |
| --- a/chrome/browser/extensions/api/dial/dial_api.cc |
| +++ b/chrome/browser/extensions/api/dial/dial_api.cc |
| @@ -8,14 +8,17 @@ |
| #include <utility> |
| #include <vector> |
| +#include "base/bind.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/time/time.h" |
| +#include "chrome/browser/extensions/api/dial/device_description_fetcher.h" |
| #include "chrome/browser/extensions/api/dial/dial_api_factory.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/extensions/api/dial.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "extensions/browser/event_router.h" |
| #include "extensions/browser/extension_system.h" |
| +#include "url/gurl.h" |
| using base::TimeDelta; |
| using content::BrowserThread; |
| @@ -50,10 +53,12 @@ DialAPI::~DialAPI() {} |
| DialRegistry* DialAPI::dial_registry() { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| if (!dial_registry_.get()) { |
| - dial_registry_.reset(new DialRegistry(this, |
| - TimeDelta::FromSeconds(kDialRefreshIntervalSecs), |
| - TimeDelta::FromSeconds(kDialExpirationSecs), |
| - kDialMaxDevices)); |
| + dial_registry_.reset(new DialRegistry( |
| + this, TimeDelta::FromSeconds(kDialRefreshIntervalSecs), |
| + TimeDelta::FromSeconds(kDialExpirationSecs), kDialMaxDevices)); |
| + if (!test_device_data_.device_id().empty()) { |
|
lazyboy
2017/01/04 22:13:13
Can we make |test_device_data_| and |test_device_d
mark a. foltz
2017/01/05 19:16:35
Done.
|
| + dial_registry_->AddDeviceForTest(test_device_data_); |
| + } |
| } |
| return dial_registry_.get(); |
| } |
| @@ -147,6 +152,13 @@ void DialAPI::SendErrorOnUIThread(const DialRegistry::DialErrorCode code) { |
| void DialAPI::ShutdownOnUIThread() {} |
| +void DialAPI::SetDeviceForTest( |
| + const DialDeviceData& device_data, |
| + const DialDeviceDescription& device_description) { |
| + test_device_data_ = device_data; |
| + test_device_description_ = device_description; |
| +} |
| + |
| namespace api { |
| DialDiscoverNowFunction::DialDiscoverNowFunction() |
| @@ -171,6 +183,74 @@ bool DialDiscoverNowFunction::Respond() { |
| return true; |
| } |
| +DialFetchDeviceDescriptionFunction::DialFetchDeviceDescriptionFunction() |
| + : dial_(nullptr) {} |
| + |
| +DialFetchDeviceDescriptionFunction::~DialFetchDeviceDescriptionFunction() {} |
| + |
| +bool DialFetchDeviceDescriptionFunction::RunAsync() { |
| + dial_ = DialAPIFactory::GetForBrowserContext(browser_context()).get(); |
| + params_ = api::dial::FetchDeviceDescription::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + |
| + // DialRegistry lives on the IO thread. Hop on over there to get the URL to |
| + // fetch. |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&DialFetchDeviceDescriptionFunction:: |
| + GetDeviceDescriptionUrlOnIOThread, |
| + this, params_->device_label)); |
| + return true; |
| +} |
| + |
| +void DialFetchDeviceDescriptionFunction::GetDeviceDescriptionUrlOnIOThread( |
| + const std::string& label) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + const GURL& device_description_url = |
| + dial_->dial_registry()->GetDeviceDescriptionURL(label); |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&DialFetchDeviceDescriptionFunction::MaybeStartFetch, this, |
| + device_description_url)); |
| +} |
| + |
| +void DialFetchDeviceDescriptionFunction::MaybeStartFetch(const GURL& url) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (url.is_empty()) { |
| + SetError("Device not found"); |
| + SendResponse(false); |
| + return; |
| + } |
| + |
| + if (dial_->test_device_data_.device_description_url() == url) { |
| + OnFetchComplete(dial_->test_device_description_); |
| + return; |
| + } |
| + |
| + device_description_fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>( |
| + url, Profile::FromBrowserContext(browser_context()), |
| + base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchComplete, |
| + this), |
| + base::BindOnce(&DialFetchDeviceDescriptionFunction::OnFetchError, this)); |
| + |
| + device_description_fetcher_->Start(); |
| +} |
| + |
| +void DialFetchDeviceDescriptionFunction::OnFetchComplete( |
| + const DialDeviceDescription& result) { |
| + api::dial::DialDeviceDescription device_description; |
| + device_description.device_label = params_->device_label; |
| + device_description.app_url = result.app_url.spec(); |
| + device_description.device_description = result.device_description; |
| + SetResult(device_description.ToValue()); |
| + SendResponse(true); |
| +} |
| + |
| +void DialFetchDeviceDescriptionFunction::OnFetchError( |
| + const std::string& message) { |
| + SetError(message); |
| + SendResponse(false); |
| +} |
| + |
| } // namespace api |
| } // namespace extensions |