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