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.
5 #include "chrome/browser/extensions/event_router_forwarder.h"
8 #include "base/values.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "extensions/browser/event_router.h"
15 using content::BrowserThread
;
17 namespace extensions
{
19 EventRouterForwarder::EventRouterForwarder() {
22 EventRouterForwarder::~EventRouterForwarder() {
25 void EventRouterForwarder::BroadcastEventToRenderers(
26 const std::string
& event_name
,
27 scoped_ptr
<base::ListValue
> event_args
,
28 const GURL
& event_url
) {
29 HandleEvent(std::string(), event_name
, event_args
.Pass(), 0, true, event_url
);
32 void EventRouterForwarder::DispatchEventToRenderers(
33 const std::string
& event_name
,
34 scoped_ptr
<base::ListValue
> event_args
,
36 bool use_profile_to_restrict_events
,
37 const GURL
& event_url
) {
40 HandleEvent(std::string(),
44 use_profile_to_restrict_events
,
48 void EventRouterForwarder::BroadcastEventToExtension(
49 const std::string
& extension_id
,
50 const std::string
& event_name
,
51 scoped_ptr
<base::ListValue
> event_args
,
52 const GURL
& event_url
) {
53 HandleEvent(extension_id
, event_name
, event_args
.Pass(), 0, true, event_url
);
56 void EventRouterForwarder::DispatchEventToExtension(
57 const std::string
& extension_id
,
58 const std::string
& event_name
,
59 scoped_ptr
<base::ListValue
> event_args
,
61 bool use_profile_to_restrict_events
,
62 const GURL
& event_url
) {
65 HandleEvent(extension_id
, event_name
, event_args
.Pass(), profile
,
66 use_profile_to_restrict_events
, event_url
);
69 void EventRouterForwarder::HandleEvent(const std::string
& extension_id
,
70 const std::string
& event_name
,
71 scoped_ptr
<base::ListValue
> event_args
,
73 bool use_profile_to_restrict_events
,
74 const GURL
& event_url
) {
75 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
76 BrowserThread::PostTask(
77 BrowserThread::UI
, FROM_HERE
,
78 base::Bind(&EventRouterForwarder::HandleEvent
, this,
79 extension_id
, event_name
, base::Passed(&event_args
),
80 profile_ptr
, use_profile_to_restrict_events
, event_url
));
84 if (!g_browser_process
|| !g_browser_process
->profile_manager())
87 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
88 Profile
* profile
= NULL
;
90 profile
= reinterpret_cast<Profile
*>(profile_ptr
);
91 if (!profile_manager
->IsValidProfile(profile
))
95 CallEventRouter(profile
, extension_id
, event_name
, event_args
.Pass(),
96 use_profile_to_restrict_events
? profile
: NULL
, event_url
);
98 std::vector
<Profile
*> profiles(profile_manager
->GetLoadedProfiles());
99 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
100 scoped_ptr
<base::ListValue
> per_profile_event_args(
101 event_args
->DeepCopy());
103 profiles
[i
], extension_id
, event_name
, per_profile_event_args
.Pass(),
104 use_profile_to_restrict_events
? profiles
[i
] : NULL
, event_url
);
109 void EventRouterForwarder::CallEventRouter(
111 const std::string
& extension_id
,
112 const std::string
& event_name
,
113 scoped_ptr
<base::ListValue
> event_args
,
114 Profile
* restrict_to_profile
,
115 const GURL
& event_url
) {
116 #if defined(OS_CHROMEOS)
117 // Extension does not exist for chromeos login. This needs to be
118 // removed once we have an extension service for login screen.
119 // crosbug.com/12856.
120 if (!extensions::EventRouter::Get(profile
))
124 scoped_ptr
<Event
> event(new Event(event_name
, event_args
.Pass()));
125 event
->restrict_to_browser_context
= restrict_to_profile
;
126 event
->event_url
= event_url
;
127 if (extension_id
.empty()) {
128 extensions::EventRouter::Get(profile
)->BroadcastEvent(event
.Pass());
130 extensions::EventRouter::Get(profile
)
131 ->DispatchEventToExtension(extension_id
, event
.Pass());
135 } // namespace extensions