| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/extensions/extension_function.h" | 5 #include "chrome/browser/extensions/extension_function.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 9 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 10 #include "chrome/browser/extensions/extensions_service.h" | 10 #include "chrome/browser/extensions/extensions_service.h" |
| 11 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
| 12 | 12 |
| 13 ExtensionFunction::ExtensionFunction() |
| 14 : request_id_(-1), name_(""), has_callback_(false) { |
| 15 } |
| 16 |
| 17 ExtensionFunction::~ExtensionFunction() { |
| 18 } |
| 19 |
| 13 Extension* ExtensionFunction::GetExtension() { | 20 Extension* ExtensionFunction::GetExtension() { |
| 14 ExtensionsService* service = profile_->GetExtensionsService(); | 21 ExtensionsService* service = profile_->GetExtensionsService(); |
| 15 DCHECK(service); | 22 DCHECK(service); |
| 16 return service->GetExtensionById(extension_id_, false); | 23 return service->GetExtensionById(extension_id_, false); |
| 17 } | 24 } |
| 18 | 25 |
| 26 Browser* ExtensionFunction::GetCurrentBrowser() { |
| 27 return dispatcher()->GetCurrentBrowser(include_incognito_); |
| 28 } |
| 29 |
| 30 AsyncExtensionFunction::AsyncExtensionFunction() |
| 31 : args_(NULL), bad_message_(false) { |
| 32 } |
| 33 |
| 34 AsyncExtensionFunction::~AsyncExtensionFunction() { |
| 35 } |
| 36 |
| 19 void AsyncExtensionFunction::SetArgs(const ListValue* args) { | 37 void AsyncExtensionFunction::SetArgs(const ListValue* args) { |
| 20 DCHECK(!args_.get()); // Should only be called once. | 38 DCHECK(!args_.get()); // Should only be called once. |
| 21 args_.reset(static_cast<ListValue*>(args->DeepCopy())); | 39 args_.reset(static_cast<ListValue*>(args->DeepCopy())); |
| 22 } | 40 } |
| 23 | 41 |
| 24 const std::string AsyncExtensionFunction::GetResult() { | 42 const std::string AsyncExtensionFunction::GetResult() { |
| 25 std::string json; | 43 std::string json; |
| 26 // Some functions might not need to return any results. | 44 // Some functions might not need to return any results. |
| 27 if (result_.get()) | 45 if (result_.get()) |
| 28 base::JSONWriter::Write(result_.get(), false, &json); | 46 base::JSONWriter::Write(result_.get(), false, &json); |
| 29 return json; | 47 return json; |
| 30 } | 48 } |
| 31 | 49 |
| 50 const std::string AsyncExtensionFunction::GetError() { |
| 51 return error_; |
| 52 } |
| 53 |
| 54 void AsyncExtensionFunction::Run() { |
| 55 if (!RunImpl()) |
| 56 SendResponse(false); |
| 57 } |
| 58 |
| 32 void AsyncExtensionFunction::SendResponse(bool success) { | 59 void AsyncExtensionFunction::SendResponse(bool success) { |
| 33 if (!dispatcher()) | 60 if (!dispatcher()) |
| 34 return; | 61 return; |
| 35 if (bad_message_) { | 62 if (bad_message_) { |
| 36 dispatcher()->HandleBadMessage(this); | 63 dispatcher()->HandleBadMessage(this); |
| 37 } else { | 64 } else { |
| 38 dispatcher()->SendResponse(this, success); | 65 dispatcher()->SendResponse(this, success); |
| 39 } | 66 } |
| 40 } | 67 } |
| 41 | 68 |
| 42 bool AsyncExtensionFunction::HasOptionalArgument(size_t index) { | 69 bool AsyncExtensionFunction::HasOptionalArgument(size_t index) { |
| 43 Value* value; | 70 Value* value; |
| 44 return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL); | 71 return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL); |
| 45 } | 72 } |
| 73 |
| 74 SyncExtensionFunction::SyncExtensionFunction() { |
| 75 } |
| 76 |
| 77 SyncExtensionFunction::~SyncExtensionFunction() { |
| 78 } |
| 79 |
| 80 void SyncExtensionFunction::Run() { |
| 81 SendResponse(RunImpl()); |
| 82 } |
| OLD | NEW |