Commit 5affd5cc authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Convert PrivetURLFetcher::TokenCallback to OnceCallback.

Also mark some PrivetJSONOperation::ResultCallback usage as
RepeatingCallback, as determined earlier in r515600.

Change-Id: I88e8389b6967dabaef036446814d1f70e1ab61e4
Reviewed-on: https://chromium-review.googlesource.com/804753
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521620}
parent b8b936ba
...@@ -61,7 +61,7 @@ class PrivetHTTPClient { ...@@ -61,7 +61,7 @@ class PrivetHTTPClient {
PrivetURLFetcher::Delegate* delegate) = 0; PrivetURLFetcher::Delegate* delegate) = 0;
virtual void RefreshPrivetToken( virtual void RefreshPrivetToken(
const PrivetURLFetcher::TokenCallback& token_callback) = 0; PrivetURLFetcher::TokenCallback token_callback) = 0;
}; };
class PrivetDataReadOperation { class PrivetDataReadOperation {
......
...@@ -218,8 +218,8 @@ void PrivetRegisterOperationImpl::OnParsedJson( ...@@ -218,8 +218,8 @@ void PrivetRegisterOperationImpl::OnParsedJson(
} }
void PrivetRegisterOperationImpl::OnNeedPrivetToken( void PrivetRegisterOperationImpl::OnNeedPrivetToken(
const PrivetURLFetcher::TokenCallback& callback) { PrivetURLFetcher::TokenCallback callback) {
privet_client_->RefreshPrivetToken(callback); privet_client_->RefreshPrivetToken(std::move(callback));
} }
void PrivetRegisterOperationImpl::SendRequest(const std::string& action) { void PrivetRegisterOperationImpl::SendRequest(const std::string& action) {
...@@ -297,9 +297,8 @@ void PrivetRegisterOperationImpl::OnPrivetInfoDone( ...@@ -297,9 +297,8 @@ void PrivetRegisterOperationImpl::OnPrivetInfoDone(
} }
void PrivetRegisterOperationImpl::StartInfoOperation() { void PrivetRegisterOperationImpl::StartInfoOperation() {
info_operation_ = privet_client_->CreateInfoOperation( info_operation_ = privet_client_->CreateInfoOperation(base::BindRepeating(
base::Bind(&PrivetRegisterOperationImpl::OnPrivetInfoDone, &PrivetRegisterOperationImpl::OnPrivetInfoDone, base::Unretained(this)));
base::Unretained(this)));
info_operation_->Start(); info_operation_->Start();
} }
...@@ -369,8 +368,8 @@ void PrivetJSONOperationImpl::OnParsedJson(PrivetURLFetcher* fetcher, ...@@ -369,8 +368,8 @@ void PrivetJSONOperationImpl::OnParsedJson(PrivetURLFetcher* fetcher,
} }
void PrivetJSONOperationImpl::OnNeedPrivetToken( void PrivetJSONOperationImpl::OnNeedPrivetToken(
const PrivetURLFetcher::TokenCallback& callback) { PrivetURLFetcher::TokenCallback callback) {
privet_client_->RefreshPrivetToken(callback); privet_client_->RefreshPrivetToken(std::move(callback));
} }
#if BUILDFLAG(ENABLE_PRINT_PREVIEW) #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
...@@ -395,8 +394,8 @@ void PrivetLocalPrintOperationImpl::Start() { ...@@ -395,8 +394,8 @@ void PrivetLocalPrintOperationImpl::Start() {
// We need to get the /info response so we can know which APIs are available. // We need to get the /info response so we can know which APIs are available.
// TODO(noamsml): Use cached info when available. // TODO(noamsml): Use cached info when available.
info_operation_ = privet_client_->CreateInfoOperation( info_operation_ = privet_client_->CreateInfoOperation(
base::Bind(&PrivetLocalPrintOperationImpl::OnPrivetInfoDone, base::BindRepeating(&PrivetLocalPrintOperationImpl::OnPrivetInfoDone,
base::Unretained(this))); base::Unretained(this)));
info_operation_->Start(); info_operation_->Start();
started_ = true; started_ = true;
...@@ -621,8 +620,8 @@ void PrivetLocalPrintOperationImpl::OnParsedJson( ...@@ -621,8 +620,8 @@ void PrivetLocalPrintOperationImpl::OnParsedJson(
} }
void PrivetLocalPrintOperationImpl::OnNeedPrivetToken( void PrivetLocalPrintOperationImpl::OnNeedPrivetToken(
const PrivetURLFetcher::TokenCallback& callback) { PrivetURLFetcher::TokenCallback callback) {
privet_client_->RefreshPrivetToken(callback); privet_client_->RefreshPrivetToken(std::move(callback));
} }
void PrivetLocalPrintOperationImpl::SetData( void PrivetLocalPrintOperationImpl::SetData(
...@@ -722,35 +721,32 @@ std::unique_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher( ...@@ -722,35 +721,32 @@ std::unique_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher(
} }
void PrivetHTTPClientImpl::RefreshPrivetToken( void PrivetHTTPClientImpl::RefreshPrivetToken(
const PrivetURLFetcher::TokenCallback& callback) { PrivetURLFetcher::TokenCallback callback) {
token_callbacks_.push_back(callback); token_callbacks_.push_back(std::move(callback));
if (!info_operation_) { if (info_operation_)
info_operation_ = CreateInfoOperation( return;
base::Bind(&PrivetHTTPClientImpl::OnPrivetInfoDone,
base::Unretained(this))); info_operation_ = CreateInfoOperation(base::BindRepeating(
info_operation_->Start(); &PrivetHTTPClientImpl::OnPrivetInfoDone, base::Unretained(this)));
} info_operation_->Start();
} }
void PrivetHTTPClientImpl::OnPrivetInfoDone( void PrivetHTTPClientImpl::OnPrivetInfoDone(
const base::DictionaryValue* value) { const base::DictionaryValue* value) {
info_operation_.reset(); info_operation_.reset();
std::string token;
// If this does not succeed, token will be empty, and an empty string // If this does not succeed, token will be empty, and an empty string
// is our sentinel value, since empty X-Privet-Tokens are not allowed. // is our sentinel value, since empty X-Privet-Tokens are not allowed.
if (value) { std::string token;
if (value)
value->GetString(kPrivetInfoKeyToken, &token); value->GetString(kPrivetInfoKeyToken, &token);
}
TokenCallbackVector token_callbacks; TokenCallbackVector token_callbacks;
token_callbacks_.swap(token_callbacks); token_callbacks_.swap(token_callbacks);
for (TokenCallbackVector::iterator i = token_callbacks.begin(); for (auto& callback : token_callbacks)
i != token_callbacks.end(); i++) { std::move(callback).Run(token);
i->Run(token);
}
} }
PrivetV1HTTPClientImpl::PrivetV1HTTPClientImpl( PrivetV1HTTPClientImpl::PrivetV1HTTPClientImpl(
......
...@@ -65,8 +65,7 @@ class PrivetRegisterOperationImpl ...@@ -65,8 +65,7 @@ class PrivetRegisterOperationImpl
const base::DictionaryValue& value, const base::DictionaryValue& value,
bool has_error) override; bool has_error) override;
void OnNeedPrivetToken( void OnNeedPrivetToken(PrivetURLFetcher::TokenCallback callback) override;
const PrivetURLFetcher::TokenCallback& callback) override;
PrivetHTTPClient* GetHTTPClient() override; PrivetHTTPClient* GetHTTPClient() override;
...@@ -131,8 +130,7 @@ class PrivetJSONOperationImpl : public PrivetJSONOperation, ...@@ -131,8 +130,7 @@ class PrivetJSONOperationImpl : public PrivetJSONOperation,
void OnParsedJson(PrivetURLFetcher* fetcher, void OnParsedJson(PrivetURLFetcher* fetcher,
const base::DictionaryValue& value, const base::DictionaryValue& value,
bool has_error) override; bool has_error) override;
void OnNeedPrivetToken( void OnNeedPrivetToken(PrivetURLFetcher::TokenCallback callback) override;
const PrivetURLFetcher::TokenCallback& callback) override;
private: private:
PrivetHTTPClient* privet_client_; PrivetHTTPClient* privet_client_;
...@@ -170,8 +168,7 @@ class PrivetLocalPrintOperationImpl ...@@ -170,8 +168,7 @@ class PrivetLocalPrintOperationImpl
void OnParsedJson(PrivetURLFetcher* fetcher, void OnParsedJson(PrivetURLFetcher* fetcher,
const base::DictionaryValue& value, const base::DictionaryValue& value,
bool has_error) override; bool has_error) override;
void OnNeedPrivetToken( void OnNeedPrivetToken(PrivetURLFetcher::TokenCallback callback) override;
const PrivetURLFetcher::TokenCallback& callback) override;
private: private:
typedef base::Callback<void(bool, const base::DictionaryValue* value)> typedef base::Callback<void(bool, const base::DictionaryValue* value)>
...@@ -239,10 +236,10 @@ class PrivetHTTPClientImpl : public PrivetHTTPClient { ...@@ -239,10 +236,10 @@ class PrivetHTTPClientImpl : public PrivetHTTPClient {
net::URLFetcher::RequestType request_type, net::URLFetcher::RequestType request_type,
PrivetURLFetcher::Delegate* delegate) override; PrivetURLFetcher::Delegate* delegate) override;
void RefreshPrivetToken( void RefreshPrivetToken(
const PrivetURLFetcher::TokenCallback& token_callback) override; PrivetURLFetcher::TokenCallback token_callback) override;
private: private:
typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector; using TokenCallbackVector = std::vector<PrivetURLFetcher::TokenCallback>;
void OnPrivetInfoDone(const base::DictionaryValue* value); void OnPrivetInfoDone(const base::DictionaryValue* value);
......
...@@ -1056,9 +1056,8 @@ class PrivetHttpWithServerTest : public ::testing::Test, ...@@ -1056,9 +1056,8 @@ class PrivetHttpWithServerTest : public ::testing::Test,
"test", server_->host_port_pair(), context_getter_); "test", server_->host_port_pair(), context_getter_);
} }
void OnNeedPrivetToken( void OnNeedPrivetToken(PrivetURLFetcher::TokenCallback callback) override {
const PrivetURLFetcher::TokenCallback& callback) override { std::move(callback).Run("abc");
callback.Run("abc");
} }
void OnError(int response_code, PrivetURLFetcher::ErrorType error) override { void OnError(int response_code, PrivetURLFetcher::ErrorType error) override {
......
...@@ -58,8 +58,7 @@ std::string MakeRangeHeader(int start, int end) { ...@@ -58,8 +58,7 @@ std::string MakeRangeHeader(int start, int end) {
} // namespace } // namespace
void PrivetURLFetcher::Delegate::OnNeedPrivetToken( void PrivetURLFetcher::Delegate::OnNeedPrivetToken(TokenCallback callback) {
const TokenCallback& callback) {
OnError(0, TOKEN_ERROR); OnError(0, TOKEN_ERROR);
} }
......
...@@ -38,7 +38,7 @@ class PrivetURLFetcher : public net::URLFetcherDelegate { ...@@ -38,7 +38,7 @@ class PrivetURLFetcher : public net::URLFetcherDelegate {
UNKNOWN_ERROR, UNKNOWN_ERROR,
}; };
typedef base::Callback<void(const std::string& /*token*/)> TokenCallback; using TokenCallback = base::OnceCallback<void(const std::string& /*token*/)>;
class Delegate { class Delegate {
public: public:
...@@ -46,7 +46,7 @@ class PrivetURLFetcher : public net::URLFetcherDelegate { ...@@ -46,7 +46,7 @@ class PrivetURLFetcher : public net::URLFetcherDelegate {
// If you do not implement this method for PrivetV1 callers, you will always // If you do not implement this method for PrivetV1 callers, you will always
// get a TOKEN_ERROR error when your token is invalid. // get a TOKEN_ERROR error when your token is invalid.
virtual void OnNeedPrivetToken(const TokenCallback& callback); virtual void OnNeedPrivetToken(TokenCallback callback);
// |response_code| is only needed for RESPONSE_CODE_ERROR. // |response_code| is only needed for RESPONSE_CODE_ERROR.
virtual void OnError(int response_code, ErrorType error) = 0; virtual void OnError(int response_code, ErrorType error) = 0;
......
...@@ -55,9 +55,7 @@ class MockPrivetURLFetcherDelegate : public PrivetURLFetcher::Delegate { ...@@ -55,9 +55,7 @@ class MockPrivetURLFetcherDelegate : public PrivetURLFetcher::Delegate {
MOCK_METHOD1(OnParsedJsonInternal, void(bool has_error)); MOCK_METHOD1(OnParsedJsonInternal, void(bool has_error));
virtual void OnNeedPrivetToken( virtual void OnNeedPrivetToken(PrivetURLFetcher::TokenCallback callback) {}
const PrivetURLFetcher::TokenCallback& callback) {
}
bool OnRawData(bool response_is_file, bool OnRawData(bool response_is_file,
const std::string& data, const std::string& data,
......
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