Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/automation/automation_util.h" | 5 #include "chrome/browser/automation/automation_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/scoped_ptr.h" | |
| 10 #include "base/time.h" | |
| 9 #include "base/values.h" | 11 #include "base/values.h" |
| 10 #include "chrome/browser/automation/automation_provider_json.h" | 12 #include "chrome/browser/automation/automation_provider_json.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/renderer_host/browser_render_process_host.h" | 14 #include "chrome/browser/renderer_host/browser_render_process_host.h" |
| 13 #include "chrome/common/net/url_request_context_getter.h" | 15 #include "chrome/common/net/url_request_context_getter.h" |
| 14 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 15 #include "content/browser/browser_thread.h" | 17 #include "content/browser/browser_thread.h" |
| 16 #include "content/browser/renderer_host/render_view_host.h" | 18 #include "content/browser/renderer_host/render_view_host.h" |
| 19 #include "net/base/cookie_monster.h" | |
| 17 #include "net/base/cookie_store.h" | 20 #include "net/base/cookie_store.h" |
| 18 #include "net/url_request/url_request_context.h" | 21 #include "net/url_request/url_request_context.h" |
| 19 | 22 |
| 20 namespace { | 23 namespace { |
| 21 | 24 |
| 22 void GetCookiesOnIOThread( | 25 void GetCookiesOnIOThread( |
| 23 const GURL& url, | 26 const GURL& url, |
| 24 const scoped_refptr<URLRequestContextGetter>& context_getter, | 27 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 25 base::WaitableEvent* event, | 28 base::WaitableEvent* event, |
| 26 std::string* cookies) { | 29 std::string* cookies) { |
| 27 *cookies = context_getter->GetCookieStore()->GetCookies(url); | 30 *cookies = context_getter->GetCookieStore()->GetCookies(url); |
| 28 event->Signal(); | 31 event->Signal(); |
| 29 } | 32 } |
| 30 | 33 |
| 34 void GetCanonicalCookiesOnIOThread( | |
| 35 const GURL& url, | |
| 36 const scoped_refptr<URLRequestContextGetter>& context_getter, | |
| 37 base::WaitableEvent* event, | |
| 38 net::CookieList* cookie_list) { | |
| 39 *cookie_list = context_getter->GetCookieStore()->GetCookieMonster()-> | |
| 40 GetAllCookiesForURL(url); | |
| 41 event->Signal(); | |
| 42 } | |
| 43 | |
| 31 void SetCookieOnIOThread( | 44 void SetCookieOnIOThread( |
| 32 const GURL& url, | 45 const GURL& url, |
| 33 const std::string& value, | 46 const std::string& value, |
| 34 const scoped_refptr<URLRequestContextGetter>& context_getter, | 47 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 35 base::WaitableEvent* event, | 48 base::WaitableEvent* event, |
| 36 bool* success) { | 49 bool* success) { |
| 37 *success = context_getter->GetCookieStore()->SetCookie(url, value); | 50 *success = context_getter->GetCookieStore()->SetCookie(url, value); |
| 38 event->Signal(); | 51 event->Signal(); |
| 39 } | 52 } |
| 40 | 53 |
| 54 void SetCookieWithDetailsOnIOThread( | |
| 55 const GURL& url, | |
| 56 const net::CookieMonster::CanonicalCookie& cookie, | |
| 57 const std::string& original_domain, | |
| 58 const scoped_refptr<URLRequestContextGetter>& context_getter, | |
| 59 base::WaitableEvent* event, | |
| 60 bool* success) { | |
| 61 net::CookieMonster* cookie_monster = context_getter->GetCookieStore()-> | |
| 62 GetCookieMonster(); | |
| 63 *success = cookie_monster->SetCookieWithDetails( | |
| 64 url, cookie.Name(), cookie.Value(), original_domain, | |
| 65 cookie.Path(), cookie.ExpiryDate(), cookie.IsSecure(), | |
| 66 cookie.IsHttpOnly()); | |
| 67 event->Signal(); | |
| 68 } | |
| 69 | |
| 41 void DeleteCookieOnIOThread( | 70 void DeleteCookieOnIOThread( |
| 42 const GURL& url, | 71 const GURL& url, |
| 43 const std::string& name, | 72 const std::string& name, |
| 44 const scoped_refptr<URLRequestContextGetter>& context_getter, | 73 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 45 base::WaitableEvent* event) { | 74 base::WaitableEvent* event) { |
| 46 context_getter->GetCookieStore()->DeleteCookie(url, name); | 75 context_getter->GetCookieStore()->DeleteCookie(url, name); |
| 47 event->Signal(); | 76 event->Signal(); |
| 48 } | 77 } |
| 49 | 78 |
| 50 } // namespace | 79 } // namespace |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 url, cookie_name, context_getter, &event))); | 154 url, cookie_name, context_getter, &event))); |
| 126 event.Wait(); | 155 event.Wait(); |
| 127 *success = true; | 156 *success = true; |
| 128 } | 157 } |
| 129 } | 158 } |
| 130 | 159 |
| 131 void GetCookiesJSON(AutomationProvider* provider, | 160 void GetCookiesJSON(AutomationProvider* provider, |
| 132 DictionaryValue* args, | 161 DictionaryValue* args, |
| 133 IPC::Message* reply_message) { | 162 IPC::Message* reply_message) { |
| 134 AutomationJSONReply reply(provider, reply_message); | 163 AutomationJSONReply reply(provider, reply_message); |
| 135 Browser* browser; | |
| 136 std::string error; | |
| 137 if (!GetBrowserFromJSONArgs(args, &browser, &error)) { | |
| 138 reply.SendError(error); | |
| 139 return; | |
| 140 } | |
| 141 std::string url; | 164 std::string url; |
| 142 if (!args->GetString("url", &url)) { | 165 if (!args->GetString("url", &url)) { |
| 143 reply.SendError("'url' missing or invalid"); | 166 reply.SendError("'url' missing or invalid"); |
| 144 return; | 167 return; |
| 145 } | 168 } |
| 146 | 169 |
| 147 // Since we may be on the UI thread don't call GetURLRequestContext(). | 170 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 148 scoped_refptr<URLRequestContextGetter> context_getter = | 171 scoped_refptr<URLRequestContextGetter> context_getter = |
| 149 browser->profile()->GetRequestContext(); | 172 provider->profile()->GetRequestContext(); |
| 150 | 173 |
| 151 std::string cookies; | 174 net::CookieList cookie_list; |
| 152 base::WaitableEvent event(true /* manual reset */, | 175 base::WaitableEvent event(true /* manual reset */, |
| 153 false /* not initially signaled */); | 176 false /* not initially signaled */); |
| 154 Task* task = NewRunnableFunction( | 177 Task* task = NewRunnableFunction( |
| 155 &GetCookiesOnIOThread, | 178 &GetCanonicalCookiesOnIOThread, |
| 156 GURL(url), context_getter, &event, &cookies); | 179 GURL(url), context_getter, &event, &cookie_list); |
| 157 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { | 180 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { |
| 158 reply.SendError("Couldn't post task to get the cookies"); | 181 reply.SendError("Couldn't post task to get the cookies"); |
| 159 return; | 182 return; |
| 160 } | 183 } |
| 161 event.Wait(); | 184 event.Wait(); |
| 162 | 185 |
| 186 ListValue* list = new ListValue(); | |
| 187 for (size_t i = 0; i < cookie_list.size(); ++i) { | |
| 188 const net::CookieMonster::CanonicalCookie& cookie = cookie_list[i]; | |
| 189 DictionaryValue* cookie_dict = new DictionaryValue(); | |
| 190 cookie_dict->SetString("name", cookie.Name()); | |
| 191 cookie_dict->SetString("value", cookie.Value()); | |
| 192 cookie_dict->SetString("path", cookie.Path()); | |
| 193 cookie_dict->SetString("domain", cookie.Domain()); | |
| 194 cookie_dict->SetBoolean("secure", cookie.IsSecure()); | |
| 195 cookie_dict->SetBoolean("http_only", cookie.IsHttpOnly()); | |
| 196 if (cookie.DoesExpire()) | |
| 197 cookie_dict->SetDouble("expiry", cookie.ExpiryDate().ToDoubleT()); | |
|
Joe
2011/03/23 01:55:45
Is it possible to have a cookie that never expires
| |
| 198 list->Append(cookie_dict); | |
| 199 } | |
| 163 DictionaryValue dict; | 200 DictionaryValue dict; |
| 164 dict.SetString("cookies", cookies); | 201 dict.Set("cookies", list); |
| 165 reply.SendSuccess(&dict); | 202 reply.SendSuccess(&dict); |
| 166 } | 203 } |
| 167 | 204 |
| 168 void DeleteCookieJSON(AutomationProvider* provider, | 205 void DeleteCookieJSON(AutomationProvider* provider, |
| 169 DictionaryValue* args, | 206 DictionaryValue* args, |
| 170 IPC::Message* reply_message) { | 207 IPC::Message* reply_message) { |
| 171 AutomationJSONReply reply(provider, reply_message); | 208 AutomationJSONReply reply(provider, reply_message); |
| 172 Browser* browser; | |
| 173 std::string error; | |
| 174 if (!GetBrowserFromJSONArgs(args, &browser, &error)) { | |
| 175 reply.SendError(error); | |
| 176 return; | |
| 177 } | |
| 178 std::string url, name; | 209 std::string url, name; |
| 179 if (!args->GetString("url", &url)) { | 210 if (!args->GetString("url", &url)) { |
| 180 reply.SendError("'url' missing or invalid"); | 211 reply.SendError("'url' missing or invalid"); |
| 181 return; | 212 return; |
| 182 } | 213 } |
| 183 if (!args->GetString("name", &name)) { | 214 if (!args->GetString("name", &name)) { |
| 184 reply.SendError("'name' missing or invalid"); | 215 reply.SendError("'name' missing or invalid"); |
| 185 return; | 216 return; |
| 186 } | 217 } |
| 187 | 218 |
| 188 // Since we may be on the UI thread don't call GetURLRequestContext(). | 219 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 189 scoped_refptr<URLRequestContextGetter> context_getter = | 220 scoped_refptr<URLRequestContextGetter> context_getter = |
| 190 browser->profile()->GetRequestContext(); | 221 provider->profile()->GetRequestContext(); |
| 191 | 222 |
| 192 base::WaitableEvent event(true /* manual reset */, | 223 base::WaitableEvent event(true /* manual reset */, |
| 193 false /* not initially signaled */); | 224 false /* not initially signaled */); |
| 194 Task* task = NewRunnableFunction( | 225 Task* task = NewRunnableFunction( |
| 195 &DeleteCookieOnIOThread, | 226 &DeleteCookieOnIOThread, |
| 196 GURL(url), name, context_getter, &event); | 227 GURL(url), name, context_getter, &event); |
| 197 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { | 228 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { |
| 198 reply.SendError("Couldn't post task to delete the cookie"); | 229 reply.SendError("Couldn't post task to delete the cookie"); |
| 199 return; | 230 return; |
| 200 } | 231 } |
| 201 event.Wait(); | 232 event.Wait(); |
| 202 reply.SendSuccess(NULL); | 233 reply.SendSuccess(NULL); |
| 203 } | 234 } |
| 204 | 235 |
| 205 void SetCookieJSON(AutomationProvider* provider, | 236 void SetCookieJSON(AutomationProvider* provider, |
| 206 DictionaryValue* args, | 237 DictionaryValue* args, |
| 207 IPC::Message* reply_message) { | 238 IPC::Message* reply_message) { |
| 208 AutomationJSONReply reply(provider, reply_message); | 239 AutomationJSONReply reply(provider, reply_message); |
| 209 Browser* browser; | 240 std::string url; |
| 210 std::string error; | |
| 211 if (!GetBrowserFromJSONArgs(args, &browser, &error)) { | |
| 212 reply.SendError(error); | |
| 213 return; | |
| 214 } | |
| 215 std::string url, cookie; | |
| 216 if (!args->GetString("url", &url)) { | 241 if (!args->GetString("url", &url)) { |
| 217 reply.SendError("'url' missing or invalid"); | 242 reply.SendError("'url' missing or invalid"); |
| 218 return; | 243 return; |
| 219 } | 244 } |
| 220 if (!args->GetString("cookie", &cookie)) { | 245 DictionaryValue* cookie_dict; |
| 246 if (!args->GetDictionary("cookie", &cookie_dict)) { | |
| 221 reply.SendError("'cookie' missing or invalid"); | 247 reply.SendError("'cookie' missing or invalid"); |
| 222 return; | 248 return; |
| 223 } | 249 } |
| 250 std::string name, value; | |
| 251 std::string domain; | |
| 252 std::string path = "/"; | |
| 253 bool secure = false; | |
| 254 double expiry = 0; | |
| 255 bool http_only = false; | |
| 256 if (!cookie_dict->GetString("name", &name)) { | |
| 257 reply.SendError("'name' missing or invalid"); | |
| 258 return; | |
| 259 } | |
| 260 if (!cookie_dict->GetString("value", &value)) { | |
| 261 reply.SendError("'value' missing or invalid"); | |
| 262 return; | |
| 263 } | |
| 264 if (cookie_dict->HasKey("domain") && | |
| 265 !cookie_dict->GetString("domain", &domain)) { | |
| 266 reply.SendError("optional 'domain' invalid"); | |
| 267 return; | |
| 268 } | |
| 269 if (cookie_dict->HasKey("path") && | |
| 270 !cookie_dict->GetString("path", &path)) { | |
| 271 reply.SendError("optional 'path' invalid"); | |
| 272 return; | |
| 273 } | |
| 274 if (cookie_dict->HasKey("secure") && | |
| 275 !cookie_dict->GetBoolean("secure", &secure)) { | |
| 276 reply.SendError("optional 'secure' invalid"); | |
| 277 return; | |
| 278 } | |
| 279 if (cookie_dict->HasKey("expiry")) { | |
| 280 int expiry_int; | |
| 281 if (cookie_dict->GetInteger("expiry", &expiry_int)) { | |
| 282 expiry = expiry_int; | |
| 283 } else if (!cookie_dict->GetDouble("expiry", &expiry)) { | |
| 284 reply.SendError("optional 'expiry' invalid"); | |
| 285 return; | |
|
Joe
2011/03/23 01:55:45
How is a session only cookie indicated?
| |
| 286 } | |
| 287 } | |
| 288 if (cookie_dict->HasKey("http_only") && | |
| 289 !cookie_dict->GetBoolean("http_only", &http_only)) { | |
| 290 reply.SendError("optional 'http_only' invalid"); | |
| 291 return; | |
| 292 } | |
| 293 | |
| 294 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( | |
| 295 net::CookieMonster::CanonicalCookie::Create( | |
| 296 GURL(url), name, value, domain, path, base::Time(), | |
| 297 base::Time::FromDoubleT(expiry), secure, http_only)); | |
| 298 if (!cookie.get()) { | |
| 299 reply.SendError("given 'cookie' parameters are invalid"); | |
| 300 return; | |
| 301 } | |
| 224 | 302 |
| 225 // Since we may be on the UI thread don't call GetURLRequestContext(). | 303 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 226 scoped_refptr<URLRequestContextGetter> context_getter = | 304 scoped_refptr<URLRequestContextGetter> context_getter = |
| 227 browser->profile()->GetRequestContext(); | 305 provider->profile()->GetRequestContext(); |
| 228 | 306 |
| 229 base::WaitableEvent event(true /* manual reset */, | 307 base::WaitableEvent event(true /* manual reset */, |
| 230 false /* not initially signaled */); | 308 false /* not initially signaled */); |
| 231 bool success = false; | 309 bool success = false; |
| 232 Task* task = NewRunnableFunction( | 310 Task* task = NewRunnableFunction( |
| 233 &SetCookieOnIOThread, | 311 &SetCookieWithDetailsOnIOThread, |
| 234 GURL(url), cookie, context_getter, &event, &success); | 312 GURL(url), *cookie.get(), domain, context_getter, &event, &success); |
| 235 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { | 313 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { |
| 236 reply.SendError("Couldn't post task to set the cookie"); | 314 reply.SendError("Couldn't post task to set the cookie"); |
| 237 return; | 315 return; |
| 238 } | 316 } |
| 239 event.Wait(); | 317 event.Wait(); |
| 240 | 318 |
| 241 if (!success) { | 319 if (!success) { |
| 242 reply.SendError("Could not set the cookie"); | 320 reply.SendError("Could not set the cookie"); |
| 243 return; | 321 return; |
| 244 } | 322 } |
| 245 reply.SendSuccess(NULL); | 323 reply.SendSuccess(NULL); |
| 246 } | 324 } |
| 247 | 325 |
| 248 } // namespace automation_util | 326 } // namespace automation_util |
| OLD | NEW |