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

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

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