| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_reader.h" | 7 #include "base/json_reader.h" |
| 8 #include "base/json_writer.h" | 8 #include "base/json_writer.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 10 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 11 | 11 |
| 12 void AsyncExtensionFunction::SetArgs(const std::string& args) { | 12 void AsyncExtensionFunction::SetArgs(const std::string& args) { |
| 13 DCHECK(!args_); // should only be called once | 13 DCHECK(!args_); // should only be called once |
| 14 if (!args.empty()) { | 14 if (!args.empty()) { |
| 15 JSONReader reader; | 15 JSONReader reader; |
| 16 args_ = reader.JsonToValue(args, false, false); | 16 args_ = reader.JsonToValue(args, false, false); |
| 17 | 17 |
| 18 // Since we do the serialization in the v8 extension, we should always get | 18 // Since we do the serialization in the v8 extension, we should always get |
| 19 // valid JSON. | 19 // valid JSON. |
| 20 if (!args_) { | 20 if (!args_) { |
| 21 DCHECK(false); | 21 DCHECK(false); |
| 22 return; | 22 return; |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 } | 25 } |
| 26 | 26 |
| 27 const std::string AsyncExtensionFunction::GetResult() { | 27 const std::string AsyncExtensionFunction::GetResult() { |
| 28 std::string json; | 28 std::string json; |
| 29 JSONWriter::Write(result_.get(), false, &json); | 29 // Some functions might not need to return any results. |
| 30 if (result_.get()) |
| 31 JSONWriter::Write(result_.get(), false, &json); |
| 30 return json; | 32 return json; |
| 31 } | 33 } |
| 32 | 34 |
| 33 void AsyncExtensionFunction::SendResponse(bool success) { | 35 void AsyncExtensionFunction::SendResponse(bool success) { |
| 34 if (bad_message_) { | 36 if (bad_message_) { |
| 35 dispatcher_->HandleBadMessage(this); | 37 dispatcher_->HandleBadMessage(this); |
| 36 } else { | 38 } else { |
| 37 dispatcher_->SendResponse(this, success); | 39 dispatcher_->SendResponse(this, success); |
| 38 } | 40 } |
| 39 } | 41 } |
| 40 | 42 |
| 41 std::string AsyncExtensionFunction::extension_id() { | 43 std::string AsyncExtensionFunction::extension_id() { |
| 42 return dispatcher_->extension_id(); | 44 return dispatcher_->extension_id(); |
| 43 } | 45 } |
| 44 | 46 |
| 45 Profile* AsyncExtensionFunction::profile() { | 47 Profile* AsyncExtensionFunction::profile() { |
| 46 return dispatcher_->profile(); | 48 return dispatcher_->profile(); |
| 47 } | 49 } |
| OLD | NEW |