summaryrefslogtreecommitdiffstats
path: root/sync/internal_api
diff options
context:
space:
mode:
authorzea@chromium.org <zea@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-30 03:45:14 +0000
committerzea@chromium.org <zea@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-30 03:45:14 +0000
commitdaae3462e123480220b6545b94d44659ab20dac1 (patch)
treee9ea57a0f513c5b4866cae24db93b03fa3b702fd /sync/internal_api
parented7c6617764efaf49a091ecd69ef898653435cca (diff)
downloadchromium_src-daae3462e123480220b6545b94d44659ab20dac1.zip
chromium_src-daae3462e123480220b6545b94d44659ab20dac1.tar.gz
chromium_src-daae3462e123480220b6545b94d44659ab20dac1.tar.bz2
Split NetworkTimeTracker into service part and observer part.
The service will now get time updates from sync's http bridge (with time data from the sync server). Original codereview by haitaol@chromium.org at https://codereview.chromium.org/12211117/ BUG=177072 TBR=akalin@chromium.org Review URL: https://chromiumcodereview.appspot.com/12833011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@197258 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync/internal_api')
-rw-r--r--sync/internal_api/http_bridge.cc38
-rw-r--r--sync/internal_api/http_bridge_unittest.cc6
-rw-r--r--sync/internal_api/public/http_bridge.h32
3 files changed, 65 insertions, 11 deletions
diff --git a/sync/internal_api/http_bridge.cc b/sync/internal_api/http_bridge.cc
index df864bf..a295fd5 100644
--- a/sync/internal_api/http_bridge.cc
+++ b/sync/internal_api/http_bridge.cc
@@ -58,16 +58,20 @@ HttpBridge::RequestContextGetter::GetNetworkTaskRunner() const {
HttpBridgeFactory::HttpBridgeFactory(
net::URLRequestContextGetter* baseline_context_getter,
- const std::string& user_agent)
+ const std::string& user_agent,
+ const NetworkTimeUpdateCallback& network_time_update_callback)
: request_context_getter_(
new HttpBridge::RequestContextGetter(
- baseline_context_getter, user_agent)) {}
+ baseline_context_getter, user_agent)),
+ network_time_update_callback_(network_time_update_callback) {
+}
HttpBridgeFactory::~HttpBridgeFactory() {
}
HttpPostProviderInterface* HttpBridgeFactory::Create() {
- HttpBridge* http = new HttpBridge(request_context_getter_);
+ HttpBridge* http = new HttpBridge(request_context_getter_,
+ network_time_update_callback_);
http->AddRef();
return http;
}
@@ -130,12 +134,15 @@ HttpBridge::URLFetchState::URLFetchState() : url_poster(NULL),
error_code(-1) {}
HttpBridge::URLFetchState::~URLFetchState() {}
-HttpBridge::HttpBridge(HttpBridge::RequestContextGetter* context_getter)
+HttpBridge::HttpBridge(
+ HttpBridge::RequestContextGetter* context_getter,
+ const NetworkTimeUpdateCallback& network_time_update_callback)
: context_getter_for_request_(context_getter),
network_task_runner_(
context_getter_for_request_->GetNetworkTaskRunner()),
created_on_loop_(MessageLoop::current()),
- http_post_completed_(false, false) {
+ http_post_completed_(false, false),
+ network_time_update_callback_(network_time_update_callback) {
}
HttpBridge::~HttpBridge() {
@@ -225,6 +232,7 @@ void HttpBridge::MakeAsynchronousPost() {
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();
}
@@ -285,6 +293,7 @@ void HttpBridge::OnURLFetchComplete(const net::URLFetcher* source) {
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());
@@ -299,6 +308,7 @@ void HttpBridge::OnURLFetchComplete(const net::URLFetcher* source) {
source->GetResponseAsString(&fetch_state_.response_content);
fetch_state_.response_headers = source->GetResponseHeaders();
+ UpdateNetworkTime();
// End of the line for url_poster_. It lives only on the IO loop.
// We defer deletion because we're inside a callback from a component of the
@@ -316,4 +326,22 @@ net::URLRequestContextGetter* HttpBridge::GetRequestContextGetterForTest()
return context_getter_for_request_;
}
+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);
+ }
+}
+
} // namespace syncer
diff --git a/sync/internal_api/http_bridge_unittest.cc b/sync/internal_api/http_bridge_unittest.cc
index 2692480..93df2b7 100644
--- a/sync/internal_api/http_bridge_unittest.cc
+++ b/sync/internal_api/http_bridge_unittest.cc
@@ -55,7 +55,8 @@ class SyncHttpBridgeTest : public testing::Test {
HttpBridge* bridge = new HttpBridge(
new HttpBridge::RequestContextGetter(
fake_default_request_context_getter_,
- "user agent"));
+ "user agent"),
+ NetworkTimeUpdateCallback());
return bridge;
}
@@ -121,7 +122,8 @@ class ShuntedHttpBridge : public HttpBridge {
SyncHttpBridgeTest* test, bool never_finishes)
: HttpBridge(
new HttpBridge::RequestContextGetter(
- baseline_context_getter, "user agent")),
+ baseline_context_getter, "user agent"),
+ NetworkTimeUpdateCallback()),
test_(test), never_finishes_(never_finishes) { }
protected:
virtual void MakeAsynchronousPost() OVERRIDE {
diff --git a/sync/internal_api/public/http_bridge.h b/sync/internal_api/public/http_bridge.h
index 6619adf..7830190 100644
--- a/sync/internal_api/public/http_bridge.h
+++ b/sync/internal_api/public/http_bridge.h
@@ -8,12 +8,14 @@
#include <string>
#include "base/basictypes.h"
+#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "googleurl/src/gurl.h"
+#include "net/base/network_time_notifier.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
@@ -35,6 +37,15 @@ class URLFetcher;
namespace syncer {
+// Callback for updating the network time.
+// Params:
+// const base::Time& network_time - the new network time.
+// const base::TimeDelta& resolution - how precise the reading is.
+// const base::TimeDelta& latency - the http request's latency.
+typedef base::Callback<void(const base::Time&,
+ const base::TimeDelta&,
+ const base::TimeDelta&)> NetworkTimeUpdateCallback;
+
// A bridge between the syncer and Chromium HTTP layers.
// Provides a way for the sync backend to use Chromium directly for HTTP
// requests rather than depending on a third party provider (e.g libcurl).
@@ -46,8 +57,6 @@ class SYNC_EXPORT_PRIVATE HttpBridge
public HttpPostProviderInterface,
public net::URLFetcherDelegate {
public:
- friend class SyncHttpBridgeTest;
-
// A request context used for HTTP requests bridged from the sync backend.
// A bridged RequestContext has a dedicated in-memory cookie store and does
// not use a cache. Thus the same type can be used for incognito mode.
@@ -102,7 +111,8 @@ class SYNC_EXPORT_PRIVATE HttpBridge
DISALLOW_COPY_AND_ASSIGN(RequestContextGetter);
};
- explicit HttpBridge(RequestContextGetter* context);
+ HttpBridge(RequestContextGetter* context,
+ const NetworkTimeUpdateCallback& network_time_update_callback);
// HttpPostProvider implementation.
virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE;
@@ -136,6 +146,7 @@ class SYNC_EXPORT_PRIVATE HttpBridge
virtual void MakeAsynchronousPost();
private:
+ friend class SyncHttpBridgeTest;
friend class ::HttpBridgeTest;
// Called on the IO loop to issue the network request. The extra level
@@ -149,6 +160,8 @@ class SYNC_EXPORT_PRIVATE HttpBridge
// fetcher.
void DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher);
+ void UpdateNetworkTime();
+
// Gets a customized net::URLRequestContext for bridged requests. See
// RequestContext definition for details.
const scoped_refptr<RequestContextGetter> context_getter_for_request_;
@@ -185,6 +198,11 @@ class SYNC_EXPORT_PRIVATE HttpBridge
// deleted on. We must manually delete url_poster_ on the IO loop.
net::URLFetcher* url_poster;
+ // Start and finish time of request. Set immediately before sending
+ // request and after receiving response.
+ base::Time start_time;
+ base::Time end_time;
+
// Used to support 'Abort' functionality.
bool aborted;
@@ -204,6 +222,9 @@ class SYNC_EXPORT_PRIVATE HttpBridge
mutable base::Lock fetch_state_lock_;
URLFetchState fetch_state_;
+ // Callback for updating network time.
+ NetworkTimeUpdateCallback network_time_update_callback_;
+
DISALLOW_COPY_AND_ASSIGN(HttpBridge);
};
@@ -211,7 +232,8 @@ class SYNC_EXPORT HttpBridgeFactory : public HttpPostProviderFactory {
public:
HttpBridgeFactory(
net::URLRequestContextGetter* baseline_context_getter,
- const std::string& user_agent);
+ const std::string& user_agent,
+ const NetworkTimeUpdateCallback& network_time_update_callback);
virtual ~HttpBridgeFactory();
// HttpPostProviderFactory:
@@ -226,6 +248,8 @@ class SYNC_EXPORT HttpBridgeFactory : public HttpPostProviderFactory {
const scoped_refptr<HttpBridge::RequestContextGetter>
request_context_getter_;
+ NetworkTimeUpdateCallback network_time_update_callback_;
+
DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory);
};