Chromium Code Reviews| 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/extension_panel_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/extension_window_controller.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/ui/panels/panel.h" | |
| 15 #include "chrome/common/extensions/extension_constants.h" | |
| 16 | |
| 17 namespace events = extension_event_names; | |
|
stevenjb
2012/07/20 16:54:33
Ideally, extension_event_names should be extension
| |
| 18 | |
| 19 ExtensionPanelEventRouter::ExtensionPanelEventRouter(Profile* profile) | |
| 20 : profile_(profile), | |
| 21 focused_profile_(NULL), | |
| 22 focused_window_id_(extension_misc::kUnknownWindowId) { | |
| 23 } | |
| 24 | |
| 25 ExtensionPanelEventRouter::~ExtensionPanelEventRouter() { | |
| 26 } | |
| 27 | |
| 28 void ExtensionPanelEventRouter::OnPanelOpened(Panel* panel) { | |
| 29 if (!profile_->IsSameProfile(panel->profile())) | |
| 30 return; | |
| 31 | |
| 32 base::ListValue args; | |
| 33 DCHECK(panel->extension_window_controller()); | |
| 34 DictionaryValue* window_dictionary = | |
| 35 panel->extension_window_controller()->CreateWindowValue(); | |
| 36 args.Append(window_dictionary); | |
| 37 DispatchEvent(events::kOnWindowCreated, panel->profile(), &args); | |
| 38 } | |
| 39 | |
| 40 void ExtensionPanelEventRouter::OnPanelActiveStatusChanged( | |
| 41 Panel* panel, bool is_active_now) { | |
| 42 Profile* window_profile = NULL; | |
| 43 int window_id = extension_misc::kUnknownWindowId; | |
| 44 if (is_active_now && profile_->IsSameProfile(panel->profile())) { | |
| 45 window_profile = panel->profile(); | |
| 46 window_id = panel->extension_window_controller()->GetWindowId(); | |
| 47 } | |
| 48 | |
| 49 if (focused_window_id_ == window_id) | |
| 50 return; | |
| 51 | |
| 52 // 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
| |
| 53 // incognito profile, or NULL iff this profile is losing focus. | |
| 54 Profile* previous_focused_profile = focused_profile_; | |
| 55 focused_profile_ = window_profile; | |
| 56 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
| |
| 57 | |
| 58 base::ListValue real_args; | |
| 59 real_args.Append(Value::CreateIntegerValue(window_id)); | |
| 60 std::string real_json_args; | |
| 61 base::JSONWriter::Write(&real_args, &real_json_args); | |
| 62 | |
| 63 // When switching between windows in the default and incognito profiles, | |
| 64 // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that | |
| 65 // can't see the new focused window across the incognito boundary. | |
| 66 // See crbug.com/46610. | |
| 67 std::string none_json_args; | |
| 68 if (focused_profile_ != NULL && previous_focused_profile != NULL && | |
| 69 focused_profile_ != previous_focused_profile) { | |
| 70 base::ListValue none_args; | |
| 71 none_args.Append( | |
| 72 Value::CreateIntegerValue(extension_misc::kUnknownWindowId)); | |
| 73 base::JSONWriter::Write(&none_args, &none_json_args); | |
| 74 } | |
| 75 | |
| 76 if (!window_profile) | |
| 77 window_profile = previous_focused_profile; | |
| 78 | |
| 79 extensions::ExtensionSystem::Get(window_profile)->event_router()-> | |
| 80 DispatchEventsToRenderersAcrossIncognito(events::kOnWindowFocusedChanged, | |
| 81 real_json_args, | |
| 82 window_profile, | |
| 83 none_json_args, | |
| 84 GURL()); | |
| 85 } | |
| 86 | |
| 87 void ExtensionPanelEventRouter::OnPanelClosed(Panel* panel) { | |
| 88 if (!profile_->IsSameProfile(panel->profile())) | |
| 89 return; | |
| 90 | |
| 91 int window_id = panel->extension_window_controller()->GetWindowId(); | |
| 92 base::ListValue args; | |
| 93 args.Append(Value::CreateIntegerValue(window_id)); | |
| 94 DispatchEvent(events::kOnWindowRemoved, panel->profile(), &args); | |
| 95 } | |
| 96 | |
| 97 void ExtensionPanelEventRouter::DispatchEvent(const char* event_name, | |
| 98 Profile* profile, | |
| 99 base::ListValue* args) { | |
| 100 std::string json_args; | |
| 101 base::JSONWriter::Write(args, &json_args); | |
| 102 extensions::ExtensionSystem::Get(profile)->event_router()-> | |
| 103 DispatchEventToRenderers(event_name, json_args, profile, GURL()); | |
| 104 } | |
| OLD | NEW |