Skip to content

Commit

Permalink
[google_apis] Use base::Contains() instead of std::find() in //g_apis
Browse files Browse the repository at this point in the history
Use base::Contains() instead of std::find() where appropriate in
//google_apis.

Bug: 561800
Change-Id: I481ced191d9f838591838f5eaea1695c169203ba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4778037
Reviewed-by: David Roger <droger@chromium.org>
Commit-Queue: Ho Cheung <uioptt24@gmail.com>
Cr-Commit-Position: refs/heads/main@{#1184063}
  • Loading branch information
gz83 authored and Chromium LUCI CQ committed Aug 16, 2023
1 parent 1c41f4e commit f4e0fab
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 47 deletions.
5 changes: 3 additions & 2 deletions google_apis/drive/drive_api_requests_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <memory>
#include <utility>

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
Expand Down Expand Up @@ -277,7 +278,7 @@ class DriveApiRequestsTest : public testing::Test {
std::unique_ptr<net::test_server::HttpResponse> HandleChildrenDeleteRequest(
const net::test_server::HttpRequest& request) {
if (request.method != net::test_server::METHOD_DELETE ||
request.relative_url.find("/children/") == std::string::npos) {
!base::Contains(request.relative_url, "/children/")) {
// The request is not the "Children: delete" request. Delegate the
// processing to the next handler.
return nullptr;
Expand Down Expand Up @@ -315,7 +316,7 @@ class DriveApiRequestsTest : public testing::Test {
std::unique_ptr<net::test_server::HttpResponse> HandleDeleteRequest(
const net::test_server::HttpRequest& request) {
if (request.method != net::test_server::METHOD_DELETE ||
request.relative_url.find("/files/") == std::string::npos) {
!base::Contains(request.relative_url, "/files/")) {
// The file is not file deletion request. Delegate the processing to the
// next handler.
return nullptr;
Expand Down
7 changes: 4 additions & 3 deletions google_apis/drive/drive_base_requests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <memory>
#include <utility>

#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
Expand Down Expand Up @@ -127,8 +128,8 @@ void GenerateMultipartBody(MultipartType multipart_type,
while (true) {
boundary = net::GenerateMimeMultipartBoundary();
bool conflict_with_content = false;
for (auto& part : parts) {
if (part.data.find(boundary, 0) != std::string::npos) {
for (const auto& part : parts) {
if (base::Contains(part.data, boundary)) {
conflict_with_content = true;
break;
}
Expand All @@ -152,7 +153,7 @@ void GenerateMultipartBody(MultipartType multipart_type,
output->data.clear();
if (data_offset)
data_offset->clear();
for (auto& part : parts) {
for (const auto& part : parts) {
output->data.append(base::StringPrintf(
kMultipartItemHeaderFormat, boundary.c_str(), part.type.c_str()));
if (data_offset)
Expand Down
5 changes: 3 additions & 2 deletions google_apis/gaia/core_account_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#include "google_apis/gaia/core_account_id.h"

#include "base/check.h"
#include "base/containers/contains.h"
#include "google_apis/gaia/gaia_auth_util.h"

namespace {
// Returns whether the string looks like an email (the test is
// crude an only checks whether it includes an '@').
bool IsEmailString(const std::string& string) {
return string.find('@') != std::string::npos;
return base::Contains(string, '@');
}
} // anonymous namespace

Expand Down Expand Up @@ -99,4 +100,4 @@ std::vector<std::string> ToStringList(
for (const auto& account_id : account_ids)
account_ids_string.push_back(account_id.ToString());
return account_ids_string;
}
}
2 changes: 1 addition & 1 deletion google_apis/gaia/fake_gaia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ void FakeGaia::HandleEmbeddedLookupAccountLookup(
std::string email;
const bool is_saml =
GetQueryParameter(request.content, "identifier", &email) &&
saml_account_idp_map_.find(email) != saml_account_idp_map_.end();
base::Contains(saml_account_idp_map_, email);

if (!is_saml)
return;
Expand Down
11 changes: 6 additions & 5 deletions google_apis/gaia/gaia_auth_fetcher_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/memory/ref_counted.h"
Expand Down Expand Up @@ -270,7 +271,7 @@ TEST_F(GaiaAuthFetcherTest, StartAuthCodeForOAuth2TokenExchangeSuccess) {
EXPECT_EQ(google_apis::GetOmitCredentialsModeForGaiaRequests(),
received_requests_.at(0).credentials_mode);
std::string body = GetRequestBodyAsString(&received_requests_.at(0));
EXPECT_EQ(std::string::npos, body.find("device_type=chrome"));
EXPECT_FALSE(base::Contains(body, "device_type=chrome"));
EXPECT_TRUE(auth.HasPendingFetch());

auth.TestOnURLLoadCompleteInternal(net::OK, net::HTTP_OK,
Expand All @@ -289,8 +290,8 @@ TEST_F(GaiaAuthFetcherTest, StartAuthCodeForOAuth2TokenExchangeDeviceId) {
EXPECT_EQ(google_apis::GetOmitCredentialsModeForGaiaRequests(),
received_requests_.at(0).credentials_mode);
std::string body = GetRequestBodyAsString(&received_requests_.at(0));
EXPECT_NE(std::string::npos, body.find("device_type=chrome"));
EXPECT_NE(std::string::npos, body.find("device_id=device_ABCDE_1"));
EXPECT_TRUE(base::Contains(body, "device_type=chrome"));
EXPECT_TRUE(base::Contains(body, "device_id=device_ABCDE_1"));
}

TEST_F(GaiaAuthFetcherTest,
Expand All @@ -308,8 +309,8 @@ TEST_F(GaiaAuthFetcherTest,
EXPECT_EQ(google_apis::GetOmitCredentialsModeForGaiaRequests(),
received_requests_.at(0).credentials_mode);
std::string body = GetRequestBodyAsString(&received_requests_.at(0));
EXPECT_NE(std::string::npos,
body.find("bound_token_registration_jwt=registration_jwt"));
EXPECT_TRUE(
base::Contains(body, "bound_token_registration_jwt=registration_jwt"));
EXPECT_TRUE(auth.HasPendingFetch());

auth.TestOnURLLoadCompleteInternal(net::OK, net::HTTP_OK,
Expand Down
3 changes: 2 additions & 1 deletion google_apis/gaia/gaia_auth_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>

#include "base/base64url.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ std::string SanitizeEmail(base::StringPiece email_address) {
std::string sanitized(email_address);

// Apply a default domain if necessary.
if (sanitized.find('@') == std::string::npos) {
if (!base::Contains(sanitized, '@')) {
sanitized += '@';
sanitized += kGmailDomain;
}
Expand Down
7 changes: 3 additions & 4 deletions google_apis/gaia/oauth2_access_token_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "google_apis/gaia/oauth2_access_token_manager.h"

#include "base/containers/contains.h"
#include "base/memory/ref_counted.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
Expand Down Expand Up @@ -38,17 +39,15 @@ class FakeOAuth2AccessTokenManagerDelegate
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
OAuth2AccessTokenConsumer* consumer) override {
EXPECT_NE(account_ids_to_refresh_tokens_.find(account_id),
account_ids_to_refresh_tokens_.end());
EXPECT_TRUE(base::Contains(account_ids_to_refresh_tokens_, account_id));
return GaiaAccessTokenFetcher::
CreateExchangeRefreshTokenForAccessTokenInstance(
consumer, url_loader_factory,
account_ids_to_refresh_tokens_[account_id]);
}

bool HasRefreshToken(const CoreAccountId& account_id) const override {
return account_ids_to_refresh_tokens_.find(account_id) !=
account_ids_to_refresh_tokens_.end();
return base::Contains(account_ids_to_refresh_tokens_, account_id);
}

scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory()
Expand Down
28 changes: 10 additions & 18 deletions google_apis/gcm/engine/gcm_store_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
Expand Down Expand Up @@ -297,11 +298,9 @@ TEST_F(GCMStoreImplTest, Registrations) {
LoadGCMStore(gcm_store.get(), &load_result);

ASSERT_EQ(2u, load_result->registrations.size());
ASSERT_TRUE(load_result->registrations.find(kAppName) !=
load_result->registrations.end());
EXPECT_TRUE(base::Contains(load_result->registrations, kAppName));
EXPECT_EQ(registration, load_result->registrations[kAppName]);
ASSERT_TRUE(load_result->registrations.find(kAppName2) !=
load_result->registrations.end());
EXPECT_TRUE(base::Contains(load_result->registrations, kAppName2));
EXPECT_EQ(registration2, load_result->registrations[kAppName2]);

gcm_store->RemoveRegistration(
Expand All @@ -313,8 +312,7 @@ TEST_F(GCMStoreImplTest, Registrations) {
LoadGCMStore(gcm_store.get(), &load_result);

ASSERT_EQ(1u, load_result->registrations.size());
ASSERT_TRUE(load_result->registrations.find(kAppName) !=
load_result->registrations.end());
EXPECT_TRUE(base::Contains(load_result->registrations, kAppName));
EXPECT_EQ(registration, load_result->registrations[kAppName]);
}

Expand Down Expand Up @@ -660,11 +658,9 @@ TEST_F(GCMStoreImplTest, HeartbeatInterval) {
LoadGCMStore(gcm_store.get(), &load_result);

EXPECT_EQ(2UL, load_result->heartbeat_intervals.size());
ASSERT_TRUE(load_result->heartbeat_intervals.find(scope1) !=
load_result->heartbeat_intervals.end());
EXPECT_TRUE(base::Contains(load_result->heartbeat_intervals, scope1));
EXPECT_EQ(heartbeat1, load_result->heartbeat_intervals[scope1]);
ASSERT_TRUE(load_result->heartbeat_intervals.find(scope2) !=
load_result->heartbeat_intervals.end());
EXPECT_TRUE(base::Contains(load_result->heartbeat_intervals, scope2));
EXPECT_EQ(heartbeat2, load_result->heartbeat_intervals[scope2]);

gcm_store->RemoveHeartbeatInterval(
Expand All @@ -676,8 +672,7 @@ TEST_F(GCMStoreImplTest, HeartbeatInterval) {
LoadGCMStore(gcm_store.get(), &load_result);

EXPECT_EQ(1UL, load_result->heartbeat_intervals.size());
ASSERT_TRUE(load_result->heartbeat_intervals.find(scope1) !=
load_result->heartbeat_intervals.end());
EXPECT_TRUE(base::Contains(load_result->heartbeat_intervals, scope1));
EXPECT_EQ(heartbeat1, load_result->heartbeat_intervals[scope1]);
}

Expand Down Expand Up @@ -766,10 +761,8 @@ TEST_F(GCMStoreImplTest, InstanceIDData) {
LoadGCMStore(gcm_store.get(), &load_result);

ASSERT_EQ(2u, load_result->instance_id_data.size());
ASSERT_TRUE(load_result->instance_id_data.find(kAppName) !=
load_result->instance_id_data.end());
ASSERT_TRUE(load_result->instance_id_data.find(kAppName2) !=
load_result->instance_id_data.end());
EXPECT_TRUE(base::Contains(load_result->instance_id_data, kAppName));
EXPECT_TRUE(base::Contains(load_result->instance_id_data, kAppName2));
EXPECT_EQ(instance_id_data, load_result->instance_id_data[kAppName]);
EXPECT_EQ(instance_id_data2, load_result->instance_id_data[kAppName2]);

Expand All @@ -782,8 +775,7 @@ TEST_F(GCMStoreImplTest, InstanceIDData) {
LoadGCMStore(gcm_store.get(), &load_result);

ASSERT_EQ(1u, load_result->instance_id_data.size());
ASSERT_TRUE(load_result->instance_id_data.find(kAppName2) !=
load_result->instance_id_data.end());
EXPECT_TRUE(base::Contains(load_result->instance_id_data, kAppName2));
EXPECT_EQ(instance_id_data2, load_result->instance_id_data[kAppName2]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "google_apis/gcm/engine/instance_id_delete_token_request_handler.h"

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "google_apis/gcm/base/gcm_util.h"
Expand Down Expand Up @@ -52,8 +53,9 @@ void InstanceIDDeleteTokenRequestHandler::BuildRequestBody(std::string* body){
UnregistrationRequest::Status
InstanceIDDeleteTokenRequestHandler::ParseResponse(
const std::string& response) {
if (response.find(kTokenPrefix) == std::string::npos)
if (!base::Contains(response, kTokenPrefix)) {
return UnregistrationRequest::RESPONSE_PARSING_FAILED;
}

return UnregistrationRequest::SUCCESS;
}
Expand Down
22 changes: 15 additions & 7 deletions google_apis/gcm/engine/registration_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <utility>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/histogram.h"
Expand Down Expand Up @@ -53,20 +54,27 @@ const char kTooManyRegistrations[] = "TOO_MANY_REGISTRATIONS";

// Gets correct status from the error message.
RegistrationRequest::Status GetStatusFromError(const std::string& error) {
if (error.find(kDeviceRegistrationError) != std::string::npos)
if (base::Contains(error, kDeviceRegistrationError)) {
return RegistrationRequest::DEVICE_REGISTRATION_ERROR;
if (error.find(kAuthenticationFailed) != std::string::npos)
}
if (base::Contains(error, kAuthenticationFailed)) {
return RegistrationRequest::AUTHENTICATION_FAILED;
if (error.find(kInvalidSender) != std::string::npos)
}
if (base::Contains(error, kInvalidSender)) {
return RegistrationRequest::INVALID_SENDER;
if (error.find(kInvalidParameters) != std::string::npos)
}
if (base::Contains(error, kInvalidParameters)) {
return RegistrationRequest::INVALID_PARAMETERS;
if (error.find(kInternalServerError) != std::string::npos)
}
if (base::Contains(error, kInternalServerError)) {
return RegistrationRequest::INTERNAL_SERVER_ERROR;
if (error.find(kQuotaExceeded) != std::string::npos)
}
if (base::Contains(error, kQuotaExceeded)) {
return RegistrationRequest::QUOTA_EXCEEDED;
if (error.find(kTooManyRegistrations) != std::string::npos)
}
if (base::Contains(error, kTooManyRegistrations)) {
return RegistrationRequest::TOO_MANY_REGISTRATIONS;
}
// Should not be reached, unless the server adds new error types.
return RegistrationRequest::UNKNOWN_ERROR;
}
Expand Down
10 changes: 7 additions & 3 deletions google_apis/gcm/engine/unregistration_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <utility>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/strings/escape.h"
Expand Down Expand Up @@ -47,12 +48,15 @@ const char kDeviceRegistrationError[] = "PHONE_REGISTRATION_ERROR";

// Gets correct status from the error message.
UnregistrationRequest::Status GetStatusFromError(const std::string& error) {
if (error.find(kInvalidParameters) != std::string::npos)
if (base::Contains(error, kInvalidParameters)) {
return UnregistrationRequest::INVALID_PARAMETERS;
if (error.find(kInternalServerError) != std::string::npos)
}
if (base::Contains(error, kInternalServerError)) {
return UnregistrationRequest::INTERNAL_SERVER_ERROR;
if (error.find(kDeviceRegistrationError) != std::string::npos)
}
if (base::Contains(error, kDeviceRegistrationError)) {
return UnregistrationRequest::DEVICE_REGISTRATION_ERROR;
}
// Should not be reached, unless the server adds new error types.
return UnregistrationRequest::UNKNOWN_ERROR;
}
Expand Down

0 comments on commit f4e0fab

Please sign in to comment.