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.
5 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
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/profiles/profile.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 #include "extensions/browser/extension_system.h"
16 namespace extensions
{
18 TabsWindowsAPI::TabsWindowsAPI(content::BrowserContext
* context
)
19 : browser_context_(context
) {
20 EventRouter
* event_router
= EventRouter::Get(browser_context_
);
23 event_router
->RegisterObserver(this, api::tabs::OnCreated::kEventName
);
24 event_router
->RegisterObserver(this, api::tabs::OnUpdated::kEventName
);
25 event_router
->RegisterObserver(this, api::tabs::OnMoved::kEventName
);
26 event_router
->RegisterObserver(this,
27 api::tabs::OnSelectionChanged::kEventName
);
28 event_router
->RegisterObserver(this, api::tabs::OnActiveChanged::kEventName
);
29 event_router
->RegisterObserver(this, api::tabs::OnActivated::kEventName
);
30 event_router
->RegisterObserver(this,
31 api::tabs::OnHighlightChanged::kEventName
);
32 event_router
->RegisterObserver(this, api::tabs::OnHighlighted::kEventName
);
33 event_router
->RegisterObserver(this, api::tabs::OnDetached::kEventName
);
34 event_router
->RegisterObserver(this, api::tabs::OnAttached::kEventName
);
35 event_router
->RegisterObserver(this, api::tabs::OnRemoved::kEventName
);
36 event_router
->RegisterObserver(this, api::tabs::OnReplaced::kEventName
);
38 // Windows API Events.
39 event_router
->RegisterObserver(this, api::windows::OnCreated::kEventName
);
40 event_router
->RegisterObserver(this, api::windows::OnRemoved::kEventName
);
41 event_router
->RegisterObserver(this,
42 api::windows::OnFocusChanged::kEventName
);
45 TabsWindowsAPI::~TabsWindowsAPI() {
49 TabsWindowsAPI
* TabsWindowsAPI::Get(content::BrowserContext
* context
) {
50 return BrowserContextKeyedAPIFactory
<TabsWindowsAPI
>::Get(context
);
53 TabsEventRouter
* TabsWindowsAPI::tabs_event_router() {
54 if (!tabs_event_router_
.get())
55 tabs_event_router_
.reset(
56 new TabsEventRouter(Profile::FromBrowserContext(browser_context_
)));
57 return tabs_event_router_
.get();
60 WindowsEventRouter
* TabsWindowsAPI::windows_event_router() {
61 if (!windows_event_router_
)
62 windows_event_router_
.reset(
63 new WindowsEventRouter(Profile::FromBrowserContext(browser_context_
)));
64 return windows_event_router_
.get();
67 void TabsWindowsAPI::Shutdown() {
68 EventRouter::Get(browser_context_
)->UnregisterObserver(this);
71 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<TabsWindowsAPI
> >
72 g_factory
= LAZY_INSTANCE_INITIALIZER
;
74 BrowserContextKeyedAPIFactory
<TabsWindowsAPI
>*
75 TabsWindowsAPI::GetFactoryInstance() {
76 return g_factory
.Pointer();
79 void TabsWindowsAPI::OnListenerAdded(const EventListenerInfo
& details
) {
80 // Initialize the event routers.
82 windows_event_router();
83 EventRouter::Get(browser_context_
)->UnregisterObserver(this);
86 } // namespace extensions