Commit 6ea3c2a1 authored by pavely@chromium.org's avatar pavely@chromium.org

Cleanup changes in gcm_network_channel

No functionality change, just moving things around.
- Move functions so that it is more convenient to read .cc file top to bottom.
- replace ANDROID with OS_ANDROID. Looking at other places in the code OS_ANDROID is much more popular way to check for android.

R=rlarocque@chromium.org

Review URL: https://codereview.chromium.org/217653002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@260357 0039d316-1c4b-4281-b951-d872f2087c98
parent 90a98785
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "base/sha1.h" #include "base/sha1.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#if !defined(ANDROID) #if !defined(OS_ANDROID)
// channel_common.proto defines ANDROID constant that conflicts with Android // channel_common.proto defines ANDROID constant that conflicts with Android
// build. At the same time TiclInvalidationService is not used on Android so it // build. At the same time TiclInvalidationService is not used on Android so it
// is safe to exclude these protos from Android build. // is safe to exclude these protos from Android build.
...@@ -100,53 +100,6 @@ void RecordOutgoingMessageStatus(OutgoingMessageStatus status) { ...@@ -100,53 +100,6 @@ void RecordOutgoingMessageStatus(OutgoingMessageStatus status) {
} // namespace } // namespace
GCMNetworkChannelDiagnostic::GCMNetworkChannelDiagnostic(
GCMNetworkChannel* parent)
: parent_(parent),
last_message_empty_echo_token_(false),
last_post_response_code_(0),
registration_result_(gcm::GCMClient::UNKNOWN_ERROR),
sent_messages_count_(0) {}
scoped_ptr<base::DictionaryValue>
GCMNetworkChannelDiagnostic::CollectDebugData() const {
scoped_ptr<base::DictionaryValue> status(new base::DictionaryValue);
status->SetString("GCMNetworkChannel.Channel", "GCM");
std::string reg_id_hash = base::SHA1HashString(registration_id_);
status->SetString("GCMNetworkChannel.HashedRegistrationID",
base::HexEncode(reg_id_hash.c_str(), reg_id_hash.size()));
status->SetString("GCMNetworkChannel.RegistrationResult",
GCMClientResultToString(registration_result_));
status->SetBoolean("GCMNetworkChannel.HadLastMessageEmptyEchoToken",
last_message_empty_echo_token_);
status->SetString(
"GCMNetworkChannel.LastMessageReceivedTime",
base::TimeFormatShortDateAndTime(last_message_received_time_));
status->SetInteger("GCMNetworkChannel.LastPostResponseCode",
last_post_response_code_);
status->SetInteger("GCMNetworkChannel.SentMessages", sent_messages_count_);
status->SetInteger("GCMNetworkChannel.ReceivedMessages",
parent_->GetReceivedMessagesCount());
return status.Pass();
}
std::string GCMNetworkChannelDiagnostic::GCMClientResultToString(
const gcm::GCMClient::Result result) const {
#define ENUM_CASE(x) case x: return #x; break;
switch (result) {
ENUM_CASE(gcm::GCMClient::SUCCESS);
ENUM_CASE(gcm::GCMClient::NETWORK_ERROR);
ENUM_CASE(gcm::GCMClient::SERVER_ERROR);
ENUM_CASE(gcm::GCMClient::TTL_EXCEEDED);
ENUM_CASE(gcm::GCMClient::UNKNOWN_ERROR);
ENUM_CASE(gcm::GCMClient::NOT_SIGNED_IN);
ENUM_CASE(gcm::GCMClient::INVALID_PARAMETER);
ENUM_CASE(gcm::GCMClient::ASYNC_OPERATION_PENDING);
}
NOTREACHED();
return "";
}
GCMNetworkChannel::GCMNetworkChannel( GCMNetworkChannel::GCMNetworkChannel(
scoped_refptr<net::URLRequestContextGetter> request_context_getter, scoped_refptr<net::URLRequestContextGetter> request_context_getter,
scoped_ptr<GCMNetworkChannelDelegate> delegate) scoped_ptr<GCMNetworkChannelDelegate> delegate)
...@@ -164,22 +117,6 @@ GCMNetworkChannel::~GCMNetworkChannel() { ...@@ -164,22 +117,6 @@ GCMNetworkChannel::~GCMNetworkChannel() {
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
} }
void GCMNetworkChannel::UpdateCredentials(
const std::string& email,
const std::string& token) {
// Do nothing. We get access token by requesting it for every message.
}
void GCMNetworkChannel::RequestDetailedStatus(
base::Callback<void(const base::DictionaryValue&)> callback) {
callback.Run(*diagnostic_info_.CollectDebugData());
}
void GCMNetworkChannel::ResetRegisterBackoffEntryForTest(
const net::BackoffEntry::Policy* policy) {
register_backoff_entry_.reset(new net::BackoffEntry(policy));
}
void GCMNetworkChannel::Register() { void GCMNetworkChannel::Register() {
delegate_->Register(base::Bind(&GCMNetworkChannel::OnRegisterComplete, delegate_->Register(base::Bind(&GCMNetworkChannel::OnRegisterComplete,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
...@@ -235,13 +172,6 @@ void GCMNetworkChannel::SendMessage(const std::string& message) { ...@@ -235,13 +172,6 @@ void GCMNetworkChannel::SendMessage(const std::string& message) {
} }
} }
void GCMNetworkChannel::SetMessageReceiver(
invalidation::MessageCallback* incoming_receiver) {
delegate_->SetMessageReceiver(base::Bind(
&GCMNetworkChannel::OnIncomingMessage, weak_factory_.GetWeakPtr()));
SyncNetworkChannel::SetMessageReceiver(incoming_receiver);
}
void GCMNetworkChannel::RequestAccessToken() { void GCMNetworkChannel::RequestAccessToken() {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
delegate_->RequestToken(base::Bind(&GCMNetworkChannel::OnGetTokenComplete, delegate_->RequestToken(base::Bind(&GCMNetworkChannel::OnGetTokenComplete,
...@@ -290,9 +220,39 @@ void GCMNetworkChannel::OnGetTokenComplete( ...@@ -290,9 +220,39 @@ void GCMNetworkChannel::OnGetTokenComplete(
cached_message_.clear(); cached_message_.clear();
} }
void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(fetcher_, source);
// Free fetcher at the end of function.
scoped_ptr<net::URLFetcher> fetcher = fetcher_.Pass();
net::URLRequestStatus status = fetcher->GetStatus();
diagnostic_info_.last_post_response_code_ =
status.is_success() ? source->GetResponseCode() : status.error();
if (status.is_success() &&
fetcher->GetResponseCode() == net::HTTP_UNAUTHORIZED) {
DVLOG(1) << "URLFetcher failure: HTTP_UNAUTHORIZED";
delegate_->InvalidateToken(access_token_);
}
if (!status.is_success() ||
(fetcher->GetResponseCode() != net::HTTP_OK &&
fetcher->GetResponseCode() != net::HTTP_NO_CONTENT)) {
DVLOG(1) << "URLFetcher failure";
RecordOutgoingMessageStatus(POST_FAILURE);
NotifyStateChange(TRANSIENT_INVALIDATION_ERROR);
return;
}
RecordOutgoingMessageStatus(OUTGOING_MESSAGE_SUCCESS);
NotifyStateChange(INVALIDATIONS_ENABLED);
DVLOG(2) << "URLFetcher success";
}
void GCMNetworkChannel::OnIncomingMessage(const std::string& message, void GCMNetworkChannel::OnIncomingMessage(const std::string& message,
const std::string& echo_token) { const std::string& echo_token) {
#if !defined(ANDROID) #if !defined(OS_ANDROID)
if (!echo_token.empty()) if (!echo_token.empty())
echo_token_ = echo_token; echo_token_ = echo_token;
diagnostic_info_.last_message_empty_echo_token_ = echo_token.empty(); diagnostic_info_.last_message_empty_echo_token_ = echo_token.empty();
...@@ -322,36 +282,6 @@ void GCMNetworkChannel::OnIncomingMessage(const std::string& message, ...@@ -322,36 +282,6 @@ void GCMNetworkChannel::OnIncomingMessage(const std::string& message,
#endif #endif
} }
void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(fetcher_, source);
// Free fetcher at the end of function.
scoped_ptr<net::URLFetcher> fetcher = fetcher_.Pass();
net::URLRequestStatus status = fetcher->GetStatus();
diagnostic_info_.last_post_response_code_ =
status.is_success() ? source->GetResponseCode() : status.error();
if (status.is_success() &&
fetcher->GetResponseCode() == net::HTTP_UNAUTHORIZED) {
DVLOG(1) << "URLFetcher failure: HTTP_UNAUTHORIZED";
delegate_->InvalidateToken(access_token_);
}
if (!status.is_success() ||
(fetcher->GetResponseCode() != net::HTTP_OK &&
fetcher->GetResponseCode() != net::HTTP_NO_CONTENT)) {
DVLOG(1) << "URLFetcher failure";
RecordOutgoingMessageStatus(POST_FAILURE);
NotifyStateChange(TRANSIENT_INVALIDATION_ERROR);
return;
}
RecordOutgoingMessageStatus(OUTGOING_MESSAGE_SUCCESS);
NotifyStateChange(INVALIDATIONS_ENABLED);
DVLOG(2) << "URLFetcher success";
}
void GCMNetworkChannel::OnNetworkChanged( void GCMNetworkChannel::OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType connection_type) { net::NetworkChangeNotifier::ConnectionType connection_type) {
// Network connection is restored. Let's notify cacheinvalidations so it has // Network connection is restored. Let's notify cacheinvalidations so it has
...@@ -363,7 +293,7 @@ void GCMNetworkChannel::OnNetworkChanged( ...@@ -363,7 +293,7 @@ void GCMNetworkChannel::OnNetworkChanged(
GURL GCMNetworkChannel::BuildUrl(const std::string& registration_id) { GURL GCMNetworkChannel::BuildUrl(const std::string& registration_id) {
DCHECK(!registration_id.empty()); DCHECK(!registration_id.empty());
#if !defined(ANDROID) #if !defined(OS_ANDROID)
ipc::invalidation::EndpointId endpoint_id; ipc::invalidation::EndpointId endpoint_id;
endpoint_id.set_c2dm_registration_id(registration_id); endpoint_id.set_c2dm_registration_id(registration_id);
endpoint_id.set_client_key(std::string()); endpoint_id.set_client_key(std::string());
...@@ -418,4 +348,73 @@ bool GCMNetworkChannel::Base64DecodeURLSafe(const std::string& input, ...@@ -418,4 +348,73 @@ bool GCMNetworkChannel::Base64DecodeURLSafe(const std::string& input,
return base::Base64Decode(padded_input, output); return base::Base64Decode(padded_input, output);
} }
void GCMNetworkChannel::SetMessageReceiver(
invalidation::MessageCallback* incoming_receiver) {
delegate_->SetMessageReceiver(base::Bind(
&GCMNetworkChannel::OnIncomingMessage, weak_factory_.GetWeakPtr()));
SyncNetworkChannel::SetMessageReceiver(incoming_receiver);
}
void GCMNetworkChannel::RequestDetailedStatus(
base::Callback<void(const base::DictionaryValue&)> callback) {
callback.Run(*diagnostic_info_.CollectDebugData());
}
void GCMNetworkChannel::UpdateCredentials(const std::string& email,
const std::string& token) {
// Do nothing. We get access token by requesting it for every message.
}
void GCMNetworkChannel::ResetRegisterBackoffEntryForTest(
const net::BackoffEntry::Policy* policy) {
register_backoff_entry_.reset(new net::BackoffEntry(policy));
}
GCMNetworkChannelDiagnostic::GCMNetworkChannelDiagnostic(
GCMNetworkChannel* parent)
: parent_(parent),
last_message_empty_echo_token_(false),
last_post_response_code_(0),
registration_result_(gcm::GCMClient::UNKNOWN_ERROR),
sent_messages_count_(0) {}
scoped_ptr<base::DictionaryValue>
GCMNetworkChannelDiagnostic::CollectDebugData() const {
scoped_ptr<base::DictionaryValue> status(new base::DictionaryValue);
status->SetString("GCMNetworkChannel.Channel", "GCM");
std::string reg_id_hash = base::SHA1HashString(registration_id_);
status->SetString("GCMNetworkChannel.HashedRegistrationID",
base::HexEncode(reg_id_hash.c_str(), reg_id_hash.size()));
status->SetString("GCMNetworkChannel.RegistrationResult",
GCMClientResultToString(registration_result_));
status->SetBoolean("GCMNetworkChannel.HadLastMessageEmptyEchoToken",
last_message_empty_echo_token_);
status->SetString(
"GCMNetworkChannel.LastMessageReceivedTime",
base::TimeFormatShortDateAndTime(last_message_received_time_));
status->SetInteger("GCMNetworkChannel.LastPostResponseCode",
last_post_response_code_);
status->SetInteger("GCMNetworkChannel.SentMessages", sent_messages_count_);
status->SetInteger("GCMNetworkChannel.ReceivedMessages",
parent_->GetReceivedMessagesCount());
return status.Pass();
}
std::string GCMNetworkChannelDiagnostic::GCMClientResultToString(
const gcm::GCMClient::Result result) const {
#define ENUM_CASE(x) case x: return #x; break;
switch (result) {
ENUM_CASE(gcm::GCMClient::SUCCESS);
ENUM_CASE(gcm::GCMClient::NETWORK_ERROR);
ENUM_CASE(gcm::GCMClient::SERVER_ERROR);
ENUM_CASE(gcm::GCMClient::TTL_EXCEEDED);
ENUM_CASE(gcm::GCMClient::UNKNOWN_ERROR);
ENUM_CASE(gcm::GCMClient::NOT_SIGNED_IN);
ENUM_CASE(gcm::GCMClient::INVALID_PARAMETER);
ENUM_CASE(gcm::GCMClient::ASYNC_OPERATION_PENDING);
}
NOTREACHED();
return "";
}
} // namespace syncer } // namespace syncer
...@@ -379,19 +379,6 @@ TEST_F(GCMNetworkChannelTest, RequestTokenNeverCompletes) { ...@@ -379,19 +379,6 @@ TEST_F(GCMNetworkChannelTest, RequestTokenNeverCompletes) {
EXPECT_FALSE(delegate()->request_token_callback.is_null()); EXPECT_FALSE(delegate()->request_token_callback.is_null());
} }
#if !defined(ANDROID)
TEST_F(GCMNetworkChannelTest, BuildUrl) {
GURL url = BuildUrl("registration.id");
EXPECT_TRUE(url.SchemeIsHTTPOrHTTPS());
EXPECT_FALSE(url.host().empty());
EXPECT_FALSE(url.path().empty());
std::vector<std::string> parts;
Tokenize(url.path(), "/", &parts);
std::string buffer;
EXPECT_TRUE(Base64DecodeURLSafe(parts[parts.size() - 1], &buffer));
}
#endif
TEST_F(GCMNetworkChannelTest, Base64EncodeDecode) { TEST_F(GCMNetworkChannelTest, Base64EncodeDecode) {
std::string input; std::string input;
std::string plain; std::string plain;
...@@ -450,7 +437,18 @@ TEST_F(GCMNetworkChannelTest, TransientError) { ...@@ -450,7 +437,18 @@ TEST_F(GCMNetworkChannelTest, TransientError) {
EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state()); EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state());
} }
#if !defined(ANDROID) #if !defined(OS_ANDROID)
TEST_F(GCMNetworkChannelTest, BuildUrl) {
GURL url = BuildUrl("registration.id");
EXPECT_TRUE(url.SchemeIsHTTPOrHTTPS());
EXPECT_FALSE(url.host().empty());
EXPECT_FALSE(url.path().empty());
std::vector<std::string> parts;
Tokenize(url.path(), "/", &parts);
std::string buffer;
EXPECT_TRUE(Base64DecodeURLSafe(parts[parts.size() - 1], &buffer));
}
TEST_F(GCMNetworkChannelTest, EchoToken) { TEST_F(GCMNetworkChannelTest, EchoToken) {
url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"), url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"),
std::string(), std::string(),
......
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