OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Implements the Chrome Extensions Cookies API. | 5 // Implements the Chrome Extensions Cookies API. |
6 | 6 |
7 #include "chrome/browser/extensions/api/cookies/cookies_api.h" | 7 #include "chrome/browser/extensions/api/cookies/cookies_api.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 namespace Get = extensions::api::cookies::Get; | 42 namespace Get = extensions::api::cookies::Get; |
43 namespace GetAll = extensions::api::cookies::GetAll; | 43 namespace GetAll = extensions::api::cookies::GetAll; |
44 namespace GetAllCookieStores = extensions::api::cookies::GetAllCookieStores; | 44 namespace GetAllCookieStores = extensions::api::cookies::GetAllCookieStores; |
45 namespace Remove = extensions::api::cookies::Remove; | 45 namespace Remove = extensions::api::cookies::Remove; |
46 namespace Set = extensions::api::cookies::Set; | 46 namespace Set = extensions::api::cookies::Set; |
47 | 47 |
48 namespace extensions { | 48 namespace extensions { |
49 namespace cookies = api::cookies; | 49 namespace cookies = api::cookies; |
50 namespace keys = cookies_api_constants; | 50 namespace keys = cookies_api_constants; |
51 | 51 |
52 CookiesEventRouter::CookiesEventRouter(Profile* profile) | 52 CookiesEventRouter::CookiesEventRouter(content::BrowserContext* context) |
53 : profile_(profile) { | 53 : profile_(Profile::FromBrowserContext(context)) { |
54 CHECK(registrar_.IsEmpty()); | 54 CHECK(registrar_.IsEmpty()); |
55 registrar_.Add(this, | 55 registrar_.Add(this, |
56 chrome::NOTIFICATION_COOKIE_CHANGED, | 56 chrome::NOTIFICATION_COOKIE_CHANGED, |
57 content::NotificationService::AllBrowserContextsAndSources()); | 57 content::NotificationService::AllBrowserContextsAndSources()); |
58 } | 58 } |
59 | 59 |
60 CookiesEventRouter::~CookiesEventRouter() { | 60 CookiesEventRouter::~CookiesEventRouter() { |
61 } | 61 } |
62 | 62 |
63 void CookiesEventRouter::Observe( | 63 void CookiesEventRouter::Observe( |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 args->Append(dict); | 123 args->Append(dict); |
124 | 124 |
125 GURL cookie_domain = | 125 GURL cookie_domain = |
126 cookies_helpers::GetURLFromCanonicalCookie(*details->cookie); | 126 cookies_helpers::GetURLFromCanonicalCookie(*details->cookie); |
127 DispatchEvent(profile, | 127 DispatchEvent(profile, |
128 cookies::OnChanged::kEventName, | 128 cookies::OnChanged::kEventName, |
129 args.Pass(), | 129 args.Pass(), |
130 cookie_domain); | 130 cookie_domain); |
131 } | 131 } |
132 | 132 |
133 void CookiesEventRouter::DispatchEvent( | 133 void CookiesEventRouter::DispatchEvent(content::BrowserContext* context, |
134 Profile* profile, | 134 const std::string& event_name, |
135 const std::string& event_name, | 135 scoped_ptr<base::ListValue> event_args, |
136 scoped_ptr<base::ListValue> event_args, | 136 GURL& cookie_domain) { |
137 GURL& cookie_domain) { | 137 EventRouter* router = |
138 EventRouter* router = profile ? | 138 context ? extensions::ExtensionSystem::Get(context)->event_router() |
139 extensions::ExtensionSystem::Get(profile)->event_router() : NULL; | 139 : NULL; |
140 if (!router) | 140 if (!router) |
141 return; | 141 return; |
142 scoped_ptr<Event> event(new Event(event_name, event_args.Pass())); | 142 scoped_ptr<Event> event(new Event(event_name, event_args.Pass())); |
143 event->restrict_to_browser_context = profile; | 143 event->restrict_to_browser_context = context; |
144 event->event_url = cookie_domain; | 144 event->event_url = cookie_domain; |
145 router->BroadcastEvent(event.Pass()); | 145 router->BroadcastEvent(event.Pass()); |
146 } | 146 } |
147 | 147 |
148 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, | 148 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, |
149 bool check_host_permissions) { | 149 bool check_host_permissions) { |
150 *url = GURL(url_string); | 150 *url = GURL(url_string); |
151 if (!url->is_valid()) { | 151 if (!url->is_valid()) { |
152 error_ = ErrorUtils::FormatErrorMessage( | 152 error_ = ErrorUtils::FormatErrorMessage( |
153 keys::kInvalidUrlError, url_string); | 153 keys::kInvalidUrlError, url_string); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // Read/validate input parameters. | 209 // Read/validate input parameters. |
210 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 210 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
211 return false; | 211 return false; |
212 | 212 |
213 std::string store_id = | 213 std::string store_id = |
214 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 214 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
215 : std::string(); | 215 : std::string(); |
216 net::URLRequestContextGetter* store_context = NULL; | 216 net::URLRequestContextGetter* store_context = NULL; |
217 if (!ParseStoreContext(&store_id, &store_context)) | 217 if (!ParseStoreContext(&store_id, &store_context)) |
218 return false; | 218 return false; |
219 store_context_ = store_context; | 219 store_browser_context_ = store_context; |
220 if (!parsed_args_->details.store_id.get()) | 220 if (!parsed_args_->details.store_id.get()) |
221 parsed_args_->details.store_id.reset(new std::string(store_id)); | 221 parsed_args_->details.store_id.reset(new std::string(store_id)); |
222 | 222 |
223 store_context_ = store_context; | 223 store_browser_context_ = store_context; |
224 | 224 |
225 bool rv = BrowserThread::PostTask( | 225 bool rv = BrowserThread::PostTask( |
226 BrowserThread::IO, FROM_HERE, | 226 BrowserThread::IO, FROM_HERE, |
227 base::Bind(&CookiesGetFunction::GetCookieOnIOThread, this)); | 227 base::Bind(&CookiesGetFunction::GetCookieOnIOThread, this)); |
228 DCHECK(rv); | 228 DCHECK(rv); |
229 | 229 |
230 // Will finish asynchronously. | 230 // Will finish asynchronously. |
231 return true; | 231 return true; |
232 } | 232 } |
233 | 233 |
234 void CookiesGetFunction::GetCookieOnIOThread() { | 234 void CookiesGetFunction::GetCookieOnIOThread() { |
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
236 net::CookieStore* cookie_store = | 236 net::CookieStore* cookie_store = |
237 store_context_->GetURLRequestContext()->cookie_store(); | 237 store_browser_context_->GetURLRequestContext()->cookie_store(); |
238 cookies_helpers::GetCookieListFromStore( | 238 cookies_helpers::GetCookieListFromStore( |
239 cookie_store, url_, | 239 cookie_store, url_, |
240 base::Bind(&CookiesGetFunction::GetCookieCallback, this)); | 240 base::Bind(&CookiesGetFunction::GetCookieCallback, this)); |
241 } | 241 } |
242 | 242 |
243 void CookiesGetFunction::GetCookieCallback(const net::CookieList& cookie_list) { | 243 void CookiesGetFunction::GetCookieCallback(const net::CookieList& cookie_list) { |
244 net::CookieList::const_iterator it; | 244 net::CookieList::const_iterator it; |
245 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { | 245 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
246 // Return the first matching cookie. Relies on the fact that the | 246 // Return the first matching cookie. Relies on the fact that the |
247 // CookieMonster returns them in canonical order (longest path, then | 247 // CookieMonster returns them in canonical order (longest path, then |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 !ParseUrl(*parsed_args_->details.url, &url_, false)) { | 283 !ParseUrl(*parsed_args_->details.url, &url_, false)) { |
284 return false; | 284 return false; |
285 } | 285 } |
286 | 286 |
287 std::string store_id = | 287 std::string store_id = |
288 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 288 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
289 : std::string(); | 289 : std::string(); |
290 net::URLRequestContextGetter* store_context = NULL; | 290 net::URLRequestContextGetter* store_context = NULL; |
291 if (!ParseStoreContext(&store_id, &store_context)) | 291 if (!ParseStoreContext(&store_id, &store_context)) |
292 return false; | 292 return false; |
293 store_context_ = store_context; | 293 store_browser_context_ = store_context; |
294 if (!parsed_args_->details.store_id.get()) | 294 if (!parsed_args_->details.store_id.get()) |
295 parsed_args_->details.store_id.reset(new std::string(store_id)); | 295 parsed_args_->details.store_id.reset(new std::string(store_id)); |
296 | 296 |
297 bool rv = BrowserThread::PostTask( | 297 bool rv = BrowserThread::PostTask( |
298 BrowserThread::IO, FROM_HERE, | 298 BrowserThread::IO, FROM_HERE, |
299 base::Bind(&CookiesGetAllFunction::GetAllCookiesOnIOThread, this)); | 299 base::Bind(&CookiesGetAllFunction::GetAllCookiesOnIOThread, this)); |
300 DCHECK(rv); | 300 DCHECK(rv); |
301 | 301 |
302 // Will finish asynchronously. | 302 // Will finish asynchronously. |
303 return true; | 303 return true; |
304 } | 304 } |
305 | 305 |
306 void CookiesGetAllFunction::GetAllCookiesOnIOThread() { | 306 void CookiesGetAllFunction::GetAllCookiesOnIOThread() { |
307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
308 net::CookieStore* cookie_store = | 308 net::CookieStore* cookie_store = |
309 store_context_->GetURLRequestContext()->cookie_store(); | 309 store_browser_context_->GetURLRequestContext()->cookie_store(); |
310 cookies_helpers::GetCookieListFromStore( | 310 cookies_helpers::GetCookieListFromStore( |
311 cookie_store, url_, | 311 cookie_store, url_, |
312 base::Bind(&CookiesGetAllFunction::GetAllCookiesCallback, this)); | 312 base::Bind(&CookiesGetAllFunction::GetAllCookiesCallback, this)); |
313 } | 313 } |
314 | 314 |
315 void CookiesGetAllFunction::GetAllCookiesCallback( | 315 void CookiesGetAllFunction::GetAllCookiesCallback( |
316 const net::CookieList& cookie_list) { | 316 const net::CookieList& cookie_list) { |
317 const extensions::Extension* extension = GetExtension(); | 317 const extensions::Extension* extension = GetExtension(); |
318 if (extension) { | 318 if (extension) { |
319 std::vector<linked_ptr<Cookie> > match_vector; | 319 std::vector<linked_ptr<Cookie> > match_vector; |
(...skipping 27 matching lines...) Expand all Loading... |
347 // Read/validate input parameters. | 347 // Read/validate input parameters. |
348 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 348 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
349 return false; | 349 return false; |
350 | 350 |
351 std::string store_id = | 351 std::string store_id = |
352 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 352 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
353 : std::string(); | 353 : std::string(); |
354 net::URLRequestContextGetter* store_context = NULL; | 354 net::URLRequestContextGetter* store_context = NULL; |
355 if (!ParseStoreContext(&store_id, &store_context)) | 355 if (!ParseStoreContext(&store_id, &store_context)) |
356 return false; | 356 return false; |
357 store_context_ = store_context; | 357 store_browser_context_ = store_context; |
358 if (!parsed_args_->details.store_id.get()) | 358 if (!parsed_args_->details.store_id.get()) |
359 parsed_args_->details.store_id.reset(new std::string(store_id)); | 359 parsed_args_->details.store_id.reset(new std::string(store_id)); |
360 | 360 |
361 bool rv = BrowserThread::PostTask( | 361 bool rv = BrowserThread::PostTask( |
362 BrowserThread::IO, FROM_HERE, | 362 BrowserThread::IO, FROM_HERE, |
363 base::Bind(&CookiesSetFunction::SetCookieOnIOThread, this)); | 363 base::Bind(&CookiesSetFunction::SetCookieOnIOThread, this)); |
364 DCHECK(rv); | 364 DCHECK(rv); |
365 | 365 |
366 // Will finish asynchronously. | 366 // Will finish asynchronously. |
367 return true; | 367 return true; |
368 } | 368 } |
369 | 369 |
370 void CookiesSetFunction::SetCookieOnIOThread() { | 370 void CookiesSetFunction::SetCookieOnIOThread() { |
371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
372 net::CookieMonster* cookie_monster = | 372 net::CookieMonster* cookie_monster = |
373 store_context_->GetURLRequestContext()->cookie_store()-> | 373 store_browser_context_->GetURLRequestContext() |
374 GetCookieMonster(); | 374 ->cookie_store() |
| 375 ->GetCookieMonster(); |
375 | 376 |
376 base::Time expiration_time; | 377 base::Time expiration_time; |
377 if (parsed_args_->details.expiration_date.get()) { | 378 if (parsed_args_->details.expiration_date.get()) { |
378 // Time::FromDoubleT converts double time 0 to empty Time object. So we need | 379 // Time::FromDoubleT converts double time 0 to empty Time object. So we need |
379 // to do special handling here. | 380 // to do special handling here. |
380 expiration_time = (*parsed_args_->details.expiration_date == 0) ? | 381 expiration_time = (*parsed_args_->details.expiration_date == 0) ? |
381 base::Time::UnixEpoch() : | 382 base::Time::UnixEpoch() : |
382 base::Time::FromDoubleT(*parsed_args_->details.expiration_date); | 383 base::Time::FromDoubleT(*parsed_args_->details.expiration_date); |
383 } | 384 } |
384 | 385 |
(...skipping 12 matching lines...) Expand all Loading... |
397 : false, | 398 : false, |
398 parsed_args_->details.http_only.get() ? *parsed_args_->details.http_only | 399 parsed_args_->details.http_only.get() ? *parsed_args_->details.http_only |
399 : false, | 400 : false, |
400 net::COOKIE_PRIORITY_DEFAULT, | 401 net::COOKIE_PRIORITY_DEFAULT, |
401 base::Bind(&CookiesSetFunction::PullCookie, this)); | 402 base::Bind(&CookiesSetFunction::PullCookie, this)); |
402 } | 403 } |
403 | 404 |
404 void CookiesSetFunction::PullCookie(bool set_cookie_result) { | 405 void CookiesSetFunction::PullCookie(bool set_cookie_result) { |
405 // Pull the newly set cookie. | 406 // Pull the newly set cookie. |
406 net::CookieMonster* cookie_monster = | 407 net::CookieMonster* cookie_monster = |
407 store_context_->GetURLRequestContext()->cookie_store()-> | 408 store_browser_context_->GetURLRequestContext() |
408 GetCookieMonster(); | 409 ->cookie_store() |
| 410 ->GetCookieMonster(); |
409 success_ = set_cookie_result; | 411 success_ = set_cookie_result; |
410 cookies_helpers::GetCookieListFromStore( | 412 cookies_helpers::GetCookieListFromStore( |
411 cookie_monster, url_, | 413 cookie_monster, url_, |
412 base::Bind(&CookiesSetFunction::PullCookieCallback, this)); | 414 base::Bind(&CookiesSetFunction::PullCookieCallback, this)); |
413 } | 415 } |
414 | 416 |
415 void CookiesSetFunction::PullCookieCallback( | 417 void CookiesSetFunction::PullCookieCallback( |
416 const net::CookieList& cookie_list) { | 418 const net::CookieList& cookie_list) { |
417 net::CookieList::const_iterator it; | 419 net::CookieList::const_iterator it; |
418 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { | 420 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 // Read/validate input parameters. | 462 // Read/validate input parameters. |
461 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 463 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
462 return false; | 464 return false; |
463 | 465 |
464 std::string store_id = | 466 std::string store_id = |
465 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 467 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
466 : std::string(); | 468 : std::string(); |
467 net::URLRequestContextGetter* store_context = NULL; | 469 net::URLRequestContextGetter* store_context = NULL; |
468 if (!ParseStoreContext(&store_id, &store_context)) | 470 if (!ParseStoreContext(&store_id, &store_context)) |
469 return false; | 471 return false; |
470 store_context_ = store_context; | 472 store_browser_context_ = store_context; |
471 if (!parsed_args_->details.store_id.get()) | 473 if (!parsed_args_->details.store_id.get()) |
472 parsed_args_->details.store_id.reset(new std::string(store_id)); | 474 parsed_args_->details.store_id.reset(new std::string(store_id)); |
473 | 475 |
474 // Pass the work off to the IO thread. | 476 // Pass the work off to the IO thread. |
475 bool rv = BrowserThread::PostTask( | 477 bool rv = BrowserThread::PostTask( |
476 BrowserThread::IO, FROM_HERE, | 478 BrowserThread::IO, FROM_HERE, |
477 base::Bind(&CookiesRemoveFunction::RemoveCookieOnIOThread, this)); | 479 base::Bind(&CookiesRemoveFunction::RemoveCookieOnIOThread, this)); |
478 DCHECK(rv); | 480 DCHECK(rv); |
479 | 481 |
480 // Will return asynchronously. | 482 // Will return asynchronously. |
481 return true; | 483 return true; |
482 } | 484 } |
483 | 485 |
484 void CookiesRemoveFunction::RemoveCookieOnIOThread() { | 486 void CookiesRemoveFunction::RemoveCookieOnIOThread() { |
485 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
486 | 488 |
487 // Remove the cookie | 489 // Remove the cookie |
488 net::CookieStore* cookie_store = | 490 net::CookieStore* cookie_store = |
489 store_context_->GetURLRequestContext()->cookie_store(); | 491 store_browser_context_->GetURLRequestContext()->cookie_store(); |
490 cookie_store->DeleteCookieAsync( | 492 cookie_store->DeleteCookieAsync( |
491 url_, parsed_args_->details.name, | 493 url_, parsed_args_->details.name, |
492 base::Bind(&CookiesRemoveFunction::RemoveCookieCallback, this)); | 494 base::Bind(&CookiesRemoveFunction::RemoveCookieCallback, this)); |
493 } | 495 } |
494 | 496 |
495 void CookiesRemoveFunction::RemoveCookieCallback() { | 497 void CookiesRemoveFunction::RemoveCookieCallback() { |
496 // Build the callback result | 498 // Build the callback result |
497 Remove::Results::Details details; | 499 Remove::Results::Details details; |
498 details.name = parsed_args_->details.name; | 500 details.name = parsed_args_->details.name; |
499 details.url = url_.spec(); | 501 details.url = url_.spec(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 return g_factory.Pointer(); | 581 return g_factory.Pointer(); |
580 } | 582 } |
581 | 583 |
582 void CookiesAPI::OnListenerAdded( | 584 void CookiesAPI::OnListenerAdded( |
583 const extensions::EventListenerInfo& details) { | 585 const extensions::EventListenerInfo& details) { |
584 cookies_event_router_.reset(new CookiesEventRouter(profile_)); | 586 cookies_event_router_.reset(new CookiesEventRouter(profile_)); |
585 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 587 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
586 } | 588 } |
587 | 589 |
588 } // namespace extensions | 590 } // namespace extensions |
OLD | NEW |