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 CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ |
6 #define CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_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" |
17 #include "content/public/common/url_fetcher_delegate.h" | |
18 #include "googleurl/src/gurl.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" | 19 #include "net/url_request/url_request_context.h" |
20 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
21 #include "sync/internal_api/http_post_provider_factory.h" | 21 #include "sync/internal_api/http_post_provider_factory.h" |
22 #include "sync/internal_api/http_post_provider_interface.h" | 22 #include "sync/internal_api/http_post_provider_interface.h" |
23 | 23 |
24 class MessageLoop; | 24 class MessageLoop; |
25 class HttpBridgeTest; | 25 class HttpBridgeTest; |
26 | 26 |
27 namespace net { | 27 namespace net { |
28 class HttpResponseHeaders; | 28 class HttpResponseHeaders; |
| 29 class URLFetcher; |
29 } | 30 } |
30 | 31 |
31 namespace browser_sync { | 32 namespace browser_sync { |
32 | 33 |
33 // A bridge between the syncer and Chromium HTTP layers. | 34 // A bridge between the syncer and Chromium HTTP layers. |
34 // Provides a way for the sync backend to use Chromium directly for HTTP | 35 // Provides a way for the sync backend to use Chromium directly for HTTP |
35 // requests rather than depending on a third party provider (e.g libcurl). | 36 // requests rather than depending on a third party provider (e.g libcurl). |
36 // This is a one-time use bridge. Create one for each request you want to make. | 37 // This is a one-time use bridge. Create one for each request you want to make. |
37 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus | 38 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus |
38 // needs to stick around across context switches, etc. | 39 // needs to stick around across context switches, etc. |
39 class HttpBridge : public base::RefCountedThreadSafe<HttpBridge>, | 40 class HttpBridge : public base::RefCountedThreadSafe<HttpBridge>, |
40 public sync_api::HttpPostProviderInterface, | 41 public sync_api::HttpPostProviderInterface, |
41 public content::URLFetcherDelegate { | 42 public net::URLFetcherDelegate { |
42 public: | 43 public: |
43 // 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. |
44 // 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 |
45 // 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. |
46 class RequestContext : public net::URLRequestContext { | 47 class RequestContext : public net::URLRequestContext { |
47 public: | 48 public: |
48 // |baseline_context| is used to obtain the accept-language, | 49 // |baseline_context| is used to obtain the accept-language, |
49 // accept-charsets, and proxy service information for bridged requests. | 50 // accept-charsets, and proxy service information for bridged requests. |
50 // Typically |baseline_context| should be the net::URLRequestContext of the | 51 // Typically |baseline_context| should be the net::URLRequestContext of the |
51 // currently active profile. | 52 // currently active profile. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 116 |
116 // WARNING: these response content methods are used to extract plain old data | 117 // WARNING: these response content methods are used to extract plain old data |
117 // and not null terminated strings, so you should make sure you have read | 118 // and not null terminated strings, so you should make sure you have read |
118 // GetResponseContentLength() characters when using GetResponseContent. e.g | 119 // GetResponseContentLength() characters when using GetResponseContent. e.g |
119 // string r(b->GetResponseContent(), b->GetResponseContentLength()). | 120 // string r(b->GetResponseContent(), b->GetResponseContentLength()). |
120 virtual int GetResponseContentLength() const OVERRIDE; | 121 virtual int GetResponseContentLength() const OVERRIDE; |
121 virtual const char* GetResponseContent() const OVERRIDE; | 122 virtual const char* GetResponseContent() const OVERRIDE; |
122 virtual const std::string GetResponseHeaderValue( | 123 virtual const std::string GetResponseHeaderValue( |
123 const std::string& name) const OVERRIDE; | 124 const std::string& name) const OVERRIDE; |
124 | 125 |
125 // content::URLFetcherDelegate implementation. | 126 // net::URLFetcherDelegate implementation. |
126 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 127 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
127 | 128 |
128 #if defined(UNIT_TEST) | 129 #if defined(UNIT_TEST) |
129 net::URLRequestContextGetter* GetRequestContextGetter() const { | 130 net::URLRequestContextGetter* GetRequestContextGetter() const { |
130 return context_getter_for_request_; | 131 return context_getter_for_request_; |
131 } | 132 } |
132 #endif | 133 #endif |
133 | 134 |
134 protected: | 135 protected: |
135 friend class base::RefCountedThreadSafe<HttpBridge>; | 136 friend class base::RefCountedThreadSafe<HttpBridge>; |
136 | 137 |
137 virtual ~HttpBridge(); | 138 virtual ~HttpBridge(); |
138 | 139 |
139 // Protected virtual so the unit test can override to shunt network requests. | 140 // Protected virtual so the unit test can override to shunt network requests. |
140 virtual void MakeAsynchronousPost(); | 141 virtual void MakeAsynchronousPost(); |
141 | 142 |
142 private: | 143 private: |
143 friend class ::HttpBridgeTest; | 144 friend class ::HttpBridgeTest; |
144 | 145 |
145 // Called on the IO loop to issue the network request. The extra level | 146 // Called on the IO loop to issue the network request. The extra level |
146 // of indirection is so that the unit test can override this behavior but we | 147 // of indirection is so that the unit test can override this behavior but we |
147 // still have a function to statically pass to PostTask. | 148 // still have a function to statically pass to PostTask. |
148 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); } | 149 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); } |
149 | 150 |
150 // Used to destroy a fetcher when the bridge is Abort()ed, to ensure that | 151 // Used to destroy a fetcher when the bridge is Abort()ed, to ensure that |
151 // a reference to |this| is held while flushing any pending fetch completion | 152 // a reference to |this| is held while flushing any pending fetch completion |
152 // callbacks coming from the IO thread en route to finally destroying the | 153 // callbacks coming from the IO thread en route to finally destroying the |
153 // fetcher. | 154 // fetcher. |
154 void DestroyURLFetcherOnIOThread(content::URLFetcher* fetcher); | 155 void DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher); |
155 | 156 |
156 // Gets a customized net::URLRequestContext for bridged requests. See | 157 // Gets a customized net::URLRequestContext for bridged requests. See |
157 // RequestContext definition for details. | 158 // RequestContext definition for details. |
158 scoped_refptr<RequestContextGetter> context_getter_for_request_; | 159 scoped_refptr<RequestContextGetter> context_getter_for_request_; |
159 | 160 |
160 // The message loop of the thread we were created on. This is the thread that | 161 // The message loop of the thread we were created on. This is the thread that |
161 // will block on MakeSynchronousPost while the IO thread fetches data from | 162 // will block on MakeSynchronousPost while the IO thread fetches data from |
162 // the network. | 163 // the network. |
163 // This should be the main syncer thread (SyncerThread) which is what blocks | 164 // This should be the main syncer thread (SyncerThread) which is what blocks |
164 // on network IO through curl_easy_perform. | 165 // on network IO through curl_easy_perform. |
(...skipping 13 matching lines...) Expand all Loading... |
178 base::WaitableEvent http_post_completed_; | 179 base::WaitableEvent http_post_completed_; |
179 | 180 |
180 struct URLFetchState { | 181 struct URLFetchState { |
181 URLFetchState(); | 182 URLFetchState(); |
182 ~URLFetchState(); | 183 ~URLFetchState(); |
183 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO | 184 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO |
184 // LOOP, so we can block created_on_loop_ while the fetch is in progress. | 185 // LOOP, so we can block created_on_loop_ while the fetch is in progress. |
185 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the | 186 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the |
186 // same thread that created it, which isn't the same thread |this| gets | 187 // same thread that created it, which isn't the same thread |this| gets |
187 // deleted on. We must manually delete url_poster_ on the IO loop. | 188 // deleted on. We must manually delete url_poster_ on the IO loop. |
188 content::URLFetcher* url_poster; | 189 net::URLFetcher* url_poster; |
189 | 190 |
190 // Used to support 'Abort' functionality. | 191 // Used to support 'Abort' functionality. |
191 bool aborted; | 192 bool aborted; |
192 | 193 |
193 // Cached response data. | 194 // Cached response data. |
194 bool request_completed; | 195 bool request_completed; |
195 bool request_succeeded; | 196 bool request_succeeded; |
196 int http_response_code; | 197 int http_response_code; |
197 int error_code; | 198 int error_code; |
198 std::string response_content; | 199 std::string response_content; |
(...skipping 26 matching lines...) Expand all Loading... |
225 HttpBridge::RequestContextGetter* GetRequestContextGetter(); | 226 HttpBridge::RequestContextGetter* GetRequestContextGetter(); |
226 | 227 |
227 scoped_refptr<HttpBridge::RequestContextGetter> request_context_getter_; | 228 scoped_refptr<HttpBridge::RequestContextGetter> request_context_getter_; |
228 | 229 |
229 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); | 230 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); |
230 }; | 231 }; |
231 | 232 |
232 } // namespace browser_sync | 233 } // namespace browser_sync |
233 | 234 |
234 #endif // CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 235 #endif // CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ |
OLD | NEW |