- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / tabs / windows_event_router.cc
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/windows_event_router.h"
6
7 #include "base/values.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/event_router.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_system.h"
12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/extensions/window_controller.h"
14 #include "chrome/browser/extensions/window_controller_list.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/extensions/api/windows.h"
17 #include "chrome/common/extensions/extension_constants.h"
18 #include "content/public/browser/notification_service.h"
19
20 #if defined(TOOLKIT_GTK)
21 #include "ui/base/x/active_window_watcher_x.h"
22 #endif
23
24 namespace extensions {
25
26 namespace windows = extensions::api::windows;
27
28 WindowsEventRouter::WindowsEventRouter(Profile* profile)
29     : profile_(profile),
30       focused_profile_(NULL),
31       focused_window_id_(extension_misc::kUnknownWindowId) {
32   DCHECK(!profile->IsOffTheRecord());
33
34   WindowControllerList::GetInstance()->AddObserver(this);
35 #if defined(TOOLKIT_VIEWS)
36   views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this);
37 #elif defined(TOOLKIT_GTK)
38   ui::ActiveWindowWatcherX::AddObserver(this);
39 #elif defined(OS_MACOSX)
40   // Needed for when no suitable window can be passed to an extension as the
41   // currently focused window.
42   registrar_.Add(this, chrome::NOTIFICATION_NO_KEY_WINDOW,
43                  content::NotificationService::AllSources());
44 #endif
45 }
46
47 WindowsEventRouter::~WindowsEventRouter() {
48   WindowControllerList::GetInstance()->RemoveObserver(this);
49 #if defined(TOOLKIT_VIEWS)
50   views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this);
51 #elif defined(TOOLKIT_GTK)
52   ui::ActiveWindowWatcherX::RemoveObserver(this);
53 #endif
54 }
55
56 void WindowsEventRouter::OnWindowControllerAdded(
57     WindowController* window_controller) {
58   if (!profile_->IsSameProfile(window_controller->profile()))
59     return;
60
61   scoped_ptr<base::ListValue> args(new base::ListValue());
62   base::DictionaryValue* window_dictionary =
63       window_controller->CreateWindowValue();
64   args->Append(window_dictionary);
65   DispatchEvent(windows::OnCreated::kEventName, window_controller->profile(),
66                 args.Pass());
67 }
68
69 void WindowsEventRouter::OnWindowControllerRemoved(
70     WindowController* window_controller) {
71   if (!profile_->IsSameProfile(window_controller->profile()))
72     return;
73
74   int window_id = window_controller->GetWindowId();
75   scoped_ptr<base::ListValue> args(new base::ListValue());
76   args->Append(new base::FundamentalValue(window_id));
77   DispatchEvent(windows::OnRemoved::kEventName,
78                 window_controller->profile(),
79                 args.Pass());
80 }
81
82 #if defined(TOOLKIT_VIEWS)
83 void WindowsEventRouter::OnNativeFocusChange(
84     gfx::NativeView focused_before,
85     gfx::NativeView focused_now) {
86   if (!focused_now)
87     OnActiveWindowChanged(NULL);
88 }
89 #elif defined(TOOLKIT_GTK)
90 void WindowsEventRouter::ActiveWindowChanged(
91     GdkWindow* active_window) {
92   if (!active_window)
93     OnActiveWindowChanged(NULL);
94 }
95 #endif
96
97 void WindowsEventRouter::Observe(
98     int type,
99     const content::NotificationSource& source,
100     const content::NotificationDetails& details) {
101 #if defined(OS_MACOSX)
102   if (chrome::NOTIFICATION_NO_KEY_WINDOW == type) {
103       OnActiveWindowChanged(NULL);
104       return;
105   }
106 #endif
107 }
108
109 static void WillDispatchWindowFocusedEvent(Profile* new_active_profile,
110                                            int window_id,
111                                            Profile* profile,
112                                            const Extension* extension,
113                                            base::ListValue* event_args) {
114   // When switching between windows in the default and incognito profiles,
115   // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that
116   // can't see the new focused window across the incognito boundary.
117   // See crbug.com/46610.
118   if (new_active_profile && new_active_profile != profile &&
119       !extension_util::CanCrossIncognito(
120           extension,
121           extensions::ExtensionSystem::Get(profile)->extension_service())) {
122     event_args->Clear();
123     event_args->Append(new base::FundamentalValue(
124         extension_misc::kUnknownWindowId));
125   } else {
126     event_args->Clear();
127     event_args->Append(new base::FundamentalValue(window_id));
128   }
129 }
130
131 void WindowsEventRouter::OnActiveWindowChanged(
132     WindowController* window_controller) {
133   Profile* window_profile = NULL;
134   int window_id = extension_misc::kUnknownWindowId;
135   if (window_controller &&
136       profile_->IsSameProfile(window_controller->profile())) {
137     window_profile = window_controller->profile();
138     window_id = window_controller->GetWindowId();
139   }
140
141   if (focused_window_id_ == window_id)
142     return;
143
144   // window_profile is either the default profile for the active window, its
145   // incognito profile, or NULL iff the previous profile is losing focus.
146   focused_profile_ = window_profile;
147   focused_window_id_ = window_id;
148
149   scoped_ptr<Event> event(new Event(windows::OnFocusChanged::kEventName,
150                                     make_scoped_ptr(new base::ListValue())));
151   event->will_dispatch_callback =
152       base::Bind(&WillDispatchWindowFocusedEvent, window_profile, window_id);
153   ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass());
154 }
155
156 void WindowsEventRouter::DispatchEvent(const std::string& event_name,
157                                       Profile* profile,
158                                       scoped_ptr<base::ListValue> args) {
159   scoped_ptr<Event> event(new Event(event_name, args.Pass()));
160   event->restrict_to_profile = profile;
161   ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass());
162 }
163
164 }  // namespace extensions