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/renderer/dom_ui_bindings.h" | 5 #include "chrome/renderer/dom_ui_bindings.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/scoped_ptr.h" | |
8 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
9 #include "base/values.h" | 10 #include "base/values.h" |
10 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
13 | 14 |
15 namespace { | |
16 | |
17 // Creates a Value which is a copy of the CppVariant |value|. All Object are | |
evanm
2011/01/25 18:24:56
typo: "All Object*s*"
| |
18 // treated as Lists for now since CppVariant does not distinguish arrays in any | |
19 // convenient way and we currently have no need of non array objects. | |
20 Value* CreateValueFromCppVariant(const CppVariant& value) { | |
21 if (value.isBool()) | |
22 return Value::CreateBooleanValue(value.ToBoolean()); | |
23 if (value.isDouble()) | |
24 return Value::CreateRealValue(value.ToDouble()); | |
25 if (value.isInt32()) | |
26 return Value::CreateIntegerValue(value.ToInt32()); | |
27 if (value.isString()) | |
28 return Value::CreateStringValue(value.ToString()); | |
29 | |
30 if (value.isObject()) { | |
31 // We currently assume all objects are arrays. | |
evanm
2011/01/25 18:24:56
Can we check this assumption somehow? Even with j
arv (Not doing code reviews)
2011/01/26 00:50:32
One would need to evaluate JS (which can be done t
| |
32 std::vector<CppVariant> vector = value.ToVector(); | |
33 ListValue* list = new ListValue(); | |
34 for (size_t i = 0; i < vector.size(); ++i) { | |
35 list->Append(CreateValueFromCppVariant(vector[i])); | |
36 } | |
37 return list; | |
38 } | |
39 | |
40 // Covers null and undefined. | |
41 return Value::CreateNullValue(); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
14 DOMBoundBrowserObject::DOMBoundBrowserObject() | 46 DOMBoundBrowserObject::DOMBoundBrowserObject() |
15 : sender_(NULL), | 47 : sender_(NULL), |
16 routing_id_(0) { | 48 routing_id_(0) { |
17 } | 49 } |
18 | 50 |
19 DOMBoundBrowserObject::~DOMBoundBrowserObject() { | 51 DOMBoundBrowserObject::~DOMBoundBrowserObject() { |
20 STLDeleteContainerPointers(properties_.begin(), properties_.end()); | 52 STLDeleteContainerPointers(properties_.begin(), properties_.end()); |
21 } | 53 } |
22 | 54 |
23 DOMUIBindings::DOMUIBindings() { | 55 DOMUIBindings::DOMUIBindings() { |
(...skipping 11 matching lines...) Expand all Loading... | |
35 // Require the first parameter to be the message name. | 67 // Require the first parameter to be the message name. |
36 if (!args[0].isString()) | 68 if (!args[0].isString()) |
37 return; | 69 return; |
38 const std::string message = args[0].ToString(); | 70 const std::string message = args[0].ToString(); |
39 | 71 |
40 // If they've provided an optional message parameter, convert that into JSON. | 72 // If they've provided an optional message parameter, convert that into JSON. |
41 std::string content; | 73 std::string content; |
42 if (args.size() == 2) { | 74 if (args.size() == 2) { |
43 if (!args[1].isObject()) | 75 if (!args[1].isObject()) |
44 return; | 76 return; |
45 std::vector<std::string> strings = args[1].ToStringVector(); | 77 |
46 ListValue value; | 78 scoped_ptr<Value> value(CreateValueFromCppVariant(args[1])); |
47 for (size_t i = 0; i < strings.size(); ++i) { | 79 base::JSONWriter::Write(value.get(), /* pretty_print= */ false, &content); |
48 value.Append(Value::CreateStringValue(strings[i])); | |
49 } | |
50 base::JSONWriter::Write(&value, /* pretty_print= */ false, &content); | |
51 } | 80 } |
52 | 81 |
53 // Retrieve the source frame's url | 82 // Retrieve the source frame's url |
54 GURL source_url; | 83 GURL source_url; |
55 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); | 84 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); |
56 if (webframe) | 85 if (webframe) |
57 source_url = webframe->url(); | 86 source_url = webframe->url(); |
58 | 87 |
59 // Send the message up to the browser. | 88 // Send the message up to the browser. |
60 sender()->Send( | 89 sender()->Send( |
61 new ViewHostMsg_DOMUISend(routing_id(), source_url, message, content)); | 90 new ViewHostMsg_DOMUISend(routing_id(), source_url, message, content)); |
62 } | 91 } |
63 | 92 |
64 void DOMBoundBrowserObject::SetProperty(const std::string& name, | 93 void DOMBoundBrowserObject::SetProperty(const std::string& name, |
65 const std::string& value) { | 94 const std::string& value) { |
66 CppVariant* cpp_value = new CppVariant; | 95 CppVariant* cpp_value = new CppVariant; |
67 cpp_value->Set(value); | 96 cpp_value->Set(value); |
68 BindProperty(name, cpp_value); | 97 BindProperty(name, cpp_value); |
69 properties_.push_back(cpp_value); | 98 properties_.push_back(cpp_value); |
70 } | 99 } |
OLD | NEW |