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

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: 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 538 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 if (!session_id) { 587 if (!session_id) {
587 SetInvalidIdError(*params->session_id); 588 SetInvalidIdError(*params->session_id);
588 return false; 589 return false;
589 } 590 }
590 591
591 return session_id->IsForeign() ? 592 return session_id->IsForeign() ?
592 RestoreForeignSession(*session_id, browser) 593 RestoreForeignSession(*session_id, browser)
593 : RestoreLocalSession(*session_id, browser); 594 : RestoreLocalSession(*session_id, browser);
594 } 595 }
595 596
597 SessionsEventRouter::SessionsEventRouter(Profile* profile)
598 : profile_(profile),
599 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) {
600 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
601 // incognito mode)
602 if (tab_restore_service_) {
603 tab_restore_service_->LoadTabsFromLastSession();
604 tab_restore_service_->AddObserver(this);
605 }
606 }
607
608 SessionsEventRouter::~SessionsEventRouter() {
609 if (tab_restore_service_)
610 tab_restore_service_->RemoveObserver(this);
611 }
612
613 void SessionsEventRouter::TabRestoreServiceChanged(
614 TabRestoreService* service) {
615 scoped_ptr<base::ListValue> args(new base::ListValue());
616 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr(
617 new Event(api::sessions::OnChanged::kEventName, args.Pass())));
618 }
619
620 void SessionsEventRouter::TabRestoreServiceDestroyed(
621 TabRestoreService* service) {
622 tab_restore_service_ = NULL;
623 }
624
625 SessionsAPI::SessionsAPI(content::BrowserContext* context)
626 : browser_context_(context) {
627 EventRouter::Get(browser_context_)->RegisterObserver(this,
628 api::sessions::OnChanged::kEventName);
629 }
630
631 SessionsAPI::~SessionsAPI() {
632 }
633
634 void SessionsAPI::Shutdown() {
635 EventRouter::Get(browser_context_)->UnregisterObserver(this);
636 }
637
638 static base::LazyInstance<BrowserContextKeyedAPIFactory<SessionsAPI> >
639 g_factory = LAZY_INSTANCE_INITIALIZER;
640
641 BrowserContextKeyedAPIFactory<SessionsAPI>*
642 SessionsAPI::GetFactoryInstance() {
643 return g_factory.Pointer();
644 }
645
646 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) {
647 sessions_event_router_.reset(
648 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_)));
649 EventRouter::Get(browser_context_)->UnregisterObserver(this);
650 }
651
596 } // namespace extensions 652 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698