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 events::HistogramValue histogram_value
,
27 const std::string
& event_name
,
28 scoped_ptr
<base::ListValue
> event_args
,
29 const GURL
& event_url
) {
30 HandleEvent(std::string(), histogram_value
, event_name
, event_args
.Pass(), 0,
34 void EventRouterForwarder::DispatchEventToRenderers(
35 events::HistogramValue histogram_value
,
36 const std::string
& event_name
,
37 scoped_ptr
<base::ListValue
> event_args
,
39 bool use_profile_to_restrict_events
,
40 const GURL
& event_url
) {
43 HandleEvent(std::string(), histogram_value
, event_name
, event_args
.Pass(),
44 profile
, use_profile_to_restrict_events
, event_url
);
47 void EventRouterForwarder::BroadcastEventToExtension(
48 const std::string
& extension_id
,
49 events::HistogramValue histogram_value
,
50 const std::string
& event_name
,
51 scoped_ptr
<base::ListValue
> event_args
,
52 const GURL
& event_url
) {
53 HandleEvent(extension_id
, histogram_value
, event_name
, event_args
.Pass(), 0,
57 void EventRouterForwarder::DispatchEventToExtension(
58 const std::string
& extension_id
,
59 events::HistogramValue histogram_value
,
60 const std::string
& event_name
,
61 scoped_ptr
<base::ListValue
> event_args
,
63 bool use_profile_to_restrict_events
,
64 const GURL
& event_url
) {
67 HandleEvent(extension_id
, histogram_value
, event_name
, event_args
.Pass(),
68 profile
, use_profile_to_restrict_events
, event_url
);
71 void EventRouterForwarder::HandleEvent(const std::string
& extension_id
,
72 events::HistogramValue histogram_value
,
73 const std::string
& event_name
,
74 scoped_ptr
<base::ListValue
> event_args
,
76 bool use_profile_to_restrict_events
,
77 const GURL
& event_url
) {
78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
79 BrowserThread::PostTask(
80 BrowserThread::UI
, FROM_HERE
,
81 base::Bind(&EventRouterForwarder::HandleEvent
, this, extension_id
,
82 histogram_value
, event_name
, base::Passed(&event_args
),
83 profile_ptr
, use_profile_to_restrict_events
, event_url
));
87 if (!g_browser_process
|| !g_browser_process
->profile_manager())
90 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
91 Profile
* profile
= NULL
;
93 profile
= reinterpret_cast<Profile
*>(profile_ptr
);
94 if (!profile_manager
->IsValidProfile(profile
))
98 CallEventRouter(profile
, extension_id
, histogram_value
, event_name
,
100 use_profile_to_restrict_events
? profile
: NULL
, event_url
);
102 std::vector
<Profile
*> profiles(profile_manager
->GetLoadedProfiles());
103 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
104 scoped_ptr
<base::ListValue
> per_profile_event_args(
105 event_args
->DeepCopy());
106 CallEventRouter(profiles
[i
], extension_id
, histogram_value
, event_name
,
107 per_profile_event_args
.Pass(),
108 use_profile_to_restrict_events
? profiles
[i
] : NULL
,
114 void EventRouterForwarder::CallEventRouter(
116 const std::string
& extension_id
,
117 events::HistogramValue histogram_value
,
118 const std::string
& event_name
,
119 scoped_ptr
<base::ListValue
> event_args
,
120 Profile
* restrict_to_profile
,
121 const GURL
& event_url
) {
122 #if defined(OS_CHROMEOS)
123 // Extension does not exist for chromeos login. This needs to be
124 // removed once we have an extension service for login screen.
125 // crosbug.com/12856.
126 if (!extensions::EventRouter::Get(profile
))
130 scoped_ptr
<Event
> event(
131 new Event(histogram_value
, event_name
, event_args
.Pass()));
132 event
->restrict_to_browser_context
= restrict_to_profile
;
133 event
->event_url
= event_url
;
134 if (extension_id
.empty()) {
135 extensions::EventRouter::Get(profile
)->BroadcastEvent(event
.Pass());
137 extensions::EventRouter::Get(profile
)
138 ->DispatchEventToExtension(extension_id
, event
.Pass());
142 } // namespace extensions