OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/extension_function.h" | 5 #include "extensions/browser/extension_function.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/public/browser/notification_source.h" | 8 #include "content/public/browser/notification_source.h" |
9 #include "content/public/browser/notification_types.h" | 9 #include "content/public/browser/notification_types.h" |
10 #include "content/public/browser/render_frame_host.h" | 10 #include "content/public/browser/render_frame_host.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 } | 38 } |
39 | 39 |
40 virtual ~MultipleArgumentsResponseValue() {} | 40 virtual ~MultipleArgumentsResponseValue() {} |
41 | 41 |
42 virtual bool Apply() OVERRIDE { return true; } | 42 virtual bool Apply() OVERRIDE { return true; } |
43 }; | 43 }; |
44 | 44 |
45 class ErrorResponseValue : public ExtensionFunction::ResponseValueObject { | 45 class ErrorResponseValue : public ExtensionFunction::ResponseValueObject { |
46 public: | 46 public: |
47 ErrorResponseValue(ExtensionFunction* function, const std::string& error) { | 47 ErrorResponseValue(ExtensionFunction* function, const std::string& error) { |
48 DCHECK_NE("", error); | 48 // It would be nice to DCHECK(!error.empty()) but too many legacy extension |
| 49 // function implementations don't set error but signal failure. |
49 function->SetError(error); | 50 function->SetError(error); |
50 } | 51 } |
51 | 52 |
52 virtual ~ErrorResponseValue() {} | 53 virtual ~ErrorResponseValue() {} |
53 | 54 |
54 virtual bool Apply() OVERRIDE { return false; } | 55 virtual bool Apply() OVERRIDE { return false; } |
55 }; | 56 }; |
56 | 57 |
57 class BadMessageResponseValue : public ExtensionFunction::ResponseValueObject { | 58 class BadMessageResponseValue : public ExtensionFunction::ResponseValueObject { |
58 public: | 59 public: |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( | 226 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( |
226 ResponseValue result) { | 227 ResponseValue result) { |
227 return scoped_ptr<ResponseActionObject>(new RespondNowAction( | 228 return scoped_ptr<ResponseActionObject>(new RespondNowAction( |
228 result.Pass(), base::Bind(&ExtensionFunction::SendResponse, this))); | 229 result.Pass(), base::Bind(&ExtensionFunction::SendResponse, this))); |
229 } | 230 } |
230 | 231 |
231 ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() { | 232 ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() { |
232 return scoped_ptr<ResponseActionObject>(new RespondLaterAction()); | 233 return scoped_ptr<ResponseActionObject>(new RespondLaterAction()); |
233 } | 234 } |
234 | 235 |
235 void ExtensionFunction::Run() { | 236 void ExtensionFunction::Done(ResponseValue result) { |
236 if (!RunImpl()) | 237 SendResponse(result->Apply()); |
237 SendResponse(false); | |
238 } | |
239 | |
240 bool ExtensionFunction::RunImpl() { | |
241 RunImplTypesafe()->Execute(); | |
242 return true; | |
243 } | |
244 | |
245 ExtensionFunction::ResponseAction ExtensionFunction::RunImplTypesafe() { | |
246 NOTREACHED() | |
247 << "ExtensionFunctions must override either RunImpl or RunImplTypesafe"; | |
248 return RespondNow(NoArguments()); | |
249 } | |
250 | |
251 void ExtensionFunction::SendResponseTypesafe(ResponseValue response) { | |
252 SendResponse(response->Apply()); | |
253 } | 238 } |
254 | 239 |
255 bool ExtensionFunction::ShouldSkipQuotaLimiting() const { | 240 bool ExtensionFunction::ShouldSkipQuotaLimiting() const { |
256 return false; | 241 return false; |
257 } | 242 } |
258 | 243 |
259 bool ExtensionFunction::HasOptionalArgument(size_t index) { | 244 bool ExtensionFunction::HasOptionalArgument(size_t index) { |
260 base::Value* value; | 245 base::Value* value; |
261 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); | 246 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); |
262 } | 247 } |
263 | 248 |
264 void ExtensionFunction::SendResponseImpl(bool success) { | 249 void ExtensionFunction::SendResponseImpl(bool success) { |
265 DCHECK(!response_callback_.is_null()); | 250 DCHECK(!response_callback_.is_null()); |
266 | 251 |
267 ResponseType type = success ? SUCCEEDED : FAILED; | 252 ResponseType type = success ? SUCCEEDED : FAILED; |
268 if (bad_message_) { | 253 if (bad_message_) { |
269 type = BAD_MESSAGE; | 254 type = BAD_MESSAGE; |
270 LOG(ERROR) << "Bad extension message " << name_; | 255 LOG(ERROR) << "Bad extension message " << name_; |
271 } | 256 } |
272 | 257 |
273 // If results were never set, we send an empty argument list. | 258 // If results were never set, we send an empty argument list. |
274 if (!results_) | 259 if (!results_) |
275 results_.reset(new base::ListValue()); | 260 results_.reset(new base::ListValue()); |
276 | 261 |
277 response_callback_.Run(type, *results_, GetError()); | 262 response_callback_.Run(type, *results_, GetError()); |
278 } | 263 } |
279 | 264 |
| 265 void ExtensionFunction::OnRespondingLater(ResponseValue value) { |
| 266 SendResponse(value->Apply()); |
| 267 } |
| 268 |
280 UIThreadExtensionFunction::UIThreadExtensionFunction() | 269 UIThreadExtensionFunction::UIThreadExtensionFunction() |
281 : render_view_host_(NULL), | 270 : render_view_host_(NULL), |
282 render_frame_host_(NULL), | 271 render_frame_host_(NULL), |
283 context_(NULL), | 272 context_(NULL), |
284 delegate_(NULL) { | 273 delegate_(NULL) { |
285 } | 274 } |
286 | 275 |
287 UIThreadExtensionFunction::~UIThreadExtensionFunction() { | 276 UIThreadExtensionFunction::~UIThreadExtensionFunction() { |
288 if (dispatcher() && render_view_host()) | 277 if (dispatcher() && render_view_host()) |
289 dispatcher()->OnExtensionFunctionCompleted(GetExtension()); | 278 dispatcher()->OnExtensionFunctionCompleted(GetExtension()); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 void IOThreadExtensionFunction::SendResponse(bool success) { | 351 void IOThreadExtensionFunction::SendResponse(bool success) { |
363 SendResponseImpl(success); | 352 SendResponseImpl(success); |
364 } | 353 } |
365 | 354 |
366 AsyncExtensionFunction::AsyncExtensionFunction() { | 355 AsyncExtensionFunction::AsyncExtensionFunction() { |
367 } | 356 } |
368 | 357 |
369 AsyncExtensionFunction::~AsyncExtensionFunction() { | 358 AsyncExtensionFunction::~AsyncExtensionFunction() { |
370 } | 359 } |
371 | 360 |
| 361 ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() { |
| 362 return RunAsync() ? RespondLater() : RespondNow(Error(error_)); |
| 363 } |
| 364 |
372 SyncExtensionFunction::SyncExtensionFunction() { | 365 SyncExtensionFunction::SyncExtensionFunction() { |
373 } | 366 } |
374 | 367 |
375 SyncExtensionFunction::~SyncExtensionFunction() { | 368 SyncExtensionFunction::~SyncExtensionFunction() { |
376 } | 369 } |
377 | 370 |
378 bool SyncExtensionFunction::RunImpl() { | 371 ExtensionFunction::ResponseAction SyncExtensionFunction::Run() { |
379 SendResponse(RunSync()); | 372 return RespondNow(RunSync() ? MultipleArguments(results_.get()) |
380 return true; | 373 : Error(error_)); |
381 } | 374 } |
382 | 375 |
383 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { | 376 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
384 } | 377 } |
385 | 378 |
386 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { | 379 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
387 } | 380 } |
388 | 381 |
389 bool SyncIOThreadExtensionFunction::RunImpl() { | 382 ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() { |
390 SendResponse(RunSync()); | 383 return RespondNow(RunSync() ? MultipleArguments(results_.get()) |
391 return true; | 384 : Error(error_)); |
392 } | 385 } |
OLD | NEW |