Chromium Code Reviews| Index: chrome/browser/extensions/extension_panel_event_router.cc |
| diff --git a/chrome/browser/extensions/extension_panel_event_router.cc b/chrome/browser/extensions/extension_panel_event_router.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5bf48737f45ee749fcd9271d9febd0c6079b7f74 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_panel_event_router.cc |
| @@ -0,0 +1,104 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/extension_panel_event_router.h" |
| + |
| +#include "base/json/json_writer.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/extensions/extension_event_names.h" |
| +#include "chrome/browser/extensions/extension_event_router.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| +#include "chrome/browser/extensions/extension_window_controller.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/panels/panel.h" |
| +#include "chrome/common/extensions/extension_constants.h" |
| + |
| +namespace events = extension_event_names; |
|
stevenjb
2012/07/20 16:54:33
Ideally, extension_event_names should be extension
|
| + |
| +ExtensionPanelEventRouter::ExtensionPanelEventRouter(Profile* profile) |
| + : profile_(profile), |
| + focused_profile_(NULL), |
| + focused_window_id_(extension_misc::kUnknownWindowId) { |
| +} |
| + |
| +ExtensionPanelEventRouter::~ExtensionPanelEventRouter() { |
| +} |
| + |
| +void ExtensionPanelEventRouter::OnPanelOpened(Panel* panel) { |
| + if (!profile_->IsSameProfile(panel->profile())) |
| + return; |
| + |
| + base::ListValue args; |
| + DCHECK(panel->extension_window_controller()); |
| + DictionaryValue* window_dictionary = |
| + panel->extension_window_controller()->CreateWindowValue(); |
| + args.Append(window_dictionary); |
| + DispatchEvent(events::kOnWindowCreated, panel->profile(), &args); |
| +} |
| + |
| +void ExtensionPanelEventRouter::OnPanelActiveStatusChanged( |
| + Panel* panel, bool is_active_now) { |
| + Profile* window_profile = NULL; |
| + int window_id = extension_misc::kUnknownWindowId; |
| + if (is_active_now && profile_->IsSameProfile(panel->profile())) { |
| + window_profile = panel->profile(); |
| + window_id = panel->extension_window_controller()->GetWindowId(); |
| + } |
| + |
| + if (focused_window_id_ == window_id) |
| + return; |
| + |
| + // window_profile is either this panel's profile's default profile, its |
|
stevenjb
2012/07/20 16:54:33
nit: "the panel profile's" or "the default profile
|
| + // incognito profile, or NULL iff this profile is losing focus. |
| + Profile* previous_focused_profile = focused_profile_; |
| + focused_profile_ = window_profile; |
| + focused_window_id_ = window_id; |
|
stevenjb
2012/07/20 16:54:33
Couldn't window_id still be kUnknownWindowId at th
jennb
2012/07/20 18:09:37
Yes, that's valid and expected when the current fo
|
| + |
| + base::ListValue real_args; |
| + real_args.Append(Value::CreateIntegerValue(window_id)); |
| + std::string real_json_args; |
| + base::JSONWriter::Write(&real_args, &real_json_args); |
| + |
| + // When switching between windows in the default and incognito profiles, |
| + // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that |
| + // can't see the new focused window across the incognito boundary. |
| + // See crbug.com/46610. |
| + std::string none_json_args; |
| + if (focused_profile_ != NULL && previous_focused_profile != NULL && |
| + focused_profile_ != previous_focused_profile) { |
| + base::ListValue none_args; |
| + none_args.Append( |
| + Value::CreateIntegerValue(extension_misc::kUnknownWindowId)); |
| + base::JSONWriter::Write(&none_args, &none_json_args); |
| + } |
| + |
| + if (!window_profile) |
| + window_profile = previous_focused_profile; |
| + |
| + extensions::ExtensionSystem::Get(window_profile)->event_router()-> |
| + DispatchEventsToRenderersAcrossIncognito(events::kOnWindowFocusedChanged, |
| + real_json_args, |
| + window_profile, |
| + none_json_args, |
| + GURL()); |
| +} |
| + |
| +void ExtensionPanelEventRouter::OnPanelClosed(Panel* panel) { |
| + if (!profile_->IsSameProfile(panel->profile())) |
| + return; |
| + |
| + int window_id = panel->extension_window_controller()->GetWindowId(); |
| + base::ListValue args; |
| + args.Append(Value::CreateIntegerValue(window_id)); |
| + DispatchEvent(events::kOnWindowRemoved, panel->profile(), &args); |
| +} |
| + |
| +void ExtensionPanelEventRouter::DispatchEvent(const char* event_name, |
| + Profile* profile, |
| + base::ListValue* args) { |
| + std::string json_args; |
| + base::JSONWriter::Write(args, &json_args); |
| + extensions::ExtensionSystem::Get(profile)->event_router()-> |
| + DispatchEventToRenderers(event_name, json_args, profile, GURL()); |
| +} |