Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/extensions/extension_cookies_api.cc

Issue 6525016: Adding callbacks to `chrome.cookies.{set,remove}`. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Dropping samples.json, addressing Jochen's comments." Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 bool rv = BrowserThread::PostTask( 170 bool rv = BrowserThread::PostTask(
171 BrowserThread::IO, FROM_HERE, 171 BrowserThread::IO, FROM_HERE,
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
jochen (gone - plz use gerrit) 2011/02/17 09:27:55 no empty line
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
181 net::CookieStore* cookie_store = store_context_->GetCookieStore(); 182 net::CookieStore* cookie_store = store_context_->GetCookieStore();
182 cookie_list_ = 183
jochen (gone - plz use gerrit) 2011/02/17 09:27:55 no empty line
184 net::CookieList cookie_list =
183 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); 185 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; 186 net::CookieList::iterator it;
195 for (it = cookie_list_.begin(); it != cookie_list_.end(); ++it) { 187 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
196 // Return the first matching cookie. Relies on the fact that the 188 // Return the first matching cookie. Relies on the fact that the
197 // CookieMonster returns them in canonical order (longest path, then 189 // CookieMonster returns them in canonical order (longest path, then
198 // earliest creation time). 190 // earliest creation time).
199 if (it->Name() == name_) { 191 if (it->Name() == name_) {
200 result_.reset( 192 result_.reset(
201 extension_cookies_helpers::CreateCookieValue(*it, store_id_)); 193 extension_cookies_helpers::CreateCookieValue(*it, store_id_));
202 break; 194 break;
203 } 195 }
204 } 196 }
205 197
206 // The cookie doesn't exist; return null. 198 // The cookie doesn't exist; return null.
207 if (it == cookie_list_.end()) 199 if (it == cookie_list.end())
208 result_.reset(Value::CreateNullValue()); 200 result_.reset(Value::CreateNullValue());
209 201
202 bool rv = BrowserThread::PostTask(
203 BrowserThread::UI, FROM_HERE,
204 NewRunnableMethod(this, &GetCookieFunction::RespondOnUIThread));
205 DCHECK(rv);
206 }
207
208 void GetCookieFunction::RespondOnUIThread() {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
210 SendResponse(true); 210 SendResponse(true);
211 } 211 }
212 212
213 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {} 213 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {}
214 214
215 GetAllCookiesFunction::~GetAllCookiesFunction() {} 215 GetAllCookiesFunction::~GetAllCookiesFunction() {}
216 216
217 bool GetAllCookiesFunction::RunImpl() { 217 bool GetAllCookiesFunction::RunImpl() {
218 // Return false if the arguments are malformed. 218 // Return false if the arguments are malformed.
219 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); 219 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_));
(...skipping 14 matching lines...) Expand all
234 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread)); 234 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread));
235 DCHECK(rv); 235 DCHECK(rv);
236 236
237 // Will finish asynchronously. 237 // Will finish asynchronously.
238 return true; 238 return true;
239 } 239 }
240 240
241 void GetAllCookiesFunction::GetAllCookiesOnIOThread() { 241 void GetAllCookiesFunction::GetAllCookiesOnIOThread() {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
243 net::CookieStore* cookie_store = store_context_->GetCookieStore(); 243 net::CookieStore* cookie_store = store_context_->GetCookieStore();
244 cookie_list_ = 244 net::CookieList cookie_list =
245 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); 245 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_);
246 246
247 const Extension* extension = GetExtension();
248 if (extension) {
249 ListValue* matching_list = new ListValue();
250 extension_cookies_helpers::AppendMatchingCookiesToList(
251 cookie_list, store_id_, url_, details_,
252 GetExtension(), matching_list);
253 result_.reset(matching_list);
254 }
247 bool rv = BrowserThread::PostTask( 255 bool rv = BrowserThread::PostTask(
248 BrowserThread::UI, FROM_HERE, 256 BrowserThread::UI, FROM_HERE,
249 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread)); 257 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread));
250 DCHECK(rv); 258 DCHECK(rv);
251 } 259 }
252 260
253 void GetAllCookiesFunction::RespondOnUIThread() { 261 void GetAllCookiesFunction::RespondOnUIThread() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 262 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); 263 SendResponse(true);
265 } 264 }
266 265
267 SetCookieFunction::SetCookieFunction() 266 SetCookieFunction::SetCookieFunction()
268 : secure_(false), 267 : secure_(false),
269 http_only_(false), 268 http_only_(false),
270 success_(false) { 269 success_(false) {
271 } 270 }
272 271
273 SetCookieFunction::~SetCookieFunction() { 272 SetCookieFunction::~SetCookieFunction() {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 } 335 }
337 336
338 void SetCookieFunction::SetCookieOnIOThread() { 337 void SetCookieFunction::SetCookieOnIOThread() {
339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
340 net::CookieMonster* cookie_monster = 339 net::CookieMonster* cookie_monster =
341 store_context_->GetCookieStore()->GetCookieMonster(); 340 store_context_->GetCookieStore()->GetCookieMonster();
342 success_ = cookie_monster->SetCookieWithDetails( 341 success_ = cookie_monster->SetCookieWithDetails(
343 url_, name_, value_, domain_, path_, expiration_time_, 342 url_, name_, value_, domain_, path_, expiration_time_,
344 secure_, http_only_); 343 secure_, http_only_);
345 344
345 // Pull the newly set cookie.
346 net::CookieList cookie_list =
347 extension_cookies_helpers::GetCookieListFromStore(cookie_monster, url_);
348 net::CookieList::iterator it;
349 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
350 // Return the first matching cookie. Relies on the fact that the
351 // CookieMonster returns them in canonical order (longest path, then
352 // earliest creation time).
353 if (it->Name() == name_) {
354 result_.reset(
355 extension_cookies_helpers::CreateCookieValue(*it, store_id_));
356 break;
357 }
358 }
359
346 bool rv = BrowserThread::PostTask( 360 bool rv = BrowserThread::PostTask(
347 BrowserThread::UI, FROM_HERE, 361 BrowserThread::UI, FROM_HERE,
348 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread)); 362 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread));
349 DCHECK(rv); 363 DCHECK(rv);
350 } 364 }
351 365
352 void SetCookieFunction::RespondOnUIThread() { 366 void SetCookieFunction::RespondOnUIThread() {
353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
354 if (!success_) { 368 if (!success_) {
355 error_ = ExtensionErrorUtils::FormatErrorMessage( 369 error_ = ExtensionErrorUtils::FormatErrorMessage(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 // Read/validate input parameters. 407 // Read/validate input parameters.
394 GURL url; 408 GURL url;
395 if (!ParseUrl(details, &url, true)) 409 if (!ParseUrl(details, &url, true))
396 return false; 410 return false;
397 411
398 std::string name; 412 std::string name;
399 // Get the cookie name string or return false. 413 // Get the cookie name string or return false.
400 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name)); 414 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name));
401 415
402 URLRequestContextGetter* store_context = NULL; 416 URLRequestContextGetter* store_context = NULL;
403 if (!ParseStoreContext(details, &store_context, NULL)) 417 std::string store_id;
418 if (!ParseStoreContext(details, &store_context, &store_id))
404 return false; 419 return false;
405 DCHECK(store_context); 420 DCHECK(store_context);
406 421
407 // We don't bother to synchronously wait for the result here, because 422 // 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 423 // CookieMonster is only ever accessed on the IO thread, so any other accesses
409 // should happen after this. 424 // should happen after this.
410 bool rv = BrowserThread::PostTask( 425 bool rv = BrowserThread::PostTask(
411 BrowserThread::IO, FROM_HERE, 426 BrowserThread::IO, FROM_HERE,
412 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); 427 new RemoveCookieTask(url, name, make_scoped_refptr(store_context)));
413 DCHECK(rv); 428 DCHECK(rv);
414 429
430 DictionaryValue* resultDictionary = new DictionaryValue();
431 resultDictionary->SetString(keys::kNameKey, name);
432 resultDictionary->SetString(keys::kUrlKey, url.spec());
433 resultDictionary->SetString(keys::kStoreIdKey, store_id);
434 result_.reset(resultDictionary);
435
415 return true; 436 return true;
416 } 437 }
417 438
418 void RemoveCookieFunction::Run() { 439 void RemoveCookieFunction::Run() {
419 SendResponse(RunImpl()); 440 SendResponse(RunImpl());
420 } 441 }
421 442
422 bool GetAllCookieStoresFunction::RunImpl() { 443 bool GetAllCookieStoresFunction::RunImpl() {
423 Profile* original_profile = profile(); 444 Profile* original_profile = profile();
424 DCHECK(original_profile); 445 DCHECK(original_profile);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 extension_cookies_helpers::CreateCookieStoreValue( 480 extension_cookies_helpers::CreateCookieStoreValue(
460 incognito_profile, incognito_tab_ids.release())); 481 incognito_profile, incognito_tab_ids.release()));
461 } 482 }
462 result_.reset(cookie_store_list); 483 result_.reset(cookie_store_list);
463 return true; 484 return true;
464 } 485 }
465 486
466 void GetAllCookieStoresFunction::Run() { 487 void GetAllCookieStoresFunction::Run() {
467 SendResponse(RunImpl()); 488 SendResponse(RunImpl());
468 } 489 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_cookies_api.h ('k') | chrome/common/extensions/api/extension_api.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698