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

Side by Side Diff: chrome/browser/extensions/api/sessions/sessions_api.cc

Issue 1642583002: Fix bug with invalid chrome.sessions.onChanged listener. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include "chrome/browser/extensions/api/sessions/sessions_api.h" 5 #include "chrome/browser/extensions/api/sessions/sessions_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 if (!session_id) { 600 if (!session_id) {
601 SetInvalidIdError(*params->session_id); 601 SetInvalidIdError(*params->session_id);
602 return false; 602 return false;
603 } 603 }
604 604
605 return session_id->IsForeign() ? 605 return session_id->IsForeign() ?
606 RestoreForeignSession(*session_id, browser) 606 RestoreForeignSession(*session_id, browser)
607 : RestoreLocalSession(*session_id, browser); 607 : RestoreLocalSession(*session_id, browser);
608 } 608 }
609 609
610 SessionsEventRouter::SessionsEventRouter(Profile* profile) 610 SessionsEventRouter::SessionsEventRouter(Profile* profile,
611 EventRouter::Observer* observer)
611 : profile_(profile), 612 : profile_(profile),
613 observer_(observer),
612 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) { 614 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) {
613 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in 615 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
614 // incognito mode) 616 // incognito mode)
615 if (tab_restore_service_) { 617 if (tab_restore_service_) {
616 tab_restore_service_->LoadTabsFromLastSession(); 618 tab_restore_service_->LoadTabsFromLastSession();
617 tab_restore_service_->AddObserver(this); 619 tab_restore_service_->AddObserver(this);
618 } 620 }
619 } 621 }
620 622
621 SessionsEventRouter::~SessionsEventRouter() { 623 SessionsEventRouter::~SessionsEventRouter() {
622 if (tab_restore_service_) 624 if (tab_restore_service_)
623 tab_restore_service_->RemoveObserver(this); 625 tab_restore_service_->RemoveObserver(this);
624 } 626 }
625 627
626 void SessionsEventRouter::TabRestoreServiceChanged( 628 void SessionsEventRouter::TabRestoreServiceChanged(
627 sessions::TabRestoreService* service) { 629 sessions::TabRestoreService* service) {
628 scoped_ptr<base::ListValue> args(new base::ListValue()); 630 scoped_ptr<base::ListValue> args(new base::ListValue());
629 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr( 631 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr(
630 new Event(events::SESSIONS_ON_CHANGED, 632 new Event(events::SESSIONS_ON_CHANGED,
631 api::sessions::OnChanged::kEventName, std::move(args)))); 633 api::sessions::OnChanged::kEventName, std::move(args))));
632 } 634 }
633 635
634 void SessionsEventRouter::TabRestoreServiceDestroyed( 636 void SessionsEventRouter::TabRestoreServiceDestroyed(
635 sessions::TabRestoreService* service) { 637 sessions::TabRestoreService* service) {
636 tab_restore_service_ = NULL; 638 tab_restore_service_ = NULL;
639 EventRouter::Get(profile_)->RegisterObserver(
640 observer_, api::sessions::OnChanged::kEventName);
637 } 641 }
638 642
639 SessionsAPI::SessionsAPI(content::BrowserContext* context) 643 SessionsAPI::SessionsAPI(content::BrowserContext* context)
640 : browser_context_(context) { 644 : browser_context_(context) {
641 EventRouter::Get(browser_context_)->RegisterObserver(this, 645 EventRouter::Get(browser_context_)->RegisterObserver(this,
642 api::sessions::OnChanged::kEventName); 646 api::sessions::OnChanged::kEventName);
643 } 647 }
644 648
645 SessionsAPI::~SessionsAPI() { 649 SessionsAPI::~SessionsAPI() {
646 } 650 }
647 651
648 void SessionsAPI::Shutdown() { 652 void SessionsAPI::Shutdown() {
649 EventRouter::Get(browser_context_)->UnregisterObserver(this); 653 EventRouter::Get(browser_context_)->UnregisterObserver(this);
650 } 654 }
651 655
652 static base::LazyInstance<BrowserContextKeyedAPIFactory<SessionsAPI> > 656 static base::LazyInstance<BrowserContextKeyedAPIFactory<SessionsAPI> >
653 g_factory = LAZY_INSTANCE_INITIALIZER; 657 g_factory = LAZY_INSTANCE_INITIALIZER;
654 658
655 BrowserContextKeyedAPIFactory<SessionsAPI>* 659 BrowserContextKeyedAPIFactory<SessionsAPI>*
656 SessionsAPI::GetFactoryInstance() { 660 SessionsAPI::GetFactoryInstance() {
657 return g_factory.Pointer(); 661 return g_factory.Pointer();
658 } 662 }
659 663
660 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { 664 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) {
661 sessions_event_router_.reset( 665 sessions_event_router_.reset(
662 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); 666 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_),
Devlin 2016/01/27 17:59:04 This doesn't seem like the right solution... why d
reat 2016/01/28 04:09:59 We do it because TabRestoreService is depends on p
Devlin 2016/01/28 22:48:05 Interesting. This stems from TabRestoreService be
667 this));
663 EventRouter::Get(browser_context_)->UnregisterObserver(this); 668 EventRouter::Get(browser_context_)->UnregisterObserver(this);
664 } 669 }
665 670
666 } // namespace extensions 671 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698