| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #if defined(BROWSER_SYNC) | 5 #if defined(BROWSER_SYNC) |
| 6 | 6 |
| 7 #ifndef CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 7 #ifndef CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ |
| 8 #define CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 8 #define CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
| 13 #include "base/waitable_event.h" | 13 #include "base/waitable_event.h" |
| 14 #include "chrome/browser/net/url_fetcher.h" | 14 #include "chrome/browser/net/url_fetcher.h" |
| 15 #include "chrome/browser/net/url_request_context_getter.h" |
| 15 #include "chrome/browser/sync/engine/syncapi.h" | 16 #include "chrome/browser/sync/engine/syncapi.h" |
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 18 #include "testing/gtest/include/gtest/gtest_prod.h" | 19 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 19 | 20 |
| 20 class MessageLoop; | 21 class MessageLoop; |
| 21 class HttpBridgeTest; | 22 class HttpBridgeTest; |
| 22 | 23 |
| 23 namespace browser_sync { | 24 namespace browser_sync { |
| 24 | 25 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 46 // |baseline_context| is used to obtain the accept-language, | 47 // |baseline_context| is used to obtain the accept-language, |
| 47 // accept-charsets, and proxy service information for bridged requests. | 48 // accept-charsets, and proxy service information for bridged requests. |
| 48 // Typically |baseline_context| should be the URLRequestContext of the | 49 // Typically |baseline_context| should be the URLRequestContext of the |
| 49 // currently active profile. | 50 // currently active profile. |
| 50 explicit RequestContext(URLRequestContext* baseline_context); | 51 explicit RequestContext(URLRequestContext* baseline_context); |
| 51 virtual ~RequestContext(); | 52 virtual ~RequestContext(); |
| 52 | 53 |
| 53 // Set the user agent for requests using this context. The default is | 54 // Set the user agent for requests using this context. The default is |
| 54 // the browser's UA string. | 55 // the browser's UA string. |
| 55 void set_user_agent(const std::string& ua) { user_agent_ = ua; } | 56 void set_user_agent(const std::string& ua) { user_agent_ = ua; } |
| 56 bool is_user_agent_set() const { return !user_agent_.empty(); } | |
| 57 | 57 |
| 58 virtual const std::string& GetUserAgent(const GURL& url) const { | 58 virtual const std::string& GetUserAgent(const GURL& url) const { |
| 59 // If the user agent is set explicitly return that, otherwise call the | 59 // If the user agent is set explicitly return that, otherwise call the |
| 60 // base class method to return default value. | 60 // base class method to return default value. |
| 61 return user_agent_.empty() ? | 61 return user_agent_.empty() ? |
| 62 URLRequestContext::GetUserAgent(url) : user_agent_; | 62 URLRequestContext::GetUserAgent(url) : user_agent_; |
| 63 } | 63 } |
| 64 | 64 |
| 65 virtual bool AllowSendingCookies(const URLRequest* request) const { | 65 virtual bool AllowSendingCookies(const URLRequest* request) const { |
| 66 return false; | 66 return false; |
| 67 } | 67 } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 std::string user_agent_; | 70 std::string user_agent_; |
| 71 URLRequestContext* baseline_context_; | 71 URLRequestContext* baseline_context_; |
| 72 | 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(RequestContext); | 73 DISALLOW_COPY_AND_ASSIGN(RequestContext); |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 HttpBridge(RequestContext* context, MessageLoop* io_loop); | 76 // Lazy-getter for RequestContext objects. |
| 77 class RequestContextGetter : public URLRequestContextGetter { |
| 78 public: |
| 79 explicit RequestContextGetter( |
| 80 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 // URLRequestContextGetter implementation. |
| 86 virtual URLRequestContext* GetURLRequestContext(); |
| 87 |
| 88 private: |
| 89 // User agent to apply to the URLRequestContext. |
| 90 std::string user_agent_; |
| 91 |
| 92 scoped_refptr<URLRequestContextGetter> baseline_context_getter_; |
| 93 |
| 94 // Lazily initialized by GetURLRequestContext(). |
| 95 scoped_refptr<RequestContext> context_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(RequestContextGetter); |
| 98 }; |
| 99 |
| 100 HttpBridge(RequestContextGetter* context, MessageLoop* io_loop); |
| 77 virtual ~HttpBridge(); | 101 virtual ~HttpBridge(); |
| 78 | 102 |
| 79 // sync_api::HttpPostProvider implementation. | 103 // sync_api::HttpPostProvider implementation. |
| 80 virtual void SetUserAgent(const char* user_agent); | 104 virtual void SetUserAgent(const char* user_agent); |
| 81 virtual void SetExtraRequestHeaders(const char* headers); | 105 virtual void SetExtraRequestHeaders(const char* headers); |
| 82 virtual void SetURL(const char* url, int port); | 106 virtual void SetURL(const char* url, int port); |
| 83 virtual void SetPostPayload(const char* content_type, int content_length, | 107 virtual void SetPostPayload(const char* content_type, int content_length, |
| 84 const char* content); | 108 const char* content); |
| 85 virtual bool MakeSynchronousPost(int* os_error_code, int* response_code); | 109 virtual bool MakeSynchronousPost(int* os_error_code, int* response_code); |
| 86 | 110 |
| 87 // WARNING: these response content methods are used to extract plain old data | 111 // WARNING: these response content methods are used to extract plain old data |
| 88 // and not null terminated strings, so you should make sure you have read | 112 // and not null terminated strings, so you should make sure you have read |
| 89 // GetResponseContentLength() characters when using GetResponseContent. e.g | 113 // GetResponseContentLength() characters when using GetResponseContent. e.g |
| 90 // string r(b->GetResponseContent(), b->GetResponseContentLength()). | 114 // string r(b->GetResponseContent(), b->GetResponseContentLength()). |
| 91 virtual int GetResponseContentLength() const; | 115 virtual int GetResponseContentLength() const; |
| 92 virtual const char* GetResponseContent() const; | 116 virtual const char* GetResponseContent() const; |
| 93 | 117 |
| 94 // URLFetcher::Delegate implementation. | 118 // URLFetcher::Delegate implementation. |
| 95 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, | 119 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, |
| 96 const URLRequestStatus& status, | 120 const URLRequestStatus& status, |
| 97 int response_code, | 121 int response_code, |
| 98 const ResponseCookies& cookies, | 122 const ResponseCookies& cookies, |
| 99 const std::string& data); | 123 const std::string& data); |
| 100 | 124 |
| 101 URLRequestContext* GetRequestContext() const; | 125 #if defined(UNIT_TEST) |
| 126 URLRequestContextGetter* GetRequestContextGetter() const { |
| 127 return context_getter_for_request_; |
| 128 } |
| 129 #endif |
| 102 | 130 |
| 103 protected: | 131 protected: |
| 104 // Protected virtual so the unit test can override to shunt network requests. | 132 // Protected virtual so the unit test can override to shunt network requests. |
| 105 virtual void MakeAsynchronousPost(); | 133 virtual void MakeAsynchronousPost(); |
| 106 | 134 |
| 107 private: | 135 private: |
| 108 friend class ::HttpBridgeTest; | 136 friend class ::HttpBridgeTest; |
| 109 | 137 |
| 110 // Called on the io_loop_ to issue the network request. The extra level | 138 // Called on the io_loop_ to issue the network request. The extra level |
| 111 // of indirection is so that the unit test can override this behavior but we | 139 // of indirection is so that the unit test can override this behavior but we |
| 112 // still have a function to statically pass to PostTask. | 140 // still have a function to statically pass to PostTask. |
| 113 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); } | 141 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); } |
| 114 | 142 |
| 115 // A customized URLRequestContext for bridged requests. See RequestContext | 143 // Gets a customized URLRequestContext for bridged requests. See |
| 116 // definition for details. | 144 // RequestContext definition for details. |
| 117 RequestContext* context_for_request_; | 145 RequestContextGetter* context_getter_for_request_; |
| 118 | 146 |
| 119 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO LOOP, | 147 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO LOOP, |
| 120 // so we can block created_on_loop_ while the fetch is in progress. | 148 // so we can block created_on_loop_ while the fetch is in progress. |
| 121 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the same | 149 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the same |
| 122 // thread that created it, which isn't the same thread |this| gets deleted on. | 150 // thread that created it, which isn't the same thread |this| gets deleted on. |
| 123 // We must manually delete url_poster_ on the io_loop_. | 151 // We must manually delete url_poster_ on the io_loop_. |
| 124 URLFetcher* url_poster_; | 152 URLFetcher* url_poster_; |
| 125 | 153 |
| 126 // The message loop of the thread we were created on. This is the thread that | 154 // The message loop of the thread we were created on. This is the thread that |
| 127 // will block on MakeSynchronousPost while the IO thread fetches data from | 155 // will block on MakeSynchronousPost while the IO thread fetches data from |
| (...skipping 30 matching lines...) Expand all Loading... |
| 158 // use the io_loop_ passed on construction for network requests, rather than | 186 // use the io_loop_ passed on construction for network requests, rather than |
| 159 // ChromeThread::IO's message loop (which won't exist in testing). | 187 // ChromeThread::IO's message loop (which won't exist in testing). |
| 160 bool use_io_loop_for_testing_; | 188 bool use_io_loop_for_testing_; |
| 161 | 189 |
| 162 DISALLOW_COPY_AND_ASSIGN(HttpBridge); | 190 DISALLOW_COPY_AND_ASSIGN(HttpBridge); |
| 163 }; | 191 }; |
| 164 | 192 |
| 165 class HttpBridgeFactory | 193 class HttpBridgeFactory |
| 166 : public sync_api::HttpPostProviderFactory { | 194 : public sync_api::HttpPostProviderFactory { |
| 167 public: | 195 public: |
| 168 explicit HttpBridgeFactory(URLRequestContext* baseline_context); | 196 explicit HttpBridgeFactory(URLRequestContextGetter* baseline_context_getter); |
| 169 virtual ~HttpBridgeFactory(); | 197 virtual ~HttpBridgeFactory(); |
| 170 virtual sync_api::HttpPostProviderInterface* Create(); | 198 virtual sync_api::HttpPostProviderInterface* Create(); |
| 171 virtual void Destroy(sync_api::HttpPostProviderInterface* http); | 199 virtual void Destroy(sync_api::HttpPostProviderInterface* http); |
| 172 private: | 200 private: |
| 173 // This request context is built on top of the baseline context and shares | 201 // This request context is built on top of the baseline context and shares |
| 174 // common components. | 202 // common components. |
| 175 HttpBridge::RequestContext* GetRequestContext(); | 203 HttpBridge::RequestContextGetter* GetRequestContextGetter(); |
| 176 // We must Release() this from the IO thread. | 204 // We must Release() this from the IO thread. |
| 177 HttpBridge::RequestContext* request_context_; | 205 HttpBridge::RequestContextGetter* request_context_getter_; |
| 178 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); | 206 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory); |
| 179 }; | 207 }; |
| 180 | 208 |
| 181 } // namespace browser_sync | 209 } // namespace browser_sync |
| 182 | 210 |
| 183 #endif // CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ | 211 #endif // CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_ |
| 184 | 212 |
| 185 #endif // defined(BROWSER_SYNC) | 213 #endif // defined(BROWSER_SYNC) |
| OLD | NEW |