forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Upboarding]: Setup for TileInfoFetcher.
- Created TileInfoFetcher interface. - Implemented main logic on TileInfoFetcherImpl, need to nail down the error response and other details. - TODO: Add unittests. Bug: 1063996 Change-Id: I82c8c22c6ba56f16bb0f483ec78ff799b87e7702 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2115887 Reviewed-by: Xing Liu <xingliu@chromium.org> Commit-Queue: Hesen Zhang <hesen@chromium.org> Cr-Commit-Position: refs/heads/master@{#753066}
- Loading branch information
Hesen Zhang
authored and
Commit Bot
committed
Mar 25, 2020
1 parent
29126a5
commit 5991112
Showing
5 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
chrome/browser/upboarding/query_tiles/internal/query_tile_types.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_QUERY_TILE_TYPES_H_ | ||
#define CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_QUERY_TILE_TYPES_H_ | ||
|
||
enum class TileInfoRequestStatus { | ||
// Initial status, request is not sent. | ||
kInit = 0, | ||
// Request completed successfully. | ||
kSuccess = 1, | ||
// Request failed. | ||
kFailure = 2, | ||
// Max value. | ||
kMaxValue = kFailure, | ||
}; | ||
|
||
#endif // CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_QUERY_TILE_TYPES_H_ |
117 changes: 117 additions & 0 deletions
117
chrome/browser/upboarding/query_tiles/internal/tile_info_fetcher.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/upboarding/query_tiles/internal/tile_info_fetcher.h" | ||
|
||
#include <utility> | ||
|
||
#include "net/base/url_util.h" | ||
#include "net/http/http_request_headers.h" | ||
#include "net/http/http_status_code.h" | ||
#include "net/traffic_annotation/network_traffic_annotation.h" | ||
#include "services/network/public/cpp/resource_request.h" | ||
#include "services/network/public/cpp/shared_url_loader_factory.h" | ||
#include "services/network/public/cpp/simple_url_loader.h" | ||
|
||
namespace upboarding { | ||
namespace { | ||
|
||
class TileInfoFetcherImpl : public TileInfoFetcher { | ||
public: | ||
TileInfoFetcherImpl( | ||
const GURL& url, | ||
const std::string& locale, | ||
const std::string& accept_languages, | ||
const std::string& api_key, | ||
const net::NetworkTrafficAnnotationTag& traffic_annotation, | ||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, | ||
FinishedCallback callback) | ||
: url_loader_factory_(url_loader_factory), | ||
callback_(std::move(callback)) { | ||
tile_info_request_status_ = TileInfoRequestStatus::kInit; | ||
// Start fetching. | ||
auto resource_request = | ||
BuildGetRequest(url, locale, accept_languages, api_key); | ||
url_loader_ = network::SimpleURLLoader::Create(std::move(resource_request), | ||
traffic_annotation); | ||
|
||
url_loader_->SetOnResponseStartedCallback( | ||
base::BindRepeating(&TileInfoFetcherImpl::OnResponseStarted, | ||
weak_ptr_factory_.GetWeakPtr())); | ||
// TODO(hesen): Estimate max size of response then replace to | ||
// DownloadToString method. | ||
url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie( | ||
url_loader_factory_.get(), | ||
base::BindOnce(&TileInfoFetcherImpl::OnDownloadComplete, | ||
weak_ptr_factory_.GetWeakPtr())); | ||
} | ||
|
||
private: | ||
// Build the request to get tile info. | ||
std::unique_ptr<network::ResourceRequest> BuildGetRequest( | ||
const GURL& url, | ||
const std::string& locale, | ||
const std::string& accept_languages, | ||
const std::string& api_key) { | ||
auto request = std::make_unique<network::ResourceRequest>(); | ||
request->method = net::HttpRequestHeaders::kGetMethod; | ||
request->headers.SetHeader("x-goog-api-key", api_key); | ||
if (!accept_languages.empty()) | ||
request->headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage, | ||
accept_languages); | ||
return request; | ||
} | ||
|
||
// Called when start receiving HTTP response. | ||
void OnResponseStarted(const GURL& final_url, | ||
const network::mojom::URLResponseHead& response_head) { | ||
int response_code = -1; | ||
if (response_head.headers) | ||
response_code = response_head.headers->response_code(); | ||
|
||
// TODO(hesen): Handle more possible status, and record status to UMA. | ||
if (response_code == -1 || (response_code < 200 || response_code > 299)) | ||
tile_info_request_status_ = TileInfoRequestStatus::kFailure; | ||
} | ||
|
||
// Called after receiving HTTP response. Processes the response code. | ||
void OnDownloadComplete(std::unique_ptr<std::string> response_body) { | ||
std::move(callback_).Run(tile_info_request_status_, | ||
std::move(response_body)); | ||
} | ||
|
||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; | ||
|
||
// Simple URL loader to fetch proto from network. | ||
std::unique_ptr<network::SimpleURLLoader> url_loader_; | ||
|
||
// Callback to be executed after fetching is done. | ||
FinishedCallback callback_; | ||
|
||
// Status of the tile info request. | ||
TileInfoRequestStatus tile_info_request_status_; | ||
|
||
base::WeakPtrFactory<TileInfoFetcherImpl> weak_ptr_factory_{this}; | ||
}; | ||
|
||
} // namespace | ||
|
||
// static | ||
std::unique_ptr<TileInfoFetcher> TileInfoFetcher::CreateAndFetchForTileInfo( | ||
const GURL& url, | ||
const std::string& locale, | ||
const std::string& accept_languages, | ||
const std::string& api_key, | ||
const net::NetworkTrafficAnnotationTag& traffic_annotation, | ||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, | ||
FinishedCallback callback) { | ||
return std::make_unique<TileInfoFetcherImpl>( | ||
url, locale, accept_languages, api_key, traffic_annotation, | ||
url_loader_factory, std::move(callback)); | ||
} | ||
|
||
TileInfoFetcher::TileInfoFetcher() = default; | ||
TileInfoFetcher::~TileInfoFetcher() = default; | ||
|
||
} // namespace upboarding |
57 changes: 57 additions & 0 deletions
57
chrome/browser/upboarding/query_tiles/internal/tile_info_fetcher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_TILE_INFO_FETCHER_H_ | ||
#define CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_TILE_INFO_FETCHER_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "base/callback.h" | ||
#include "base/macros.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "chrome/browser/upboarding/query_tiles/internal/query_tile_types.h" | ||
#include "net/base/backoff_entry.h" | ||
#include "url/gurl.h" | ||
|
||
namespace network { | ||
class SharedURLLoaderFactory; | ||
} // namespace network | ||
|
||
namespace net { | ||
struct NetworkTrafficAnnotationTag; | ||
} // namespace net | ||
|
||
namespace upboarding { | ||
|
||
class TileInfoFetcher { | ||
public: | ||
// Called after the fetch task is done, |status| and serialized response | ||
// |data| will be returned. Invoked with |nullptr| if status is not success. | ||
using FinishedCallback = base::OnceCallback<void( | ||
TileInfoRequestStatus status, | ||
const std::unique_ptr<std::string> response_body)>; | ||
|
||
// Method to create a fetcher and start the fetch task immediately. | ||
static std::unique_ptr<TileInfoFetcher> CreateAndFetchForTileInfo( | ||
const GURL& url, | ||
const std::string& locale, | ||
const std::string& accept_languages, | ||
const std::string& api_key, | ||
const net::NetworkTrafficAnnotationTag& traffic_annotation, | ||
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, | ||
FinishedCallback callback); | ||
|
||
virtual ~TileInfoFetcher(); | ||
|
||
TileInfoFetcher(const TileInfoFetcher& other) = delete; | ||
TileInfoFetcher& operator=(const TileInfoFetcher& other) = delete; | ||
|
||
protected: | ||
TileInfoFetcher(); | ||
}; | ||
|
||
} // namespace upboarding | ||
|
||
#endif // CHROME_BROWSER_UPBOARDING_QUERY_TILES_INTERNAL_TILE_INFO_FETCHER_H_ |
19 changes: 19 additions & 0 deletions
19
chrome/browser/upboarding/query_tiles/internal/tile_info_fetcher_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/upboarding/query_tiles/internal/tile_info_fetcher.h" | ||
#include "base/test/task_environment.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
|
||
namespace upboarding { | ||
class TileInfoFetcherTest : public testing::Test { | ||
public: | ||
TileInfoFetcherTest(); | ||
~TileInfoFetcherTest() override = default; | ||
|
||
TileInfoFetcherTest(const TileInfoFetcherTest& other) = delete; | ||
TileInfoFetcherTest& operator=(const TileInfoFetcherTest& other) = delete; | ||
}; | ||
|
||
} // namespace upboarding |