| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 43 public: | 43 public: |
| 44 // A request context used for HTTP requests bridged from the sync backend. | 44 // A request context used for HTTP requests bridged from the sync backend. |
| 45 // A bridged RequestContext has a dedicated in-memory cookie store and does | 45 // A bridged RequestContext has a dedicated in-memory cookie store and does |
| 46 // not use a cache. Thus the same type can be used for incognito mode. | 46 // not use a cache. Thus the same type can be used for incognito mode. |
| 47 class RequestContext : public net::URLRequestContext { | 47 class RequestContext : public net::URLRequestContext { |
| 48 public: | 48 public: |
| 49 // |baseline_context| is used to obtain the accept-language, | 49 // |baseline_context| is used to obtain the accept-language, |
| 50 // accept-charsets, and proxy service information for bridged requests. | 50 // accept-charsets, and proxy service information for bridged requests. |
| 51 // Typically |baseline_context| should be the net::URLRequestContext of the | 51 // Typically |baseline_context| should be the net::URLRequestContext of the |
| 52 // currently active profile. | 52 // currently active profile. |
| 53 explicit RequestContext(net::URLRequestContext* baseline_context); | 53 RequestContext( |
| 54 net::URLRequestContext* baseline_context, |
| 55 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 56 network_task_runner, |
| 57 const std::string& user_agent); |
| 54 | 58 |
| 55 // The destructor MUST be called on the IO thread. | 59 // The destructor MUST be called on the IO thread. |
| 56 virtual ~RequestContext(); | 60 virtual ~RequestContext(); |
| 57 | 61 |
| 58 // Set the user agent for requests using this context. The default is | 62 virtual const std::string& GetUserAgent(const GURL& url) const OVERRIDE; |
| 59 // the browser's UA string. | |
| 60 void set_user_agent(const std::string& ua) { user_agent_ = ua; } | |
| 61 | |
| 62 virtual const std::string& GetUserAgent(const GURL& url) const OVERRIDE { | |
| 63 // If the user agent is set explicitly return that, otherwise call the | |
| 64 // base class method to return default value. | |
| 65 return user_agent_.empty() ? | |
| 66 net::URLRequestContext::GetUserAgent(url) : user_agent_; | |
| 67 } | |
| 68 | 63 |
| 69 private: | 64 private: |
| 70 std::string user_agent_; | 65 net::URLRequestContext* const baseline_context_; |
| 71 net::URLRequestContext* baseline_context_; | 66 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 67 const std::string user_agent_; |
| 72 | 68 |
| 73 DISALLOW_COPY_AND_ASSIGN(RequestContext); | 69 DISALLOW_COPY_AND_ASSIGN(RequestContext); |
| 74 }; | 70 }; |
| 75 | 71 |
| 76 // Lazy-getter for RequestContext objects. | 72 // Lazy-getter for RequestContext objects. |
| 77 class RequestContextGetter : public net::URLRequestContextGetter { | 73 class RequestContextGetter : public net::URLRequestContextGetter { |
| 78 public: | 74 public: |
| 79 explicit RequestContextGetter( | 75 RequestContextGetter( |
| 80 net::URLRequestContextGetter* baseline_context_getter); | 76 net::URLRequestContextGetter* baseline_context_getter, |
| 81 | 77 const std::string& user_agent); |
| 82 void set_user_agent(const std::string& ua) { user_agent_ = ua; } | |
| 83 bool is_user_agent_set() const { return !user_agent_.empty(); } | |
| 84 | 78 |
| 85 // net::URLRequestContextGetter implementation. | 79 // net::URLRequestContextGetter implementation. |
| 86 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; | 80 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; |
| 87 virtual scoped_refptr<base::SingleThreadTaskRunner> | 81 virtual scoped_refptr<base::SingleThreadTaskRunner> |
| 88 GetNetworkTaskRunner() const OVERRIDE; | 82 GetNetworkTaskRunner() const OVERRIDE; |
| 89 | 83 |
| 90 protected: | 84 protected: |
| 91 virtual ~RequestContextGetter(); | 85 virtual ~RequestContextGetter(); |
| 92 | 86 |
| 93 private: | 87 private: |
| 88 scoped_refptr<net::URLRequestContextGetter> baseline_context_getter_; |
| 89 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 94 // User agent to apply to the net::URLRequestContext. | 90 // User agent to apply to the net::URLRequestContext. |
| 95 std::string user_agent_; | 91 const std::string user_agent_; |
| 96 | |
| 97 scoped_refptr<net::URLRequestContextGetter> baseline_context_getter_; | |
| 98 | 92 |
| 99 // Lazily initialized by GetURLRequestContext(). | 93 // Lazily initialized by GetURLRequestContext(). |
| 100 scoped_ptr<RequestContext> context_; | 94 scoped_ptr<RequestContext> context_; |
| 101 | 95 |
| 102 DISALLOW_COPY_AND_ASSIGN(RequestContextGetter); | 96 DISALLOW_COPY_AND_ASSIGN(RequestContextGetter); |
| 103 }; | 97 }; |
| 104 | 98 |
| 105 explicit HttpBridge(RequestContextGetter* context); | 99 explicit HttpBridge(RequestContextGetter* context); |
| 106 | 100 |
| 107 // csync::HttpPostProvider implementation. | 101 // csync::HttpPostProvider implementation. |
| 108 virtual void SetUserAgent(const char* user_agent) OVERRIDE; | |
| 109 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE; | 102 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE; |
| 110 virtual void SetURL(const char* url, int port) OVERRIDE; | 103 virtual void SetURL(const char* url, int port) OVERRIDE; |
| 111 virtual void SetPostPayload(const char* content_type, int content_length, | 104 virtual void SetPostPayload(const char* content_type, int content_length, |
| 112 const char* content) OVERRIDE; | 105 const char* content) OVERRIDE; |
| 113 virtual bool MakeSynchronousPost(int* error_code, | 106 virtual bool MakeSynchronousPost(int* error_code, |
| 114 int* response_code) OVERRIDE; | 107 int* response_code) OVERRIDE; |
| 115 virtual void Abort() OVERRIDE; | 108 virtual void Abort() OVERRIDE; |
| 116 | 109 |
| 117 // WARNING: these response content methods are used to extract plain old data | 110 // WARNING: these response content methods are used to extract plain old data |
| 118 // and not null terminated strings, so you should make sure you have read | 111 // and not null terminated strings, so you should make sure you have read |
| (...skipping 30 matching lines...) Expand all Loading... |
| 149 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); } | 142 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); } |
| 150 | 143 |
| 151 // Used to destroy a fetcher when the bridge is Abort()ed, to ensure that | 144 // Used to destroy a fetcher when the bridge is Abort()ed, to ensure that |
| 152 // a reference to |this| is held while flushing any pending fetch completion | 145 // a reference to |this| is held while flushing any pending fetch completion |
| 153 // callbacks coming from the IO thread en route to finally destroying the | 146 // callbacks coming from the IO thread en route to finally destroying the |
| 154 // fetcher. | 147 // fetcher. |
| 155 void DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher); | 148 void DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher); |
| 156 | 149 |
| 157 // Gets a customized net::URLRequestContext for bridged requests. See | 150 // Gets a customized net::URLRequestContext for bridged requests. See |
| 158 // RequestContext definition for details. | 151 // RequestContext definition for details. |
| 159 scoped_refptr<RequestContextGetter> context_getter_for_request_; | 152 const scoped_refptr<RequestContextGetter> context_getter_for_request_; |
| 153 |
| 154 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 160 | 155 |
| 161 // The message loop of the thread we were created on. This is the thread that | 156 // The message loop of the thread we were created on. This is the thread that |
| 162 // will block on MakeSynchronousPost while the IO thread fetches data from | 157 // will block on MakeSynchronousPost while the IO thread fetches data from |
| 163 // the network. | 158 // the network. |
| 164 // This should be the main syncer thread (SyncerThread) which is what blocks | 159 // This should be the main syncer thread (SyncerThread) which is what blocks |
| 165 // on network IO through curl_easy_perform. | 160 // on network IO through curl_easy_perform. |
| 166 MessageLoop* const created_on_loop_; | 161 MessageLoop* const created_on_loop_; |
| 167 | 162 |
| 168 // The URL to POST to. | 163 // The URL to POST to. |
| 169 GURL url_for_request_; | 164 GURL url_for_request_; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // this flow needs to be synchronized to gracefully clean up URLFetcher and | 200 // this flow needs to be synchronized to gracefully clean up URLFetcher and |
| 206 // return appropriate values in |error_code|. | 201 // return appropriate values in |error_code|. |
| 207 mutable base::Lock fetch_state_lock_; | 202 mutable base::Lock fetch_state_lock_; |
| 208 URLFetchState fetch_state_; | 203 URLFetchState fetch_state_; |
| 209 | 204 |
| 210 DISALLOW_COPY_AND_ASSIGN(HttpBridge); | 205 DISALLOW_COPY_AND_ASSIGN(HttpBridge); |
| 211 }; | 206 }; |
| 212 | 207 |
| 213 class HttpBridgeFactory : public csync::HttpPostProviderFactory { | 208 class HttpBridgeFactory : public csync::HttpPostProviderFactory { |
| 214 public: | 209 public: |
| 215 explicit HttpBridgeFactory( | 210 HttpBridgeFactory( |
| 216 net::URLRequestContextGetter* baseline_context_getter); | 211 net::URLRequestContextGetter* baseline_context_getter, |
| 212 const std::string& user_agent); |
| 217 virtual ~HttpBridgeFactory(); | 213 virtual ~HttpBridgeFactory(); |
| 218 | 214 |
| 219 // csync::HttpPostProviderFactory: | 215 // csync::HttpPostProviderFactory: |
| 220 virtual csync::HttpPostProviderInterface* Create() OVERRIDE; | 216 virtual csync::HttpPostProviderInterface* Create() OVERRIDE; |
| 221 virtual void Destroy(csync::HttpPostProviderInterface* http) OVERRIDE; | 217 virtual void Destroy(csync::HttpPostProviderInterface* http) OVERRIDE; |
| 222 | 218 |
| 223 private: | 219 private: |
| 224 // This request context is built on top of the baseline context and shares | 220 // This request context is built on top of the baseline context and shares |
| 225 // common components. | 221 // common components. |
| 226 HttpBridge::RequestContextGetter* GetRequestContextGetter(); | 222 HttpBridge::RequestContextGetter* GetRequestContextGetter(); |
| 227 | 223 |
| 228 scoped_refptr<HttpBridge::RequestContextGetter> request_context_getter_; | 224 const scoped_refptr<HttpBridge::RequestContextGetter> |
| 225 request_context_getter_; |
| 229 | 226 |
| 230 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); | 227 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); |
| 231 }; | 228 }; |
| 232 | 229 |
| 233 } // namespace browser_sync | 230 } // namespace browser_sync |
| 234 | 231 |
| 235 #endif // CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 232 #endif // SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_ |
| OLD | NEW |