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