OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/dom_automation_controller.h" | 5 #include "content/renderer/dom_automation_controller.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
7 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/metrics/statistics_recorder.h" |
8 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
9 #include "content/common/child_process_messages.h" | 13 #include "content/common/child_process_messages.h" |
10 #include "content/common/view_messages.h" | 14 #include "content/common/view_messages.h" |
11 #include "content/renderer/render_view_impl.h" | 15 |
12 #include "content/renderer/v8_value_converter_impl.h" | 16 using webkit_glue::CppArgumentList; |
13 #include "gin/handle.h" | 17 using webkit_glue::CppVariant; |
14 #include "gin/object_template_builder.h" | |
15 #include "third_party/WebKit/public/web/WebFrame.h" | |
16 #include "third_party/WebKit/public/web/WebKit.h" | |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 gin::WrapperInfo DomAutomationController::kWrapperInfo = { | 21 DomAutomationController::DomAutomationController() |
21 gin::kEmbedderNativeGin}; | 22 : sender_(NULL), |
22 | 23 routing_id_(MSG_ROUTING_NONE), |
23 // static | 24 automation_id_(MSG_ROUTING_NONE) { |
24 void DomAutomationController::Install(blink::WebFrame* frame) { | 25 BindCallback("send", base::Bind(&DomAutomationController::Send, |
25 v8::Isolate* isolate = blink::mainThreadIsolate(); | 26 base::Unretained(this))); |
26 v8::HandleScope handle_scope(isolate); | 27 BindCallback("setAutomationId", |
27 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 28 base::Bind(&DomAutomationController::SetAutomationId, |
28 if (context.IsEmpty()) | 29 base::Unretained(this))); |
29 return; | 30 BindCallback("sendJSON", base::Bind(&DomAutomationController::SendJSON, |
30 | 31 base::Unretained(this))); |
31 v8::Context::Scope context_scope(context); | 32 BindCallback("sendWithId", base::Bind(&DomAutomationController::SendWithId, |
32 | 33 base::Unretained(this))); |
33 gin::Handle<DomAutomationController> controller = | |
34 gin::CreateHandle(isolate, new DomAutomationController(frame)); | |
35 v8::Handle<v8::Object> global = context->Global(); | |
36 global->Set(gin::StringToV8(isolate, "domAutomationController"), | |
37 controller.ToV8()); | |
38 } | 34 } |
39 | 35 |
40 DomAutomationController::DomAutomationController(blink::WebFrame* frame) | 36 void DomAutomationController::Send(const CppArgumentList& args, |
41 : frame_(frame), automation_id_(MSG_ROUTING_NONE) {} | 37 CppVariant* result) { |
| 38 if (args.size() != 1) { |
| 39 result->SetNull(); |
| 40 return; |
| 41 } |
42 | 42 |
43 DomAutomationController::~DomAutomationController() {} | 43 if (automation_id_ == MSG_ROUTING_NONE) { |
| 44 result->SetNull(); |
| 45 return; |
| 46 } |
44 | 47 |
45 gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( | 48 if (!sender_) { |
46 v8::Isolate* isolate) { | 49 NOTREACHED(); |
47 return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( | 50 result->SetNull(); |
48 isolate) | 51 return; |
49 .SetMethod("send", &DomAutomationController::Send) | 52 } |
50 .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) | |
51 .SetMethod("sendJSON", &DomAutomationController::SendJSON) | |
52 .SetMethod("sendWithId", &DomAutomationController::SendWithId); | |
53 } | |
54 | |
55 bool DomAutomationController::Send(const gin::Arguments& args) { | |
56 if (automation_id_ == MSG_ROUTING_NONE) | |
57 return false; | |
58 | 53 |
59 std::string json; | 54 std::string json; |
60 JSONStringValueSerializer serializer(&json); | 55 JSONStringValueSerializer serializer(&json); |
61 scoped_ptr<base::Value> value; | 56 scoped_ptr<base::Value> value; |
62 | 57 |
63 // Warning: note that JSON officially requires the root-level object to be | 58 // Warning: note that JSON officially requires the root-level object to be |
64 // an object (e.g. {foo:3}) or an array, while here we're serializing | 59 // an object (e.g. {foo:3}) or an array, while here we're serializing |
65 // strings, bools, etc. to "JSON". This only works because (a) the JSON | 60 // strings, bools, etc. to "JSON". This only works because (a) the JSON |
66 // writer is lenient, and (b) on the receiving side we wrap the JSON string | 61 // writer is lenient, and (b) on the receiving side we wrap the JSON string |
67 // in square brackets, converting it to an array, then parsing it and | 62 // in square brackets, converting it to an array, then parsing it and |
68 // grabbing the 0th element to get the value out. | 63 // grabbing the 0th element to get the value out. |
69 if (args.PeekNext()->IsString() || args.PeekNext()->IsBoolean() || | 64 switch (args[0].type) { |
70 args.PeekNext()->IsNumber()) { | 65 case NPVariantType_String: { |
71 V8ValueConverterImpl conv; | 66 value.reset(new base::StringValue(args[0].ToString())); |
72 value.reset( | 67 break; |
73 conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext())); | 68 } |
74 } else { | 69 case NPVariantType_Bool: { |
75 return false; | 70 value.reset(new base::FundamentalValue(args[0].ToBoolean())); |
| 71 break; |
| 72 } |
| 73 case NPVariantType_Int32: { |
| 74 value.reset(new base::FundamentalValue(args[0].ToInt32())); |
| 75 break; |
| 76 } |
| 77 case NPVariantType_Double: { |
| 78 // The value that is sent back is an integer while it is treated |
| 79 // as a double in this binding. The reason being that KJS treats |
| 80 // any number value as a double. Refer for more details, |
| 81 // chrome/third_party/webkit/src/JavaScriptCore/bindings/c/c_utility.cpp |
| 82 value.reset(new base::FundamentalValue(args[0].ToInt32())); |
| 83 break; |
| 84 } |
| 85 default: { |
| 86 result->SetNull(); |
| 87 return; |
| 88 } |
76 } | 89 } |
77 | 90 |
78 if (!serializer.Serialize(*value)) | 91 if (!serializer.Serialize(*value)) { |
79 return false; | 92 result->SetNull(); |
| 93 return; |
| 94 } |
80 | 95 |
81 RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); | 96 bool succeeded = sender_->Send( |
82 bool succeeded = render_view->Send(new ViewHostMsg_DomOperationResponse( | 97 new ViewHostMsg_DomOperationResponse(routing_id_, json, automation_id_)); |
83 render_view->GetRoutingID(), json, automation_id_)); | 98 result->Set(succeeded); |
84 | 99 |
85 automation_id_ = MSG_ROUTING_NONE; | 100 automation_id_ = MSG_ROUTING_NONE; |
86 return succeeded; | |
87 } | 101 } |
88 | 102 |
89 bool DomAutomationController::SendJSON(const std::string& json) { | 103 void DomAutomationController::SendJSON(const CppArgumentList& args, |
90 if (automation_id_ == MSG_ROUTING_NONE) | 104 CppVariant* result) { |
91 return false; | 105 if (args.size() != 1) { |
92 RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); | 106 result->SetNull(); |
93 bool result = render_view->Send(new ViewHostMsg_DomOperationResponse( | 107 return; |
94 render_view->GetRoutingID(), json, automation_id_)); | 108 } |
| 109 |
| 110 if (automation_id_ == MSG_ROUTING_NONE) { |
| 111 result->SetNull(); |
| 112 return; |
| 113 } |
| 114 |
| 115 if (!sender_) { |
| 116 NOTREACHED(); |
| 117 result->SetNull(); |
| 118 return; |
| 119 } |
| 120 |
| 121 if (args[0].type != NPVariantType_String) { |
| 122 result->SetNull(); |
| 123 return; |
| 124 } |
| 125 |
| 126 std::string json = args[0].ToString(); |
| 127 result->Set(sender_->Send( |
| 128 new ViewHostMsg_DomOperationResponse(routing_id_, json, automation_id_))); |
95 | 129 |
96 automation_id_ = MSG_ROUTING_NONE; | 130 automation_id_ = MSG_ROUTING_NONE; |
97 return result; | |
98 } | 131 } |
99 | 132 |
100 bool DomAutomationController::SendWithId(int automation_id, | 133 void DomAutomationController::SendWithId(const CppArgumentList& args, |
101 const std::string& str) { | 134 CppVariant* result) { |
102 RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); | 135 if (args.size() != 2) { |
103 return render_view->Send(new ViewHostMsg_DomOperationResponse( | 136 result->SetNull(); |
104 render_view->GetRoutingID(), str, automation_id)); | 137 return; |
| 138 } |
| 139 |
| 140 if (!sender_) { |
| 141 NOTREACHED(); |
| 142 result->SetNull(); |
| 143 return; |
| 144 } |
| 145 |
| 146 if (!args[0].isNumber() || args[1].type != NPVariantType_String) { |
| 147 result->SetNull(); |
| 148 return; |
| 149 } |
| 150 |
| 151 result->Set(sender_->Send( |
| 152 new ViewHostMsg_DomOperationResponse(routing_id_, args[1].ToString(), |
| 153 args[0].ToInt32()))); |
105 } | 154 } |
106 | 155 |
107 bool DomAutomationController::SetAutomationId(int automation_id) { | 156 void DomAutomationController::SetAutomationId( |
108 automation_id_ = automation_id; | 157 const CppArgumentList& args, CppVariant* result) { |
109 return true; | 158 if (args.size() != 1) { |
| 159 result->SetNull(); |
| 160 return; |
| 161 } |
| 162 |
| 163 // The check here is for NumberType and not Int32 as |
| 164 // KJS::JSType only defines a NumberType (no Int32) |
| 165 if (!args[0].isNumber()) { |
| 166 result->SetNull(); |
| 167 return; |
| 168 } |
| 169 |
| 170 automation_id_ = args[0].ToInt32(); |
| 171 result->Set(true); |
110 } | 172 } |
111 | 173 |
112 } // namespace content | 174 } // namespace content |
OLD | NEW |