OLD | NEW |
| (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_event_router_forwarder.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "chrome/browser/browser_process.h" | |
9 #include "chrome/browser/extensions/extension_event_router.h" | |
10 #include "chrome/browser/profiles/profile_manager.h" | |
11 #include "googleurl/src/gurl.h" | |
12 | |
13 using content::BrowserThread; | |
14 | |
15 ExtensionEventRouterForwarder::ExtensionEventRouterForwarder() { | |
16 } | |
17 | |
18 ExtensionEventRouterForwarder::~ExtensionEventRouterForwarder() { | |
19 } | |
20 | |
21 void ExtensionEventRouterForwarder::BroadcastEventToRenderers( | |
22 const std::string& event_name, | |
23 const std::string& event_args, | |
24 const GURL& event_url) { | |
25 HandleEvent("", event_name, event_args, 0, true, event_url); | |
26 } | |
27 | |
28 void ExtensionEventRouterForwarder::DispatchEventToRenderers( | |
29 const std::string& event_name, | |
30 const std::string& event_args, | |
31 void* profile, | |
32 bool use_profile_to_restrict_events, | |
33 const GURL& event_url) { | |
34 if (!profile) | |
35 return; | |
36 HandleEvent("", event_name, event_args, profile, | |
37 use_profile_to_restrict_events, event_url); | |
38 } | |
39 | |
40 void ExtensionEventRouterForwarder::BroadcastEventToExtension( | |
41 const std::string& extension_id, | |
42 const std::string& event_name, | |
43 const std::string& event_args, | |
44 const GURL& event_url) { | |
45 HandleEvent(extension_id, event_name, event_args, 0, true, event_url); | |
46 } | |
47 | |
48 void ExtensionEventRouterForwarder::DispatchEventToExtension( | |
49 const std::string& extension_id, | |
50 const std::string& event_name, | |
51 const std::string& event_args, | |
52 void* profile, | |
53 bool use_profile_to_restrict_events, | |
54 const GURL& event_url) { | |
55 if (!profile) | |
56 return; | |
57 HandleEvent(extension_id, event_name, event_args, profile, | |
58 use_profile_to_restrict_events, event_url); | |
59 } | |
60 | |
61 void ExtensionEventRouterForwarder::HandleEvent( | |
62 const std::string& extension_id, | |
63 const std::string& event_name, | |
64 const std::string& event_args, | |
65 void* profile_ptr, | |
66 bool use_profile_to_restrict_events, | |
67 const GURL& event_url) { | |
68 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
69 BrowserThread::PostTask( | |
70 BrowserThread::UI, FROM_HERE, | |
71 base::Bind(&ExtensionEventRouterForwarder::HandleEvent, this, | |
72 extension_id, event_name, event_args, profile_ptr, | |
73 use_profile_to_restrict_events, event_url)); | |
74 return; | |
75 } | |
76 | |
77 if (!g_browser_process || !g_browser_process->profile_manager()) | |
78 return; | |
79 | |
80 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
81 Profile* profile = NULL; | |
82 if (profile_ptr) { | |
83 profile = reinterpret_cast<Profile*>(profile_ptr); | |
84 if (!profile_manager->IsValidProfile(profile)) | |
85 return; | |
86 } | |
87 if (profile) { | |
88 CallExtensionEventRouter( | |
89 profile, extension_id, event_name, event_args, | |
90 use_profile_to_restrict_events ? profile : NULL, event_url); | |
91 } else { | |
92 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); | |
93 for (size_t i = 0; i < profiles.size(); ++i) { | |
94 CallExtensionEventRouter( | |
95 profiles[i], extension_id, event_name, event_args, | |
96 use_profile_to_restrict_events ? profiles[i] : NULL, event_url); | |
97 } | |
98 } | |
99 } | |
100 | |
101 void ExtensionEventRouterForwarder::CallExtensionEventRouter( | |
102 Profile* profile, | |
103 const std::string& extension_id, | |
104 const std::string& event_name, | |
105 const std::string& event_args, | |
106 Profile* restrict_to_profile, | |
107 const GURL& event_url) { | |
108 // We may not have an extension in cases like chromeos login | |
109 // (crosbug.com/12856), chrome_frame_net_tests.exe which reuses the chrome | |
110 // browser single process framework. | |
111 if (!profile->GetExtensionEventRouter()) | |
112 return; | |
113 | |
114 if (extension_id.empty()) { | |
115 profile->GetExtensionEventRouter()-> | |
116 DispatchEventToRenderers( | |
117 event_name, event_args, restrict_to_profile, event_url, | |
118 extensions::EventFilteringInfo()); | |
119 } else { | |
120 profile->GetExtensionEventRouter()-> | |
121 DispatchEventToExtension( | |
122 extension_id, | |
123 event_name, event_args, restrict_to_profile, event_url); | |
124 } | |
125 } | |
OLD | NEW |