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

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 'cookie_list_' member var, setting 'result_' on IO thread. 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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 return true; 143 return true;
144 } 144 }
145 145
146 GetCookieFunction::GetCookieFunction() {} 146 GetCookieFunction::GetCookieFunction() {}
147 147
148 GetCookieFunction::~GetCookieFunction() {} 148 GetCookieFunction::~GetCookieFunction() {}
149 149
150 bool GetCookieFunction::RunImpl() { 150 bool GetCookieFunction::RunImpl() {
151 // Return false if the arguments are malformed. 151 // Return false if the arguments are malformed.
152 DictionaryValue* details; 152 DictionaryValue* details;
153 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); 153 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 so if this fails, you return with undefined result
Mike West 2011/02/16 11:07:28 Based on what Aaron said in his comments, it seems
Aaron Boodman 2011/02/16 19:45:10 Agree the current mechanism is bad, but I'd rather
154 DCHECK(details); 154 DCHECK(details);
155 155
156 // Read/validate input parameters. 156 // Read/validate input parameters.
157 if (!ParseUrl(details, &url_, true)) 157 if (!ParseUrl(details, &url_, true))
158 return false; 158 return false;
159 159
160 // Get the cookie name string or return false. 160 // Get the cookie name string or return false.
161 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name_)); 161 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name_));
162 162
163 URLRequestContextGetter* store_context = NULL; 163 URLRequestContextGetter* store_context = NULL;
164 if (!ParseStoreContext(details, &store_context, &store_id_)) 164 if (!ParseStoreContext(details, &store_context, &store_id_))
165 return false; 165 return false;
166 166
167 DCHECK(store_context && !store_id_.empty()); 167 DCHECK(store_context && !store_id_.empty());
168 store_context_ = store_context; 168 store_context_ = store_context;
169 169
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 // Start with "null" as return value
181 result_.reset(Value::CreateNullValue());
182
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
181 net::CookieStore* cookie_store = store_context_->GetCookieStore(); 184 net::CookieStore* cookie_store = store_context_->GetCookieStore();
182 cookie_list_ = 185
186 net::CookieList cookie_list_ =
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 no trailing underscore for local variables
Mike West 2011/02/16 11:07:28 Done.
183 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); 187 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; 188 net::CookieList::iterator it;
195 for (it = cookie_list_.begin(); it != cookie_list_.end(); ++it) { 189 for (it = cookie_list_.begin(); it != cookie_list_.end(); ++it) {
196 // Return the first matching cookie. Relies on the fact that the
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 why did you remove the comment?
Mike West 2011/02/16 11:07:28 Copy/paste error. Fixed now.
197 // CookieMonster returns them in canonical order (longest path, then
198 // earliest creation time).
199 if (it->Name() == name_) { 190 if (it->Name() == name_) {
200 result_.reset( 191 result_.reset(
201 extension_cookies_helpers::CreateCookieValue(*it, store_id_)); 192 extension_cookies_helpers::CreateCookieValue(*it, store_id_));
202 break; 193 break;
203 } 194 }
204 } 195 }
205 196
206 // The cookie doesn't exist; return null. 197 bool rv = BrowserThread::PostTask(
207 if (it == cookie_list_.end()) 198 BrowserThread::UI, FROM_HERE,
208 result_.reset(Value::CreateNullValue()); 199 NewRunnableMethod(this, &GetCookieFunction::RespondOnUIThread));
200 DCHECK(rv);
201 }
209 202
203 void GetCookieFunction::RespondOnUIThread() {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
210 SendResponse(true); 205 SendResponse(true);
211 } 206 }
212 207
213 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {} 208 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {}
214 209
215 GetAllCookiesFunction::~GetAllCookiesFunction() {} 210 GetAllCookiesFunction::~GetAllCookiesFunction() {}
216 211
217 bool GetAllCookiesFunction::RunImpl() { 212 bool GetAllCookiesFunction::RunImpl() {
218 // Return false if the arguments are malformed. 213 // Return false if the arguments are malformed.
219 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); 214 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_));
(...skipping 14 matching lines...) Expand all
234 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread)); 229 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread));
235 DCHECK(rv); 230 DCHECK(rv);
236 231
237 // Will finish asynchronously. 232 // Will finish asynchronously.
238 return true; 233 return true;
239 } 234 }
240 235
241 void GetAllCookiesFunction::GetAllCookiesOnIOThread() { 236 void GetAllCookiesFunction::GetAllCookiesOnIOThread() {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
243 net::CookieStore* cookie_store = store_context_->GetCookieStore(); 238 net::CookieStore* cookie_store = store_context_->GetCookieStore();
244 cookie_list_ = 239 net::CookieList cookie_list_ =
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 no underscore
Mike West 2011/02/16 11:07:28 Done.
245 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); 240 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_);
246 241
247 bool rv = BrowserThread::PostTask(
248 BrowserThread::UI, FROM_HERE,
249 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread));
250 DCHECK(rv);
251 }
252
253 void GetAllCookiesFunction::RespondOnUIThread() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255
256 const Extension* extension = GetExtension(); 242 const Extension* extension = GetExtension();
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 not sure whether Extension is thread safe?
Mike West 2011/02/16 11:07:28 No idea. I was copy/pasting here for consistency
Aaron Boodman 2011/02/16 19:45:10 It is. The intention is that the const Extension*
257 if (extension) { 243 if (extension) {
258 ListValue* matching_list = new ListValue(); 244 ListValue* matching_list = new ListValue();
259 extension_cookies_helpers::AppendMatchingCookiesToList( 245 extension_cookies_helpers::AppendMatchingCookiesToList(
260 cookie_list_, store_id_, url_, details_, 246 cookie_list_, store_id_, url_, details_,
261 GetExtension(), matching_list); 247 GetExtension(), matching_list);
262 result_.reset(matching_list); 248 result_.reset(matching_list);
263 } 249 }
250 bool rv = BrowserThread::PostTask(
251 BrowserThread::UI, FROM_HERE,
252 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread));
253 DCHECK(rv);
254 }
255
256 void GetAllCookiesFunction::RespondOnUIThread() {
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
264 SendResponse(true); 258 SendResponse(true);
265 } 259 }
266 260
267 SetCookieFunction::SetCookieFunction() 261 SetCookieFunction::SetCookieFunction()
268 : secure_(false), 262 : secure_(false),
269 http_only_(false), 263 http_only_(false),
270 success_(false) { 264 success_(false) {
271 } 265 }
272 266
273 SetCookieFunction::~SetCookieFunction() { 267 SetCookieFunction::~SetCookieFunction() {
274 } 268 }
275 269
276 bool SetCookieFunction::RunImpl() { 270 bool SetCookieFunction::RunImpl() {
271 // Start with "null" being returned to callback, in case things fail in
272 // this synchronous bit of the workflow.
273 result_.reset(Value::CreateNullValue());
274
277 // Return false if the arguments are malformed. 275 // Return false if the arguments are malformed.
278 DictionaryValue* details; 276 DictionaryValue* details;
279 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); 277 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
280 DCHECK(details); 278 DCHECK(details);
281 279
282 // Read/validate input parameters. 280 // Read/validate input parameters.
283 if (!ParseUrl(details, &url_, true)) 281 if (!ParseUrl(details, &url_, true))
284 return false; 282 return false;
285 // The macros below return false if argument types are not as expected. 283 // The macros below return false if argument types are not as expected.
286 if (details->HasKey(keys::kNameKey)) 284 if (details->HasKey(keys::kNameKey))
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 BrowserThread::IO, FROM_HERE, 328 BrowserThread::IO, FROM_HERE,
331 NewRunnableMethod(this, &SetCookieFunction::SetCookieOnIOThread)); 329 NewRunnableMethod(this, &SetCookieFunction::SetCookieOnIOThread));
332 DCHECK(rv); 330 DCHECK(rv);
333 331
334 // Will finish asynchronously. 332 // Will finish asynchronously.
335 return true; 333 return true;
336 } 334 }
337 335
338 void SetCookieFunction::SetCookieOnIOThread() { 336 void SetCookieFunction::SetCookieOnIOThread() {
339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
340 net::CookieMonster* cookie_monster = 338 net::CookieStore* cookie_store = store_context_->GetCookieStore();
341 store_context_->GetCookieStore()->GetCookieMonster(); 339 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster();
342 success_ = cookie_monster->SetCookieWithDetails( 340 success_ = cookie_monster->SetCookieWithDetails(
343 url_, name_, value_, domain_, path_, expiration_time_, 341 url_, name_, value_, domain_, path_, expiration_time_,
344 secure_, http_only_); 342 secure_, http_only_);
345 343
344 // Pull the newly set cookie.
345 net::CookieList cookie_list_ =
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 no underscore
Mike West 2011/02/16 11:07:28 Done.
346 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_);
347 net::CookieList::iterator it;
348 for (it = cookie_list_.begin(); it != cookie_list_.end(); ++it) {
349 if (it->Name() == name_) {
350 result_.reset(
351 extension_cookies_helpers::CreateCookieValue(*it, store_id_));
352 break;
353 }
354 }
355
346 bool rv = BrowserThread::PostTask( 356 bool rv = BrowserThread::PostTask(
347 BrowserThread::UI, FROM_HERE, 357 BrowserThread::UI, FROM_HERE,
348 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread)); 358 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread));
349 DCHECK(rv); 359 DCHECK(rv);
350 } 360 }
351 361
352 void SetCookieFunction::RespondOnUIThread() { 362 void SetCookieFunction::RespondOnUIThread() {
353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
354 if (!success_) { 364 if (!success_) {
355 error_ = ExtensionErrorUtils::FormatErrorMessage( 365 error_ = ExtensionErrorUtils::FormatErrorMessage(
356 keys::kCookieSetFailedError, name_); 366 keys::kCookieSetFailedError, name_);
357 } 367 }
368
358 SendResponse(success_); 369 SendResponse(success_);
359 } 370 }
360 371
361 namespace { 372 namespace {
362 373
363 class RemoveCookieTask : public Task { 374 class RemoveCookieTask : public Task {
364 public: 375 public:
365 RemoveCookieTask(const GURL& url, 376 RemoveCookieTask(const GURL& url,
366 const std::string& name, 377 const std::string& name,
367 const scoped_refptr<URLRequestContextGetter>& context_getter) 378 const scoped_refptr<URLRequestContextGetter>& context_getter)
(...skipping 10 matching lines...) Expand all
378 const GURL url_; 389 const GURL url_;
379 const std::string name_; 390 const std::string name_;
380 const scoped_refptr<URLRequestContextGetter> context_getter_; 391 const scoped_refptr<URLRequestContextGetter> context_getter_;
381 392
382 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTask); 393 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTask);
383 }; 394 };
384 395
385 } // namespace 396 } // namespace
386 397
387 bool RemoveCookieFunction::RunImpl() { 398 bool RemoveCookieFunction::RunImpl() {
399 // Start with "null" being returned to callback, in case things fail in
400 // this syncronous bit of the workflow.
401 result_.reset(Value::CreateNullValue());
402
388 // Return false if the arguments are malformed. 403 // Return false if the arguments are malformed.
389 DictionaryValue* details; 404 DictionaryValue* details;
390 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); 405 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
391 DCHECK(details); 406 DCHECK(details);
392 407
393 // Read/validate input parameters. 408 // Read/validate input parameters.
394 GURL url; 409 GURL url;
395 if (!ParseUrl(details, &url, true)) 410 if (!ParseUrl(details, &url, true))
396 return false; 411 return false;
397 412
398 std::string name; 413 std::string name;
399 // Get the cookie name string or return false. 414 // Get the cookie name string or return false.
400 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name)); 415 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name));
401 416
402 URLRequestContextGetter* store_context = NULL; 417 URLRequestContextGetter* store_context = NULL;
403 if (!ParseStoreContext(details, &store_context, NULL)) 418 std::string store_id_string;
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 just store_id
Mike West 2011/02/16 11:07:28 Done.
419 if (!ParseStoreContext(details, &store_context, &store_id_string))
404 return false; 420 return false;
405 DCHECK(store_context); 421 DCHECK(store_context);
406 422
407 // We don't bother to synchronously wait for the result here, because 423 // 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 424 // CookieMonster is only ever accessed on the IO thread, so any other accesses
409 // should happen after this. 425 // should happen after this.
410 bool rv = BrowserThread::PostTask( 426 bool rv = BrowserThread::PostTask(
411 BrowserThread::IO, FROM_HERE, 427 BrowserThread::IO, FROM_HERE,
412 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); 428 new RemoveCookieTask(url, name, make_scoped_refptr(store_context)));
413 DCHECK(rv); 429 DCHECK(rv);
414 430
431 DictionaryValue* resultDictionary = new DictionaryValue();
432 std::string url_string;
jochen (gone - plz use gerrit) 2011/02/16 10:02:09 just url
Mike West 2011/02/16 11:07:28 `url` is already a variable in this scope (line 40
433 details->GetString(keys::kUrlKey, &url_string);
434 resultDictionary->SetString(keys::kNameKey, name);
435 resultDictionary->SetString(keys::kUrlKey, url_string);
436 resultDictionary->SetString(keys::kStoreIdKey, store_id_string);
437 result_.reset(resultDictionary);
438
415 return true; 439 return true;
416 } 440 }
417 441
418 void RemoveCookieFunction::Run() { 442 void RemoveCookieFunction::Run() {
419 SendResponse(RunImpl()); 443 SendResponse(RunImpl());
420 } 444 }
421 445
422 bool GetAllCookieStoresFunction::RunImpl() { 446 bool GetAllCookieStoresFunction::RunImpl() {
423 Profile* original_profile = profile(); 447 Profile* original_profile = profile();
424 DCHECK(original_profile); 448 DCHECK(original_profile);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 extension_cookies_helpers::CreateCookieStoreValue( 483 extension_cookies_helpers::CreateCookieStoreValue(
460 incognito_profile, incognito_tab_ids.release())); 484 incognito_profile, incognito_tab_ids.release()));
461 } 485 }
462 result_.reset(cookie_store_list); 486 result_.reset(cookie_store_list);
463 return true; 487 return true;
464 } 488 }
465 489
466 void GetAllCookieStoresFunction::Run() { 490 void GetAllCookieStoresFunction::Run() {
467 SendResponse(RunImpl()); 491 SendResponse(RunImpl());
468 } 492 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698