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

Side by Side Diff: chrome/browser/extensions/api/cookies/cookies_api.cc

Issue 2327723002: [Extensions] Convert some ChromeSyncExtensionFunctions (Closed)
Patch Set: fix Created 4 years, 3 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
OLDNEW
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 <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "chrome/browser/chrome_notification_types.h" 18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" 19 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h"
20 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" 20 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h"
21 #include "chrome/browser/extensions/chrome_extension_function_details.h"
21 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h" 23 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_list.h" 24 #include "chrome/browser/ui/browser_list.h"
24 #include "chrome/common/extensions/api/cookies.h" 25 #include "chrome/common/extensions/api/cookies.h"
25 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
27 #include "extensions/browser/event_router.h" 28 #include "extensions/browser/event_router.h"
28 #include "extensions/common/error_utils.h" 29 #include "extensions/common/error_utils.h"
29 #include "extensions/common/extension.h" 30 #include "extensions/common/extension.h"
30 #include "extensions/common/permissions/permissions_data.h" 31 #include "extensions/common/permissions/permissions_data.h"
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 BrowserThread::UI, FROM_HERE, 519 BrowserThread::UI, FROM_HERE,
519 base::Bind(&CookiesRemoveFunction::RespondOnUIThread, this)); 520 base::Bind(&CookiesRemoveFunction::RespondOnUIThread, this));
520 DCHECK(rv); 521 DCHECK(rv);
521 } 522 }
522 523
523 void CookiesRemoveFunction::RespondOnUIThread() { 524 void CookiesRemoveFunction::RespondOnUIThread() {
524 DCHECK_CURRENTLY_ON(BrowserThread::UI); 525 DCHECK_CURRENTLY_ON(BrowserThread::UI);
525 SendResponse(true); 526 SendResponse(true);
526 } 527 }
527 528
528 bool CookiesGetAllCookieStoresFunction::RunSync() { 529 ExtensionFunction::ResponseAction CookiesGetAllCookieStoresFunction::Run() {
529 Profile* original_profile = GetProfile(); 530 Profile* original_profile = Profile::FromBrowserContext(browser_context());
530 DCHECK(original_profile); 531 DCHECK(original_profile);
531 std::unique_ptr<base::ListValue> original_tab_ids(new base::ListValue()); 532 std::unique_ptr<base::ListValue> original_tab_ids(new base::ListValue());
532 Profile* incognito_profile = NULL; 533 Profile* incognito_profile = NULL;
533 std::unique_ptr<base::ListValue> incognito_tab_ids; 534 std::unique_ptr<base::ListValue> incognito_tab_ids;
534 if (include_incognito() && GetProfile()->HasOffTheRecordProfile()) { 535 if (include_incognito() && original_profile->HasOffTheRecordProfile()) {
535 incognito_profile = GetProfile()->GetOffTheRecordProfile(); 536 incognito_profile = original_profile->GetOffTheRecordProfile();
536 if (incognito_profile) 537 if (incognito_profile)
537 incognito_tab_ids.reset(new base::ListValue()); 538 incognito_tab_ids.reset(new base::ListValue());
538 } 539 }
539 DCHECK(original_profile != incognito_profile); 540 DCHECK(original_profile != incognito_profile);
540 541
541 // Iterate through all browser instances, and for each browser, 542 // Iterate through all browser instances, and for each browser,
542 // add its tab IDs to either the regular or incognito tab ID list depending 543 // add its tab IDs to either the regular or incognito tab ID list depending
543 // whether the browser is regular or incognito. 544 // whether the browser is regular or incognito.
544 for (auto* browser : *BrowserList::GetInstance()) { 545 for (auto* browser : *BrowserList::GetInstance()) {
545 if (browser->profile() == original_profile) { 546 if (browser->profile() == original_profile) {
546 cookies_helpers::AppendToTabIdList(browser, original_tab_ids.get()); 547 cookies_helpers::AppendToTabIdList(browser, original_tab_ids.get());
547 } else if (incognito_tab_ids.get() && 548 } else if (incognito_tab_ids.get() &&
548 browser->profile() == incognito_profile) { 549 browser->profile() == incognito_profile) {
549 cookies_helpers::AppendToTabIdList(browser, incognito_tab_ids.get()); 550 cookies_helpers::AppendToTabIdList(browser, incognito_tab_ids.get());
550 } 551 }
551 } 552 }
552 // Return a list of all cookie stores with at least one open tab. 553 // Return a list of all cookie stores with at least one open tab.
553 std::vector<cookies::CookieStore> cookie_stores; 554 std::vector<cookies::CookieStore> cookie_stores;
554 if (original_tab_ids->GetSize() > 0) { 555 if (original_tab_ids->GetSize() > 0) {
555 cookie_stores.push_back(cookies_helpers::CreateCookieStore( 556 cookie_stores.push_back(cookies_helpers::CreateCookieStore(
556 original_profile, original_tab_ids.release())); 557 original_profile, original_tab_ids.release()));
557 } 558 }
558 if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 && 559 if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 &&
559 incognito_profile) { 560 incognito_profile) {
560 cookie_stores.push_back(cookies_helpers::CreateCookieStore( 561 cookie_stores.push_back(cookies_helpers::CreateCookieStore(
561 incognito_profile, incognito_tab_ids.release())); 562 incognito_profile, incognito_tab_ids.release()));
562 } 563 }
563 results_ = GetAllCookieStores::Results::Create(cookie_stores); 564 return RespondNow(
564 return true; 565 ArgumentList(GetAllCookieStores::Results::Create(cookie_stores)));
565 } 566 }
566 567
567 CookiesAPI::CookiesAPI(content::BrowserContext* context) 568 CookiesAPI::CookiesAPI(content::BrowserContext* context)
568 : browser_context_(context) { 569 : browser_context_(context) {
569 EventRouter::Get(browser_context_) 570 EventRouter::Get(browser_context_)
570 ->RegisterObserver(this, cookies::OnChanged::kEventName); 571 ->RegisterObserver(this, cookies::OnChanged::kEventName);
571 } 572 }
572 573
573 CookiesAPI::~CookiesAPI() { 574 CookiesAPI::~CookiesAPI() {
574 } 575 }
575 576
576 void CookiesAPI::Shutdown() { 577 void CookiesAPI::Shutdown() {
577 EventRouter::Get(browser_context_)->UnregisterObserver(this); 578 EventRouter::Get(browser_context_)->UnregisterObserver(this);
578 } 579 }
579 580
580 static base::LazyInstance<BrowserContextKeyedAPIFactory<CookiesAPI> > 581 static base::LazyInstance<BrowserContextKeyedAPIFactory<CookiesAPI> >
581 g_factory = LAZY_INSTANCE_INITIALIZER; 582 g_factory = LAZY_INSTANCE_INITIALIZER;
582 583
583 // static 584 // static
584 BrowserContextKeyedAPIFactory<CookiesAPI>* CookiesAPI::GetFactoryInstance() { 585 BrowserContextKeyedAPIFactory<CookiesAPI>* CookiesAPI::GetFactoryInstance() {
585 return g_factory.Pointer(); 586 return g_factory.Pointer();
586 } 587 }
587 588
588 void CookiesAPI::OnListenerAdded(const EventListenerInfo& details) { 589 void CookiesAPI::OnListenerAdded(const EventListenerInfo& details) {
589 cookies_event_router_.reset(new CookiesEventRouter(browser_context_)); 590 cookies_event_router_.reset(new CookiesEventRouter(browser_context_));
590 EventRouter::Get(browser_context_)->UnregisterObserver(this); 591 EventRouter::Get(browser_context_)->UnregisterObserver(this);
591 } 592 }
592 593
593 } // namespace extensions 594 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698