| 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 "base/values.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "extensions/browser/extension_event_histogram_value.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(events::HistogramValue histogram_value, |
| 39 const std::string& event_name, |
| 38 scoped_ptr<base::ListValue> event_args, | 40 scoped_ptr<base::ListValue> event_args, |
| 39 const GURL& event_url); | 41 const GURL& event_url); |
| 40 | 42 |
| 41 // Calls | 43 // Calls |
| 42 // DispatchEventToExtension(extension_id, event_name, event_args, | 44 // DispatchEventToExtension(extension_id, event_name, event_args, |
| 43 // profile, event_url) | 45 // profile, event_url) |
| 44 // on all (original) profiles' EventRouters. | 46 // on all (original) profiles' EventRouters. |
| 45 // May be called on any thread. | 47 // May be called on any thread. |
| 46 void BroadcastEventToExtension(const std::string& extension_id, | 48 void BroadcastEventToExtension(const std::string& extension_id, |
| 49 events::HistogramValue histogram_value, |
| 47 const std::string& event_name, | 50 const std::string& event_name, |
| 48 scoped_ptr<base::ListValue> event_args, | 51 scoped_ptr<base::ListValue> event_args, |
| 49 const GURL& event_url); | 52 const GURL& event_url); |
| 50 | 53 |
| 51 // Calls | 54 // Calls |
| 52 // DispatchEventToRenderers(event_name, event_args, | 55 // DispatchEventToRenderers(event_name, event_args, |
| 53 // use_profile_to_restrict_events ? profile : NULL, event_url) | 56 // use_profile_to_restrict_events ? profile : NULL, event_url) |
| 54 // on |profile|'s EventRouter. May be called on any thread. | 57 // on |profile|'s EventRouter. May be called on any thread. |
| 55 void DispatchEventToRenderers(const std::string& event_name, | 58 void DispatchEventToRenderers(events::HistogramValue histogram_value, |
| 59 const std::string& event_name, |
| 56 scoped_ptr<base::ListValue> event_args, | 60 scoped_ptr<base::ListValue> event_args, |
| 57 void* profile, | 61 void* profile, |
| 58 bool use_profile_to_restrict_events, | 62 bool use_profile_to_restrict_events, |
| 59 const GURL& event_url); | 63 const GURL& event_url); |
| 60 | 64 |
| 61 // Calls | 65 // Calls |
| 62 // DispatchEventToExtension(extension_id, event_name, event_args, | 66 // DispatchEventToExtension(extension_id, event_name, event_args, |
| 63 // use_profile_to_restrict_events ? profile : NULL, event_url) | 67 // use_profile_to_restrict_events ? profile : NULL, event_url) |
| 64 // on |profile|'s EventRouter. May be called on any thread. | 68 // on |profile|'s EventRouter. May be called on any thread. |
| 65 void DispatchEventToExtension(const std::string& extension_id, | 69 void DispatchEventToExtension(const std::string& extension_id, |
| 70 events::HistogramValue histogram_value, |
| 66 const std::string& event_name, | 71 const std::string& event_name, |
| 67 scoped_ptr<base::ListValue> event_args, | 72 scoped_ptr<base::ListValue> event_args, |
| 68 void* profile, | 73 void* profile, |
| 69 bool use_profile_to_restrict_events, | 74 bool use_profile_to_restrict_events, |
| 70 const GURL& event_url); | 75 const GURL& event_url); |
| 71 | 76 |
| 72 protected: | 77 protected: |
| 73 // Protected for testing. | 78 // Protected for testing. |
| 74 virtual ~EventRouterForwarder(); | 79 virtual ~EventRouterForwarder(); |
| 75 | 80 |
| 76 // Helper function for {Broadcast,Dispatch}EventTo{Extension,Renderers}. | 81 // Helper function for {Broadcast,Dispatch}EventTo{Extension,Renderers}. |
| 77 // Virtual for testing. | 82 // Virtual for testing. |
| 78 virtual void HandleEvent(const std::string& extension_id, | 83 virtual void HandleEvent(const std::string& extension_id, |
| 84 events::HistogramValue histogram_value, |
| 79 const std::string& event_name, | 85 const std::string& event_name, |
| 80 scoped_ptr<base::ListValue> event_args, | 86 scoped_ptr<base::ListValue> event_args, |
| 81 void* profile, | 87 void* profile, |
| 82 bool use_profile_to_restrict_events, | 88 bool use_profile_to_restrict_events, |
| 83 const GURL& event_url); | 89 const GURL& event_url); |
| 84 | 90 |
| 85 // Calls DispatchEventToRenderers or DispatchEventToExtension (depending on | 91 // Calls DispatchEventToRenderers or DispatchEventToExtension (depending on |
| 86 // whether extension_id == "" or not) of |profile|'s EventRouter. | 92 // whether extension_id == "" or not) of |profile|'s EventRouter. |
| 87 // |profile| may never be NULL. | 93 // |profile| may never be NULL. |
| 88 // Virtual for testing. | 94 // Virtual for testing. |
| 89 virtual void CallEventRouter(Profile* profile, | 95 virtual void CallEventRouter(Profile* profile, |
| 90 const std::string& extension_id, | 96 const std::string& extension_id, |
| 97 events::HistogramValue histogram_value, |
| 91 const std::string& event_name, | 98 const std::string& event_name, |
| 92 scoped_ptr<base::ListValue> event_args, | 99 scoped_ptr<base::ListValue> event_args, |
| 93 Profile* restrict_to_profile, | 100 Profile* restrict_to_profile, |
| 94 const GURL& event_url); | 101 const GURL& event_url); |
| 95 | 102 |
| 96 private: | 103 private: |
| 97 friend class base::RefCountedThreadSafe<EventRouterForwarder>; | 104 friend class base::RefCountedThreadSafe<EventRouterForwarder>; |
| 98 | 105 |
| 99 DISALLOW_COPY_AND_ASSIGN(EventRouterForwarder); | 106 DISALLOW_COPY_AND_ASSIGN(EventRouterForwarder); |
| 100 }; | 107 }; |
| 101 | 108 |
| 102 } // namespace extensions | 109 } // namespace extensions |
| 103 | 110 |
| 104 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ | 111 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_ |
| OLD | NEW |