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 #ifndef CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_EVENT_ROUTER_H_
10 #include "base/basictypes.h"
11 #include "base/containers/scoped_ptr_map.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/extensions/window_controller_list_observer.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "extensions/browser/app_window/app_window_registry.h"
18 #include "extensions/browser/extension_event_histogram_value.h"
20 #if !defined(OS_MACOSX)
21 #include "ui/views/focus/widget_focus_manager.h"
30 namespace extensions
{
33 class AppWindowController
;
34 class WindowControllerList
;
36 // The WindowsEventRouter sends chrome.windows.* events to listeners
37 // inside extension process renderers. The router listens to *all* events,
38 // but will only route events within a profile to extension processes in the
40 class WindowsEventRouter
: public AppWindowRegistry::Observer
,
41 public WindowControllerListObserver
,
42 #if !defined(OS_MACOSX)
43 public views::WidgetFocusChangeListener
,
45 public content::NotificationObserver
{
47 explicit WindowsEventRouter(Profile
* profile
);
48 ~WindowsEventRouter() override
;
50 // |window_controller| is NULL to indicate a focused window has lost focus.
51 void OnActiveWindowChanged(WindowController
* window_controller
);
54 // extensions::AppWindowRegistry::Observer:
55 void OnAppWindowAdded(extensions::AppWindow
* app_window
) override
;
56 void OnAppWindowRemoved(extensions::AppWindow
* app_window
) override
;
57 void OnAppWindowActivated(extensions::AppWindow
* app_window
) override
;
59 // WindowControllerListObserver methods:
60 void OnWindowControllerAdded(WindowController
* window_controller
) override
;
61 void OnWindowControllerRemoved(WindowController
* window
) override
;
63 #if !defined(OS_MACOSX)
64 void OnNativeFocusChanged(gfx::NativeView focused_now
) override
;
67 // content::NotificationObserver.
68 void Observe(int type
,
69 const content::NotificationSource
& source
,
70 const content::NotificationDetails
& details
) override
;
72 void DispatchEvent(events::HistogramValue histogram_value
,
73 const std::string
& event_name
,
74 WindowController
* window_controller
,
75 scoped_ptr
<base::ListValue
> args
);
76 bool HasEventListener(const std::string
& event_name
);
77 void AddAppWindow(extensions::AppWindow
* app_window
);
79 content::NotificationRegistrar registrar_
;
81 // The main profile that owns this event router.
84 // The profile the currently focused window belongs to; either the main or
85 // incognito profile or NULL (none of the above). We remember this in order
86 // to correctly handle focus changes between non-OTR and OTR windows.
87 Profile
* focused_profile_
;
89 // The currently focused window. We keep this so as to avoid sending multiple
90 // windows.onFocusChanged events with the same windowId.
91 int focused_window_id_
;
93 typedef base::ScopedPtrMap
<int, scoped_ptr
<AppWindowController
>> AppWindowMap
;
94 // Map of application windows, the key to the session of the app window.
95 AppWindowMap app_windows_
;
97 // Observed AppWindowRegistry.
98 ScopedObserver
<AppWindowRegistry
, AppWindowRegistry::Observer
>
99 observed_app_registry_
;
101 // Observed WindowControllerList.
102 ScopedObserver
<WindowControllerList
, WindowControllerListObserver
>
103 observed_controller_list_
;
105 DISALLOW_COPY_AND_ASSIGN(WindowsEventRouter
);
108 } // namespace extensions
110 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_EVENT_ROUTER_H_