Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / extensions / event_router_forwarder.cc
blobff198a1eab0b01a37fcb5b39ebee48e62c035aa2
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"
7 #include "base/bind.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 "url/gurl.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,
31 true, event_url);
34 void EventRouterForwarder::DispatchEventToRenderers(
35 events::HistogramValue histogram_value,
36 const std::string& event_name,
37 scoped_ptr<base::ListValue> event_args,
38 void* profile,
39 bool use_profile_to_restrict_events,
40 const GURL& event_url) {
41 if (!profile)
42 return;
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,
54 true, event_url);
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,
62 void* profile,
63 bool use_profile_to_restrict_events,
64 const GURL& event_url) {
65 if (!profile)
66 return;
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,
75 void* profile_ptr,
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));
84 return;
87 if (!g_browser_process || !g_browser_process->profile_manager())
88 return;
90 ProfileManager* profile_manager = g_browser_process->profile_manager();
91 Profile* profile = NULL;
92 if (profile_ptr) {
93 profile = reinterpret_cast<Profile*>(profile_ptr);
94 if (!profile_manager->IsValidProfile(profile))
95 return;
97 if (profile) {
98 CallEventRouter(profile, extension_id, histogram_value, event_name,
99 event_args.Pass(),
100 use_profile_to_restrict_events ? profile : NULL, event_url);
101 } else {
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,
109 event_url);
114 void EventRouterForwarder::CallEventRouter(
115 Profile* profile,
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))
127 return;
128 #endif
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());
136 } else {
137 extensions::EventRouter::Get(profile)
138 ->DispatchEventToExtension(extension_id, event.Pass());
142 } // namespace extensions