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"
13 #include "extensions/browser/extension_system.h"
16 using content::BrowserThread
;
18 namespace extensions
{
20 EventRouterForwarder::EventRouterForwarder() {
23 EventRouterForwarder::~EventRouterForwarder() {
26 void EventRouterForwarder::BroadcastEventToRenderers(
27 const std::string
& event_name
,
28 scoped_ptr
<base::ListValue
> event_args
,
29 const GURL
& event_url
) {
30 HandleEvent(std::string(), event_name
, event_args
.Pass(), 0, true, event_url
);
33 void EventRouterForwarder::DispatchEventToRenderers(
34 const std::string
& event_name
,
35 scoped_ptr
<base::ListValue
> event_args
,
37 bool use_profile_to_restrict_events
,
38 const GURL
& event_url
) {
41 HandleEvent(std::string(),
45 use_profile_to_restrict_events
,
49 void EventRouterForwarder::BroadcastEventToExtension(
50 const std::string
& extension_id
,
51 const std::string
& event_name
,
52 scoped_ptr
<base::ListValue
> event_args
,
53 const GURL
& event_url
) {
54 HandleEvent(extension_id
, event_name
, event_args
.Pass(), 0, true, event_url
);
57 void EventRouterForwarder::DispatchEventToExtension(
58 const std::string
& extension_id
,
59 const std::string
& event_name
,
60 scoped_ptr
<base::ListValue
> event_args
,
62 bool use_profile_to_restrict_events
,
63 const GURL
& event_url
) {
66 HandleEvent(extension_id
, event_name
, event_args
.Pass(), profile
,
67 use_profile_to_restrict_events
, event_url
);
70 void EventRouterForwarder::HandleEvent(const std::string
& extension_id
,
71 const std::string
& event_name
,
72 scoped_ptr
<base::ListValue
> event_args
,
74 bool use_profile_to_restrict_events
,
75 const GURL
& event_url
) {
76 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
77 BrowserThread::PostTask(
78 BrowserThread::UI
, FROM_HERE
,
79 base::Bind(&EventRouterForwarder::HandleEvent
, this,
80 extension_id
, event_name
, base::Passed(&event_args
),
81 profile_ptr
, use_profile_to_restrict_events
, event_url
));
85 if (!g_browser_process
|| !g_browser_process
->profile_manager())
88 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
89 Profile
* profile
= NULL
;
91 profile
= reinterpret_cast<Profile
*>(profile_ptr
);
92 if (!profile_manager
->IsValidProfile(profile
))
96 CallEventRouter(profile
, extension_id
, event_name
, event_args
.Pass(),
97 use_profile_to_restrict_events
? profile
: NULL
, event_url
);
99 std::vector
<Profile
*> profiles(profile_manager
->GetLoadedProfiles());
100 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
101 scoped_ptr
<base::ListValue
> per_profile_event_args(
102 event_args
->DeepCopy());
104 profiles
[i
], extension_id
, event_name
, per_profile_event_args
.Pass(),
105 use_profile_to_restrict_events
? profiles
[i
] : NULL
, event_url
);
110 void EventRouterForwarder::CallEventRouter(
112 const std::string
& extension_id
,
113 const std::string
& event_name
,
114 scoped_ptr
<base::ListValue
> event_args
,
115 Profile
* restrict_to_profile
,
116 const GURL
& event_url
) {
117 #if defined(OS_CHROMEOS)
118 // Extension does not exist for chromeos login. This needs to be
119 // removed once we have an extension service for login screen.
120 // crosbug.com/12856.
121 if (!extensions::ExtensionSystem::Get(profile
)->event_router())
125 scoped_ptr
<Event
> event(new Event(event_name
, event_args
.Pass()));
126 event
->restrict_to_browser_context
= restrict_to_profile
;
127 event
->event_url
= event_url
;
128 if (extension_id
.empty()) {
129 ExtensionSystem::Get(profile
)->event_router()->BroadcastEvent(event
.Pass());
131 ExtensionSystem::Get(profile
)->event_router()->
132 DispatchEventToExtension(extension_id
, event
.Pass());
136 } // namespace extensions