| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | |
| 6 #define CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/synchronization/lock.h" | |
| 16 #include "base/synchronization/waitable_event.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 #include "net/url_request/url_fetcher_delegate.h" | |
| 19 #include "net/url_request/url_request_context.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 #include "sync/internal_api/public/http_post_provider_factory.h" | |
| 22 #include "sync/internal_api/public/http_post_provider_interface.h" | |
| 23 | |
| 24 class MessageLoop; | |
| 25 class HttpBridgeTest; | |
| 26 | |
| 27 namespace net { | |
| 28 class HttpResponseHeaders; | |
| 29 class URLFetcher; | |
| 30 } | |
| 31 | |
| 32 namespace browser_sync { | |
| 33 | |
| 34 // A bridge between the syncer and Chromium HTTP layers. | |
| 35 // Provides a way for the sync backend to use Chromium directly for HTTP | |
| 36 // requests rather than depending on a third party provider (e.g libcurl). | |
| 37 // This is a one-time use bridge. Create one for each request you want to make. | |
| 38 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus | |
| 39 // needs to stick around across context switches, etc. | |
| 40 class HttpBridge : public base::RefCountedThreadSafe<HttpBridge>, | |
| 41 public csync::HttpPostProviderInterface, | |
| 42 public net::URLFetcherDelegate { | |
| 43 public: | |
| 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 | |
| 46 // not use a cache. Thus the same type can be used for incognito mode. | |
| 47 class RequestContext : public net::URLRequestContext { | |
| 48 public: | |
| 49 // |baseline_context| is used to obtain the accept-language, | |
| 50 // accept-charsets, and proxy service information for bridged requests. | |
| 51 // Typically |baseline_context| should be the net::URLRequestContext of the | |
| 52 // currently active profile. | |
| 53 explicit RequestContext(net::URLRequestContext* baseline_context); | |
| 54 | |
| 55 // The destructor MUST be called on the IO thread. | |
| 56 virtual ~RequestContext(); | |
| 57 | |
| 58 // Set the user agent for requests using this context. The default is | |
| 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 | |
| 69 private: | |
| 70 std::string user_agent_; | |
| 71 net::URLRequestContext* baseline_context_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(RequestContext); | |
| 74 }; | |
| 75 | |
| 76 // Lazy-getter for RequestContext objects. | |
| 77 class RequestContextGetter : public net::URLRequestContextGetter { | |
| 78 public: | |
| 79 explicit RequestContextGetter( | |
| 80 net::URLRequestContextGetter* baseline_context_getter); | |
| 81 | |
| 82 void set_user_agent(const std::string& ua) { user_agent_ = ua; } | |
| 83 bool is_user_agent_set() const { return !user_agent_.empty(); } | |
| 84 | |
| 85 // net::URLRequestContextGetter implementation. | |
| 86 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; | |
| 87 virtual scoped_refptr<base::SingleThreadTaskRunner> | |
| 88 GetNetworkTaskRunner() const OVERRIDE; | |
| 89 | |
| 90 protected: | |
| 91 virtual ~RequestContextGetter(); | |
| 92 | |
| 93 private: | |
| 94 // User agent to apply to the net::URLRequestContext. | |
| 95 std::string user_agent_; | |
| 96 | |
| 97 scoped_refptr<net::URLRequestContextGetter> baseline_context_getter_; | |
| 98 | |
| 99 // Lazily initialized by GetURLRequestContext(). | |
| 100 scoped_ptr<RequestContext> context_; | |
| 101 | |
| 102 DISALLOW_COPY_AND_ASSIGN(RequestContextGetter); | |
| 103 }; | |
| 104 | |
| 105 explicit HttpBridge(RequestContextGetter* context); | |
| 106 | |
| 107 // csync::HttpPostProvider implementation. | |
| 108 virtual void SetUserAgent(const char* user_agent) OVERRIDE; | |
| 109 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE; | |
| 110 virtual void SetURL(const char* url, int port) OVERRIDE; | |
| 111 virtual void SetPostPayload(const char* content_type, int content_length, | |
| 112 const char* content) OVERRIDE; | |
| 113 virtual bool MakeSynchronousPost(int* error_code, | |
| 114 int* response_code) OVERRIDE; | |
| 115 virtual void Abort() OVERRIDE; | |
| 116 | |
| 117 // 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 | |
| 119 // GetResponseContentLength() characters when using GetResponseContent. e.g | |
| 120 // string r(b->GetResponseContent(), b->GetResponseContentLength()). | |
| 121 virtual int GetResponseContentLength() const OVERRIDE; | |
| 122 virtual const char* GetResponseContent() const OVERRIDE; | |
| 123 virtual const std::string GetResponseHeaderValue( | |
| 124 const std::string& name) const OVERRIDE; | |
| 125 | |
| 126 // net::URLFetcherDelegate implementation. | |
| 127 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 128 | |
| 129 #if defined(UNIT_TEST) | |
| 130 net::URLRequestContextGetter* GetRequestContextGetter() const { | |
| 131 return context_getter_for_request_; | |
| 132 } | |
| 133 #endif | |
| 134 | |
| 135 protected: | |
| 136 friend class base::RefCountedThreadSafe<HttpBridge>; | |
| 137 | |
| 138 virtual ~HttpBridge(); | |
| 139 | |
| 140 // Protected virtual so the unit test can override to shunt network requests. | |
| 141 virtual void MakeAsynchronousPost(); | |
| 142 | |
| 143 private: | |
| 144 friend class ::HttpBridgeTest; | |
| 145 | |
| 146 // Called on the IO loop to issue the network request. The extra level | |
| 147 // of indirection is so that the unit test can override this behavior but we | |
| 148 // still have a function to statically pass to PostTask. | |
| 149 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); } | |
| 150 | |
| 151 // 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 | |
| 153 // callbacks coming from the IO thread en route to finally destroying the | |
| 154 // fetcher. | |
| 155 void DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher); | |
| 156 | |
| 157 // Gets a customized net::URLRequestContext for bridged requests. See | |
| 158 // RequestContext definition for details. | |
| 159 scoped_refptr<RequestContextGetter> context_getter_for_request_; | |
| 160 | |
| 161 // 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 | |
| 163 // the network. | |
| 164 // This should be the main syncer thread (SyncerThread) which is what blocks | |
| 165 // on network IO through curl_easy_perform. | |
| 166 MessageLoop* const created_on_loop_; | |
| 167 | |
| 168 // The URL to POST to. | |
| 169 GURL url_for_request_; | |
| 170 | |
| 171 // POST payload information. | |
| 172 std::string content_type_; | |
| 173 std::string request_content_; | |
| 174 std::string extra_headers_; | |
| 175 | |
| 176 // A waitable event we use to provide blocking semantics to | |
| 177 // MakeSynchronousPost. We block created_on_loop_ while the IO loop fetches | |
| 178 // network request. | |
| 179 base::WaitableEvent http_post_completed_; | |
| 180 | |
| 181 struct URLFetchState { | |
| 182 URLFetchState(); | |
| 183 ~URLFetchState(); | |
| 184 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO | |
| 185 // LOOP, so we can block created_on_loop_ while the fetch is in progress. | |
| 186 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the | |
| 187 // same thread that created it, which isn't the same thread |this| gets | |
| 188 // deleted on. We must manually delete url_poster_ on the IO loop. | |
| 189 net::URLFetcher* url_poster; | |
| 190 | |
| 191 // Used to support 'Abort' functionality. | |
| 192 bool aborted; | |
| 193 | |
| 194 // Cached response data. | |
| 195 bool request_completed; | |
| 196 bool request_succeeded; | |
| 197 int http_response_code; | |
| 198 int error_code; | |
| 199 std::string response_content; | |
| 200 scoped_refptr<net::HttpResponseHeaders> response_headers; | |
| 201 }; | |
| 202 | |
| 203 // This lock synchronizes use of state involved in the flow to fetch a URL | |
| 204 // using URLFetcher. Because we can Abort() from any thread, for example, | |
| 205 // this flow needs to be synchronized to gracefully clean up URLFetcher and | |
| 206 // return appropriate values in |error_code|. | |
| 207 mutable base::Lock fetch_state_lock_; | |
| 208 URLFetchState fetch_state_; | |
| 209 | |
| 210 DISALLOW_COPY_AND_ASSIGN(HttpBridge); | |
| 211 }; | |
| 212 | |
| 213 class HttpBridgeFactory : public csync::HttpPostProviderFactory { | |
| 214 public: | |
| 215 explicit HttpBridgeFactory( | |
| 216 net::URLRequestContextGetter* baseline_context_getter); | |
| 217 virtual ~HttpBridgeFactory(); | |
| 218 | |
| 219 // csync::HttpPostProviderFactory: | |
| 220 virtual csync::HttpPostProviderInterface* Create() OVERRIDE; | |
| 221 virtual void Destroy(csync::HttpPostProviderInterface* http) OVERRIDE; | |
| 222 | |
| 223 private: | |
| 224 // This request context is built on top of the baseline context and shares | |
| 225 // common components. | |
| 226 HttpBridge::RequestContextGetter* GetRequestContextGetter(); | |
| 227 | |
| 228 scoped_refptr<HttpBridge::RequestContextGetter> request_context_getter_; | |
| 229 | |
| 230 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); | |
| 231 }; | |
| 232 | |
| 233 } // namespace browser_sync | |
| 234 | |
| 235 #endif // CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | |
| OLD | NEW |