| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ |
| 6 #define SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | |
| 12 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 16 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 17 #include "net/base/network_time_notifier.h" | 16 #include "net/base/network_time_notifier.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "sync/base/sync_export.h" | 20 #include "sync/base/sync_export.h" |
| 22 #include "sync/internal_api/public/base/cancelation_observer.h" | 21 #include "sync/internal_api/public/base/cancelation_observer.h" |
| 23 #include "sync/internal_api/public/http_post_provider_factory.h" | 22 #include "sync/internal_api/public/http_post_provider_factory.h" |
| 24 #include "sync/internal_api/public/http_post_provider_interface.h" | 23 #include "sync/internal_api/public/http_post_provider_interface.h" |
| 24 #include "sync/internal_api/public/network_time_update_callback.h" |
| 25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 26 | 26 |
| 27 class HttpBridgeTest; | 27 class HttpBridgeTest; |
| 28 | 28 |
| 29 namespace base { | 29 namespace base { |
| 30 class MessageLoop; | 30 class MessageLoop; |
| 31 } | 31 } |
| 32 | 32 |
| 33 namespace net { | 33 namespace net { |
| 34 class HttpResponseHeaders; | 34 class HttpResponseHeaders; |
| 35 class HttpUserAgentSettings; | 35 class HttpUserAgentSettings; |
| 36 class URLFetcher; | 36 class URLFetcher; |
| 37 } | 37 } |
| 38 | 38 |
| 39 namespace syncer { | 39 namespace syncer { |
| 40 | 40 |
| 41 class CancelationSignal; | 41 class CancelationSignal; |
| 42 | 42 |
| 43 // Callback for updating the network time. | |
| 44 // Params: | |
| 45 // const base::Time& network_time - the new network time. | |
| 46 // const base::TimeDelta& resolution - how precise the reading is. | |
| 47 // const base::TimeDelta& latency - the http request's latency. | |
| 48 typedef base::Callback<void(const base::Time&, | |
| 49 const base::TimeDelta&, | |
| 50 const base::TimeDelta&)> NetworkTimeUpdateCallback; | |
| 51 | |
| 52 // A bridge between the syncer and Chromium HTTP layers. | 43 // A bridge between the syncer and Chromium HTTP layers. |
| 53 // Provides a way for the sync backend to use Chromium directly for HTTP | 44 // Provides a way for the sync backend to use Chromium directly for HTTP |
| 54 // requests rather than depending on a third party provider (e.g libcurl). | 45 // requests rather than depending on a third party provider (e.g libcurl). |
| 55 // This is a one-time use bridge. Create one for each request you want to make. | 46 // This is a one-time use bridge. Create one for each request you want to make. |
| 56 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus | 47 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus |
| 57 // needs to stick around across context switches, etc. | 48 // needs to stick around across context switches, etc. |
| 58 class SYNC_EXPORT_PRIVATE HttpBridge | 49 class SYNC_EXPORT_PRIVATE HttpBridge |
| 59 : public base::RefCountedThreadSafe<HttpBridge>, | 50 : public base::RefCountedThreadSafe<HttpBridge>, |
| 60 public HttpPostProviderInterface, | 51 public HttpPostProviderInterface, |
| 61 public net::URLFetcherDelegate { | 52 public net::URLFetcherDelegate { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 NetworkTimeUpdateCallback network_time_update_callback_; | 221 NetworkTimeUpdateCallback network_time_update_callback_; |
| 231 | 222 |
| 232 DISALLOW_COPY_AND_ASSIGN(HttpBridge); | 223 DISALLOW_COPY_AND_ASSIGN(HttpBridge); |
| 233 }; | 224 }; |
| 234 | 225 |
| 235 class SYNC_EXPORT HttpBridgeFactory : public HttpPostProviderFactory, | 226 class SYNC_EXPORT HttpBridgeFactory : public HttpPostProviderFactory, |
| 236 public CancelationObserver { | 227 public CancelationObserver { |
| 237 public: | 228 public: |
| 238 HttpBridgeFactory( | 229 HttpBridgeFactory( |
| 239 net::URLRequestContextGetter* baseline_context_getter, | 230 net::URLRequestContextGetter* baseline_context_getter, |
| 240 const NetworkTimeUpdateCallback& network_time_update_callback, | 231 const NetworkTimeUpdateCallback& network_time_update_callback); |
| 241 CancelationSignal* cancelation_signal); | |
| 242 virtual ~HttpBridgeFactory(); | 232 virtual ~HttpBridgeFactory(); |
| 243 | 233 |
| 244 // HttpPostProviderFactory: | 234 // HttpPostProviderFactory: |
| 245 virtual void Init(const std::string& user_agent) OVERRIDE; | 235 virtual void Init(const std::string& user_agent) OVERRIDE; |
| 236 virtual void RegisterCancelationSignal( |
| 237 CancelationSignal* cancelation_signal) OVERRIDE; |
| 246 virtual HttpPostProviderInterface* Create() OVERRIDE; | 238 virtual HttpPostProviderInterface* Create() OVERRIDE; |
| 247 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE; | 239 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE; |
| 248 | 240 |
| 249 // CancelationObserver implementation: | 241 // CancelationObserver implementation: |
| 250 virtual void OnSignalReceived() OVERRIDE; | 242 virtual void OnSignalReceived() OVERRIDE; |
| 251 | 243 |
| 252 private: | 244 private: |
| 253 // Protects |request_context_getter_| and |baseline_request_context_getter_|. | 245 // Protects |request_context_getter_| and |baseline_request_context_getter_|. |
| 254 base::Lock context_getter_lock_; | 246 base::Lock context_getter_lock_; |
| 255 | 247 |
| 256 // This request context is the starting point for the request_context_getter_ | 248 // This request context is the starting point for the request_context_getter_ |
| 257 // that we eventually use to make requests. During shutdown we must drop all | 249 // that we eventually use to make requests. During shutdown we must drop all |
| 258 // references to it before the ProfileSyncService's Shutdown() call is | 250 // references to it before the ProfileSyncService's Shutdown() call is |
| 259 // complete. | 251 // complete. |
| 260 scoped_refptr<net::URLRequestContextGetter> baseline_request_context_getter_; | 252 scoped_refptr<net::URLRequestContextGetter> baseline_request_context_getter_; |
| 261 | 253 |
| 262 // This request context is built on top of the baseline context and shares | 254 // This request context is built on top of the baseline context and shares |
| 263 // common components. Takes a reference to the | 255 // common components. Takes a reference to the |
| 264 // baseline_request_context_getter_. It's mostly used on sync thread when | 256 // baseline_request_context_getter_. It's mostly used on sync thread when |
| 265 // creating connection but is released as soon as possible during shutdown. | 257 // creating connection but is released as soon as possible during shutdown. |
| 266 // Protected by |context_getter_lock_|. | 258 // Protected by |context_getter_lock_|. |
| 267 scoped_refptr<HttpBridge::RequestContextGetter> request_context_getter_; | 259 scoped_refptr<HttpBridge::RequestContextGetter> request_context_getter_; |
| 268 | 260 |
| 269 NetworkTimeUpdateCallback network_time_update_callback_; | 261 NetworkTimeUpdateCallback network_time_update_callback_; |
| 270 | 262 |
| 271 CancelationSignal* const cancelation_signal_; | 263 CancelationSignal* cancelation_signal_; |
| 272 | 264 |
| 273 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); | 265 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); |
| 274 }; | 266 }; |
| 275 | 267 |
| 276 } // namespace syncer | 268 } // namespace syncer |
| 277 | 269 |
| 278 #endif // SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ | 270 #endif // SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ |
| OLD | NEW |