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/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" 15 16 namespace extensions { 17 18 TabsWindowsAPI::TabsWindowsAPI(content::BrowserContext* context) 19 : browser_context_(context) { 20 EventRouter* event_router = EventRouter::Get(browser_context_); 21 22 // Tabs API Events. 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); 37 event_router->RegisterObserver(this, api::tabs::OnZoomChange::kEventName); 38 39 // Windows API Events. 40 event_router->RegisterObserver(this, api::windows::OnCreated::kEventName); 41 event_router->RegisterObserver(this, api::windows::OnRemoved::kEventName); 42 event_router->RegisterObserver(this, 43 api::windows::OnFocusChanged::kEventName); 44 } 45 46 TabsWindowsAPI::~TabsWindowsAPI() { 47 } 48 49 // static 50 TabsWindowsAPI* TabsWindowsAPI::Get(content::BrowserContext* context) { 51 return BrowserContextKeyedAPIFactory<TabsWindowsAPI>::Get(context); 52 } 53 54 TabsEventRouter* TabsWindowsAPI::tabs_event_router() { 55 if (!tabs_event_router_.get()) 56 tabs_event_router_.reset( 57 new TabsEventRouter(Profile::FromBrowserContext(browser_context_))); 58 return tabs_event_router_.get(); 59 } 60 61 WindowsEventRouter* TabsWindowsAPI::windows_event_router() { 62 if (!windows_event_router_) 63 windows_event_router_.reset( 64 new WindowsEventRouter(Profile::FromBrowserContext(browser_context_))); 65 return windows_event_router_.get(); 66 } 67 68 void TabsWindowsAPI::Shutdown() { 69 EventRouter::Get(browser_context_)->UnregisterObserver(this); 70 } 71 72 static base::LazyInstance<BrowserContextKeyedAPIFactory<TabsWindowsAPI> > 73 g_factory = LAZY_INSTANCE_INITIALIZER; 74 75 BrowserContextKeyedAPIFactory<TabsWindowsAPI>* 76 TabsWindowsAPI::GetFactoryInstance() { 77 return g_factory.Pointer(); 78 } 79 80 void TabsWindowsAPI::OnListenerAdded(const EventListenerInfo& details) { 81 // Initialize the event routers. 82 tabs_event_router(); 83 windows_event_router(); 84 EventRouter::Get(browser_context_)->UnregisterObserver(this); 85 } 86 87 } // namespace extensions 88