OLD | NEW |
---|---|
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 Loading... | |
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 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 if (!session_id) { | 571 if (!session_id) { |
571 SetInvalidIdError(*params->session_id); | 572 SetInvalidIdError(*params->session_id); |
572 return false; | 573 return false; |
573 } | 574 } |
574 | 575 |
575 return session_id->IsForeign() ? | 576 return session_id->IsForeign() ? |
576 RestoreForeignSession(*session_id, browser) | 577 RestoreForeignSession(*session_id, browser) |
577 : RestoreLocalSession(*session_id, browser); | 578 : RestoreLocalSession(*session_id, browser); |
578 } | 579 } |
579 | 580 |
581 SessionsEventRouter::SessionsEventRouter(Profile* profile) | |
582 : profile_(profile), tab_restore_service_(NULL) { | |
not at google - send to devlin
2014/03/21 11:03:06
just construct with the right value for tab_store_
| |
583 tab_restore_service_ = | |
584 TabRestoreServiceFactory::GetForProfile(profile_); | |
585 | |
586 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in | |
587 // incognito mode) | |
588 if (tab_restore_service_) { | |
589 // This does nothing if the tabs have already been loaded or they | |
590 // shouldn't be loaded. | |
not at google - send to devlin
2014/03/21 11:03:06
this comment is already on the interface
| |
591 tab_restore_service_->LoadTabsFromLastSession(); | |
592 tab_restore_service_->AddObserver(this); | |
593 } | |
594 } | |
595 | |
596 SessionsEventRouter::~SessionsEventRouter() { | |
597 if (tab_restore_service_) | |
598 tab_restore_service_->RemoveObserver(this); | |
599 } | |
600 | |
601 void SessionsEventRouter::TabRestoreServiceChanged( | |
602 TabRestoreService* service) { | |
603 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
604 scoped_ptr<Event> event(new Event( | |
605 api::sessions::OnChanged::kEventName, args.Pass())); | |
606 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); | |
not at google - send to devlin
2014/03/21 11:03:06
consider inlining |event|, but up to you:
Extensi
| |
607 } | |
608 | |
609 void SessionsEventRouter::TabRestoreServiceDestroyed( | |
610 TabRestoreService* service) { | |
611 tab_restore_service_ = NULL; | |
612 } | |
613 | |
614 SessionsAPI::SessionsAPI(content::BrowserContext* context) | |
615 : browser_context_(context) { | |
616 EventRouter* event_router = | |
617 ExtensionSystem::Get(browser_context_)->event_router(); | |
618 event_router->RegisterObserver(this, | |
619 api::sessions::OnChanged::kEventName); | |
not at google - send to devlin
2014/03/21 11:03:06
since you're inlining this in Shutdown() (I like i
| |
620 } | |
621 | |
622 SessionsAPI::~SessionsAPI() { | |
623 } | |
624 | |
625 void SessionsAPI::Shutdown() { | |
626 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver( | |
627 this); | |
628 } | |
629 | |
630 static base::LazyInstance<BrowserContextKeyedAPIFactory<SessionsAPI> > | |
631 g_factory = LAZY_INSTANCE_INITIALIZER; | |
632 | |
633 BrowserContextKeyedAPIFactory<SessionsAPI>* | |
634 SessionsAPI::GetFactoryInstance() { | |
635 return g_factory.Pointer(); | |
636 } | |
637 | |
638 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { | |
639 sessions_event_router_.reset( | |
640 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); | |
641 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver( | |
not at google - send to devlin
2014/03/21 11:03:06
I don't understand this. Why unregister when a lis
Yoyo Zhou
2014/03/21 15:56:02
Basically you lazily register with EventRouter whe
not at google - send to devlin
2014/03/21 15:56:56
Ah I see.
| |
642 this); | |
643 } | |
644 | |
580 } // namespace extensions | 645 } // namespace extensions |
OLD | NEW |