Commit 557ee6b4 authored by wutao's avatar wutao Committed by Commit Bot

ambient: Use multiple downloader

There could be multiple URL requests at the same time. This patch
creates URL loader for each request.

Bug: b/153504737
Test: manual
Change-Id: I990f60e09242fc1ce65e23b8068aa0edda59601c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2141614Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: Tao Wu <wutao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#763482}
parent 0e39edf4
......@@ -215,21 +215,20 @@ void AmbientBackendControllerImpl::FetchScreenUpdateInfoInternal(
gaia_id, access_token, client_id);
auto resource_request = CreateResourceRequest(request);
// |base::Unretained| is safe because this instance outlives
// |backdrop_url_loader_|.
DCHECK(!backdrop_url_loader_);
backdrop_url_loader_ = std::make_unique<BackdropURLLoader>();
backdrop_url_loader_->Start(
auto backdrop_url_loader = std::make_unique<BackdropURLLoader>();
auto* loader_ptr = backdrop_url_loader.get();
loader_ptr->Start(
std::move(resource_request), request.body, NO_TRAFFIC_ANNOTATION_YET,
base::BindOnce(&AmbientBackendControllerImpl::OnScreenUpdateInfoFetched,
base::Unretained(this), std::move(callback)));
weak_factory_.GetWeakPtr(), std::move(callback),
std::move(backdrop_url_loader)));
}
void AmbientBackendControllerImpl::OnScreenUpdateInfoFetched(
OnScreenUpdateInfoFetchedCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response) {
DCHECK(backdrop_url_loader_);
backdrop_url_loader_.reset();
DCHECK(backdrop_url_loader);
// Parse the |ScreenUpdate| out from the response string.
// Note that the |backdrop_screen_update| can be a dummy instance if the
......@@ -257,21 +256,20 @@ void AmbientBackendControllerImpl::StartToGetSettings(
client_id);
auto resource_request = CreateResourceRequest(request);
// |base::Unretained| is safe because this instance outlives
// |backdrop_url_loader_|.
DCHECK(!backdrop_url_loader_);
backdrop_url_loader_ = std::make_unique<BackdropURLLoader>();
backdrop_url_loader_->Start(
auto backdrop_url_loader = std::make_unique<BackdropURLLoader>();
auto* loader_ptr = backdrop_url_loader.get();
loader_ptr->Start(
std::move(resource_request), request.body, NO_TRAFFIC_ANNOTATION_YET,
base::BindOnce(&AmbientBackendControllerImpl::OnGetSettings,
base::Unretained(this), std::move(callback)));
weak_factory_.GetWeakPtr(), std::move(callback),
std::move(backdrop_url_loader)));
}
void AmbientBackendControllerImpl::OnGetSettings(
GetSettingsCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response) {
DCHECK(backdrop_url_loader_);
backdrop_url_loader_.reset();
DCHECK(backdrop_url_loader);
int topic_source = BackdropClientConfig::ParseGetSettingsResponse(*response);
if (topic_source == -1)
......@@ -296,21 +294,20 @@ void AmbientBackendControllerImpl::StartToUpdateSettings(
gaia_id, access_token, client_id, static_cast<int>(topic_source));
auto resource_request = CreateResourceRequest(request);
// |base::Unretained| is safe because this instance outlives
// |backdrop_url_loader_|.
DCHECK(!backdrop_url_loader_);
backdrop_url_loader_ = std::make_unique<BackdropURLLoader>();
backdrop_url_loader_->Start(
auto backdrop_url_loader = std::make_unique<BackdropURLLoader>();
auto* loader_ptr = backdrop_url_loader.get();
loader_ptr->Start(
std::move(resource_request), request.body, NO_TRAFFIC_ANNOTATION_YET,
base::BindOnce(&AmbientBackendControllerImpl::OnUpdateSettings,
base::Unretained(this), std::move(callback)));
weak_factory_.GetWeakPtr(), std::move(callback),
std::move(backdrop_url_loader)));
}
void AmbientBackendControllerImpl::OnUpdateSettings(
UpdateSettingsCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response) {
DCHECK(backdrop_url_loader_);
backdrop_url_loader_.reset();
DCHECK(backdrop_url_loader);
const bool success =
BackdropClientConfig::ParseUpdateSettingsResponse(*response);
......
......@@ -40,14 +40,17 @@ class AmbientBackendControllerImpl : public AmbientBackendController {
const std::string& gaia_id,
const std::string& access_token);
void OnScreenUpdateInfoFetched(OnScreenUpdateInfoFetchedCallback callback,
std::unique_ptr<std::string> response);
void OnScreenUpdateInfoFetched(
OnScreenUpdateInfoFetchedCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response);
void StartToGetSettings(GetSettingsCallback callback,
const std::string& gaia_id,
const std::string& access_token);
void OnGetSettings(GetSettingsCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response);
void StartToUpdateSettings(AmbientModeTopicSource topic_source,
......@@ -56,11 +59,9 @@ class AmbientBackendControllerImpl : public AmbientBackendController {
const std::string& access_token);
void OnUpdateSettings(UpdateSettingsCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response);
// The url loader for the Backdrop service request.
std::unique_ptr<BackdropURLLoader> backdrop_url_loader_;
BackdropClientConfig backdrop_client_config_;
base::WeakPtrFactory<AmbientBackendControllerImpl> weak_factory_{this};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment