| 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 30 matching lines...) Expand all Loading... |
| 41 namespace Get = extensions::api::cookies::Get; | 41 namespace Get = extensions::api::cookies::Get; |
| 42 namespace GetAll = extensions::api::cookies::GetAll; | 42 namespace GetAll = extensions::api::cookies::GetAll; |
| 43 namespace GetAllCookieStores = extensions::api::cookies::GetAllCookieStores; | 43 namespace GetAllCookieStores = extensions::api::cookies::GetAllCookieStores; |
| 44 namespace Remove = extensions::api::cookies::Remove; | 44 namespace Remove = extensions::api::cookies::Remove; |
| 45 namespace Set = extensions::api::cookies::Set; | 45 namespace Set = extensions::api::cookies::Set; |
| 46 | 46 |
| 47 namespace extensions { | 47 namespace extensions { |
| 48 namespace cookies = api::cookies; | 48 namespace cookies = api::cookies; |
| 49 namespace keys = cookies_api_constants; | 49 namespace keys = cookies_api_constants; |
| 50 | 50 |
| 51 namespace { |
| 52 |
| 53 bool ParseUrl(ChromeAsyncExtensionFunction* function, |
| 54 const std::string& url_string, |
| 55 GURL* url, |
| 56 bool check_host_permissions) { |
| 57 *url = GURL(url_string); |
| 58 if (!url->is_valid()) { |
| 59 function->SetError( |
| 60 ErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, url_string)); |
| 61 return false; |
| 62 } |
| 63 // Check against host permissions if needed. |
| 64 if (check_host_permissions && |
| 65 !PermissionsData::HasHostPermission(function->GetExtension(), *url)) { |
| 66 function->SetError(ErrorUtils::FormatErrorMessage( |
| 67 keys::kNoHostPermissionsError, url->spec())); |
| 68 return false; |
| 69 } |
| 70 return true; |
| 71 } |
| 72 |
| 73 bool ParseStoreContext(ChromeAsyncExtensionFunction* function, |
| 74 std::string* store_id, |
| 75 net::URLRequestContextGetter** context) { |
| 76 DCHECK((context || store_id->empty())); |
| 77 Profile* store_profile = NULL; |
| 78 if (!store_id->empty()) { |
| 79 store_profile = cookies_helpers::ChooseProfileFromStoreId( |
| 80 *store_id, function->GetProfile(), function->include_incognito()); |
| 81 if (!store_profile) { |
| 82 function->SetError(ErrorUtils::FormatErrorMessage( |
| 83 keys::kInvalidStoreIdError, *store_id)); |
| 84 return false; |
| 85 } |
| 86 } else { |
| 87 // The store ID was not specified; use the current execution context's |
| 88 // cookie store by default. |
| 89 // GetCurrentBrowser() already takes into account incognito settings. |
| 90 Browser* current_browser = function->GetCurrentBrowser(); |
| 91 if (!current_browser) { |
| 92 function->SetError(keys::kNoCookieStoreFoundError); |
| 93 return false; |
| 94 } |
| 95 store_profile = current_browser->profile(); |
| 96 *store_id = cookies_helpers::GetStoreIdFromProfile(store_profile); |
| 97 } |
| 98 |
| 99 if (context) |
| 100 *context = store_profile->GetRequestContext(); |
| 101 DCHECK(context); |
| 102 |
| 103 return true; |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 |
| 51 CookiesEventRouter::CookiesEventRouter(content::BrowserContext* context) | 108 CookiesEventRouter::CookiesEventRouter(content::BrowserContext* context) |
| 52 : profile_(Profile::FromBrowserContext(context)) { | 109 : profile_(Profile::FromBrowserContext(context)) { |
| 53 CHECK(registrar_.IsEmpty()); | 110 CHECK(registrar_.IsEmpty()); |
| 54 registrar_.Add(this, | 111 registrar_.Add(this, |
| 55 chrome::NOTIFICATION_COOKIE_CHANGED, | 112 chrome::NOTIFICATION_COOKIE_CHANGED, |
| 56 content::NotificationService::AllBrowserContextsAndSources()); | 113 content::NotificationService::AllBrowserContextsAndSources()); |
| 57 } | 114 } |
| 58 | 115 |
| 59 CookiesEventRouter::~CookiesEventRouter() { | 116 CookiesEventRouter::~CookiesEventRouter() { |
| 60 } | 117 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 GURL& cookie_domain) { | 192 GURL& cookie_domain) { |
| 136 EventRouter* router = context ? extensions::EventRouter::Get(context) : NULL; | 193 EventRouter* router = context ? extensions::EventRouter::Get(context) : NULL; |
| 137 if (!router) | 194 if (!router) |
| 138 return; | 195 return; |
| 139 scoped_ptr<Event> event(new Event(event_name, event_args.Pass())); | 196 scoped_ptr<Event> event(new Event(event_name, event_args.Pass())); |
| 140 event->restrict_to_browser_context = context; | 197 event->restrict_to_browser_context = context; |
| 141 event->event_url = cookie_domain; | 198 event->event_url = cookie_domain; |
| 142 router->BroadcastEvent(event.Pass()); | 199 router->BroadcastEvent(event.Pass()); |
| 143 } | 200 } |
| 144 | 201 |
| 145 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, | |
| 146 bool check_host_permissions) { | |
| 147 *url = GURL(url_string); | |
| 148 if (!url->is_valid()) { | |
| 149 error_ = ErrorUtils::FormatErrorMessage( | |
| 150 keys::kInvalidUrlError, url_string); | |
| 151 return false; | |
| 152 } | |
| 153 // Check against host permissions if needed. | |
| 154 if (check_host_permissions && | |
| 155 !PermissionsData::HasHostPermission(GetExtension(), *url)) { | |
| 156 error_ = ErrorUtils::FormatErrorMessage( | |
| 157 keys::kNoHostPermissionsError, url->spec()); | |
| 158 return false; | |
| 159 } | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 bool CookiesFunction::ParseStoreContext( | |
| 164 std::string* store_id, | |
| 165 net::URLRequestContextGetter** context) { | |
| 166 DCHECK((context || store_id->empty())); | |
| 167 Profile* store_profile = NULL; | |
| 168 if (!store_id->empty()) { | |
| 169 store_profile = cookies_helpers::ChooseProfileFromStoreId( | |
| 170 *store_id, GetProfile(), include_incognito()); | |
| 171 if (!store_profile) { | |
| 172 error_ = ErrorUtils::FormatErrorMessage( | |
| 173 keys::kInvalidStoreIdError, *store_id); | |
| 174 return false; | |
| 175 } | |
| 176 } else { | |
| 177 // The store ID was not specified; use the current execution context's | |
| 178 // cookie store by default. | |
| 179 // GetCurrentBrowser() already takes into account incognito settings. | |
| 180 Browser* current_browser = GetCurrentBrowser(); | |
| 181 if (!current_browser) { | |
| 182 error_ = keys::kNoCookieStoreFoundError; | |
| 183 return false; | |
| 184 } | |
| 185 store_profile = current_browser->profile(); | |
| 186 *store_id = cookies_helpers::GetStoreIdFromProfile(store_profile); | |
| 187 } | |
| 188 | |
| 189 if (context) | |
| 190 *context = store_profile->GetRequestContext(); | |
| 191 DCHECK(context); | |
| 192 | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 CookiesGetFunction::CookiesGetFunction() { | 202 CookiesGetFunction::CookiesGetFunction() { |
| 197 } | 203 } |
| 198 | 204 |
| 199 CookiesGetFunction::~CookiesGetFunction() { | 205 CookiesGetFunction::~CookiesGetFunction() { |
| 200 } | 206 } |
| 201 | 207 |
| 202 bool CookiesGetFunction::RunImpl() { | 208 bool CookiesGetFunction::RunImpl() { |
| 203 parsed_args_ = Get::Params::Create(*args_); | 209 parsed_args_ = Get::Params::Create(*args_); |
| 204 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 210 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 205 | 211 |
| 206 // Read/validate input parameters. | 212 // Read/validate input parameters. |
| 207 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 213 if (!ParseUrl(this, parsed_args_->details.url, &url_, true)) |
| 208 return false; | 214 return false; |
| 209 | 215 |
| 210 std::string store_id = | 216 std::string store_id = |
| 211 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 217 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 212 : std::string(); | 218 : std::string(); |
| 213 net::URLRequestContextGetter* store_context = NULL; | 219 net::URLRequestContextGetter* store_context = NULL; |
| 214 if (!ParseStoreContext(&store_id, &store_context)) | 220 if (!ParseStoreContext(this, &store_id, &store_context)) |
| 215 return false; | 221 return false; |
| 216 store_browser_context_ = store_context; | 222 store_browser_context_ = store_context; |
| 217 if (!parsed_args_->details.store_id.get()) | 223 if (!parsed_args_->details.store_id.get()) |
| 218 parsed_args_->details.store_id.reset(new std::string(store_id)); | 224 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 219 | 225 |
| 220 store_browser_context_ = store_context; | 226 store_browser_context_ = store_context; |
| 221 | 227 |
| 222 bool rv = BrowserThread::PostTask( | 228 bool rv = BrowserThread::PostTask( |
| 223 BrowserThread::IO, FROM_HERE, | 229 BrowserThread::IO, FROM_HERE, |
| 224 base::Bind(&CookiesGetFunction::GetCookieOnIOThread, this)); | 230 base::Bind(&CookiesGetFunction::GetCookieOnIOThread, this)); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 } | 276 } |
| 271 | 277 |
| 272 CookiesGetAllFunction::~CookiesGetAllFunction() { | 278 CookiesGetAllFunction::~CookiesGetAllFunction() { |
| 273 } | 279 } |
| 274 | 280 |
| 275 bool CookiesGetAllFunction::RunImpl() { | 281 bool CookiesGetAllFunction::RunImpl() { |
| 276 parsed_args_ = GetAll::Params::Create(*args_); | 282 parsed_args_ = GetAll::Params::Create(*args_); |
| 277 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 283 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 278 | 284 |
| 279 if (parsed_args_->details.url.get() && | 285 if (parsed_args_->details.url.get() && |
| 280 !ParseUrl(*parsed_args_->details.url, &url_, false)) { | 286 !ParseUrl(this, *parsed_args_->details.url, &url_, false)) { |
| 281 return false; | 287 return false; |
| 282 } | 288 } |
| 283 | 289 |
| 284 std::string store_id = | 290 std::string store_id = |
| 285 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 291 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 286 : std::string(); | 292 : std::string(); |
| 287 net::URLRequestContextGetter* store_context = NULL; | 293 net::URLRequestContextGetter* store_context = NULL; |
| 288 if (!ParseStoreContext(&store_id, &store_context)) | 294 if (!ParseStoreContext(this, &store_id, &store_context)) |
| 289 return false; | 295 return false; |
| 290 store_browser_context_ = store_context; | 296 store_browser_context_ = store_context; |
| 291 if (!parsed_args_->details.store_id.get()) | 297 if (!parsed_args_->details.store_id.get()) |
| 292 parsed_args_->details.store_id.reset(new std::string(store_id)); | 298 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 293 | 299 |
| 294 bool rv = BrowserThread::PostTask( | 300 bool rv = BrowserThread::PostTask( |
| 295 BrowserThread::IO, FROM_HERE, | 301 BrowserThread::IO, FROM_HERE, |
| 296 base::Bind(&CookiesGetAllFunction::GetAllCookiesOnIOThread, this)); | 302 base::Bind(&CookiesGetAllFunction::GetAllCookiesOnIOThread, this)); |
| 297 DCHECK(rv); | 303 DCHECK(rv); |
| 298 | 304 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 } | 341 } |
| 336 | 342 |
| 337 CookiesSetFunction::~CookiesSetFunction() { | 343 CookiesSetFunction::~CookiesSetFunction() { |
| 338 } | 344 } |
| 339 | 345 |
| 340 bool CookiesSetFunction::RunImpl() { | 346 bool CookiesSetFunction::RunImpl() { |
| 341 parsed_args_ = Set::Params::Create(*args_); | 347 parsed_args_ = Set::Params::Create(*args_); |
| 342 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 348 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 343 | 349 |
| 344 // Read/validate input parameters. | 350 // Read/validate input parameters. |
| 345 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 351 if (!ParseUrl(this, parsed_args_->details.url, &url_, true)) |
| 346 return false; | 352 return false; |
| 347 | 353 |
| 348 std::string store_id = | 354 std::string store_id = |
| 349 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 355 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 350 : std::string(); | 356 : std::string(); |
| 351 net::URLRequestContextGetter* store_context = NULL; | 357 net::URLRequestContextGetter* store_context = NULL; |
| 352 if (!ParseStoreContext(&store_id, &store_context)) | 358 if (!ParseStoreContext(this, &store_id, &store_context)) |
| 353 return false; | 359 return false; |
| 354 store_browser_context_ = store_context; | 360 store_browser_context_ = store_context; |
| 355 if (!parsed_args_->details.store_id.get()) | 361 if (!parsed_args_->details.store_id.get()) |
| 356 parsed_args_->details.store_id.reset(new std::string(store_id)); | 362 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 357 | 363 |
| 358 bool rv = BrowserThread::PostTask( | 364 bool rv = BrowserThread::PostTask( |
| 359 BrowserThread::IO, FROM_HERE, | 365 BrowserThread::IO, FROM_HERE, |
| 360 base::Bind(&CookiesSetFunction::SetCookieOnIOThread, this)); | 366 base::Bind(&CookiesSetFunction::SetCookieOnIOThread, this)); |
| 361 DCHECK(rv); | 367 DCHECK(rv); |
| 362 | 368 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 } | 456 } |
| 451 | 457 |
| 452 CookiesRemoveFunction::~CookiesRemoveFunction() { | 458 CookiesRemoveFunction::~CookiesRemoveFunction() { |
| 453 } | 459 } |
| 454 | 460 |
| 455 bool CookiesRemoveFunction::RunImpl() { | 461 bool CookiesRemoveFunction::RunImpl() { |
| 456 parsed_args_ = Remove::Params::Create(*args_); | 462 parsed_args_ = Remove::Params::Create(*args_); |
| 457 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 463 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 458 | 464 |
| 459 // Read/validate input parameters. | 465 // Read/validate input parameters. |
| 460 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 466 if (!ParseUrl(this, parsed_args_->details.url, &url_, true)) |
| 461 return false; | 467 return false; |
| 462 | 468 |
| 463 std::string store_id = | 469 std::string store_id = |
| 464 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id | 470 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 465 : std::string(); | 471 : std::string(); |
| 466 net::URLRequestContextGetter* store_context = NULL; | 472 net::URLRequestContextGetter* store_context = NULL; |
| 467 if (!ParseStoreContext(&store_id, &store_context)) | 473 if (!ParseStoreContext(this, &store_id, &store_context)) |
| 468 return false; | 474 return false; |
| 469 store_browser_context_ = store_context; | 475 store_browser_context_ = store_context; |
| 470 if (!parsed_args_->details.store_id.get()) | 476 if (!parsed_args_->details.store_id.get()) |
| 471 parsed_args_->details.store_id.reset(new std::string(store_id)); | 477 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 472 | 478 |
| 473 // Pass the work off to the IO thread. | 479 // Pass the work off to the IO thread. |
| 474 bool rv = BrowserThread::PostTask( | 480 bool rv = BrowserThread::PostTask( |
| 475 BrowserThread::IO, FROM_HERE, | 481 BrowserThread::IO, FROM_HERE, |
| 476 base::Bind(&CookiesRemoveFunction::RemoveCookieOnIOThread, this)); | 482 base::Bind(&CookiesRemoveFunction::RemoveCookieOnIOThread, this)); |
| 477 DCHECK(rv); | 483 DCHECK(rv); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 504 BrowserThread::UI, FROM_HERE, | 510 BrowserThread::UI, FROM_HERE, |
| 505 base::Bind(&CookiesRemoveFunction::RespondOnUIThread, this)); | 511 base::Bind(&CookiesRemoveFunction::RespondOnUIThread, this)); |
| 506 DCHECK(rv); | 512 DCHECK(rv); |
| 507 } | 513 } |
| 508 | 514 |
| 509 void CookiesRemoveFunction::RespondOnUIThread() { | 515 void CookiesRemoveFunction::RespondOnUIThread() { |
| 510 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 516 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 511 SendResponse(true); | 517 SendResponse(true); |
| 512 } | 518 } |
| 513 | 519 |
| 514 bool CookiesGetAllCookieStoresFunction::RunImpl() { | 520 bool CookiesGetAllCookieStoresFunction::RunSync() { |
| 515 Profile* original_profile = GetProfile(); | 521 Profile* original_profile = GetProfile(); |
| 516 DCHECK(original_profile); | 522 DCHECK(original_profile); |
| 517 scoped_ptr<base::ListValue> original_tab_ids(new base::ListValue()); | 523 scoped_ptr<base::ListValue> original_tab_ids(new base::ListValue()); |
| 518 Profile* incognito_profile = NULL; | 524 Profile* incognito_profile = NULL; |
| 519 scoped_ptr<base::ListValue> incognito_tab_ids; | 525 scoped_ptr<base::ListValue> incognito_tab_ids; |
| 520 if (include_incognito() && GetProfile()->HasOffTheRecordProfile()) { | 526 if (include_incognito() && GetProfile()->HasOffTheRecordProfile()) { |
| 521 incognito_profile = GetProfile()->GetOffTheRecordProfile(); | 527 incognito_profile = GetProfile()->GetOffTheRecordProfile(); |
| 522 if (incognito_profile) | 528 if (incognito_profile) |
| 523 incognito_tab_ids.reset(new base::ListValue()); | 529 incognito_tab_ids.reset(new base::ListValue()); |
| 524 } | 530 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 546 if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 && | 552 if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 && |
| 547 incognito_profile) { | 553 incognito_profile) { |
| 548 cookie_stores.push_back(make_linked_ptr( | 554 cookie_stores.push_back(make_linked_ptr( |
| 549 cookies_helpers::CreateCookieStore( | 555 cookies_helpers::CreateCookieStore( |
| 550 incognito_profile, incognito_tab_ids.release()).release())); | 556 incognito_profile, incognito_tab_ids.release()).release())); |
| 551 } | 557 } |
| 552 results_ = GetAllCookieStores::Results::Create(cookie_stores); | 558 results_ = GetAllCookieStores::Results::Create(cookie_stores); |
| 553 return true; | 559 return true; |
| 554 } | 560 } |
| 555 | 561 |
| 556 void CookiesGetAllCookieStoresFunction::Run() { | |
| 557 SendResponse(RunImpl()); | |
| 558 } | |
| 559 | |
| 560 CookiesAPI::CookiesAPI(content::BrowserContext* context) | 562 CookiesAPI::CookiesAPI(content::BrowserContext* context) |
| 561 : browser_context_(context) { | 563 : browser_context_(context) { |
| 562 EventRouter::Get(browser_context_) | 564 EventRouter::Get(browser_context_) |
| 563 ->RegisterObserver(this, cookies::OnChanged::kEventName); | 565 ->RegisterObserver(this, cookies::OnChanged::kEventName); |
| 564 } | 566 } |
| 565 | 567 |
| 566 CookiesAPI::~CookiesAPI() { | 568 CookiesAPI::~CookiesAPI() { |
| 567 } | 569 } |
| 568 | 570 |
| 569 void CookiesAPI::Shutdown() { | 571 void CookiesAPI::Shutdown() { |
| 570 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 572 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 571 } | 573 } |
| 572 | 574 |
| 573 static base::LazyInstance<BrowserContextKeyedAPIFactory<CookiesAPI> > | 575 static base::LazyInstance<BrowserContextKeyedAPIFactory<CookiesAPI> > |
| 574 g_factory = LAZY_INSTANCE_INITIALIZER; | 576 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 575 | 577 |
| 576 // static | 578 // static |
| 577 BrowserContextKeyedAPIFactory<CookiesAPI>* CookiesAPI::GetFactoryInstance() { | 579 BrowserContextKeyedAPIFactory<CookiesAPI>* CookiesAPI::GetFactoryInstance() { |
| 578 return g_factory.Pointer(); | 580 return g_factory.Pointer(); |
| 579 } | 581 } |
| 580 | 582 |
| 581 void CookiesAPI::OnListenerAdded( | 583 void CookiesAPI::OnListenerAdded( |
| 582 const extensions::EventListenerInfo& details) { | 584 const extensions::EventListenerInfo& details) { |
| 583 cookies_event_router_.reset(new CookiesEventRouter(browser_context_)); | 585 cookies_event_router_.reset(new CookiesEventRouter(browser_context_)); |
| 584 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 586 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 585 } | 587 } |
| 586 | 588 |
| 587 } // namespace extensions | 589 } // namespace extensions |
| OLD | NEW |