Commit 6a7e9d36 authored by Mark Pilgrim's avatar Mark Pilgrim Committed by Commit Bot

Migrate GCMNetworkChannel to SimpleURLLoader

TBR=bartfab@chromium.org

Bug: 844936
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;luci.chromium.try:linux_mojo;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I573698e0994b6dc6e70f0d17e6dcbbb83aa1a142
Reviewed-on: https://chromium-review.googlesource.com/1102725
Commit-Queue: Mark Pilgrim <pilgrim@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#571533}
parent 7da098a1
......@@ -17,6 +17,7 @@
#include "chrome/browser/chromeos/settings/device_identity_provider.h"
#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_content_client.h"
......@@ -33,6 +34,7 @@
#include "components/user_manager/user.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace policy {
......@@ -300,6 +302,12 @@ AffiliatedInvalidationServiceProviderImpl::FindConnectedInvalidationService() {
}
if (!device_invalidation_service_) {
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory;
if (g_browser_process->system_network_context_manager()) {
// system_network_context_manager() can be null during unit tests.
url_loader_factory = g_browser_process->system_network_context_manager()
->GetSharedURLLoaderFactory();
}
// If no other connected invalidation service was found and no device-global
// invalidation service exists, create one.
device_invalidation_service_.reset(
......@@ -310,7 +318,8 @@ AffiliatedInvalidationServiceProviderImpl::FindConnectedInvalidationService() {
std::unique_ptr<invalidation::TiclSettingsProvider>(
new TiclDeviceSettingsProvider),
g_browser_process->gcm_driver(),
g_browser_process->system_request_context()));
g_browser_process->system_request_context(),
std::move(url_loader_factory)));
device_invalidation_service_->Init(
std::unique_ptr<syncer::InvalidationStateTracker>(
new invalidation::InvalidatorStorage(
......
......@@ -29,7 +29,9 @@
#include "components/prefs/pref_registry.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/storage_partition.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#if defined(OS_ANDROID)
#include "components/invalidation/impl/invalidation_service_android.h"
......@@ -127,7 +129,9 @@ KeyedService* ProfileInvalidationProviderFactory::BuildServiceInstanceFor(
std::unique_ptr<TiclSettingsProvider>(
new TiclProfileSettingsProvider(profile->GetPrefs())),
gcm::GCMProfileServiceFactory::GetForProfile(profile)->driver(),
profile->GetRequestContext()));
profile->GetRequestContext(),
content::BrowserContext::GetDefaultStoragePartition(profile)
->GetURLLoaderFactoryForBrowserProcess()));
service->Init(std::unique_ptr<syncer::InvalidationStateTracker>(
new InvalidatorStorage(profile->GetPrefs())));
......
......@@ -35,6 +35,7 @@
#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
#include "net/net_buildflags.h"
#include "services/network/network_service.h"
#include "services/network/public/cpp/cross_thread_shared_url_loader_factory_info.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
......@@ -220,9 +221,8 @@ class SystemNetworkContextManager::URLLoaderFactoryForSystem
// SharedURLLoaderFactory implementation:
std::unique_ptr<network::SharedURLLoaderFactoryInfo> Clone() override {
NOTREACHED() << "This isn't supported. SharedURLLoaderFactory can only be"
" used on the UI thread.";
return nullptr;
return std::make_unique<network::CrossThreadSharedURLLoaderFactoryInfo>(
this);
}
void Shutdown() { manager_ = nullptr; }
......
......@@ -22,8 +22,9 @@
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.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"
#if !defined(OS_ANDROID)
// channel_common.proto defines ANDROID constant that conflicts with Android
......@@ -113,9 +114,9 @@ void RecordOutgoingMessageStatus(OutgoingMessageStatus status) {
} // namespace
GCMNetworkChannel::GCMNetworkChannel(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
std::unique_ptr<GCMNetworkChannelDelegate> delegate)
: request_context_getter_(request_context_getter),
: url_loader_factory_(std::move(url_loader_factory)),
delegate_(std::move(delegate)),
register_backoff_entry_(new net::BackoffEntry(&kRegisterBackoffPolicy)),
gcm_channel_online_(false),
......@@ -256,46 +257,55 @@ void GCMNetworkChannel::OnGetTokenComplete(
"features that depend on it. It makes sense to control top level "
"features that use InvalidationService."
})");
fetcher_ =
net::URLFetcher::Create(BuildUrl(registration_id_), net::URLFetcher::POST,
this, traffic_annotation);
data_use_measurement::DataUseUserData::AttachToFetcher(
fetcher_.get(), data_use_measurement::DataUseUserData::INVALIDATION);
fetcher_->SetRequestContext(request_context_getter_.get());
fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES);
const std::string auth_header("Authorization: Bearer " + access_token_);
fetcher_->AddExtraRequestHeader(auth_header);
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = BuildUrl(registration_id_);
resource_request->load_flags =
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES;
resource_request->method = "POST";
resource_request->headers.SetHeader(net::HttpRequestHeaders::kAuthorization,
"Bearer " + access_token_);
if (!echo_token_.empty()) {
const std::string echo_header("echo-token: " + echo_token_);
fetcher_->AddExtraRequestHeader(echo_header);
resource_request->headers.SetHeader("echo-token", echo_token_);
}
fetcher_->SetUploadData("application/x-protobuffer", cached_message_);
fetcher_->Start();
simple_url_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
simple_url_loader_->AttachStringForUpload(cached_message_,
"application/x-protobuffer");
// TODO(https://crbug.com/808498): Re-add data use measurement once
// SimpleURLLoader supports it.
// ID=data_use_measurement::DataUseUserData::INVALIDATION
simple_url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory_.get(),
base::BindOnce(&GCMNetworkChannel::OnSimpleLoaderComplete,
base::Unretained(this)));
// Clear message to prevent accidentally resending it in the future.
cached_message_.clear();
}
void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) {
void GCMNetworkChannel::OnSimpleLoaderComplete(
std::unique_ptr<std::string> response_body) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(fetcher_.get(), source);
// Free fetcher at the end of function.
std::unique_ptr<net::URLFetcher> fetcher = std::move(fetcher_);
net::URLRequestStatus status = fetcher->GetStatus();
int net_error = simple_url_loader_->NetError();
bool is_success = (net_error == net::OK);
int response_code = -1;
if (simple_url_loader_->ResponseInfo() &&
simple_url_loader_->ResponseInfo()->headers) {
response_code =
simple_url_loader_->ResponseInfo()->headers->response_code();
}
simple_url_loader_.reset();
diagnostic_info_.last_post_response_code_ =
status.is_success() ? source->GetResponseCode() : status.error();
(response_code / 100 != 2 || is_success) ? response_code : net_error;
if (status.is_success() &&
fetcher->GetResponseCode() == net::HTTP_UNAUTHORIZED) {
DVLOG(1) << "URLFetcher failure: HTTP_UNAUTHORIZED";
if (response_code == net::HTTP_UNAUTHORIZED) {
DVLOG(1) << "SimpleURLLoader 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";
if (!response_body) {
DVLOG(1) << "SimpleURLLoader failure";
RecordOutgoingMessageStatus(POST_FAILURE);
// POST failed. Notify that http channel doesn't work.
UpdateHttpChannelState(false);
......@@ -305,7 +315,7 @@ void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) {
RecordOutgoingMessageStatus(OUTGOING_MESSAGE_SUCCESS);
// Successfully sent message. Http channel works.
UpdateHttpChannelState(true);
DVLOG(2) << "URLFetcher success";
DVLOG(2) << "SimpleURLLoader success";
}
void GCMNetworkChannel::OnIncomingMessage(const std::string& message,
......
......@@ -16,11 +16,15 @@
#include "components/invalidation/public/invalidation_export.h"
#include "net/base/backoff_entry.h"
#include "net/base/network_change_notifier.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h"
class GoogleServiceAuthError;
namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
} // namespace network
namespace syncer {
class GCMNetworkChannel;
......@@ -48,11 +52,10 @@ struct GCMNetworkChannelDiagnostic {
// messages through GCMService.
class INVALIDATION_EXPORT GCMNetworkChannel
: public SyncNetworkChannel,
public net::URLFetcherDelegate,
public net::NetworkChangeNotifier::NetworkChangeObserver {
public:
GCMNetworkChannel(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
std::unique_ptr<GCMNetworkChannelDelegate> delegate);
~GCMNetworkChannel() override;
......@@ -69,9 +72,6 @@ class INVALIDATION_EXPORT GCMNetworkChannel
void RequestDetailedStatus(
base::Callback<void(const base::DictionaryValue&)> callback) override;
// URLFetcherDelegate implementation.
void OnURLFetchComplete(const net::URLFetcher* source) override;
// NetworkChangeObserver implementation.
void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType connection_type) override;
......@@ -97,7 +97,10 @@ class INVALIDATION_EXPORT GCMNetworkChannel
void UpdateGcmChannelState(bool online);
void UpdateHttpChannelState(bool online);
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
// Callback is called when |simple_url_loader_| completes a network request.
void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
std::unique_ptr<GCMNetworkChannelDelegate> delegate_;
// Message is saved until all conditions are met: there is valid
......@@ -113,7 +116,7 @@ class INVALIDATION_EXPORT GCMNetworkChannel
std::string registration_id_;
std::unique_ptr<net::BackoffEntry> register_backoff_entry_;
std::unique_ptr<net::URLFetcher> fetcher_;
std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
// cacheinvalidation client receives echo_token with incoming message from
// GCM and shuld include it in headers with outgoing message over http.
......
......@@ -20,6 +20,7 @@
#include "components/invalidation/public/invalidation_handler.h"
#include "components/invalidation/public/object_id_invalidation_map.h"
#include "jingle/notifier/listener/push_client.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace syncer {
......@@ -324,10 +325,11 @@ NetworkChannelCreator
}
NetworkChannelCreator NonBlockingInvalidator::MakeGCMNetworkChannelCreator(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
std::unique_ptr<network::SharedURLLoaderFactoryInfo>
url_loader_factory_info,
std::unique_ptr<GCMNetworkChannelDelegate> delegate) {
return base::Bind(&SyncNetworkChannel::CreateGCMNetworkChannel,
request_context_getter,
base::Passed(&url_loader_factory_info),
base::Passed(&delegate));
}
......
......@@ -28,6 +28,10 @@ namespace base {
class SingleThreadTaskRunner;
} // namespace base
namespace network {
class SharedURLLoaderFactoryInfo;
} // namespace network
namespace syncer {
class SyncNetworkChannel;
class GCMNetworkChannelDelegate;
......@@ -72,7 +76,8 @@ class INVALIDATION_EXPORT NonBlockingInvalidator
static NetworkChannelCreator MakePushClientChannelCreator(
const notifier::NotifierOptions& notifier_options);
static NetworkChannelCreator MakeGCMNetworkChannelCreator(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
std::unique_ptr<network::SharedURLLoaderFactoryInfo>
url_loader_factory_info,
std::unique_ptr<GCMNetworkChannelDelegate> delegate);
// These methods are forwarded to the invalidation_state_tracker_.
......
......@@ -26,6 +26,7 @@
#include "google/cacheinvalidation/deps/callback.h"
#include "google/cacheinvalidation/include/types.h"
#include "jingle/notifier/listener/push_client.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace syncer {
......@@ -179,10 +180,14 @@ std::unique_ptr<SyncNetworkChannel> SyncNetworkChannel::CreatePushClientChannel(
}
std::unique_ptr<SyncNetworkChannel> SyncNetworkChannel::CreateGCMNetworkChannel(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
std::unique_ptr<network::SharedURLLoaderFactoryInfo>
url_loader_factory_info,
std::unique_ptr<GCMNetworkChannelDelegate> delegate) {
return std::make_unique<GCMNetworkChannel>(request_context_getter,
std::move(delegate));
DCHECK(url_loader_factory_info);
return std::make_unique<GCMNetworkChannel>(
network::SharedURLLoaderFactory::Create(
std::move(url_loader_factory_info)),
std::move(delegate));
}
void SyncNetworkChannel::NotifyNetworkStatusChange(bool online) {
......
......@@ -24,6 +24,10 @@
#include "google/cacheinvalidation/include/system-resources.h"
#include "jingle/notifier/base/notifier_options.h"
namespace network {
class SharedURLLoaderFactoryInfo;
} // namespace network
namespace syncer {
class GCMNetworkChannelDelegate;
......@@ -135,7 +139,8 @@ class INVALIDATION_EXPORT SyncNetworkChannel
static std::unique_ptr<SyncNetworkChannel> CreatePushClientChannel(
const notifier::NotifierOptions& notifier_options);
static std::unique_ptr<SyncNetworkChannel> CreateGCMNetworkChannel(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
std::unique_ptr<network::SharedURLLoaderFactoryInfo>
url_loader_factory_info,
std::unique_ptr<GCMNetworkChannelDelegate> delegate);
// Get the count of how many valid received messages were received.
......
......@@ -20,6 +20,7 @@
#include "components/invalidation/public/object_id_invalidation_map.h"
#include "google_apis/gaia/gaia_constants.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
static const char* kOAuth2Scopes[] = {
GaiaConstants::kGoogleTalkOAuth2Scope
......@@ -60,7 +61,8 @@ TiclInvalidationService::TiclInvalidationService(
std::unique_ptr<IdentityProvider> identity_provider,
std::unique_ptr<TiclSettingsProvider> settings_provider,
gcm::GCMDriver* gcm_driver,
const scoped_refptr<net::URLRequestContextGetter>& request_context)
const scoped_refptr<net::URLRequestContextGetter>& request_context,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: user_agent_(user_agent),
identity_provider_(std::move(identity_provider)),
settings_provider_(std::move(settings_provider)),
......@@ -68,7 +70,8 @@ TiclInvalidationService::TiclInvalidationService(
request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy),
network_channel_type_(GCM_NETWORK_CHANNEL),
gcm_driver_(gcm_driver),
request_context_(request_context) {}
request_context_(request_context),
url_loader_factory_(std::move(url_loader_factory)) {}
TiclInvalidationService::~TiclInvalidationService() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
......@@ -368,7 +371,8 @@ void TiclInvalidationService::StartInvalidator(
gcm_driver_, identity_provider_.get()));
network_channel_creator =
syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator(
request_context_, gcm_invalidation_bridge_->CreateDelegate());
url_loader_factory_->Clone(),
gcm_invalidation_bridge_->CreateDelegate());
break;
}
default: {
......
......@@ -31,6 +31,10 @@ namespace net {
class URLRequestContextGetter;
}
namespace network {
class SharedURLLoaderFactory;
}
namespace syncer {
class InvalidationStateTracker;
class Invalidator;
......@@ -60,7 +64,8 @@ class TiclInvalidationService : public InvalidationService,
std::unique_ptr<IdentityProvider> identity_provider,
std::unique_ptr<TiclSettingsProvider> settings_provider,
gcm::GCMDriver* gcm_driver,
const scoped_refptr<net::URLRequestContextGetter>& request_context);
const scoped_refptr<net::URLRequestContextGetter>& request_context,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
~TiclInvalidationService() override;
void Init(std::unique_ptr<syncer::InvalidationStateTracker>
......@@ -143,6 +148,7 @@ class TiclInvalidationService : public InvalidationService,
gcm::GCMDriver* gcm_driver_;
std::unique_ptr<GCMInvalidationBridge> gcm_invalidation_bridge_;
scoped_refptr<net::URLRequestContextGetter> request_context_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
// The invalidation logger object we use to record state changes
// and invalidations.
......
......@@ -22,6 +22,7 @@
#include "components/invalidation/impl/profile_identity_provider.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace invalidation {
......@@ -70,7 +71,7 @@ class TiclInvalidationServiceTestDelegate {
"TestUserAgent",
std::make_unique<ProfileIdentityProvider>(&token_service_),
std::unique_ptr<TiclSettingsProvider>(new FakeTiclSettingsProvider),
gcm_driver_.get(), nullptr));
gcm_driver_.get(), nullptr, nullptr));
}
void InitializeInvalidationService() {
......
......@@ -23,6 +23,7 @@
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace invalidation {
......@@ -67,7 +68,7 @@ void TiclProfileSettingsProviderTest::SetUp() {
new ProfileIdentityProvider(&token_service_)),
std::unique_ptr<TiclSettingsProvider>(
new TiclProfileSettingsProvider(&pref_service_)),
&gcm_driver_, request_context_getter_));
&gcm_driver_, request_context_getter_, nullptr /* url_loader_factory */));
invalidation_service_->Init(std::unique_ptr<syncer::InvalidationStateTracker>(
new syncer::FakeInvalidationStateTracker));
}
......
......@@ -25,6 +25,7 @@
#include "ios/chrome/browser/signin/signin_manager_factory.h"
#include "ios/web/public/web_client.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -76,7 +77,8 @@ IOSChromeProfileInvalidationProviderFactory::BuildServiceInstanceFor(
browser_state->GetPrefs()),
IOSChromeGCMProfileServiceFactory::GetForBrowserState(browser_state)
->driver(),
browser_state->GetRequestContext()));
browser_state->GetRequestContext(),
browser_state->GetSharedURLLoaderFactory()));
service->Init(
std::make_unique<InvalidatorStorage>(browser_state->GetPrefs()));
......
......@@ -9,6 +9,7 @@ namespace network {
// static
scoped_refptr<SharedURLLoaderFactory> SharedURLLoaderFactory::Create(
std::unique_ptr<SharedURLLoaderFactoryInfo> info) {
DCHECK(info);
return info->CreateFactory();
}
......
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