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_FUNCTION_DISPATCHER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/memory/ref_counted.h"
13#include "googleurl/src/gurl.h"
14#include "ui/gfx/native_widget_types.h"
15
16class Browser;
17class Extension;
18class ExtensionFunction;
19class ListValue;
20class Profile;
21class RenderViewHost;
22class TabContents;
23struct ExtensionHostMsg_DomMessage_Params;
24
25// A factory function for creating new ExtensionFunction instances.
26typedef ExtensionFunction* (*ExtensionFunctionFactory)();
27
28// ExtensionFunctionDispatcher receives requests to execute functions from
29// Chromium extensions running in a RenderViewHost and dispatches them to the
30// appropriate handler. It lives entirely on the UI thread.
31class ExtensionFunctionDispatcher {
32 public:
33  class Delegate {
34   public:
35    // Returns the browser that this delegate is associated with, if any.
36    // Returns NULL otherwise.
37    virtual Browser* GetBrowser() = 0;
38
39    // Returns the native view for this extension view, if any. This may be NULL
40    // if the view is not visible.
41    virtual gfx::NativeView GetNativeViewOfHost() = 0;
42
43    // Asks the delegate for any relevant TabContents associated with this
44    // context. For example, the TabContents in which an infobar or
45    // chrome-extension://<id> URL are being shown. Callers must check for a
46    // NULL return value (as in the case of a background page).
47    virtual TabContents* associated_tab_contents() const = 0;
48
49   protected:
50    virtual ~Delegate() {}
51  };
52
53  // The peer object allows us to notify ExtensionFunctions when we are
54  // destroyed.
55  // TODO: this should use WeakPtr
56  struct Peer : public base::RefCounted<Peer> {
57    explicit Peer(ExtensionFunctionDispatcher* dispatcher)
58        : dispatcher_(dispatcher) {}
59    ExtensionFunctionDispatcher* dispatcher_;
60
61   private:
62    friend class base::RefCounted<Peer>;
63
64    ~Peer() {}
65  };
66
67  // Gets a list of all known extension function names.
68  static void GetAllFunctionNames(std::vector<std::string>* names);
69
70  // Override a previously registered function. Returns true if successful,
71  // false if no such function was registered.
72  static bool OverrideFunction(const std::string& name,
73                               ExtensionFunctionFactory factory);
74
75  // Resets all functions to their initial implementation.
76  static void ResetFunctions();
77
78  // Creates an instance for the specified RenderViewHost and URL. If the URL
79  // does not contain a valid extension, returns NULL.
80  static ExtensionFunctionDispatcher* Create(RenderViewHost* render_view_host,
81                                             Delegate* delegate,
82                                             const GURL& url);
83
84  ~ExtensionFunctionDispatcher();
85
86  Delegate* delegate() { return delegate_; }
87
88  // Handle a request to execute an extension function.
89  void HandleRequest(const ExtensionHostMsg_DomMessage_Params& params);
90
91  // Send a response to a function.
92  void SendResponse(ExtensionFunction* api, bool success);
93
94  // Returns the current browser. Callers should generally prefer
95  // ExtensionFunction::GetCurrentBrowser() over this method, as that one
96  // provides the correct value for |include_incognito|.
97  //
98  // See the comments for ExtensionFunction::GetCurrentBrowser() for more
99  // details.
100  Browser* GetCurrentBrowser(bool include_incognito);
101
102  // Handle a malformed message.  Possibly the result of an attack, so kill
103  // the renderer.
104  void HandleBadMessage(ExtensionFunction* api);
105
106  // Gets the URL for the view we're displaying.
107  const GURL& url() { return url_; }
108
109  // Gets the ID for this extension.
110  const std::string extension_id() { return extension_id_; }
111
112  // The profile that this dispatcher is associated with.
113  Profile* profile();
114
115  // The RenderViewHost this dispatcher is associated with.
116  RenderViewHost* render_view_host() { return render_view_host_; }
117
118 private:
119  ExtensionFunctionDispatcher(RenderViewHost* render_view_host,
120                              Delegate* delegate,
121                              const Extension* extension,
122                              const GURL& url);
123
124  // We need to keep a pointer to the profile because we use it in the dtor
125  // in sending EXTENSION_FUNCTION_DISPATCHER_DESTROYED, but by that point
126  // the render_view_host_ has been deleted.
127  Profile* profile_;
128
129  RenderViewHost* render_view_host_;
130
131  Delegate* delegate_;
132
133  GURL url_;
134
135  std::string extension_id_;
136
137  scoped_refptr<Peer> peer_;
138};
139
140#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
141