| OLD | NEW |
| 1 // Copyright (c) 2010 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 // Implements the Chrome Extensions Cookies API. | 5 // Implements the Chrome Extensions Cookies API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_cookies_api.h" | 7 #include "chrome/browser/extensions/extension_cookies_api.h" |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 NewRunnableMethod(this, &GetCookieFunction::GetCookieOnIOThread)); | 172 NewRunnableMethod(this, &GetCookieFunction::GetCookieOnIOThread)); |
| 173 DCHECK(rv); | 173 DCHECK(rv); |
| 174 | 174 |
| 175 // Will finish asynchronously. | 175 // Will finish asynchronously. |
| 176 return true; | 176 return true; |
| 177 } | 177 } |
| 178 | 178 |
| 179 void GetCookieFunction::GetCookieOnIOThread() { | 179 void GetCookieFunction::GetCookieOnIOThread() { |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 181 net::CookieStore* cookie_store = store_context_->GetCookieStore(); | 181 net::CookieStore* cookie_store = store_context_->GetCookieStore(); |
| 182 cookie_list_ = | 182 net::CookieList cookie_list = |
| 183 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); | 183 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); |
| 184 | |
| 185 bool rv = BrowserThread::PostTask( | |
| 186 BrowserThread::UI, FROM_HERE, | |
| 187 NewRunnableMethod(this, &GetCookieFunction::RespondOnUIThread)); | |
| 188 DCHECK(rv); | |
| 189 } | |
| 190 | |
| 191 void GetCookieFunction::RespondOnUIThread() { | |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 193 | |
| 194 net::CookieList::iterator it; | 184 net::CookieList::iterator it; |
| 195 for (it = cookie_list_.begin(); it != cookie_list_.end(); ++it) { | 185 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
| 196 // Return the first matching cookie. Relies on the fact that the | 186 // Return the first matching cookie. Relies on the fact that the |
| 197 // CookieMonster returns them in canonical order (longest path, then | 187 // CookieMonster returns them in canonical order (longest path, then |
| 198 // earliest creation time). | 188 // earliest creation time). |
| 199 if (it->Name() == name_) { | 189 if (it->Name() == name_) { |
| 200 result_.reset( | 190 result_.reset( |
| 201 extension_cookies_helpers::CreateCookieValue(*it, store_id_)); | 191 extension_cookies_helpers::CreateCookieValue(*it, store_id_)); |
| 202 break; | 192 break; |
| 203 } | 193 } |
| 204 } | 194 } |
| 205 | 195 |
| 206 // The cookie doesn't exist; return null. | 196 // The cookie doesn't exist; return null. |
| 207 if (it == cookie_list_.end()) | 197 if (it == cookie_list.end()) |
| 208 result_.reset(Value::CreateNullValue()); | 198 result_.reset(Value::CreateNullValue()); |
| 209 | 199 |
| 200 bool rv = BrowserThread::PostTask( |
| 201 BrowserThread::UI, FROM_HERE, |
| 202 NewRunnableMethod(this, &GetCookieFunction::RespondOnUIThread)); |
| 203 DCHECK(rv); |
| 204 } |
| 205 |
| 206 void GetCookieFunction::RespondOnUIThread() { |
| 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 210 SendResponse(true); | 208 SendResponse(true); |
| 211 } | 209 } |
| 212 | 210 |
| 213 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {} | 211 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {} |
| 214 | 212 |
| 215 GetAllCookiesFunction::~GetAllCookiesFunction() {} | 213 GetAllCookiesFunction::~GetAllCookiesFunction() {} |
| 216 | 214 |
| 217 bool GetAllCookiesFunction::RunImpl() { | 215 bool GetAllCookiesFunction::RunImpl() { |
| 218 // Return false if the arguments are malformed. | 216 // Return false if the arguments are malformed. |
| 219 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); | 217 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 234 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread)); | 232 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread)); |
| 235 DCHECK(rv); | 233 DCHECK(rv); |
| 236 | 234 |
| 237 // Will finish asynchronously. | 235 // Will finish asynchronously. |
| 238 return true; | 236 return true; |
| 239 } | 237 } |
| 240 | 238 |
| 241 void GetAllCookiesFunction::GetAllCookiesOnIOThread() { | 239 void GetAllCookiesFunction::GetAllCookiesOnIOThread() { |
| 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 243 net::CookieStore* cookie_store = store_context_->GetCookieStore(); | 241 net::CookieStore* cookie_store = store_context_->GetCookieStore(); |
| 244 cookie_list_ = | 242 net::CookieList cookie_list = |
| 245 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); | 243 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); |
| 246 | 244 |
| 245 const Extension* extension = GetExtension(); |
| 246 if (extension) { |
| 247 ListValue* matching_list = new ListValue(); |
| 248 extension_cookies_helpers::AppendMatchingCookiesToList( |
| 249 cookie_list, store_id_, url_, details_, |
| 250 GetExtension(), matching_list); |
| 251 result_.reset(matching_list); |
| 252 } |
| 247 bool rv = BrowserThread::PostTask( | 253 bool rv = BrowserThread::PostTask( |
| 248 BrowserThread::UI, FROM_HERE, | 254 BrowserThread::UI, FROM_HERE, |
| 249 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread)); | 255 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread)); |
| 250 DCHECK(rv); | 256 DCHECK(rv); |
| 251 } | 257 } |
| 252 | 258 |
| 253 void GetAllCookiesFunction::RespondOnUIThread() { | 259 void GetAllCookiesFunction::RespondOnUIThread() { |
| 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 255 | |
| 256 const Extension* extension = GetExtension(); | |
| 257 if (extension) { | |
| 258 ListValue* matching_list = new ListValue(); | |
| 259 extension_cookies_helpers::AppendMatchingCookiesToList( | |
| 260 cookie_list_, store_id_, url_, details_, | |
| 261 GetExtension(), matching_list); | |
| 262 result_.reset(matching_list); | |
| 263 } | |
| 264 SendResponse(true); | 261 SendResponse(true); |
| 265 } | 262 } |
| 266 | 263 |
| 267 SetCookieFunction::SetCookieFunction() | 264 SetCookieFunction::SetCookieFunction() |
| 268 : secure_(false), | 265 : secure_(false), |
| 269 http_only_(false), | 266 http_only_(false), |
| 270 success_(false) { | 267 success_(false) { |
| 271 } | 268 } |
| 272 | 269 |
| 273 SetCookieFunction::~SetCookieFunction() { | 270 SetCookieFunction::~SetCookieFunction() { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 } | 333 } |
| 337 | 334 |
| 338 void SetCookieFunction::SetCookieOnIOThread() { | 335 void SetCookieFunction::SetCookieOnIOThread() { |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 340 net::CookieMonster* cookie_monster = | 337 net::CookieMonster* cookie_monster = |
| 341 store_context_->GetCookieStore()->GetCookieMonster(); | 338 store_context_->GetCookieStore()->GetCookieMonster(); |
| 342 success_ = cookie_monster->SetCookieWithDetails( | 339 success_ = cookie_monster->SetCookieWithDetails( |
| 343 url_, name_, value_, domain_, path_, expiration_time_, | 340 url_, name_, value_, domain_, path_, expiration_time_, |
| 344 secure_, http_only_); | 341 secure_, http_only_); |
| 345 | 342 |
| 343 // Pull the newly set cookie. |
| 344 net::CookieList cookie_list = |
| 345 extension_cookies_helpers::GetCookieListFromStore(cookie_monster, url_); |
| 346 net::CookieList::iterator it; |
| 347 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
| 348 // Return the first matching cookie. Relies on the fact that the |
| 349 // CookieMonster returns them in canonical order (longest path, then |
| 350 // earliest creation time). |
| 351 if (it->Name() == name_) { |
| 352 result_.reset( |
| 353 extension_cookies_helpers::CreateCookieValue(*it, store_id_)); |
| 354 break; |
| 355 } |
| 356 } |
| 357 |
| 346 bool rv = BrowserThread::PostTask( | 358 bool rv = BrowserThread::PostTask( |
| 347 BrowserThread::UI, FROM_HERE, | 359 BrowserThread::UI, FROM_HERE, |
| 348 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread)); | 360 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread)); |
| 349 DCHECK(rv); | 361 DCHECK(rv); |
| 350 } | 362 } |
| 351 | 363 |
| 352 void SetCookieFunction::RespondOnUIThread() { | 364 void SetCookieFunction::RespondOnUIThread() { |
| 353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 354 if (!success_) { | 366 if (!success_) { |
| 355 error_ = ExtensionErrorUtils::FormatErrorMessage( | 367 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 // Read/validate input parameters. | 405 // Read/validate input parameters. |
| 394 GURL url; | 406 GURL url; |
| 395 if (!ParseUrl(details, &url, true)) | 407 if (!ParseUrl(details, &url, true)) |
| 396 return false; | 408 return false; |
| 397 | 409 |
| 398 std::string name; | 410 std::string name; |
| 399 // Get the cookie name string or return false. | 411 // Get the cookie name string or return false. |
| 400 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name)); | 412 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name)); |
| 401 | 413 |
| 402 URLRequestContextGetter* store_context = NULL; | 414 URLRequestContextGetter* store_context = NULL; |
| 403 if (!ParseStoreContext(details, &store_context, NULL)) | 415 std::string store_id; |
| 416 if (!ParseStoreContext(details, &store_context, &store_id)) |
| 404 return false; | 417 return false; |
| 405 DCHECK(store_context); | 418 DCHECK(store_context); |
| 406 | 419 |
| 407 // We don't bother to synchronously wait for the result here, because | 420 // We don't bother to synchronously wait for the result here, because |
| 408 // CookieMonster is only ever accessed on the IO thread, so any other accesses | 421 // CookieMonster is only ever accessed on the IO thread, so any other accesses |
| 409 // should happen after this. | 422 // should happen after this. |
| 410 bool rv = BrowserThread::PostTask( | 423 bool rv = BrowserThread::PostTask( |
| 411 BrowserThread::IO, FROM_HERE, | 424 BrowserThread::IO, FROM_HERE, |
| 412 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); | 425 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); |
| 413 DCHECK(rv); | 426 DCHECK(rv); |
| 414 | 427 |
| 428 DictionaryValue* resultDictionary = new DictionaryValue(); |
| 429 resultDictionary->SetString(keys::kNameKey, name); |
| 430 resultDictionary->SetString(keys::kUrlKey, url.spec()); |
| 431 resultDictionary->SetString(keys::kStoreIdKey, store_id); |
| 432 result_.reset(resultDictionary); |
| 433 |
| 415 return true; | 434 return true; |
| 416 } | 435 } |
| 417 | 436 |
| 418 void RemoveCookieFunction::Run() { | 437 void RemoveCookieFunction::Run() { |
| 419 SendResponse(RunImpl()); | 438 SendResponse(RunImpl()); |
| 420 } | 439 } |
| 421 | 440 |
| 422 bool GetAllCookieStoresFunction::RunImpl() { | 441 bool GetAllCookieStoresFunction::RunImpl() { |
| 423 Profile* original_profile = profile(); | 442 Profile* original_profile = profile(); |
| 424 DCHECK(original_profile); | 443 DCHECK(original_profile); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 extension_cookies_helpers::CreateCookieStoreValue( | 478 extension_cookies_helpers::CreateCookieStoreValue( |
| 460 incognito_profile, incognito_tab_ids.release())); | 479 incognito_profile, incognito_tab_ids.release())); |
| 461 } | 480 } |
| 462 result_.reset(cookie_store_list); | 481 result_.reset(cookie_store_list); |
| 463 return true; | 482 return true; |
| 464 } | 483 } |
| 465 | 484 |
| 466 void GetAllCookieStoresFunction::Run() { | 485 void GetAllCookieStoresFunction::Run() { |
| 467 SendResponse(RunImpl()); | 486 SendResponse(RunImpl()); |
| 468 } | 487 } |
| OLD | NEW |