This source file includes following definitions.
- user_agent_
- GetURLRequestContext
- GetNetworkTaskRunner
- cancelation_signal_
- Init
- Create
- Destroy
- OnSignalReceived
- network_task_runner_
- error_code
- network_time_update_callback_
- SetExtraRequestHeaders
- SetURL
- SetPostPayload
- MakeSynchronousPost
- MakeAsynchronousPost
- GetResponseContentLength
- GetResponseContent
- GetResponseHeaderValue
- Abort
- DestroyURLFetcherOnIOThread
- OnURLFetchComplete
- GetRequestContextGetterForTest
- UpdateNetworkTime
#include "sync/internal_api/public/http_bridge.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/strings/string_number_conversions.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/cookies/cookie_monster.h"
#include "net/dns/host_resolver.h"
#include "net/http/http_cache.h"
#include "net/http/http_network_layer.h"
#include "net/http/http_response_headers.h"
#include "net/proxy/proxy_service.h"
#include "net/url_request/static_http_user_agent_settings.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_status.h"
#include "sync/internal_api/public/base/cancelation_signal.h"
namespace syncer {
HttpBridge::RequestContextGetter::RequestContextGetter(
net::URLRequestContextGetter* baseline_context_getter,
const std::string& user_agent)
: baseline_context_getter_(baseline_context_getter),
network_task_runner_(
baseline_context_getter_->GetNetworkTaskRunner()),
user_agent_(user_agent) {
DCHECK(baseline_context_getter_.get());
DCHECK(network_task_runner_.get());
DCHECK(!user_agent_.empty());
}
HttpBridge::RequestContextGetter::~RequestContextGetter() {}
net::URLRequestContext*
HttpBridge::RequestContextGetter::GetURLRequestContext() {
if (!context_) {
net::URLRequestContext* baseline_context =
baseline_context_getter_->GetURLRequestContext();
context_.reset(
new RequestContext(baseline_context, GetNetworkTaskRunner(),
user_agent_));
baseline_context_getter_ = NULL;
}
return context_.get();
}
scoped_refptr<base::SingleThreadTaskRunner>
HttpBridge::RequestContextGetter::GetNetworkTaskRunner() const {
return network_task_runner_;
}
HttpBridgeFactory::HttpBridgeFactory(
net::URLRequestContextGetter* baseline_context_getter,
const NetworkTimeUpdateCallback& network_time_update_callback,
CancelationSignal* cancelation_signal)
: baseline_request_context_getter_(baseline_context_getter),
network_time_update_callback_(network_time_update_callback),
cancelation_signal_(cancelation_signal) {
bool result = cancelation_signal_->TryRegisterHandler(this);
DCHECK(result);
}
HttpBridgeFactory::~HttpBridgeFactory() {
cancelation_signal_->UnregisterHandler(this);
}
void HttpBridgeFactory::Init(const std::string& user_agent) {
base::AutoLock lock(context_getter_lock_);
if (!baseline_request_context_getter_.get()) {
return;
}
request_context_getter_ =
new HttpBridge::RequestContextGetter(
baseline_request_context_getter_, user_agent);
}
HttpPostProviderInterface* HttpBridgeFactory::Create() {
base::AutoLock lock(context_getter_lock_);
CHECK(request_context_getter_.get());
HttpBridge* http = new HttpBridge(request_context_getter_.get(),
network_time_update_callback_);
http->AddRef();
return http;
}
void HttpBridgeFactory::Destroy(HttpPostProviderInterface* http) {
static_cast<HttpBridge*>(http)->Release();
}
void HttpBridgeFactory::OnSignalReceived() {
base::AutoLock lock(context_getter_lock_);
baseline_request_context_getter_ = NULL;
request_context_getter_ = NULL;
}
HttpBridge::RequestContext::RequestContext(
net::URLRequestContext* baseline_context,
const scoped_refptr<base::SingleThreadTaskRunner>&
network_task_runner,
const std::string& user_agent)
: baseline_context_(baseline_context),
network_task_runner_(network_task_runner) {
DCHECK(!user_agent.empty());
set_cookie_store(new net::CookieMonster(NULL, NULL));
set_host_resolver(baseline_context->host_resolver());
set_proxy_service(baseline_context->proxy_service());
set_ssl_config_service(baseline_context->ssl_config_service());
net::HttpNetworkSession* session =
baseline_context->http_transaction_factory()->GetSession();
DCHECK(session);
set_http_transaction_factory(new net::HttpNetworkLayer(session));
std::string accepted_language_list;
if (baseline_context->http_user_agent_settings()) {
accepted_language_list =
baseline_context->http_user_agent_settings()->GetAcceptLanguage();
}
http_user_agent_settings_.reset(new net::StaticHttpUserAgentSettings(
accepted_language_list,
user_agent));
set_http_user_agent_settings(http_user_agent_settings_.get());
set_net_log(baseline_context->net_log());
}
HttpBridge::RequestContext::~RequestContext() {
DCHECK(network_task_runner_->BelongsToCurrentThread());
delete http_transaction_factory();
}
HttpBridge::URLFetchState::URLFetchState() : url_poster(NULL),
aborted(false),
request_completed(false),
request_succeeded(false),
http_response_code(-1),
error_code(-1) {}
HttpBridge::URLFetchState::~URLFetchState() {}
HttpBridge::HttpBridge(
HttpBridge::RequestContextGetter* context_getter,
const NetworkTimeUpdateCallback& network_time_update_callback)
: created_on_loop_(base::MessageLoop::current()),
http_post_completed_(false, false),
context_getter_for_request_(context_getter),
network_task_runner_(
context_getter_for_request_->GetNetworkTaskRunner()),
network_time_update_callback_(network_time_update_callback) {
}
HttpBridge::~HttpBridge() {
}
void HttpBridge::SetExtraRequestHeaders(const char * headers) {
DCHECK(extra_headers_.empty())
<< "HttpBridge::SetExtraRequestHeaders called twice.";
extra_headers_.assign(headers);
}
void HttpBridge::SetURL(const char* url, int port) {
#if DCHECK_IS_ON
DCHECK_EQ(base::MessageLoop::current(), created_on_loop_);
{
base::AutoLock lock(fetch_state_lock_);
DCHECK(!fetch_state_.request_completed);
}
DCHECK(url_for_request_.is_empty())
<< "HttpBridge::SetURL called more than once?!";
#endif
GURL temp(url);
GURL::Replacements replacements;
std::string port_str = base::IntToString(port);
replacements.SetPort(port_str.c_str(),
url_parse::Component(0, port_str.length()));
url_for_request_ = temp.ReplaceComponents(replacements);
}
void HttpBridge::SetPostPayload(const char* content_type,
int content_length,
const char* content) {
#if DCHECK_IS_ON
DCHECK_EQ(base::MessageLoop::current(), created_on_loop_);
{
base::AutoLock lock(fetch_state_lock_);
DCHECK(!fetch_state_.request_completed);
}
DCHECK(content_type_.empty()) << "Bridge payload already set.";
DCHECK_GE(content_length, 0) << "Content length < 0";
#endif
content_type_ = content_type;
if (!content || (content_length == 0)) {
DCHECK_EQ(content_length, 0);
request_content_ = " ";
} else {
request_content_.assign(content, content_length);
}
}
bool HttpBridge::MakeSynchronousPost(int* error_code, int* response_code) {
#if DCHECK_IS_ON
DCHECK_EQ(base::MessageLoop::current(), created_on_loop_);
{
base::AutoLock lock(fetch_state_lock_);
DCHECK(!fetch_state_.request_completed);
}
DCHECK(url_for_request_.is_valid()) << "Invalid URL for request";
DCHECK(!content_type_.empty()) << "Payload not set";
#endif
if (!network_task_runner_->PostTask(
FROM_HERE,
base::Bind(&HttpBridge::CallMakeAsynchronousPost, this))) {
LOG(WARNING) << "Could not post CallMakeAsynchronousPost task";
return false;
}
http_post_completed_.Wait();
base::AutoLock lock(fetch_state_lock_);
DCHECK(fetch_state_.request_completed || fetch_state_.aborted);
*error_code = fetch_state_.error_code;
*response_code = fetch_state_.http_response_code;
return fetch_state_.request_succeeded;
}
void HttpBridge::MakeAsynchronousPost() {
DCHECK(network_task_runner_->BelongsToCurrentThread());
base::AutoLock lock(fetch_state_lock_);
DCHECK(!fetch_state_.request_completed);
if (fetch_state_.aborted)
return;
DCHECK(context_getter_for_request_.get());
fetch_state_.url_poster = net::URLFetcher::Create(
url_for_request_, net::URLFetcher::POST, this);
fetch_state_.url_poster->SetRequestContext(context_getter_for_request_.get());
fetch_state_.url_poster->SetUploadData(content_type_, request_content_);
fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_);
fetch_state_.url_poster->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES);
fetch_state_.start_time = base::Time::Now();
fetch_state_.url_poster->Start();
}
int HttpBridge::GetResponseContentLength() const {
DCHECK_EQ(base::MessageLoop::current(), created_on_loop_);
base::AutoLock lock(fetch_state_lock_);
DCHECK(fetch_state_.request_completed);
return fetch_state_.response_content.size();
}
const char* HttpBridge::GetResponseContent() const {
DCHECK_EQ(base::MessageLoop::current(), created_on_loop_);
base::AutoLock lock(fetch_state_lock_);
DCHECK(fetch_state_.request_completed);
return fetch_state_.response_content.data();
}
const std::string HttpBridge::GetResponseHeaderValue(
const std::string& name) const {
DCHECK_EQ(base::MessageLoop::current(), created_on_loop_);
base::AutoLock lock(fetch_state_lock_);
DCHECK(fetch_state_.request_completed);
std::string value;
fetch_state_.response_headers->EnumerateHeader(NULL, name, &value);
return value;
}
void HttpBridge::Abort() {
base::AutoLock lock(fetch_state_lock_);
context_getter_for_request_ = NULL;
DCHECK(!fetch_state_.aborted);
if (fetch_state_.aborted || fetch_state_.request_completed)
return;
fetch_state_.aborted = true;
if (!network_task_runner_->PostTask(
FROM_HERE,
base::Bind(&HttpBridge::DestroyURLFetcherOnIOThread, this,
fetch_state_.url_poster))) {
NOTREACHED() << "Could not post task to delete URLFetcher";
}
fetch_state_.url_poster = NULL;
fetch_state_.error_code = net::ERR_ABORTED;
http_post_completed_.Signal();
}
void HttpBridge::DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher) {
DCHECK(network_task_runner_->BelongsToCurrentThread());
delete fetcher;
}
void HttpBridge::OnURLFetchComplete(const net::URLFetcher* source) {
DCHECK(network_task_runner_->BelongsToCurrentThread());
base::AutoLock lock(fetch_state_lock_);
if (fetch_state_.aborted)
return;
fetch_state_.end_time = base::Time::Now();
fetch_state_.request_completed = true;
fetch_state_.request_succeeded =
(net::URLRequestStatus::SUCCESS == source->GetStatus().status());
fetch_state_.http_response_code = source->GetResponseCode();
fetch_state_.error_code = source->GetStatus().error();
VLOG(2) << "HttpBridge::OnURLFetchComplete for: "
<< fetch_state_.url_poster->GetURL().spec();
VLOG(1) << "HttpBridge received response code: "
<< fetch_state_.http_response_code;
source->GetResponseAsString(&fetch_state_.response_content);
fetch_state_.response_headers = source->GetResponseHeaders();
UpdateNetworkTime();
base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster);
fetch_state_.url_poster = NULL;
http_post_completed_.Signal();
}
net::URLRequestContextGetter* HttpBridge::GetRequestContextGetterForTest()
const {
base::AutoLock lock(fetch_state_lock_);
return context_getter_for_request_.get();
}
void HttpBridge::UpdateNetworkTime() {
std::string sane_time_str;
if (!fetch_state_.request_succeeded || fetch_state_.start_time.is_null() ||
fetch_state_.end_time < fetch_state_.start_time ||
!fetch_state_.response_headers->EnumerateHeader(NULL, "Sane-Time-Millis",
&sane_time_str)) {
return;
}
int64 sane_time_ms = 0;
if (base::StringToInt64(sane_time_str, &sane_time_ms)) {
network_time_update_callback_.Run(
base::Time::FromJsTime(sane_time_ms),
base::TimeDelta::FromMilliseconds(1),
fetch_state_.end_time - fetch_state_.start_time);
}
}
}