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_EXTENSION_FUNCTION_DISPATCHER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/weak_ptr.h"
     13 #include "chrome/browser/extensions/extension_function.h"
     14 #include "ipc/ipc_sender.h"
     15 #include "url/gurl.h"
     16 
     17 class ChromeRenderMessageFilter;
     18 class ExtensionInfoMap;
     19 class Profile;
     20 struct ExtensionHostMsg_Request_Params;
     21 
     22 namespace content {
     23 class RenderViewHost;
     24 class WebContents;
     25 }
     26 
     27 namespace extensions {
     28 class Extension;
     29 class ExtensionAPI;
     30 class ProcessMap;
     31 class WindowController;
     32 }
     33 
     34 // A factory function for creating new ExtensionFunction instances.
     35 typedef ExtensionFunction* (*ExtensionFunctionFactory)();
     36 
     37 // ExtensionFunctionDispatcher receives requests to execute functions from
     38 // Chrome extensions running in a RenderViewHost and dispatches them to the
     39 // appropriate handler. It lives entirely on the UI thread.
     40 //
     41 // ExtensionFunctionDispatcher should be a member of some class that hosts
     42 // RenderViewHosts and wants them to be able to display extension content.
     43 // This class should also implement ExtensionFunctionDispatcher::Delegate.
     44 //
     45 // Note that a single ExtensionFunctionDispatcher does *not* correspond to a
     46 // single RVH, a single extension, or a single URL. This is by design so that
     47 // we can gracefully handle cases like WebContents, where the RVH, extension,
     48 // and URL can all change over the lifetime of the tab. Instead, these items
     49 // are all passed into each request.
     50 class ExtensionFunctionDispatcher
     51     : public base::SupportsWeakPtr<ExtensionFunctionDispatcher> {
     52  public:
     53   class Delegate {
     54    public:
     55     // Returns the extensions::WindowController associated with this delegate,
     56     // or NULL if no window is associated with the delegate.
     57     virtual extensions::WindowController* GetExtensionWindowController() const;
     58 
     59     // Asks the delegate for any relevant WebContents associated with this
     60     // context. For example, the WebContents in which an infobar or
     61     // chrome-extension://<id> URL are being shown. Callers must check for a
     62     // NULL return value (as in the case of a background page).
     63     virtual content::WebContents* GetAssociatedWebContents() const;
     64 
     65     // If the associated web contents is not null, returns that. Otherwise,
     66     // returns the next most relevant visible web contents. Callers must check
     67     // for a NULL return value (as in the case of a background page).
     68     virtual content::WebContents* GetVisibleWebContents() const;
     69 
     70    protected:
     71     virtual ~Delegate() {}
     72   };
     73 
     74   // Gets a list of all known extension function names.
     75   static void GetAllFunctionNames(std::vector<std::string>* names);
     76 
     77   // Override a previously registered function. Returns true if successful,
     78   // false if no such function was registered.
     79   static bool OverrideFunction(const std::string& name,
     80                                ExtensionFunctionFactory factory);
     81 
     82   // Resets all functions to their initial implementation.
     83   static void ResetFunctions();
     84 
     85   // Dispatches an IO-thread extension function. Only used for specific
     86   // functions that must be handled on the IO-thread.
     87   static void DispatchOnIOThread(
     88       ExtensionInfoMap* extension_info_map,
     89       void* profile,
     90       int render_process_id,
     91       base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
     92       int routing_id,
     93       const ExtensionHostMsg_Request_Params& params);
     94 
     95   // Public constructor. Callers must ensure that:
     96   // - |delegate| outlives this object.
     97   // - This object outlives any RenderViewHost's passed to created
     98   //   ExtensionFunctions.
     99   ExtensionFunctionDispatcher(Profile* profile, Delegate* delegate);
    100 
    101   ~ExtensionFunctionDispatcher();
    102 
    103   Delegate* delegate() { return delegate_; }
    104 
    105   // Message handlers.
    106   // The response is sent to the corresponding render view in an
    107   // ExtensionMsg_Response message.
    108   void Dispatch(const ExtensionHostMsg_Request_Params& params,
    109                 content::RenderViewHost* render_view_host);
    110   // |callback| is called when the function execution completes.
    111   void DispatchWithCallback(
    112       const ExtensionHostMsg_Request_Params& params,
    113       content::RenderViewHost* render_view_host,
    114       const ExtensionFunction::ResponseCallback& callback);
    115 
    116   // Called when an ExtensionFunction is done executing, after it has sent
    117   // a response (if any) to the extension.
    118   void OnExtensionFunctionCompleted(const extensions::Extension* extension);
    119 
    120   // The profile that this dispatcher is associated with.
    121   Profile* profile() { return profile_; }
    122 
    123  private:
    124   // For a given RenderViewHost instance, UIThreadResponseCallbackWrapper
    125   // creates ExtensionFunction::ResponseCallback instances which send responses
    126   // to the corresponding render view in ExtensionMsg_Response messages.
    127   // This class tracks the lifespan of the RenderViewHost instance, and will be
    128   // destroyed automatically when it goes away.
    129   class UIThreadResponseCallbackWrapper;
    130 
    131   // Helper to check whether an ExtensionFunction has the required permissions.
    132   // This should be called after the function is fully initialized.
    133   // If the check fails, |callback| is run with an access-denied error and false
    134   // is returned. |function| must not be run in that case.
    135   static bool CheckPermissions(
    136       ExtensionFunction* function,
    137       const extensions::Extension* extension,
    138       const ExtensionHostMsg_Request_Params& params,
    139       const ExtensionFunction::ResponseCallback& callback);
    140 
    141   // Helper to create an ExtensionFunction to handle the function given by
    142   // |params|. Can be called on any thread.
    143   // Does not set subclass properties, or include_incognito.
    144   static ExtensionFunction* CreateExtensionFunction(
    145       const ExtensionHostMsg_Request_Params& params,
    146       const extensions::Extension* extension,
    147       int requesting_process_id,
    148       const extensions::ProcessMap& process_map,
    149       extensions::ExtensionAPI* api,
    150       void* profile,
    151       const ExtensionFunction::ResponseCallback& callback);
    152 
    153   // Helper to run the response callback with an access denied error. Can be
    154   // called on any thread.
    155   static void SendAccessDenied(
    156       const ExtensionFunction::ResponseCallback& callback);
    157 
    158   Profile* profile_;
    159 
    160   Delegate* delegate_;
    161 
    162   // This map doesn't own either the keys or the values. When a RenderViewHost
    163   // instance goes away, the corresponding entry in this map (if exists) will be
    164   // removed.
    165   typedef std::map<content::RenderViewHost*, UIThreadResponseCallbackWrapper*>
    166       UIThreadResponseCallbackWrapperMap;
    167   UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_;
    168 };
    169 
    170 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
    171