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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
6
7 #include "base/lazy_instance.h"
8 #include "chrome/browser/extensions/api/tabs/tabs_event_router.h"
9 #include "chrome/browser/extensions/api/tabs/windows_event_router.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/common/extensions/api/tabs.h"
12 #include "chrome/common/extensions/api/windows.h"
13 #include "extensions/browser/event_router.h"
14
15 namespace extensions {
16
TabsWindowsAPI(Profile * profile)17 TabsWindowsAPI::TabsWindowsAPI(Profile* profile) : profile_(profile) {
18 // Tabs API Events.
19 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
20 this, api::tabs::OnCreated::kEventName);
21 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
22 this, api::tabs::OnUpdated::kEventName);
23 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
24 this, api::tabs::OnMoved::kEventName);
25 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
26 this, api::tabs::OnSelectionChanged::kEventName);
27 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
28 this, api::tabs::OnActiveChanged::kEventName);
29 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
30 this, api::tabs::OnActivated::kEventName);
31 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
32 this, api::tabs::OnHighlightChanged::kEventName);
33 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
34 this, api::tabs::OnHighlighted::kEventName);
35 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
36 this, api::tabs::OnDetached::kEventName);
37 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
38 this, api::tabs::OnAttached::kEventName);
39 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
40 this, api::tabs::OnRemoved::kEventName);
41 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
42 this, api::tabs::OnReplaced::kEventName);
43
44 // Windows API Events.
45 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
46 this, api::windows::OnCreated::kEventName);
47 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
48 this, api::windows::OnRemoved::kEventName);
49 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
50 this, api::windows::OnFocusChanged::kEventName);
51 }
52
~TabsWindowsAPI()53 TabsWindowsAPI::~TabsWindowsAPI() {
54 }
55
56 // static
Get(Profile * profile)57 TabsWindowsAPI* TabsWindowsAPI::Get(Profile* profile) {
58 return ProfileKeyedAPIFactory<TabsWindowsAPI>::GetForProfile(profile);
59 }
60
tabs_event_router()61 TabsEventRouter* TabsWindowsAPI::tabs_event_router() {
62 if (!tabs_event_router_.get())
63 tabs_event_router_.reset(new TabsEventRouter(profile_));
64 return tabs_event_router_.get();
65 }
66
windows_event_router()67 WindowsEventRouter* TabsWindowsAPI::windows_event_router() {
68 if (!windows_event_router_)
69 windows_event_router_.reset(new WindowsEventRouter(profile_));
70 return windows_event_router_.get();
71 }
72
Shutdown()73 void TabsWindowsAPI::Shutdown() {
74 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
75 }
76
77 static base::LazyInstance<ProfileKeyedAPIFactory<TabsWindowsAPI> >
78 g_factory = LAZY_INSTANCE_INITIALIZER;
79
GetFactoryInstance()80 ProfileKeyedAPIFactory<TabsWindowsAPI>* TabsWindowsAPI::GetFactoryInstance() {
81 return &g_factory.Get();
82 }
83
OnListenerAdded(const EventListenerInfo & details)84 void TabsWindowsAPI::OnListenerAdded(const EventListenerInfo& details) {
85 // Initialize the event routers.
86 tabs_event_router();
87 windows_event_router();
88 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
89 }
90
91 } // namespace extensions
92