1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
#include <string>
#include <set>
#include <vector>
#include "app/gfx/native_widget_types.h"
#include "base/ref_counted.h"
#include "googleurl/src/gurl.h"
class Browser;
class Extension;
class ExtensionDOMUI;
class ExtensionFunction;
class ExtensionHost;
class ExtensionPopupHost;
class Profile;
class RenderViewHost;
class RenderViewHostDelegate;
class Value;
// A factory function for creating new ExtensionFunction instances.
typedef ExtensionFunction* (*ExtensionFunctionFactory)();
// ExtensionFunctionDispatcher receives requests to execute functions from
// Chromium extensions running in a RenderViewHost and dispatches them to the
// appropriate handler. It lives entirely on the UI thread.
class ExtensionFunctionDispatcher {
public:
class Delegate {
public:
// Returns the browser that this delegate is associated with. If the browser
// is incognito, but |include_incognito_windows| is false, we fall back to
// the toplevel browser in the original profile.
virtual Browser* GetBrowser(bool include_incognito_windows) const = 0;
// Returns the gfx::NativeWindow that contains the view hosting the
// environment in which the function dispatcher resides.
virtual gfx::NativeWindow GetFrameNativeWindow();
virtual ExtensionHost* GetExtensionHost() { return NULL; }
virtual ExtensionDOMUI* GetExtensionDOMUI() { return NULL; }
protected:
virtual ~Delegate() {}
};
// The peer object allows us to notify ExtensionFunctions when we are
// destroyed.
// TODO: this should use WeakPtr
struct Peer : public base::RefCounted<Peer> {
explicit Peer(ExtensionFunctionDispatcher* dispatcher)
: dispatcher_(dispatcher) {}
ExtensionFunctionDispatcher* dispatcher_;
private:
friend class base::RefCounted<Peer>;
~Peer() {}
};
// Gets a list of all known extension function names.
static void GetAllFunctionNames(std::vector<std::string>* names);
// Override a previously registered function. Returns true if successful,
// false if no such function was registered.
static bool OverrideFunction(const std::string& name,
ExtensionFunctionFactory factory);
// Resets all functions to their initial implementation.
static void ResetFunctions();
// Retrieves a vector of all EFD instances.
static std::set<ExtensionFunctionDispatcher*>* all_instances();
ExtensionFunctionDispatcher(RenderViewHost* render_view_host,
Delegate* delegate,
const GURL& url);
~ExtensionFunctionDispatcher();
// Handle a request to execute an extension function.
void HandleRequest(const std::string& name, const Value* args,
int request_id, bool has_callback);
// Send a response to a function.
void SendResponse(ExtensionFunction* api, bool success);
// Gets the browser extension functions should operate relative to. For
// example, for positioning windows, or alert boxes, or creating tabs.
// If |include_incognito| is false, and the appropriate browser is incognito,
// we will fall back to a regular browser window or NULL if unavailable.
Browser* GetBrowser(bool include_incognito);
// Get the extension popup hosting environment for the ExtensionHost
// or ExtensionDOMUI associted with this dispatcher.
ExtensionPopupHost* GetPopupHost();
// Gets the ExtensionHost associated with this object. In the case of
// tab hosted extension pages, this will return NULL.
ExtensionHost* GetExtensionHost();
// Gets the ExtensionDOMUI associated with this object. In the case of
// non-tab-hosted extension pages, this will return NULL.
ExtensionDOMUI* GetExtensionDOMUI();
// Returns the gfx::NativeWindow that frames the view of the extension
// containing the function dispatcher. This may return NULL. Refer to the
// ExtensionFunctionDispatcher::Delegate::GetFrameNativeWindow()
// implementation for an explanation.
gfx::NativeWindow GetFrameNativeWindow();
// Gets the extension the function is being invoked by. This should not ever
// return NULL.
Extension* GetExtension();
// Handle a malformed message. Possibly the result of an attack, so kill
// the renderer.
void HandleBadMessage(ExtensionFunction* api);
// Gets the URL for the view we're displaying.
const GURL& url() { return url_; }
// Gets the ID for this extension.
const std::string extension_id() { return url_.host(); }
// The profile that this dispatcher is associated with.
Profile* profile();
// The RenderViewHost this dispatcher is associated with.
RenderViewHost* render_view_host() { return render_view_host_; }
private:
// We need to keep a pointer to the profile because we use it in the dtor
// in sending EXTENSION_FUNCTION_DISPATCHER_DESTROYED, but by that point
// the render_view_host_ has been deleted.
Profile* profile_;
RenderViewHost* render_view_host_;
Delegate* delegate_;
GURL url_;
scoped_refptr<Peer> peer_;
// AutomationExtensionFunction requires access to the RenderViewHost
// associated with us. We make it a friend rather than exposing the
// RenderViewHost as a public method as we wouldn't want everyone to
// start assuming a 1:1 relationship between us and RenderViewHost,
// whereas AutomationExtensionFunction is by necessity "tight" with us.
friend class AutomationExtensionFunction;
};
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
|