OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2009 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 #include "chrome/browser/automation/automation_profile_impl.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "chrome/browser/automation/automation_resource_message_filter.h" | |
10 #include "chrome/browser/chrome_thread.h" | |
11 #include "chrome/browser/net/chrome_url_request_context.h" | |
12 #include "chrome/browser/profile.h" | |
13 #include "net/base/net_errors.h" | |
14 #include "net/url_request/url_request_context.h" | |
15 #include "chrome/test/automation/automation_messages.h" | |
16 | |
17 namespace AutomationRequestContext { | |
18 | |
19 // A special Request context for automation. Substitute a few things | |
20 // like cookie store, proxy settings etc to handle them differently | |
21 // for automation. | |
22 class AutomationURLRequestContext : public ChromeURLRequestContext { | |
23 public: | |
24 AutomationURLRequestContext(ChromeURLRequestContext* original_context, | |
25 net::CookieStore* automation_cookie_store, | |
26 net::CookiePolicy* automation_cookie_policy) | |
27 : ChromeURLRequestContext(original_context), | |
28 // We must hold a reference to |original_context|, since many | |
29 // of the dependencies that ChromeURLRequestContext(original_context) | |
30 // copied are scoped to |original_context|. | |
31 original_context_(original_context) { | |
32 cookie_policy_ = automation_cookie_policy; | |
33 cookie_store_ = automation_cookie_store; | |
34 } | |
35 | |
36 virtual bool IsExternal() const { | |
37 return true; | |
38 } | |
39 | |
40 private: | |
41 virtual ~AutomationURLRequestContext() { | |
42 // Clear out members before calling base class dtor since we don't | |
43 // own any of them. | |
44 | |
45 // Clear URLRequestContext members. | |
46 host_resolver_ = NULL; | |
47 proxy_service_ = NULL; | |
48 http_transaction_factory_ = NULL; | |
49 ftp_transaction_factory_ = NULL; | |
50 cookie_store_ = NULL; | |
51 transport_security_state_ = NULL; | |
52 } | |
53 | |
54 scoped_refptr<ChromeURLRequestContext> original_context_; | |
55 DISALLOW_COPY_AND_ASSIGN(AutomationURLRequestContext); | |
56 }; | |
57 | |
58 // CookieStore specialization to have automation specific | |
59 // behavior for cookies. | |
60 class AutomationCookieStore : public net::CookieStore { | |
61 public: | |
62 AutomationCookieStore(net::CookieStore* original_cookie_store, | |
63 AutomationResourceMessageFilter* automation_client, | |
64 int tab_handle) | |
65 : original_cookie_store_(original_cookie_store), | |
66 automation_client_(automation_client), | |
67 tab_handle_(tab_handle) { | |
68 } | |
69 | |
70 virtual ~AutomationCookieStore() { | |
71 DLOG(INFO) << "In " << __FUNCTION__; | |
72 } | |
73 | |
74 // CookieStore implementation. | |
75 virtual bool SetCookieWithOptions(const GURL& url, | |
76 const std::string& cookie_line, | |
77 const net::CookieOptions& options) { | |
78 // The cookie_string_ is available only once, i.e. once it is read by | |
79 // it is invalidated. | |
80 cookie_string_ = cookie_line; | |
81 return true; | |
82 } | |
83 | |
84 virtual std::string GetCookiesWithOptions(const GURL& url, | |
85 const net::CookieOptions& options) { | |
86 return cookie_string_; | |
87 } | |
88 | |
89 virtual void DeleteCookie(const GURL& url, | |
90 const std::string& cookie_name) { | |
91 NOTREACHED() << "Should not get called for an automation profile"; | |
92 } | |
93 | |
94 virtual net::CookieMonster* GetCookieMonster() { | |
95 NOTREACHED() << "Should not get called for an automation profile"; | |
96 return NULL; | |
97 } | |
98 | |
99 protected: | |
100 net::CookieStore* original_cookie_store_; | |
101 scoped_refptr<AutomationResourceMessageFilter> automation_client_; | |
102 int tab_handle_; | |
103 std::string cookie_string_; | |
104 | |
105 private: | |
106 DISALLOW_COPY_AND_ASSIGN(AutomationCookieStore); | |
107 }; | |
108 | |
109 // CookiePolicy specialization for automation specific cookie policies. | |
110 class AutomationCookiePolicy : public net::CookiePolicy { | |
111 public: | |
112 AutomationCookiePolicy(AutomationResourceMessageFilter* automation_client, | |
113 int tab_handle, net::CookieStore* cookie_store) | |
114 : automation_client_(automation_client), | |
115 tab_handle_(tab_handle), | |
116 cookie_store_(cookie_store) {} | |
117 | |
118 virtual int CanGetCookies(const GURL& url, | |
119 const GURL& first_party_for_cookies, | |
120 net::CompletionCallback* callback) { | |
121 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
122 | |
123 AutomationResourceMessageFilter::GetCookiesForUrl(tab_handle_, url, | |
124 callback, | |
125 cookie_store_.get()); | |
126 return net::ERR_IO_PENDING; | |
127 } | |
128 | |
129 virtual int CanSetCookie(const GURL& url, | |
130 const GURL& first_party_for_cookies, | |
131 const std::string& cookie_line, | |
132 net::CompletionCallback* callback) { | |
133 AutomationResourceMessageFilter::SetCookiesForUrl(tab_handle_, url, | |
134 cookie_line, | |
135 callback); | |
136 return net::ERR_IO_PENDING; | |
137 } | |
138 | |
139 private: | |
140 scoped_refptr<AutomationResourceMessageFilter> automation_client_; | |
141 int tab_handle_; | |
142 scoped_refptr<net::CookieStore> cookie_store_; | |
143 | |
144 DISALLOW_COPY_AND_ASSIGN(AutomationCookiePolicy); | |
145 }; | |
146 | |
147 class Factory : public ChromeURLRequestContextFactory { | |
148 public: | |
149 Factory(ChromeURLRequestContextGetter* original_context_getter, | |
150 Profile* profile, | |
151 AutomationResourceMessageFilter* automation_client, | |
152 int tab_handle) | |
153 : ChromeURLRequestContextFactory(profile), | |
154 original_context_getter_(original_context_getter), | |
155 automation_client_(automation_client), | |
156 tab_handle_(tab_handle) { | |
157 } | |
158 | |
159 virtual ChromeURLRequestContext* Create() { | |
160 ChromeURLRequestContext* original_context = | |
161 original_context_getter_->GetIOContext(); | |
162 | |
163 // Create an automation cookie store. | |
164 scoped_refptr<net::CookieStore> automation_cookie_store = | |
165 new AutomationCookieStore(original_context->cookie_store(), | |
166 automation_client_, | |
167 tab_handle_); | |
168 | |
169 // Create an automation cookie policy. | |
170 AutomationCookiePolicy* automation_cookie_policy = | |
171 new AutomationCookiePolicy(automation_client_, | |
172 tab_handle_, | |
173 automation_cookie_store); | |
174 | |
175 return new AutomationURLRequestContext(original_context, | |
176 automation_cookie_store, | |
177 automation_cookie_policy); | |
178 } | |
179 | |
180 private: | |
181 scoped_refptr<ChromeURLRequestContextGetter> original_context_getter_; | |
182 scoped_refptr<AutomationResourceMessageFilter> automation_client_; | |
183 int tab_handle_; | |
184 }; | |
185 | |
186 ChromeURLRequestContextGetter* CreateAutomationURLRequestContextForTab( | |
187 int tab_handle, | |
188 Profile* profile, | |
189 AutomationResourceMessageFilter* automation_client) { | |
190 ChromeURLRequestContextGetter* original_context = | |
191 static_cast<ChromeURLRequestContextGetter*>( | |
192 profile->GetRequestContext()); | |
193 | |
194 ChromeURLRequestContextGetter* request_context = | |
195 new ChromeURLRequestContextGetter( | |
196 NULL, // Don't register an observer on PrefService. | |
197 new Factory(original_context, profile, automation_client, | |
198 tab_handle)); | |
199 return request_context; | |
200 } | |
201 | |
202 } // namespace AutomationRequestContext | |
203 | |
OLD | NEW |