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