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