OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/values.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 | 15 |
15 class GURL; | 16 class GURL; |
16 | 17 |
17 namespace extensions { | 18 namespace extensions { |
18 | 19 |
19 // This class forwards events to EventRouters. | 20 // This class forwards events to EventRouters. |
20 // The advantages of this class over direct usage of EventRouters are: | 21 // 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 | 22 // - this class is thread-safe, you can call the functions from UI and IO |
22 // thread. | 23 // thread. |
23 // - the class can handle if a profile is deleted between the time of sending | 24 // - 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 // 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 // - 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 // by system URLRequestContexts. In these cases the |restrict_to_profile| |
27 // parameter remains NULL and events are broadcasted to all profiles. | 28 // parameter remains NULL and events are broadcasted to all profiles. |
28 class EventRouterForwarder | 29 class EventRouterForwarder |
29 : public base::RefCountedThreadSafe<EventRouterForwarder> { | 30 : public base::RefCountedThreadSafe<EventRouterForwarder> { |
30 public: | 31 public: |
31 EventRouterForwarder(); | 32 EventRouterForwarder(); |
32 | 33 |
33 // Calls | 34 // Calls |
34 // DispatchEventToRenderers(event_name, event_args, profile, event_url) | 35 // DispatchEventToRenderers(event_name, event_args, profile, event_url) |
35 // on all (original) profiles' EventRouters. | 36 // on all (original) profiles' EventRouters. |
36 // May be called on any thread. | 37 // May be called on any thread. |
37 void BroadcastEventToRenderers(const std::string& event_name, | 38 void BroadcastEventToRenderers(const std::string& event_name, |
38 const std::string& event_args, | 39 scoped_ptr<base::ListValue> event_args, |
39 const GURL& event_url); | 40 const GURL& event_url); |
40 | 41 |
41 // Calls | 42 // Calls |
42 // DispatchEventToExtension(extension_id, event_name, event_args, | 43 // DispatchEventToExtension(extension_id, event_name, event_args, |
43 // profile, event_url) | 44 // profile, event_url) |
44 // on all (original) profiles' EventRouters. | 45 // on all (original) profiles' EventRouters. |
45 // May be called on any thread. | 46 // May be called on any thread. |
46 void BroadcastEventToExtension(const std::string& extension_id, | 47 void BroadcastEventToExtension(const std::string& extension_id, |
47 const std::string& event_name, | 48 const std::string& event_name, |
48 const std::string& event_args, | 49 scoped_ptr<base::ListValue> event_args, |
49 const GURL& event_url); | 50 const GURL& event_url); |
50 | 51 |
51 // Calls | 52 // Calls |
52 // DispatchEventToRenderers(event_name, event_args, | 53 // DispatchEventToRenderers(event_name, event_args, |
53 // use_profile_to_restrict_events ? profile : NULL, event_url) | 54 // use_profile_to_restrict_events ? profile : NULL, event_url) |
54 // on |profile|'s EventRouter. May be called on any thread. | 55 // on |profile|'s EventRouter. May be called on any thread. |
55 void DispatchEventToRenderers(const std::string& event_name, | 56 void DispatchEventToRenderers(const std::string& event_name, |
56 const std::string& event_args, | 57 scoped_ptr<base::ListValue> event_args, |
57 void* profile, | 58 void* profile, |
58 bool use_profile_to_restrict_events, | 59 bool use_profile_to_restrict_events, |
59 const GURL& event_url); | 60 const GURL& event_url); |
60 | 61 |
61 // Calls | 62 // Calls |
62 // DispatchEventToExtension(extension_id, event_name, event_args, | 63 // DispatchEventToExtension(extension_id, event_name, event_args, |
63 // use_profile_to_restrict_events ? profile : NULL, event_url) | 64 // use_profile_to_restrict_events ? profile : NULL, event_url) |
64 // on |profile|'s EventRouter. May be called on any thread. | 65 // on |profile|'s EventRouter. May be called on any thread. |
65 void DispatchEventToExtension(const std::string& extension_id, | 66 void DispatchEventToExtension(const std::string& extension_id, |
66 const std::string& event_name, | 67 const std::string& event_name, |
67 const std::string& event_args, | 68 scoped_ptr<base::ListValue> event_args, |
68 void* profile, | 69 void* profile, |
69 bool use_profile_to_restrict_events, | 70 bool use_profile_to_restrict_events, |
70 const GURL& event_url); | 71 const GURL& event_url); |
71 | 72 |
72 protected: | 73 protected: |
73 // Protected for testing. | 74 // Protected for testing. |
74 virtual ~EventRouterForwarder(); | 75 virtual ~EventRouterForwarder(); |
75 | 76 |
76 // Helper function for {Broadcast,Dispatch}EventTo{Extension,Renderers}. | 77 // Helper function for {Broadcast,Dispatch}EventTo{Extension,Renderers}. |
77 // Virtual for testing. | 78 // Virtual for testing. |
78 virtual void HandleEvent(const std::string& extension_id, | 79 virtual void HandleEvent(const std::string& extension_id, |
79 const std::string& event_name, | 80 const std::string& event_name, |
80 const std::string& event_args, | 81 scoped_ptr<base::ListValue> event_args, |
81 void* profile, | 82 void* profile, |
82 bool use_profile_to_restrict_events, | 83 bool use_profile_to_restrict_events, |
83 const GURL& event_url); | 84 const GURL& event_url); |
84 | 85 |
85 // Calls DispatchEventToRenderers or DispatchEventToExtension (depending on | 86 // Calls DispatchEventToRenderers or DispatchEventToExtension (depending on |
86 // whether extension_id == "" or not) of |profile|'s EventRouter. | 87 // whether extension_id == "" or not) of |profile|'s EventRouter. |
87 // |profile| may never be NULL. | 88 // |profile| may never be NULL. |
88 // Virtual for testing. | 89 // Virtual for testing. |
89 virtual void CallEventRouter(Profile* profile, | 90 virtual void CallEventRouter(Profile* profile, |
90 const std::string& extension_id, | 91 const std::string& extension_id, |
91 const std::string& event_name, | 92 const std::string& event_name, |
92 const std::string& event_args, | 93 scoped_ptr<base::ListValue> event_args, |
93 Profile* restrict_to_profile, | 94 Profile* restrict_to_profile, |
94 const GURL& event_url); | 95 const GURL& event_url); |
95 | 96 |
96 private: | 97 private: |
97 friend class base::RefCountedThreadSafe<EventRouterForwarder>; | 98 friend class base::RefCountedThreadSafe<EventRouterForwarder>; |
98 | 99 |
99 DISALLOW_COPY_AND_ASSIGN(EventRouterForwarder); | 100 DISALLOW_COPY_AND_ASSIGN(EventRouterForwarder); |
100 }; | 101 }; |
101 | 102 |
102 } // namespace extensions | 103 } // namespace extensions |
103 | 104 |
104 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ | 105 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ |
OLD | NEW |