| 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/window_event_router.h" | 5 #include "chrome/browser/extensions/window_event_router.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/extensions/event_names.h" | 8 #include "chrome/browser/extensions/event_names.h" |
| 9 #include "chrome/browser/extensions/event_router.h" | 9 #include "chrome/browser/extensions/event_router.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/window_controller.h" | 11 #include "chrome/browser/extensions/window_controller.h" |
| 12 #include "chrome/browser/extensions/window_controller_list.h" | 12 #include "chrome/browser/extensions/window_controller_list.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" | 15 #include "chrome/common/extensions/extension_constants.h" |
| 16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 | 17 |
| 18 #if defined(TOOLKIT_GTK) | 18 #if defined(TOOLKIT_GTK) |
| 19 #include "ui/base/x/active_window_watcher_x.h" | 19 #include "ui/base/x/active_window_watcher_x.h" |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 namespace event_names = extensions::event_names; | 22 namespace event_names = extensions::event_names; |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 WindowEventRouter::WindowEventRouter(Profile* profile) | 26 WindowEventRouter::WindowEventRouter(Profile* profile) |
| 27 : initialized_(false), | 27 : profile_(profile), |
| 28 profile_(profile), | |
| 29 focused_profile_(NULL), | 28 focused_profile_(NULL), |
| 30 focused_window_id_(extension_misc::kUnknownWindowId) { | 29 focused_window_id_(extension_misc::kUnknownWindowId) { |
| 31 DCHECK(!profile->IsOffTheRecord()); | 30 DCHECK(!profile->IsOffTheRecord()); |
| 32 } | |
| 33 | |
| 34 WindowEventRouter::~WindowEventRouter() { | |
| 35 if (initialized_) { | |
| 36 WindowControllerList::GetInstance()->RemoveObserver(this); | |
| 37 #if defined(TOOLKIT_VIEWS) | |
| 38 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this); | |
| 39 #elif defined(TOOLKIT_GTK) | |
| 40 ui::ActiveWindowWatcherX::RemoveObserver(this); | |
| 41 #endif | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 void WindowEventRouter::Init() { | |
| 46 if (initialized_) | |
| 47 return; | |
| 48 | 31 |
| 49 WindowControllerList::GetInstance()->AddObserver(this); | 32 WindowControllerList::GetInstance()->AddObserver(this); |
| 50 #if defined(TOOLKIT_VIEWS) | 33 #if defined(TOOLKIT_VIEWS) |
| 51 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this); | 34 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this); |
| 52 #elif defined(TOOLKIT_GTK) | 35 #elif defined(TOOLKIT_GTK) |
| 53 ui::ActiveWindowWatcherX::AddObserver(this); | 36 ui::ActiveWindowWatcherX::AddObserver(this); |
| 54 #elif defined(OS_MACOSX) | 37 #elif defined(OS_MACOSX) |
| 55 // Needed for when no suitable window can be passed to an extension as the | 38 // Needed for when no suitable window can be passed to an extension as the |
| 56 // currently focused window. | 39 // currently focused window. |
| 57 registrar_.Add(this, chrome::NOTIFICATION_NO_KEY_WINDOW, | 40 registrar_.Add(this, chrome::NOTIFICATION_NO_KEY_WINDOW, |
| 58 content::NotificationService::AllSources()); | 41 content::NotificationService::AllSources()); |
| 59 #endif | 42 #endif |
| 43 } |
| 60 | 44 |
| 61 initialized_ = true; | 45 WindowEventRouter::~WindowEventRouter() { |
| 46 WindowControllerList::GetInstance()->RemoveObserver(this); |
| 47 #if defined(TOOLKIT_VIEWS) |
| 48 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this); |
| 49 #elif defined(TOOLKIT_GTK) |
| 50 ui::ActiveWindowWatcherX::RemoveObserver(this); |
| 51 #endif |
| 62 } | 52 } |
| 63 | 53 |
| 64 void WindowEventRouter::OnWindowControllerAdded( | 54 void WindowEventRouter::OnWindowControllerAdded( |
| 65 WindowController* window_controller) { | 55 WindowController* window_controller) { |
| 66 if (!profile_->IsSameProfile(window_controller->profile())) | 56 if (!profile_->IsSameProfile(window_controller->profile())) |
| 67 return; | 57 return; |
| 68 | 58 |
| 69 scoped_ptr<base::ListValue> args(new ListValue()); | 59 scoped_ptr<base::ListValue> args(new ListValue()); |
| 70 DictionaryValue* window_dictionary = window_controller->CreateWindowValue(); | 60 DictionaryValue* window_dictionary = window_controller->CreateWindowValue(); |
| 71 args->Append(window_dictionary); | 61 args->Append(window_dictionary); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 149 } |
| 160 | 150 |
| 161 void WindowEventRouter::DispatchEvent(const char* event_name, | 151 void WindowEventRouter::DispatchEvent(const char* event_name, |
| 162 Profile* profile, | 152 Profile* profile, |
| 163 scoped_ptr<base::ListValue> args) { | 153 scoped_ptr<base::ListValue> args) { |
| 164 ExtensionSystem::Get(profile)->event_router()-> | 154 ExtensionSystem::Get(profile)->event_router()-> |
| 165 DispatchEventToRenderers(event_name, args.Pass(), profile, GURL()); | 155 DispatchEventToRenderers(event_name, args.Pass(), profile, GURL()); |
| 166 } | 156 } |
| 167 | 157 |
| 168 } // namespace extensions | 158 } // namespace extensions |
| OLD | NEW |