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

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

Issue 201393002: Add onChanged callback for chrome.sessions API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update test Created 6 years, 9 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 <vector> 7 #include <vector>
8 8
9 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 17 matching lines...) Expand all
28 #include "chrome/browser/sync/profile_sync_service.h" 28 #include "chrome/browser/sync/profile_sync_service.h"
29 #include "chrome/browser/sync/profile_sync_service_factory.h" 29 #include "chrome/browser/sync/profile_sync_service_factory.h"
30 #include "chrome/browser/ui/browser.h" 30 #include "chrome/browser/ui/browser.h"
31 #include "chrome/browser/ui/browser_finder.h" 31 #include "chrome/browser/ui/browser_finder.h"
32 #include "chrome/browser/ui/host_desktop.h" 32 #include "chrome/browser/ui/host_desktop.h"
33 #include "chrome/browser/ui/tabs/tab_strip_model.h" 33 #include "chrome/browser/ui/tabs/tab_strip_model.h"
34 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
35 #include "content/public/browser/web_contents.h" 35 #include "content/public/browser/web_contents.h"
36 #include "extensions/browser/extension_function_dispatcher.h" 36 #include "extensions/browser/extension_function_dispatcher.h"
37 #include "extensions/browser/extension_function_registry.h" 37 #include "extensions/browser/extension_function_registry.h"
38 #include "extensions/browser/extension_system.h"
38 #include "extensions/common/error_utils.h" 39 #include "extensions/common/error_utils.h"
39 #include "net/base/net_util.h" 40 #include "net/base/net_util.h"
40 #include "ui/base/layout.h" 41 #include "ui/base/layout.h"
41 42
42 namespace extensions { 43 namespace extensions {
43 44
44 namespace GetRecentlyClosed = api::sessions::GetRecentlyClosed; 45 namespace GetRecentlyClosed = api::sessions::GetRecentlyClosed;
45 namespace GetDevices = api::sessions::GetDevices; 46 namespace GetDevices = api::sessions::GetDevices;
46 namespace Restore = api::sessions::Restore; 47 namespace Restore = api::sessions::Restore;
47 namespace tabs = api::tabs; 48 namespace tabs = api::tabs;
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 if (!session_id) { 578 if (!session_id) {
578 SetInvalidIdError(*params->session_id); 579 SetInvalidIdError(*params->session_id);
579 return false; 580 return false;
580 } 581 }
581 582
582 return session_id->IsForeign() ? 583 return session_id->IsForeign() ?
583 RestoreForeignSession(*session_id, browser) 584 RestoreForeignSession(*session_id, browser)
584 : RestoreLocalSession(*session_id, browser); 585 : RestoreLocalSession(*session_id, browser);
585 } 586 }
586 587
588 SessionsEventRouter::SessionsEventRouter(Profile* profile)
589 : profile_(profile),
590 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) {
591 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
592 // incognito mode)
593 if (tab_restore_service_) {
594 tab_restore_service_->LoadTabsFromLastSession();
595 tab_restore_service_->AddObserver(this);
596 }
597 }
598
599 SessionsEventRouter::~SessionsEventRouter() {
600 if (tab_restore_service_)
601 tab_restore_service_->RemoveObserver(this);
602 }
603
604 void SessionsEventRouter::TabRestoreServiceChanged(
605 TabRestoreService* service) {
606 scoped_ptr<base::ListValue> args(new base::ListValue());
607 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(
not at google - send to devlin 2014/03/26 21:12:38 You can now use just EventRouter::Get(profile_) he
608 make_scoped_ptr(
609 new Event(api::sessions::OnChanged::kEventName, args.Pass())));
610 }
611
612 void SessionsEventRouter::TabRestoreServiceDestroyed(
613 TabRestoreService* service) {
614 tab_restore_service_ = NULL;
615 }
616
617 SessionsAPI::SessionsAPI(content::BrowserContext* context)
618 : browser_context_(context) {
619 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
620 this, api::sessions::OnChanged::kEventName);
621 }
622
623 SessionsAPI::~SessionsAPI() {
624 }
625
626 void SessionsAPI::Shutdown() {
627 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver(
628 this);
629 }
630
631 static base::LazyInstance<BrowserContextKeyedAPIFactory<SessionsAPI> >
632 g_factory = LAZY_INSTANCE_INITIALIZER;
633
634 BrowserContextKeyedAPIFactory<SessionsAPI>*
635 SessionsAPI::GetFactoryInstance() {
636 return g_factory.Pointer();
637 }
638
639 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) {
640 sessions_event_router_.reset(
641 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_)));
642 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver(
643 this);
644 }
645
587 } // namespace extensions 646 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698