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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
// Copyright (c) 2012 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.
#include "chrome/browser/extensions/extension_function.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_window_controller.h"
#include "chrome/browser/extensions/extension_window_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/common/extensions/extension_messages.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/common/result_codes.h"
using content::BrowserThread;
using content::RenderViewHost;
using content::UserMetricsAction;
// static
void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) {
x->Destruct();
}
UIThreadExtensionFunction::RenderViewHostTracker::RenderViewHostTracker(
UIThreadExtensionFunction* function,
RenderViewHost* render_view_host)
: content::RenderViewHostObserver(render_view_host),
function_(function) {
registrar_.Add(this,
content::NOTIFICATION_RENDER_VIEW_HOST_DELETED,
content::Source<RenderViewHost>(function->render_view_host()));
}
void UIThreadExtensionFunction::RenderViewHostTracker::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
CHECK(type == content::NOTIFICATION_RENDER_VIEW_HOST_DELETED);
CHECK(content::Source<RenderViewHost>(source).ptr() ==
function_->render_view_host());
function_->SetRenderViewHost(NULL);
}
void UIThreadExtensionFunction::RenderViewHostTracker::RenderViewHostDestroyed(
RenderViewHost* render_view_host) {
// Overidding the default behavior of RenderViewHostObserver which is to
// delete this. In our case, we'll be deleted when the
// UIThreadExtensionFunction that contains us goes away.
}
bool UIThreadExtensionFunction::RenderViewHostTracker::OnMessageReceived(
const IPC::Message& message) {
return function_->OnMessageReceivedFromRenderView(message);
}
ExtensionFunction::ExtensionFunction()
: request_id_(-1),
profile_id_(NULL),
has_callback_(false),
include_incognito_(false),
user_gesture_(false),
args_(NULL),
bad_message_(false) {
}
ExtensionFunction::~ExtensionFunction() {
}
UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() {
return NULL;
}
IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() {
return NULL;
}
void ExtensionFunction::OnQuotaExceeded() {
error_ = QuotaLimitHeuristic::kGenericOverQuotaError;
SendResponse(false);
}
void ExtensionFunction::SetArgs(const base::ListValue* args) {
DCHECK(!args_.get()); // Should only be called once.
args_.reset(args->DeepCopy());
}
const Value* ExtensionFunction::GetResultValue() {
return result_.get();
}
const std::string ExtensionFunction::GetError() {
return error_;
}
void ExtensionFunction::SetError(const std::string& error) {
error_ = error;
}
void ExtensionFunction::Run() {
if (!RunImpl())
SendResponse(false);
}
bool ExtensionFunction::ShouldSkipQuotaLimiting() const {
return false;
}
bool ExtensionFunction::HasOptionalArgument(size_t index) {
Value* value;
return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL);
}
void ExtensionFunction::SendResponseImpl(base::ProcessHandle process,
IPC::Sender* ipc_sender,
int routing_id,
bool success) {
DCHECK(ipc_sender);
if (bad_message_) {
HandleBadMessage(process);
return;
}
// Value objects can't be directly serialized in our IPC code, so we wrap the
// result_ Value with a ListValue (also transferring ownership of result_).
base::ListValue result_wrapper;
if (result_.get())
result_wrapper.Append(result_.release());
ipc_sender->Send(new ExtensionMsg_Response(
routing_id, request_id_, success, result_wrapper, GetError()));
}
void ExtensionFunction::HandleBadMessage(base::ProcessHandle process) {
LOG(ERROR) << "bad extension message " << name_ << " : terminating renderer.";
if (content::RenderProcessHost::run_renderer_in_process()) {
// In single process mode it is better if we don't suicide but just crash.
CHECK(false);
} else {
NOTREACHED();
content::RecordAction(UserMetricsAction("BadMessageTerminate_EFD"));
if (process)
base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE,
false);
}
}
UIThreadExtensionFunction::UIThreadExtensionFunction()
: render_view_host_(NULL), profile_(NULL), delegate_(NULL) {
}
UIThreadExtensionFunction::~UIThreadExtensionFunction() {
if (dispatcher() && render_view_host())
dispatcher()->OnExtensionFunctionCompleted(GetExtension());
}
UIThreadExtensionFunction*
UIThreadExtensionFunction::AsUIThreadExtensionFunction() {
return this;
}
bool UIThreadExtensionFunction::OnMessageReceivedFromRenderView(
const IPC::Message& message) {
return false;
}
void UIThreadExtensionFunction::Destruct() const {
BrowserThread::DeleteOnUIThread::Destruct(this);
}
void UIThreadExtensionFunction::SetRenderViewHost(
RenderViewHost* render_view_host) {
render_view_host_ = render_view_host;
tracker_.reset(render_view_host ?
new RenderViewHostTracker(this, render_view_host) : NULL);
}
// TODO(stevenjb): Replace this with GetExtensionWindowController().
Browser* UIThreadExtensionFunction::GetCurrentBrowser() {
// If the delegate has an associated browser, return it.
ExtensionWindowController* window_controller =
dispatcher()->delegate()->GetExtensionWindowController();
if (window_controller) {
Browser* browser = window_controller->GetBrowser();
if (browser)
return browser;
}
// Otherwise, try to default to a reasonable browser. If |include_incognito_|
// is true, we will also search browsers in the incognito version of this
// profile. Note that the profile may already be incognito, in which case
// we will search the incognito version only, regardless of the value of
// |include_incognito|.
Profile* profile = Profile::FromBrowserContext(
render_view_host_->GetProcess()->GetBrowserContext());
Browser* browser = browser::FindAnyBrowser(profile, include_incognito_);
// NOTE(rafaelw): This can return NULL in some circumstances. In particular,
// a background_page onload chrome.tabs api call can make it into here
// before the browser is sufficiently initialized to return here.
// A similar situation may arise during shutdown.
// TODO(rafaelw): Delay creation of background_page until the browser
// is available. http://code.google.com/p/chromium/issues/detail?id=13284
return browser;
}
ExtensionWindowController*
UIThreadExtensionFunction::GetExtensionWindowController() {
// If the delegate has an associated window controller, return it.
ExtensionWindowController* window_controller =
dispatcher()->delegate()->GetExtensionWindowController();
if (window_controller)
return window_controller;
return ExtensionWindowList::GetInstance()->CurrentWindowForFunction(this);
}
bool UIThreadExtensionFunction::CanOperateOnWindow(
const ExtensionWindowController* window_controller) const {
const extensions::Extension* extension = GetExtension();
// |extension| is NULL for unit tests only.
if (extension != NULL && !window_controller->IsVisibleToExtension(extension))
return false;
if (profile() == window_controller->profile())
return true;
if (!include_incognito())
return false;
return profile()->HasOffTheRecordProfile() &&
profile()->GetOffTheRecordProfile() == window_controller->profile();
}
void UIThreadExtensionFunction::SendResponse(bool success) {
if (delegate_) {
delegate_->OnSendResponse(this, success, bad_message_);
} else {
if (!render_view_host_ || !dispatcher())
return;
SendResponseImpl(render_view_host_->GetProcess()->GetHandle(),
render_view_host_,
render_view_host_->GetRoutingID(),
success);
}
}
IOThreadExtensionFunction::IOThreadExtensionFunction()
: routing_id_(-1) {
}
IOThreadExtensionFunction::~IOThreadExtensionFunction() {
}
IOThreadExtensionFunction*
IOThreadExtensionFunction::AsIOThreadExtensionFunction() {
return this;
}
void IOThreadExtensionFunction::Destruct() const {
BrowserThread::DeleteOnIOThread::Destruct(this);
}
void IOThreadExtensionFunction::SendResponse(bool success) {
if (!ipc_sender())
return;
SendResponseImpl(ipc_sender()->peer_handle(),
ipc_sender(), routing_id_, success);
}
AsyncExtensionFunction::AsyncExtensionFunction() {
}
AsyncExtensionFunction::~AsyncExtensionFunction() {
}
SyncExtensionFunction::SyncExtensionFunction() {
}
SyncExtensionFunction::~SyncExtensionFunction() {
}
void SyncExtensionFunction::Run() {
SendResponse(RunImpl());
}
SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() {
}
SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() {
}
void SyncIOThreadExtensionFunction::Run() {
SendResponse(RunImpl());
}
|