| 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" |
| 11 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_observer.h" | 13 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "extensions/browser/extension_function_dispatcher.h" | 14 #include "extensions/browser/extension_function_dispatcher.h" |
| 15 #include "extensions/browser/extension_message_filter.h" | 15 #include "extensions/browser/extension_message_filter.h" |
| 16 #include "extensions/common/extension_api.h" | 16 #include "extensions/common/extension_api.h" |
| 17 #include "extensions/common/extension_messages.h" | 17 #include "extensions/common/extension_messages.h" |
| 18 | 18 |
| 19 using content::BrowserThread; | 19 using content::BrowserThread; |
| 20 using content::RenderViewHost; | 20 using content::RenderViewHost; |
| 21 using content::WebContents; | 21 using content::WebContents; |
| 22 using extensions::ExtensionAPI; | 22 using extensions::ExtensionAPI; |
| 23 using extensions::Feature; | 23 using extensions::Feature; |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 class MultipleArgumentsResponseValue | 27 class ArgumentListResponseValue |
| 28 : public ExtensionFunction::ResponseValueObject { | 28 : public ExtensionFunction::ResponseValueObject { |
| 29 public: | 29 public: |
| 30 MultipleArgumentsResponseValue(const std::string& function_name, | 30 ArgumentListResponseValue(const std::string& function_name, |
| 31 const char* title, | 31 const char* title, |
| 32 ExtensionFunction* function, | 32 ExtensionFunction* function, |
| 33 base::ListValue* result) | 33 scoped_ptr<base::ListValue> result) |
| 34 : function_name_(function_name), title_(title) { | 34 : function_name_(function_name), title_(title) { |
| 35 if (function->GetResultList()) { | 35 if (function->GetResultList()) { |
| 36 DCHECK_EQ(function->GetResultList(), result) | 36 DCHECK_EQ(function->GetResultList(), result.get()) |
| 37 << "The result set on this function (" << function_name_ << ") " | 37 << "The result set on this function (" << function_name_ << ") " |
| 38 << "either by calling SetResult() or directly modifying |result_| is " | 38 << "either by calling SetResult() or directly modifying |result_| is " |
| 39 << "different to the one passed to " << title_ << "(). " | 39 << "different to the one passed to " << title_ << "(). " |
| 40 << "The best way to fix this problem is to exclusively use " << title_ | 40 << "The best way to fix this problem is to exclusively use " << title_ |
| 41 << "(). SetResult() and |result_| are deprecated."; | 41 << "(). SetResult() and |result_| are deprecated."; |
| 42 } else { | 42 } else { |
| 43 function->SetResultList(make_scoped_ptr(result)); | 43 function->SetResultList(result.Pass()); |
| 44 } | 44 } |
| 45 // It would be nice to DCHECK(error.empty()) but some legacy extension | 45 // It would be nice to DCHECK(error.empty()) but some legacy extension |
| 46 // function implementations... I'm looking at chrome.input.ime... do this | 46 // function implementations... I'm looking at chrome.input.ime... do this |
| 47 // for some reason. | 47 // for some reason. |
| 48 } | 48 } |
| 49 | 49 |
| 50 virtual ~MultipleArgumentsResponseValue() {} | 50 virtual ~ArgumentListResponseValue() {} |
| 51 | 51 |
| 52 virtual bool Apply() OVERRIDE { return true; } | 52 virtual bool Apply() OVERRIDE { return true; } |
| 53 | 53 |
| 54 private: | 54 private: |
| 55 std::string function_name_; | 55 std::string function_name_; |
| 56 const char* title_; | 56 const char* title_; |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 class ErrorResponseValue : public ExtensionFunction::ResponseValueObject { | 59 class ErrorResponseValue : public ExtensionFunction::ResponseValueObject { |
| 60 public: | 60 public: |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 215 |
| 216 std::string ExtensionFunction::GetError() const { | 216 std::string ExtensionFunction::GetError() const { |
| 217 return error_; | 217 return error_; |
| 218 } | 218 } |
| 219 | 219 |
| 220 void ExtensionFunction::SetError(const std::string& error) { | 220 void ExtensionFunction::SetError(const std::string& error) { |
| 221 error_ = error; | 221 error_ = error; |
| 222 } | 222 } |
| 223 | 223 |
| 224 ExtensionFunction::ResponseValue ExtensionFunction::NoArguments() { | 224 ExtensionFunction::ResponseValue ExtensionFunction::NoArguments() { |
| 225 return ResponseValue(new MultipleArgumentsResponseValue( | 225 return ResponseValue(new ArgumentListResponseValue( |
| 226 name(), "NoArguments", this, new base::ListValue())); | 226 name(), "NoArguments", this, make_scoped_ptr(new base::ListValue()))); |
| 227 } | 227 } |
| 228 | 228 |
| 229 ExtensionFunction::ResponseValue ExtensionFunction::SingleArgument( | 229 ExtensionFunction::ResponseValue ExtensionFunction::OneArgument( |
| 230 base::Value* arg) { | 230 base::Value* arg) { |
| 231 base::ListValue* args = new base::ListValue(); | 231 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 232 args->Append(arg); | 232 args->Append(arg); |
| 233 return ResponseValue( | 233 return ResponseValue( |
| 234 new MultipleArgumentsResponseValue(name(), "SingleArgument", this, args)); | 234 new ArgumentListResponseValue(name(), "OneArgument", this, args.Pass())); |
| 235 } | 235 } |
| 236 | 236 |
| 237 ExtensionFunction::ResponseValue ExtensionFunction::MultipleArguments( | 237 ExtensionFunction::ResponseValue ExtensionFunction::TwoArguments( |
| 238 base::ListValue* args) { | 238 base::Value* arg1, |
| 239 return ResponseValue(new MultipleArgumentsResponseValue( | 239 base::Value* arg2) { |
| 240 name(), "MultipleArguments", this, args)); | 240 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 241 args->Append(arg1); |
| 242 args->Append(arg2); |
| 243 return ResponseValue( |
| 244 new ArgumentListResponseValue(name(), "TwoArguments", this, args.Pass())); |
| 245 } |
| 246 |
| 247 ExtensionFunction::ResponseValue ExtensionFunction::ArgumentList( |
| 248 scoped_ptr<base::ListValue> args) { |
| 249 return ResponseValue( |
| 250 new ArgumentListResponseValue(name(), "ArgumentList", this, args.Pass())); |
| 241 } | 251 } |
| 242 | 252 |
| 243 ExtensionFunction::ResponseValue ExtensionFunction::Error( | 253 ExtensionFunction::ResponseValue ExtensionFunction::Error( |
| 244 const std::string& error) { | 254 const std::string& error) { |
| 245 return ResponseValue(new ErrorResponseValue(this, error)); | 255 return ResponseValue(new ErrorResponseValue(this, error)); |
| 246 } | 256 } |
| 247 | 257 |
| 248 ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() { | 258 ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() { |
| 249 return ResponseValue(new BadMessageResponseValue(this)); | 259 return ResponseValue(new BadMessageResponseValue(this)); |
| 250 } | 260 } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 return false; | 410 return false; |
| 401 } | 411 } |
| 402 | 412 |
| 403 SyncExtensionFunction::SyncExtensionFunction() { | 413 SyncExtensionFunction::SyncExtensionFunction() { |
| 404 } | 414 } |
| 405 | 415 |
| 406 SyncExtensionFunction::~SyncExtensionFunction() { | 416 SyncExtensionFunction::~SyncExtensionFunction() { |
| 407 } | 417 } |
| 408 | 418 |
| 409 ExtensionFunction::ResponseAction SyncExtensionFunction::Run() { | 419 ExtensionFunction::ResponseAction SyncExtensionFunction::Run() { |
| 410 return RespondNow(RunSync() ? MultipleArguments(results_.get()) | 420 return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_)); |
| 411 : Error(error_)); | |
| 412 } | 421 } |
| 413 | 422 |
| 414 // static | 423 // static |
| 415 bool SyncExtensionFunction::ValidationFailure(SyncExtensionFunction* function) { | 424 bool SyncExtensionFunction::ValidationFailure(SyncExtensionFunction* function) { |
| 416 return false; | 425 return false; |
| 417 } | 426 } |
| 418 | 427 |
| 419 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { | 428 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
| 420 } | 429 } |
| 421 | 430 |
| 422 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { | 431 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
| 423 } | 432 } |
| 424 | 433 |
| 425 ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() { | 434 ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() { |
| 426 return RespondNow(RunSync() ? MultipleArguments(results_.get()) | 435 return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_)); |
| 427 : Error(error_)); | |
| 428 } | 436 } |
| 429 | 437 |
| 430 // static | 438 // static |
| 431 bool SyncIOThreadExtensionFunction::ValidationFailure( | 439 bool SyncIOThreadExtensionFunction::ValidationFailure( |
| 432 SyncIOThreadExtensionFunction* function) { | 440 SyncIOThreadExtensionFunction* function) { |
| 433 return false; | 441 return false; |
| 434 } | 442 } |
| OLD | NEW |