Home | History | Annotate | Download | only in extensions
      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 #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_
      7 
      8 #include <string>
      9 
     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 
     15 class GURL;
     16 
     17 namespace extensions {
     18 
     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
     22 //   thread.
     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> {
     30  public:
     31   EventRouterForwarder();
     32 
     33   // Calls
     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);
     40 
     41   // Calls
     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);
     50 
     51   // Calls
     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,
     57                                 void* profile,
     58                                 bool use_profile_to_restrict_events,
     59                                 const GURL& event_url);
     60 
     61   // Calls
     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,
     68                                 void* profile,
     69                                 bool use_profile_to_restrict_events,
     70                                 const GURL& event_url);
     71 
     72  protected:
     73   // Protected for testing.
     74   virtual ~EventRouterForwarder();
     75 
     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,
     81                            void* profile,
     82                            bool use_profile_to_restrict_events,
     83                            const GURL& event_url);
     84 
     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);
     95 
     96  private:
     97   friend class base::RefCountedThreadSafe<EventRouterForwarder>;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(EventRouterForwarder);
    100 };
    101 
    102 }  // namespace extensions
    103 
    104 #endif  // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_FORWARDER_H_
    105