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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// Copyright (c) 2011 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_util.h"
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "base/values.h"
#include "chrome/browser/automation/automation_provider.h"
#include "chrome/browser/automation/automation_provider_json.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser.h"
#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/browser_render_process_host.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "net/base/cookie_monster.h"
#include "net/base/cookie_store.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
namespace {
void GetCookiesOnIOThread(
const GURL& url,
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
base::WaitableEvent* event,
std::string* cookies) {
*cookies =
context_getter->GetURLRequestContext()->cookie_store()->GetCookies(url);
event->Signal();
}
void GetCanonicalCookiesOnIOThread(
const GURL& url,
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
base::WaitableEvent* event,
net::CookieList* cookie_list) {
*cookie_list =
context_getter->GetURLRequestContext()->cookie_store()->
GetCookieMonster()->GetAllCookiesForURL(url);
event->Signal();
}
void SetCookieOnIOThread(
const GURL& url,
const std::string& value,
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
base::WaitableEvent* event,
bool* success) {
*success =
context_getter->GetURLRequestContext()->cookie_store()->
SetCookie(url, value);
event->Signal();
}
void SetCookieWithDetailsOnIOThread(
const GURL& url,
const net::CookieMonster::CanonicalCookie& cookie,
const std::string& original_domain,
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
base::WaitableEvent* event,
bool* success) {
net::CookieMonster* cookie_monster =
context_getter->GetURLRequestContext()->cookie_store()->
GetCookieMonster();
*success = cookie_monster->SetCookieWithDetails(
url, cookie.Name(), cookie.Value(), original_domain,
cookie.Path(), cookie.ExpiryDate(), cookie.IsSecure(),
cookie.IsHttpOnly());
event->Signal();
}
void DeleteCookieOnIOThread(
const GURL& url,
const std::string& name,
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
base::WaitableEvent* event) {
context_getter->GetURLRequestContext()->cookie_store()->
DeleteCookie(url, name);
event->Signal();
}
} // namespace
namespace automation_util {
Browser* GetBrowserAt(int index) {
if (index < 0 || index >= static_cast<int>(BrowserList::size()))
return NULL;
return *(BrowserList::begin() + index);
}
TabContents* GetTabContentsAt(int browser_index, int tab_index) {
if (tab_index < 0)
return NULL;
Browser* browser = GetBrowserAt(browser_index);
if (!browser || tab_index >= browser->tab_count())
return NULL;
return browser->GetTabContentsAt(tab_index);
}
void GetCookies(const GURL& url,
TabContents* contents,
int* value_size,
std::string* value) {
*value_size = -1;
if (url.is_valid() && contents) {
// Since we may be on the UI thread don't call GetURLRequestContext().
// Get the request context specific to the current TabContents and app.
const Extension* installed_app = static_cast<BrowserRenderProcessHost*>(
contents->render_view_host()->process())->installed_app();
scoped_refptr<net::URLRequestContextGetter> context_getter =
contents->profile()->GetRequestContextForPossibleApp(installed_app);
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
CHECK(BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&GetCookiesOnIOThread,
url, context_getter, &event, value)));
event.Wait();
*value_size = static_cast<int>(value->size());
}
}
void SetCookie(const GURL& url,
const std::string& value,
TabContents* contents,
int* response_value) {
*response_value = -1;
if (url.is_valid() && contents) {
// Since we may be on the UI thread don't call GetURLRequestContext().
// Get the request context specific to the current TabContents and app.
const Extension* installed_app = static_cast<BrowserRenderProcessHost*>(
contents->render_view_host()->process())->installed_app();
scoped_refptr<net::URLRequestContextGetter> context_getter =
contents->profile()->GetRequestContextForPossibleApp(installed_app);
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
bool success = false;
CHECK(BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&SetCookieOnIOThread,
url, value, context_getter, &event,
&success)));
event.Wait();
if (success)
*response_value = 1;
}
}
void DeleteCookie(const GURL& url,
const std::string& cookie_name,
TabContents* contents,
bool* success) {
*success = false;
if (url.is_valid() && contents) {
// Since we may be on the UI thread don't call GetURLRequestContext().
// Get the request context specific to the current TabContents and app.
const Extension* installed_app = static_cast<BrowserRenderProcessHost*>(
contents->render_view_host()->process())->installed_app();
scoped_refptr<net::URLRequestContextGetter> context_getter =
contents->profile()->GetRequestContextForPossibleApp(installed_app);
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
CHECK(BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&DeleteCookieOnIOThread,
url, cookie_name, context_getter, &event)));
event.Wait();
*success = true;
}
}
void GetCookiesJSON(AutomationProvider* provider,
DictionaryValue* args,
IPC::Message* reply_message) {
AutomationJSONReply reply(provider, reply_message);
std::string url;
if (!args->GetString("url", &url)) {
reply.SendError("'url' missing or invalid");
return;
}
// Since we may be on the UI thread don't call GetURLRequestContext().
scoped_refptr<net::URLRequestContextGetter> context_getter =
provider->profile()->GetRequestContext();
net::CookieList cookie_list;
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
Task* task = NewRunnableFunction(
&GetCanonicalCookiesOnIOThread,
GURL(url), context_getter, &event, &cookie_list);
if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
reply.SendError("Couldn't post task to get the cookies");
return;
}
event.Wait();
ListValue* list = new ListValue();
for (size_t i = 0; i < cookie_list.size(); ++i) {
const net::CookieMonster::CanonicalCookie& cookie = cookie_list[i];
DictionaryValue* cookie_dict = new DictionaryValue();
cookie_dict->SetString("name", cookie.Name());
cookie_dict->SetString("value", cookie.Value());
cookie_dict->SetString("path", cookie.Path());
cookie_dict->SetString("domain", cookie.Domain());
cookie_dict->SetBoolean("secure", cookie.IsSecure());
cookie_dict->SetBoolean("http_only", cookie.IsHttpOnly());
if (cookie.DoesExpire())
cookie_dict->SetDouble("expiry", cookie.ExpiryDate().ToDoubleT());
list->Append(cookie_dict);
}
DictionaryValue dict;
dict.Set("cookies", list);
reply.SendSuccess(&dict);
}
void DeleteCookieJSON(AutomationProvider* provider,
DictionaryValue* args,
IPC::Message* reply_message) {
AutomationJSONReply reply(provider, reply_message);
std::string url, name;
if (!args->GetString("url", &url)) {
reply.SendError("'url' missing or invalid");
return;
}
if (!args->GetString("name", &name)) {
reply.SendError("'name' missing or invalid");
return;
}
// Since we may be on the UI thread don't call GetURLRequestContext().
scoped_refptr<net::URLRequestContextGetter> context_getter =
provider->profile()->GetRequestContext();
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
Task* task = NewRunnableFunction(
&DeleteCookieOnIOThread,
GURL(url), name, context_getter, &event);
if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
reply.SendError("Couldn't post task to delete the cookie");
return;
}
event.Wait();
reply.SendSuccess(NULL);
}
void SetCookieJSON(AutomationProvider* provider,
DictionaryValue* args,
IPC::Message* reply_message) {
AutomationJSONReply reply(provider, reply_message);
std::string url;
if (!args->GetString("url", &url)) {
reply.SendError("'url' missing or invalid");
return;
}
DictionaryValue* cookie_dict;
if (!args->GetDictionary("cookie", &cookie_dict)) {
reply.SendError("'cookie' missing or invalid");
return;
}
std::string name, value;
std::string domain;
std::string path = "/";
bool secure = false;
double expiry = 0;
bool http_only = false;
if (!cookie_dict->GetString("name", &name)) {
reply.SendError("'name' missing or invalid");
return;
}
if (!cookie_dict->GetString("value", &value)) {
reply.SendError("'value' missing or invalid");
return;
}
if (cookie_dict->HasKey("domain") &&
!cookie_dict->GetString("domain", &domain)) {
reply.SendError("optional 'domain' invalid");
return;
}
if (cookie_dict->HasKey("path") &&
!cookie_dict->GetString("path", &path)) {
reply.SendError("optional 'path' invalid");
return;
}
if (cookie_dict->HasKey("secure") &&
!cookie_dict->GetBoolean("secure", &secure)) {
reply.SendError("optional 'secure' invalid");
return;
}
if (cookie_dict->HasKey("expiry")) {
int expiry_int;
if (cookie_dict->GetInteger("expiry", &expiry_int)) {
expiry = expiry_int;
} else if (!cookie_dict->GetDouble("expiry", &expiry)) {
reply.SendError("optional 'expiry' invalid");
return;
}
}
if (cookie_dict->HasKey("http_only") &&
!cookie_dict->GetBoolean("http_only", &http_only)) {
reply.SendError("optional 'http_only' invalid");
return;
}
scoped_ptr<net::CookieMonster::CanonicalCookie> cookie(
net::CookieMonster::CanonicalCookie::Create(
GURL(url), name, value, domain, path, base::Time(),
base::Time::FromDoubleT(expiry), secure, http_only));
if (!cookie.get()) {
reply.SendError("given 'cookie' parameters are invalid");
return;
}
// Since we may be on the UI thread don't call GetURLRequestContext().
scoped_refptr<net::URLRequestContextGetter> context_getter =
provider->profile()->GetRequestContext();
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
bool success = false;
Task* task = NewRunnableFunction(
&SetCookieWithDetailsOnIOThread,
GURL(url), *cookie.get(), domain, context_getter, &event, &success);
if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
reply.SendError("Couldn't post task to set the cookie");
return;
}
event.Wait();
if (!success) {
reply.SendError("Could not set the cookie");
return;
}
reply.SendSuccess(NULL);
}
} // namespace automation_util
|