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 #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/values.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "extensions/browser/extension_event_histogram_value.h"
18 namespace extensions
{
20 // This class forwards events to EventRouters.
21 // The advantages of this class over direct usage of EventRouters are:
22 // - this class is thread-safe, you can call the functions from UI and IO
24 // - the class can handle if a profile is deleted between the time of sending
25 // the event from the IO thread to the UI thread.
26 // - this class can be used in contexts that are not governed by a profile, e.g.
27 // by system URLRequestContexts. In these cases the |restrict_to_profile|
28 // parameter remains NULL and events are broadcasted to all profiles.
29 class EventRouterForwarder
30 : public base::RefCountedThreadSafe
<EventRouterForwarder
> {
32 EventRouterForwarder();
35 // DispatchEventToRenderers(event_name, event_args, profile, event_url)
36 // on all (original) profiles' EventRouters.
37 // May be called on any thread.
38 void BroadcastEventToRenderers(events::HistogramValue histogram_value
,
39 const std::string
& event_name
,
40 scoped_ptr
<base::ListValue
> event_args
,
41 const GURL
& event_url
);
44 // DispatchEventToExtension(extension_id, event_name, event_args,
45 // profile, event_url)
46 // on all (original) profiles' EventRouters.
47 // May be called on any thread.
48 void BroadcastEventToExtension(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
);
55 // DispatchEventToRenderers(event_name, event_args,
56 // use_profile_to_restrict_events ? profile : NULL, event_url)
57 // on |profile|'s EventRouter. May be called on any thread.
58 void DispatchEventToRenderers(events::HistogramValue histogram_value
,
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 // DispatchEventToExtension(extension_id, event_name, event_args,
67 // use_profile_to_restrict_events ? profile : NULL, event_url)
68 // on |profile|'s EventRouter. May be called on any thread.
69 void DispatchEventToExtension(const std::string
& extension_id
,
70 events::HistogramValue histogram_value
,
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
);
78 // Protected for testing.
79 virtual ~EventRouterForwarder();
81 // Helper function for {Broadcast,Dispatch}EventTo{Extension,Renderers}.
82 // Virtual for testing.
83 virtual void HandleEvent(const std::string
& extension_id
,
84 events::HistogramValue histogram_value
,
85 const std::string
& event_name
,
86 scoped_ptr
<base::ListValue
> event_args
,
88 bool use_profile_to_restrict_events
,
89 const GURL
& event_url
);
91 // Calls DispatchEventToRenderers or DispatchEventToExtension (depending on
92 // whether extension_id == "" or not) of |profile|'s EventRouter.
93 // |profile| may never be NULL.
94 // Virtual for testing.
95 virtual void CallEventRouter(Profile
* profile
,
96 const std::string
& extension_id
,
97 events::HistogramValue histogram_value
,
98 const std::string
& event_name
,
99 scoped_ptr
<base::ListValue
> event_args
,
100 Profile
* restrict_to_profile
,
101 const GURL
& event_url
);
104 friend class base::RefCountedThreadSafe
<EventRouterForwarder
>;
106 DISALLOW_COPY_AND_ASSIGN(EventRouterForwarder
);
109 } // namespace extensions
111 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_