Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: chrome/browser/sync/glue/http_bridge.h

Issue 160542: Rolling back 22317 (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/trunk/src/
Patch Set: Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifdef CHROME_PERSONALIZATION
6
7 #ifndef CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_
8 #define CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_
9
10 #include "base/ref_counted.h"
11 #include "base/waitable_event.h"
12 #include "chrome/browser/net/url_fetcher.h"
13 #include "chrome/browser/sync/engine/syncapi.h"
14 #include "googleurl/src/gurl.h"
15 #include "net/url_request/url_request_context.h"
16 #include "testing/gtest/include/gtest/gtest_prod.h"
17
18 class MessageLoop;
19 class HttpBridgeTest;
20
21 namespace browser_sync {
22
23 // A bridge between the syncer and Chromium HTTP layers.
24 // Provides a way for the sync backend to use Chromium directly for HTTP
25 // requests rather than depending on a third party provider (e.g libcurl).
26 // This is a one-time use bridge. Create one for each request you want to make.
27 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus
28 // needs to stick around across context switches, etc.
29 class HttpBridge : public base::RefCountedThreadSafe<HttpBridge>,
30 public sync_api::HttpPostProviderInterface,
31 public URLFetcher::Delegate {
32 public:
33 // A request context used for HTTP requests bridged from the sync backend.
34 // A bridged RequestContext has a dedicated in-memory cookie store and does
35 // not use a cache. Thus the same type can be used for incognito mode.
36 // TODO(timsteele): We subclass here instead of add a factory method on
37 // ChromeURLRequestContext because:
38 // 1) we want the ability to set_user_agent
39 // 2) avoids ifdefs for now
40 // 3) not sure we want to strictly follow settings for cookie policy,
41 // accept lang/charset, since changing these could break syncing.
42 class RequestContext : public URLRequestContext {
43 public:
44 // |baseline_context| is used to obtain the accept-language,
45 // accept-charsets, and proxy service information for bridged requests.
46 // Typically |baseline_context| should be the URLRequestContext of the
47 // currently active profile.
48 explicit RequestContext(const URLRequestContext* baseline_context);
49 virtual ~RequestContext();
50
51 // Set the user agent for requests using this context. The default is
52 // the browser's UA string.
53 void set_user_agent(const std::string& ua) { user_agent_ = ua; }
54 bool is_user_agent_set() const { return !user_agent_.empty(); }
55
56 virtual const std::string& GetUserAgent(const GURL& url) const {
57 // If the user agent is set explicitly return that, otherwise call the
58 // base class method to return default value.
59 return user_agent_.empty() ?
60 URLRequestContext::GetUserAgent(url) : user_agent_;
61 }
62
63 private:
64 std::string user_agent_;
65
66 DISALLOW_COPY_AND_ASSIGN(RequestContext);
67 };
68
69 HttpBridge(RequestContext* context, MessageLoop* io_loop);
70 virtual ~HttpBridge();
71
72 // sync_api::HttpPostProvider implementation.
73 virtual void SetUserAgent(const char* user_agent);
74 virtual void SetURL(const char* url, int port);
75 virtual void SetPostPayload(const char* content_type, int content_length,
76 const char* content);
77 virtual void AddCookieForRequest(const char* cookie);
78 virtual bool MakeSynchronousPost(int* os_error_code, int* response_code);
79 virtual int GetResponseContentLength() const;
80 virtual const char* GetResponseContent() const;
81 virtual int GetResponseCookieCount() const;
82 virtual const char* GetResponseCookieAt(int cookie_number) const;
83
84 // URLFetcher::Delegate implementation.
85 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url,
86 const URLRequestStatus& status,
87 int response_code,
88 const ResponseCookies& cookies,
89 const std::string& data);
90
91 protected:
92 // Protected virtual so the unit test can override to shunt network requests.
93 virtual void MakeAsynchronousPost();
94
95 private:
96 friend class ::HttpBridgeTest;
97
98 // Called on the io_loop_ to issue the network request. The extra level
99 // of indirection is so that the unit test can override this behavior but we
100 // still have a function to statically pass to PostTask.
101 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); }
102
103 // A customized URLRequestContext for bridged requests. See RequestContext
104 // definition for details.
105 RequestContext* context_for_request_;
106
107 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO LOOP,
108 // so we can block created_on_loop_ while the fetch is in progress.
109 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the same
110 // thread that created it, which isn't the same thread |this| gets deleted on.
111 // We must manually delete url_poster_ on the io_loop_.
112 URLFetcher* url_poster_;
113
114 // The message loop of the thread we were created on. This is the thread that
115 // will block on MakeSynchronousPost while the IO thread fetches data from
116 // the network.
117 // This should be the main syncer thread (SyncerThread) which is what blocks
118 // on network IO through curl_easy_perform.
119 MessageLoop* const created_on_loop_;
120
121 // Member variable for the IO loop instead of asking ChromeThread directly,
122 // done this way for testability.
123 MessageLoop* const io_loop_;
124
125 // The URL to POST to.
126 GURL url_for_request_;
127
128 // POST payload information.
129 std::string content_type_;
130 std::string request_content_;
131
132 // Cached response data.
133 bool request_completed_;
134 bool request_succeeded_;
135 int http_response_code_;
136 int os_error_code_;
137 ResponseCookies response_cookies_;
138 std::string response_content_;
139
140 // A waitable event we use to provide blocking semantics to
141 // MakeSynchronousPost. We block created_on_loop_ while the io_loop_ fetches
142 // network request.
143 base::WaitableEvent http_post_completed_;
144
145 // This is here so that the unit test subclass can force our URLFetcher to
146 // use the io_loop_ passed on construction for network requests, rather than
147 // ChromeThread::IO's message loop (which won't exist in testing).
148 bool use_io_loop_for_testing_;
149
150 DISALLOW_COPY_AND_ASSIGN(HttpBridge);
151 };
152
153 class HttpBridgeFactory
154 : public sync_api::HttpPostProviderFactory {
155 public:
156 HttpBridgeFactory() : request_context_(NULL) { }
157 virtual ~HttpBridgeFactory();
158 virtual sync_api::HttpPostProviderInterface* Create();
159 virtual void Destroy(sync_api::HttpPostProviderInterface* http);
160 private:
161 HttpBridge::RequestContext* GetRequestContext();
162 // We must Release() this from the IO thread.
163 HttpBridge::RequestContext* request_context_;
164 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory);
165 };
166
167 } // namespace browser_sync
168
169 #endif // CHROME_BROWSER_SYNC_GLUE_HTTP_BRIDGE_H_
170
171 #endif // CHROME_PERSONALIZATION
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/bookmark_model_worker_unittest.cc ('k') | chrome/browser/sync/glue/http_bridge.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698