| 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 16 matching lines...) Expand all Loading... |
| 27 class MultipleArgumentsResponseValue | 27 class MultipleArgumentsResponseValue |
| 28 : public ExtensionFunction::ResponseValueObject { | 28 : public ExtensionFunction::ResponseValueObject { |
| 29 public: | 29 public: |
| 30 MultipleArgumentsResponseValue(ExtensionFunction* function, | 30 MultipleArgumentsResponseValue(ExtensionFunction* function, |
| 31 base::ListValue* result) { | 31 base::ListValue* result) { |
| 32 if (function->GetResultList()) { | 32 if (function->GetResultList()) { |
| 33 DCHECK_EQ(function->GetResultList(), result); | 33 DCHECK_EQ(function->GetResultList(), result); |
| 34 } else { | 34 } else { |
| 35 function->SetResultList(make_scoped_ptr(result)); | 35 function->SetResultList(make_scoped_ptr(result)); |
| 36 } | 36 } |
| 37 DCHECK_EQ("", function->GetError()); | 37 // It would be nice to DCHECK(error.empty()) but some legacy extension |
| 38 // function implementations... I'm looking at chrome.input.ime... do this |
| 39 // for some reason. |
| 38 } | 40 } |
| 39 | 41 |
| 40 virtual ~MultipleArgumentsResponseValue() {} | 42 virtual ~MultipleArgumentsResponseValue() {} |
| 41 | 43 |
| 42 virtual bool Apply() OVERRIDE { return true; } | 44 virtual bool Apply() OVERRIDE { return true; } |
| 43 }; | 45 }; |
| 44 | 46 |
| 45 class ErrorResponseValue : public ExtensionFunction::ResponseValueObject { | 47 class ErrorResponseValue : public ExtensionFunction::ResponseValueObject { |
| 46 public: | 48 public: |
| 47 ErrorResponseValue(ExtensionFunction* function, const std::string& error) { | 49 ErrorResponseValue(ExtensionFunction* function, const std::string& error) { |
| 48 DCHECK_NE("", error); | 50 // It would be nice to DCHECK(!error.empty()) but too many legacy extension |
| 51 // function implementations don't set error but signal failure. |
| 49 function->SetError(error); | 52 function->SetError(error); |
| 50 } | 53 } |
| 51 | 54 |
| 52 virtual ~ErrorResponseValue() {} | 55 virtual ~ErrorResponseValue() {} |
| 53 | 56 |
| 54 virtual bool Apply() OVERRIDE { return false; } | 57 virtual bool Apply() OVERRIDE { return false; } |
| 55 }; | 58 }; |
| 56 | 59 |
| 57 class BadMessageResponseValue : public ExtensionFunction::ResponseValueObject { | 60 class BadMessageResponseValue : public ExtensionFunction::ResponseValueObject { |
| 58 public: | 61 public: |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( | 228 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( |
| 226 ResponseValue result) { | 229 ResponseValue result) { |
| 227 return scoped_ptr<ResponseActionObject>(new RespondNowAction( | 230 return scoped_ptr<ResponseActionObject>(new RespondNowAction( |
| 228 result.Pass(), base::Bind(&ExtensionFunction::SendResponse, this))); | 231 result.Pass(), base::Bind(&ExtensionFunction::SendResponse, this))); |
| 229 } | 232 } |
| 230 | 233 |
| 231 ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() { | 234 ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() { |
| 232 return scoped_ptr<ResponseActionObject>(new RespondLaterAction()); | 235 return scoped_ptr<ResponseActionObject>(new RespondLaterAction()); |
| 233 } | 236 } |
| 234 | 237 |
| 235 void ExtensionFunction::Run() { | 238 void ExtensionFunction::Respond(ResponseValue result) { |
| 236 if (!RunImpl()) | 239 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 } | 240 } |
| 254 | 241 |
| 255 bool ExtensionFunction::ShouldSkipQuotaLimiting() const { | 242 bool ExtensionFunction::ShouldSkipQuotaLimiting() const { |
| 256 return false; | 243 return false; |
| 257 } | 244 } |
| 258 | 245 |
| 259 bool ExtensionFunction::HasOptionalArgument(size_t index) { | 246 bool ExtensionFunction::HasOptionalArgument(size_t index) { |
| 260 base::Value* value; | 247 base::Value* value; |
| 261 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); | 248 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); |
| 262 } | 249 } |
| 263 | 250 |
| 264 void ExtensionFunction::SendResponseImpl(bool success) { | 251 void ExtensionFunction::SendResponseImpl(bool success) { |
| 265 DCHECK(!response_callback_.is_null()); | 252 DCHECK(!response_callback_.is_null()); |
| 266 | 253 |
| 267 ResponseType type = success ? SUCCEEDED : FAILED; | 254 ResponseType type = success ? SUCCEEDED : FAILED; |
| 268 if (bad_message_) { | 255 if (bad_message_) { |
| 269 type = BAD_MESSAGE; | 256 type = BAD_MESSAGE; |
| 270 LOG(ERROR) << "Bad extension message " << name_; | 257 LOG(ERROR) << "Bad extension message " << name_; |
| 271 } | 258 } |
| 272 | 259 |
| 273 // If results were never set, we send an empty argument list. | 260 // If results were never set, we send an empty argument list. |
| 274 if (!results_) | 261 if (!results_) |
| 275 results_.reset(new base::ListValue()); | 262 results_.reset(new base::ListValue()); |
| 276 | 263 |
| 277 response_callback_.Run(type, *results_, GetError()); | 264 response_callback_.Run(type, *results_, GetError()); |
| 278 } | 265 } |
| 279 | 266 |
| 267 void ExtensionFunction::OnRespondingLater(ResponseValue value) { |
| 268 SendResponse(value->Apply()); |
| 269 } |
| 270 |
| 280 UIThreadExtensionFunction::UIThreadExtensionFunction() | 271 UIThreadExtensionFunction::UIThreadExtensionFunction() |
| 281 : render_view_host_(NULL), | 272 : render_view_host_(NULL), |
| 282 render_frame_host_(NULL), | 273 render_frame_host_(NULL), |
| 283 context_(NULL), | 274 context_(NULL), |
| 284 delegate_(NULL) { | 275 delegate_(NULL) { |
| 285 } | 276 } |
| 286 | 277 |
| 287 UIThreadExtensionFunction::~UIThreadExtensionFunction() { | 278 UIThreadExtensionFunction::~UIThreadExtensionFunction() { |
| 288 if (dispatcher() && render_view_host()) | 279 if (dispatcher() && render_view_host()) |
| 289 dispatcher()->OnExtensionFunctionCompleted(GetExtension()); | 280 dispatcher()->OnExtensionFunctionCompleted(GetExtension()); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 void IOThreadExtensionFunction::SendResponse(bool success) { | 353 void IOThreadExtensionFunction::SendResponse(bool success) { |
| 363 SendResponseImpl(success); | 354 SendResponseImpl(success); |
| 364 } | 355 } |
| 365 | 356 |
| 366 AsyncExtensionFunction::AsyncExtensionFunction() { | 357 AsyncExtensionFunction::AsyncExtensionFunction() { |
| 367 } | 358 } |
| 368 | 359 |
| 369 AsyncExtensionFunction::~AsyncExtensionFunction() { | 360 AsyncExtensionFunction::~AsyncExtensionFunction() { |
| 370 } | 361 } |
| 371 | 362 |
| 363 ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() { |
| 364 return RunAsync() ? RespondLater() : RespondNow(Error(error_)); |
| 365 } |
| 366 |
| 372 SyncExtensionFunction::SyncExtensionFunction() { | 367 SyncExtensionFunction::SyncExtensionFunction() { |
| 373 } | 368 } |
| 374 | 369 |
| 375 SyncExtensionFunction::~SyncExtensionFunction() { | 370 SyncExtensionFunction::~SyncExtensionFunction() { |
| 376 } | 371 } |
| 377 | 372 |
| 378 bool SyncExtensionFunction::RunImpl() { | 373 ExtensionFunction::ResponseAction SyncExtensionFunction::Run() { |
| 379 SendResponse(RunSync()); | 374 return RespondNow(RunSync() ? MultipleArguments(results_.get()) |
| 380 return true; | 375 : Error(error_)); |
| 381 } | 376 } |
| 382 | 377 |
| 383 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { | 378 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
| 384 } | 379 } |
| 385 | 380 |
| 386 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { | 381 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
| 387 } | 382 } |
| 388 | 383 |
| 389 bool SyncIOThreadExtensionFunction::RunImpl() { | 384 ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() { |
| 390 SendResponse(RunSync()); | 385 return RespondNow(RunSync() ? MultipleArguments(results_.get()) |
| 391 return true; | 386 : Error(error_)); |
| 392 } | 387 } |
| OLD | NEW |