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