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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_
#define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_
#include <map>
#include <string>
#include <vector>
#include "base/memory/weak_ptr.h"
#include "extensions/browser/extension_function.h"
#include "ipc/ipc_sender.h"
struct ExtensionHostMsg_Request_Params;
namespace content {
class BrowserContext;
class RenderFrameHost;
class RenderViewHost;
class WebContents;
}
namespace extensions {
class Extension;
class ExtensionAPI;
class ExtensionMessageFilter;
class InfoMap;
class ProcessMap;
class WindowController;
// A factory function for creating new ExtensionFunction instances.
typedef ExtensionFunction* (*ExtensionFunctionFactory)();
// ExtensionFunctionDispatcher receives requests to execute functions from
// Chrome extensions running in a RenderViewHost and dispatches them to the
// appropriate handler. It lives entirely on the UI thread.
//
// ExtensionFunctionDispatcher should be a member of some class that hosts
// RenderViewHosts and wants them to be able to display extension content.
// This class should also implement ExtensionFunctionDispatcher::Delegate.
//
// Note that a single ExtensionFunctionDispatcher does *not* correspond to a
// single RVH, a single extension, or a single URL. This is by design so that
// we can gracefully handle cases like WebContents, where the RVH, extension,
// and URL can all change over the lifetime of the tab. Instead, these items
// are all passed into each request.
class ExtensionFunctionDispatcher
: public base::SupportsWeakPtr<ExtensionFunctionDispatcher> {
public:
class Delegate {
public:
// Returns the WindowController associated with this delegate, or NULL if no
// window is associated with the delegate.
virtual WindowController* GetExtensionWindowController() const;
// Asks the delegate for any relevant WebContents associated with this
// context. For example, the WebContents in which an infobar or
// chrome-extension://<id> URL are being shown. Callers must check for a
// NULL return value (as in the case of a background page).
virtual content::WebContents* GetAssociatedWebContents() const;
// If the associated web contents is not null, returns that. Otherwise,
// returns the next most relevant visible web contents. Callers must check
// for a NULL return value (as in the case of a background page).
virtual content::WebContents* GetVisibleWebContents() const;
protected:
virtual ~Delegate() {}
};
// 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);
// Dispatches an IO-thread extension function. Only used for specific
// functions that must be handled on the IO-thread.
static void DispatchOnIOThread(
InfoMap* extension_info_map,
void* profile_id,
int render_process_id,
base::WeakPtr<ExtensionMessageFilter> ipc_sender,
int routing_id,
const ExtensionHostMsg_Request_Params& params);
// Public constructor. Callers must ensure that:
// - |delegate| outlives this object.
// - This object outlives any RenderViewHost's passed to created
// ExtensionFunctions.
ExtensionFunctionDispatcher(content::BrowserContext* browser_context,
Delegate* delegate);
~ExtensionFunctionDispatcher();
Delegate* delegate() { return delegate_; }
// Message handlers.
// The response is sent to the corresponding render view in an
// ExtensionMsg_Response message.
// TODO (jam): convert all callers to use RenderFrameHost.
void Dispatch(const ExtensionHostMsg_Request_Params& params,
content::RenderViewHost* render_view_host);
// Dispatch an extension function and calls |callback| when the execution
// completes.
void DispatchWithCallback(
const ExtensionHostMsg_Request_Params& params,
content::RenderFrameHost* render_frame_host,
const ExtensionFunction::ResponseCallback& callback);
// Called when an ExtensionFunction is done executing, after it has sent
// a response (if any) to the extension.
void OnExtensionFunctionCompleted(const Extension* extension);
// The BrowserContext that this dispatcher is associated with.
content::BrowserContext* browser_context() { return browser_context_; }
private:
// For a given RenderViewHost instance, UIThreadResponseCallbackWrapper
// creates ExtensionFunction::ResponseCallback instances which send responses
// to the corresponding render view in ExtensionMsg_Response messages.
// This class tracks the lifespan of the RenderViewHost instance, and will be
// destroyed automatically when it goes away.
class UIThreadResponseCallbackWrapper;
// Helper to check whether an ExtensionFunction has the required permissions.
// This should be called after the function is fully initialized.
// If the check fails, |callback| is run with an access-denied error and false
// is returned. |function| must not be run in that case.
static bool CheckPermissions(
ExtensionFunction* function,
const Extension* extension,
const ExtensionHostMsg_Request_Params& params,
const ExtensionFunction::ResponseCallback& callback);
// Helper to create an ExtensionFunction to handle the function given by
// |params|. Can be called on any thread.
// Does not set subclass properties, or include_incognito.
static ExtensionFunction* CreateExtensionFunction(
const ExtensionHostMsg_Request_Params& params,
const Extension* extension,
int requesting_process_id,
const ProcessMap& process_map,
ExtensionAPI* api,
void* profile_id,
const ExtensionFunction::ResponseCallback& callback);
// Helper to run the response callback with an access denied error. Can be
// called on any thread.
static void SendAccessDenied(
const ExtensionFunction::ResponseCallback& callback);
void DispatchWithCallbackInternal(
const ExtensionHostMsg_Request_Params& params,
content::RenderViewHost* render_view_host,
content::RenderFrameHost* render_frame_host,
const ExtensionFunction::ResponseCallback& callback);
content::BrowserContext* browser_context_;
Delegate* delegate_;
// This map doesn't own either the keys or the values. When a RenderViewHost
// instance goes away, the corresponding entry in this map (if exists) will be
// removed.
typedef std::map<content::RenderViewHost*, UIThreadResponseCallbackWrapper*>
UIThreadResponseCallbackWrapperMap;
UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_;
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_
|