OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" | 5 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/extensions/api/tabs/app_window_controller.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_util.h" | 11 #include "chrome/browser/extensions/extension_util.h" |
11 #include "chrome/browser/extensions/window_controller.h" | 12 #include "chrome/browser/extensions/window_controller.h" |
12 #include "chrome/browser/extensions/window_controller_list.h" | 13 #include "chrome/browser/extensions/window_controller_list.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/common/extensions/api/windows.h" | 15 #include "chrome/common/extensions/api/windows.h" |
15 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 18 #include "extensions/browser/app_window/app_window.h" |
17 #include "extensions/browser/event_router.h" | 19 #include "extensions/browser/event_router.h" |
18 #include "extensions/common/constants.h" | 20 #include "extensions/common/constants.h" |
19 | 21 |
20 using content::BrowserContext; | 22 using content::BrowserContext; |
21 | 23 |
22 namespace extensions { | 24 namespace extensions { |
23 | 25 |
24 namespace windows = extensions::api::windows; | 26 namespace windows = extensions::api::windows; |
25 | 27 |
26 WindowsEventRouter::WindowsEventRouter(Profile* profile) | 28 WindowsEventRouter::WindowsEventRouter(Profile* profile) |
27 : profile_(profile), | 29 : profile_(profile), |
28 focused_profile_(NULL), | 30 focused_profile_(NULL), |
29 focused_window_id_(extension_misc::kUnknownWindowId) { | 31 focused_window_id_(extension_misc::kUnknownWindowId), |
| 32 dispatch_events_(false) { |
30 DCHECK(!profile->IsOffTheRecord()); | 33 DCHECK(!profile->IsOffTheRecord()); |
31 | 34 |
32 WindowControllerList::GetInstance()->AddObserver(this); | 35 WindowControllerList::GetInstance()->AddObserver(this); |
| 36 AppWindowRegistry::Get(profile_)->AddObserver(this); |
33 // Needed for when no suitable window can be passed to an extension as the | 37 // Needed for when no suitable window can be passed to an extension as the |
34 // currently focused window. On Mac (even in a toolkit-views build) always | 38 // currently focused window. On Mac (even in a toolkit-views build) always |
35 // rely on the notification sent by AppControllerMac after AppKit sends | 39 // rely on the notification sent by AppControllerMac after AppKit sends |
36 // NSWindowDidBecomeKeyNotification and there is no [NSApp keyWindow]. This | 40 // NSWindowDidBecomeKeyNotification and there is no [NSApp keyWindow]. This |
37 // allows windows not created by toolkit-views to be tracked. | 41 // allows windows not created by toolkit-views to be tracked. |
38 // TODO(tapted): Remove the ifdefs (and NOTIFICATION_NO_KEY_WINDOW) when | 42 // TODO(tapted): Remove the ifdefs (and NOTIFICATION_NO_KEY_WINDOW) when |
39 // Chrome on Mac only makes windows with toolkit-views. | 43 // Chrome on Mac only makes windows with toolkit-views. |
40 #if defined(OS_MACOSX) | 44 #if defined(OS_MACOSX) |
41 registrar_.Add(this, chrome::NOTIFICATION_NO_KEY_WINDOW, | 45 registrar_.Add(this, chrome::NOTIFICATION_NO_KEY_WINDOW, |
42 content::NotificationService::AllSources()); | 46 content::NotificationService::AllSources()); |
43 #elif defined(TOOLKIT_VIEWS) | 47 #elif defined(TOOLKIT_VIEWS) |
44 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this); | 48 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this); |
45 #else | 49 #else |
46 #error Unsupported | 50 #error Unsupported |
47 #endif | 51 #endif |
| 52 |
| 53 AppWindowRegistry* registry = AppWindowRegistry::Get(profile_); |
| 54 for (AppWindow* app_window : registry->app_windows()) { |
| 55 scoped_ptr<AppWindowController> controller(new AppWindowController( |
| 56 app_window, make_scoped_ptr(new AppBaseWindow(app_window)), profile_)); |
| 57 app_windows_.set(app_window->session_id().id(), controller.Pass()); |
| 58 } |
48 } | 59 } |
49 | 60 |
50 WindowsEventRouter::~WindowsEventRouter() { | 61 WindowsEventRouter::~WindowsEventRouter() { |
| 62 AppWindowRegistry::Get(profile_)->RemoveObserver(this); |
51 WindowControllerList::GetInstance()->RemoveObserver(this); | 63 WindowControllerList::GetInstance()->RemoveObserver(this); |
52 #if !defined(OS_MACOSX) | 64 #if !defined(OS_MACOSX) |
53 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this); | 65 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this); |
54 #endif | 66 #endif |
55 } | 67 } |
56 | 68 |
| 69 void WindowsEventRouter::DispatchEvents() { |
| 70 dispatch_events_ = true; |
| 71 } |
| 72 |
| 73 void WindowsEventRouter::OnAppWindowAdded(extensions::AppWindow* app_window) { |
| 74 scoped_ptr<AppWindowController> controller(new AppWindowController( |
| 75 app_window, make_scoped_ptr(new AppBaseWindow(app_window)), profile_)); |
| 76 app_windows_.set(app_window->session_id().id(), controller.Pass()); |
| 77 } |
| 78 |
| 79 void WindowsEventRouter::OnAppWindowRemoved(extensions::AppWindow* app_window) { |
| 80 app_windows_.erase(app_window->session_id().id()); |
| 81 } |
| 82 |
57 void WindowsEventRouter::OnWindowControllerAdded( | 83 void WindowsEventRouter::OnWindowControllerAdded( |
58 WindowController* window_controller) { | 84 WindowController* window_controller) { |
| 85 if (!dispatch_events_) |
| 86 return; |
59 if (!profile_->IsSameProfile(window_controller->profile())) | 87 if (!profile_->IsSameProfile(window_controller->profile())) |
60 return; | 88 return; |
61 | 89 |
62 scoped_ptr<base::ListValue> args(new base::ListValue()); | 90 scoped_ptr<base::ListValue> args(new base::ListValue()); |
63 base::DictionaryValue* window_dictionary = | 91 base::DictionaryValue* window_dictionary = |
64 window_controller->CreateWindowValue(); | 92 window_controller->CreateWindowValue(); |
65 args->Append(window_dictionary); | 93 args->Append(window_dictionary); |
66 DispatchEvent(windows::OnCreated::kEventName, window_controller->profile(), | 94 DispatchEvent(windows::OnCreated::kEventName, window_controller->profile(), |
67 args.Pass()); | 95 args.Pass()); |
68 } | 96 } |
69 | 97 |
70 void WindowsEventRouter::OnWindowControllerRemoved( | 98 void WindowsEventRouter::OnWindowControllerRemoved( |
71 WindowController* window_controller) { | 99 WindowController* window_controller) { |
| 100 if (!dispatch_events_) |
| 101 return; |
72 if (!profile_->IsSameProfile(window_controller->profile())) | 102 if (!profile_->IsSameProfile(window_controller->profile())) |
73 return; | 103 return; |
74 | 104 |
75 int window_id = window_controller->GetWindowId(); | 105 int window_id = window_controller->GetWindowId(); |
76 scoped_ptr<base::ListValue> args(new base::ListValue()); | 106 scoped_ptr<base::ListValue> args(new base::ListValue()); |
77 args->Append(new base::FundamentalValue(window_id)); | 107 args->Append(new base::FundamentalValue(window_id)); |
78 DispatchEvent(windows::OnRemoved::kEventName, | 108 DispatchEvent(windows::OnRemoved::kEventName, |
79 window_controller->profile(), | 109 window_controller->profile(), |
80 args.Pass()); | 110 args.Pass()); |
81 } | 111 } |
82 | 112 |
83 #if !defined(OS_MACOSX) | 113 #if !defined(OS_MACOSX) |
84 void WindowsEventRouter::OnNativeFocusChanged(gfx::NativeView focused_now) { | 114 void WindowsEventRouter::OnNativeFocusChanged(gfx::NativeView focused_now) { |
| 115 if (!dispatch_events_) |
| 116 return; |
85 if (!focused_now) | 117 if (!focused_now) |
86 OnActiveWindowChanged(NULL); | 118 OnActiveWindowChanged(NULL); |
87 } | 119 } |
88 #endif | 120 #endif |
89 | 121 |
90 void WindowsEventRouter::Observe( | 122 void WindowsEventRouter::Observe( |
91 int type, | 123 int type, |
92 const content::NotificationSource& source, | 124 const content::NotificationSource& source, |
93 const content::NotificationDetails& details) { | 125 const content::NotificationDetails& details) { |
| 126 if (!dispatch_events_) |
| 127 return; |
94 #if defined(OS_MACOSX) | 128 #if defined(OS_MACOSX) |
95 if (chrome::NOTIFICATION_NO_KEY_WINDOW == type) { | 129 if (chrome::NOTIFICATION_NO_KEY_WINDOW == type) { |
96 OnActiveWindowChanged(NULL); | 130 OnActiveWindowChanged(NULL); |
97 return; | 131 return; |
98 } | 132 } |
99 #endif | 133 #endif |
100 } | 134 } |
101 | 135 |
102 static bool WillDispatchWindowFocusedEvent(BrowserContext* new_active_context, | 136 static bool WillDispatchWindowFocusedEvent(BrowserContext* new_active_context, |
103 int window_id, | 137 int window_id, |
(...skipping 27 matching lines...) Expand all Loading... |
131 } | 165 } |
132 | 166 |
133 if (focused_window_id_ == window_id) | 167 if (focused_window_id_ == window_id) |
134 return; | 168 return; |
135 | 169 |
136 // window_profile is either the default profile for the active window, its | 170 // window_profile is either the default profile for the active window, its |
137 // incognito profile, or NULL iff the previous profile is losing focus. | 171 // incognito profile, or NULL iff the previous profile is losing focus. |
138 focused_profile_ = window_profile; | 172 focused_profile_ = window_profile; |
139 focused_window_id_ = window_id; | 173 focused_window_id_ = window_id; |
140 | 174 |
| 175 if (!dispatch_events_) |
| 176 return; |
| 177 |
141 scoped_ptr<Event> event(new Event(events::UNKNOWN, | 178 scoped_ptr<Event> event(new Event(events::UNKNOWN, |
142 windows::OnFocusChanged::kEventName, | 179 windows::OnFocusChanged::kEventName, |
143 make_scoped_ptr(new base::ListValue()))); | 180 make_scoped_ptr(new base::ListValue()))); |
144 event->will_dispatch_callback = | 181 event->will_dispatch_callback = |
145 base::Bind(&WillDispatchWindowFocusedEvent, | 182 base::Bind(&WillDispatchWindowFocusedEvent, |
146 static_cast<BrowserContext*>(window_profile), | 183 static_cast<BrowserContext*>(window_profile), |
147 window_id); | 184 window_id); |
148 EventRouter::Get(profile_)->BroadcastEvent(event.Pass()); | 185 EventRouter::Get(profile_)->BroadcastEvent(event.Pass()); |
149 } | 186 } |
150 | 187 |
151 void WindowsEventRouter::DispatchEvent(const std::string& event_name, | 188 void WindowsEventRouter::DispatchEvent(const std::string& event_name, |
152 Profile* profile, | 189 Profile* profile, |
153 scoped_ptr<base::ListValue> args) { | 190 scoped_ptr<base::ListValue> args) { |
154 scoped_ptr<Event> event(new Event(events::UNKNOWN, event_name, args.Pass())); | 191 scoped_ptr<Event> event(new Event(events::UNKNOWN, event_name, args.Pass())); |
155 event->restrict_to_browser_context = profile; | 192 event->restrict_to_browser_context = profile; |
156 EventRouter::Get(profile)->BroadcastEvent(event.Pass()); | 193 EventRouter::Get(profile)->BroadcastEvent(event.Pass()); |
157 } | 194 } |
158 | 195 |
159 } // namespace extensions | 196 } // namespace extensions |
OLD | NEW |