Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: chrome/browser/extensions/extension_panel_event_router.cc

Issue 10777004: Support chrome.windows extension API events for browserless Panels. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/command_line.h"
8 #include "base/json/json_writer.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_event_names.h"
11 #include "chrome/browser/extensions/extension_event_router.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/chrome_notification_types.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/extension.h"
18 #include "chrome/common/extensions/extension_constants.h"
19 #include "content/public/browser/notification_service.h"
20
21 ExtensionPanelEventRouter::ExtensionPanelEventRouter(Profile* profile)
22 : initialized_(false),
23 profile_(profile),
24 focused_profile_(NULL),
25 focused_window_id_(extension_misc::kUnknownWindowId) {
26 }
27
28 ExtensionPanelEventRouter::~ExtensionPanelEventRouter() {
29 }
30
31 void ExtensionPanelEventRouter::Init() {
32 if (initialized_ || !CommandLine::ForCurrentProcess()->HasSwitch(
33 switches::kBrowserlessPanels))
34 return;
35
36 registrar_.Add(this, chrome::NOTIFICATION_PANEL_OPENED,
37 content::NotificationService::AllSources());
38 registrar_.Add(this, chrome::NOTIFICATION_PANEL_CHANGED_ACTIVE_STATUS,
39 content::NotificationService::AllSources());
40 registrar_.Add(this, chrome::NOTIFICATION_PANEL_CLOSED,
41 content::NotificationService::AllSources());
42 initialized_ = true;
43 }
44
45 void ExtensionPanelEventRouter::Observe(
46 int type,
47 const content::NotificationSource& source,
48 const content::NotificationDetails& details) {
49 Panel* panel = content::Source<Panel>(source).ptr();
50 switch (type) {
51 case chrome::NOTIFICATION_PANEL_OPENED:
52 OnPanelOpened(panel);
53 break;
54 case chrome::NOTIFICATION_PANEL_CHANGED_ACTIVE_STATUS: {
55 bool* is_active_now = content::Details<bool>(details).ptr();
56 OnPanelActiveStatusChanged(panel, *is_active_now);
57 break;
58 }
59 case chrome::NOTIFICATION_PANEL_CLOSED:
60 OnPanelClosed(panel);
61 break;
62 default:
63 NOTREACHED();
64 break;
65 }
66 }
67
68 void ExtensionPanelEventRouter::OnPanelOpened(Panel* panel) {
69 if (!profile_->GetExtensionEventRouter())
70 return;
71
72 ListValue args;
73 DCHECK(panel->extension_window_controller());
74 DictionaryValue* window_dictionary =
75 panel->extension_window_controller()->CreateWindowValue();
76 args.Append(window_dictionary);
77
78 std::string json_args;
79 base::JSONWriter::Write(&args, &json_args);
80 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
81 extension_event_names::kOnWindowCreated, json_args, profile_, GURL());
jianli 2012/07/13 17:46:41 Can you share the above code with the similar one
jennb 2012/07/13 22:12:14 Done.
82 }
83
84 void ExtensionPanelEventRouter::OnPanelActiveStatusChanged(
85 Panel* panel, bool is_active_now) {
86 Profile* window_profile = NULL;
87 int window_id = extension_misc::kUnknownWindowId;
88 if (is_active_now) {
89 window_profile = panel->profile();
90 window_id = panel->extension_window_controller()->GetWindowId();
91 }
92
93 if (focused_window_id_ == window_id)
94 return;
95
96 // window_profile is either this profile's default profile, its
97 // incognito profile, or NULL iff this profile is losing focus.
98 Profile* previous_focused_profile = focused_profile_;
99 focused_profile_ = window_profile;
100 focused_window_id_ = window_id;
101
102 ListValue real_args;
103 real_args.Append(Value::CreateIntegerValue(window_id));
104 std::string real_json_args;
105 base::JSONWriter::Write(&real_args, &real_json_args);
106
107 // When switching between windows in the default and incognito profiles,
108 // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that
109 // can't see the new focused window across the incognito boundary.
110 // See crbug.com/46610.
111 std::string none_json_args;
112 if (focused_profile_ != NULL && previous_focused_profile != NULL &&
113 focused_profile_ != previous_focused_profile) {
114 ListValue none_args;
115 none_args.Append(
116 Value::CreateIntegerValue(extension_misc::kUnknownWindowId));
117 base::JSONWriter::Write(&none_args, &none_json_args);
118 }
119
120 if (!window_profile)
121 window_profile = previous_focused_profile;
122 if (!window_profile->GetExtensionEventRouter())
123 return;
124 window_profile->GetExtensionEventRouter()->
125 DispatchEventsToRenderersAcrossIncognito(
126 extension_event_names::kOnWindowFocusedChanged, real_json_args,
127 window_profile, none_json_args, GURL());
128 }
129
130 void ExtensionPanelEventRouter::OnPanelClosed(Panel* panel) {
131 if (!profile_->GetExtensionEventRouter())
132 return;
133
134 int window_id = panel->extension_window_controller()->GetWindowId();
135 ListValue args;
136 args.Append(Value::CreateIntegerValue(window_id));
137
138 std::string json_args;
139 base::JSONWriter::Write(&args, &json_args);
140 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
141 extension_event_names::kOnWindowRemoved, json_args, profile_, GURL());
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698