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"
17 namespace extensions
{
19 // This class forwards events to EventRouters.
20 // The advantages of this class over direct usage of EventRouters are:
21 // - this class is thread-safe, you can call the functions from UI and IO
23 // - the class can handle if a profile is deleted between the time of sending
24 // the event from the IO thread to the UI thread.
25 // - this class can be used in contexts that are not governed by a profile, e.g.
26 // by system URLRequestContexts. In these cases the |restrict_to_profile|
27 // parameter remains NULL and events are broadcasted to all profiles.
28 class EventRouterForwarder
29 : public base::RefCountedThreadSafe
<EventRouterForwarder
> {
31 EventRouterForwarder();
34 // DispatchEventToRenderers(event_name, event_args, profile, event_url)
35 // on all (original) profiles' EventRouters.
36 // May be called on any thread.
37 void BroadcastEventToRenderers(const std::string
& event_name
,
38 scoped_ptr
<base::ListValue
> event_args
,
39 const GURL
& event_url
);
42 // DispatchEventToExtension(extension_id, event_name, event_args,
43 // profile, event_url)
44 // on all (original) profiles' EventRouters.
45 // May be called on any thread.
46 void BroadcastEventToExtension(const std::string
& extension_id
,
47 const std::string
& event_name
,
48 scoped_ptr
<base::ListValue
> event_args
,
49 const GURL
& event_url
);
52 // DispatchEventToRenderers(event_name, event_args,
53 // use_profile_to_restrict_events ? profile : NULL, event_url)
54 // on |profile|'s EventRouter. May be called on any thread.
55 void DispatchEventToRenderers(const std::string
& event_name
,
56 scoped_ptr
<base::ListValue
> event_args
,
58 bool use_profile_to_restrict_events
,
59 const GURL
& event_url
);
62 // DispatchEventToExtension(extension_id, event_name, event_args,
63 // use_profile_to_restrict_events ? profile : NULL, event_url)
64 // on |profile|'s EventRouter. May be called on any thread.
65 void DispatchEventToExtension(const std::string
& extension_id
,
66 const std::string
& event_name
,
67 scoped_ptr
<base::ListValue
> event_args
,
69 bool use_profile_to_restrict_events
,
70 const GURL
& event_url
);
73 // Protected for testing.
74 virtual ~EventRouterForwarder();
76 // Helper function for {Broadcast,Dispatch}EventTo{Extension,Renderers}.
77 // Virtual for testing.
78 virtual void HandleEvent(const std::string
& extension_id
,
79 const std::string
& event_name
,
80 scoped_ptr
<base::ListValue
> event_args
,
82 bool use_profile_to_restrict_events
,
83 const GURL
& event_url
);
85 // Calls DispatchEventToRenderers or DispatchEventToExtension (depending on
86 // whether extension_id == "" or not) of |profile|'s EventRouter.
87 // |profile| may never be NULL.
88 // Virtual for testing.
89 virtual void CallEventRouter(Profile
* profile
,
90 const std::string
& extension_id
,
91 const std::string
& event_name
,
92 scoped_ptr
<base::ListValue
> event_args
,
93 Profile
* restrict_to_profile
,
94 const GURL
& event_url
);
97 friend class base::RefCountedThreadSafe
<EventRouterForwarder
>;
99 DISALLOW_COPY_AND_ASSIGN(EventRouterForwarder
);
102 } // namespace extensions
104 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_