| 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/json/json_string_value_serializer.h" | 7 #include "base/json/json_string_value_serializer.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "content/child/v8_value_converter_impl.h" | 9 #include "content/child/v8_value_converter_impl.h" |
| 10 #include "content/common/child_process_messages.h" | 10 #include "content/common/child_process_messages.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 gin::CreateHandle(isolate, new DomAutomationController(render_frame)); | 35 gin::CreateHandle(isolate, new DomAutomationController(render_frame)); |
| 36 if (controller.IsEmpty()) | 36 if (controller.IsEmpty()) |
| 37 return; | 37 return; |
| 38 | 38 |
| 39 v8::Local<v8::Object> global = context->Global(); | 39 v8::Local<v8::Object> global = context->Global(); |
| 40 global->Set(gin::StringToV8(isolate, "domAutomationController"), | 40 global->Set(gin::StringToV8(isolate, "domAutomationController"), |
| 41 controller.ToV8()); | 41 controller.ToV8()); |
| 42 } | 42 } |
| 43 | 43 |
| 44 DomAutomationController::DomAutomationController(RenderFrame* render_frame) | 44 DomAutomationController::DomAutomationController(RenderFrame* render_frame) |
| 45 : RenderFrameObserver(render_frame), automation_id_(MSG_ROUTING_NONE) {} | 45 : RenderFrameObserver(render_frame) {} |
| 46 | 46 |
| 47 DomAutomationController::~DomAutomationController() {} | 47 DomAutomationController::~DomAutomationController() {} |
| 48 | 48 |
| 49 gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( | 49 gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( |
| 50 v8::Isolate* isolate) { | 50 v8::Isolate* isolate) { |
| 51 return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( | 51 return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( |
| 52 isolate) | 52 isolate) |
| 53 .SetMethod("send", &DomAutomationController::SendMsg) | 53 .SetMethod("send", &DomAutomationController::SendMsg) |
| 54 .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) | 54 .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) |
| 55 .SetMethod("sendJSON", &DomAutomationController::SendJSON) | 55 .SetMethod("sendJSON", &DomAutomationController::SendJSON) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 77 | 77 |
| 78 v8::Local<v8::Object> global = context->Global(); | 78 v8::Local<v8::Object> global = context->Global(); |
| 79 global->Set(gin::StringToV8(isolate, "domAutomationController"), | 79 global->Set(gin::StringToV8(isolate, "domAutomationController"), |
| 80 controller.ToV8()); | 80 controller.ToV8()); |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool DomAutomationController::SendMsg(const gin::Arguments& args) { | 83 bool DomAutomationController::SendMsg(const gin::Arguments& args) { |
| 84 if (!render_frame()) | 84 if (!render_frame()) |
| 85 return false; | 85 return false; |
| 86 | 86 |
| 87 if (automation_id_ == MSG_ROUTING_NONE) | |
| 88 return false; | |
| 89 | |
| 90 std::string json; | 87 std::string json; |
| 91 JSONStringValueSerializer serializer(&json); | 88 JSONStringValueSerializer serializer(&json); |
| 92 std::unique_ptr<base::Value> value; | 89 std::unique_ptr<base::Value> value; |
| 93 | 90 |
| 94 // Warning: note that JSON officially requires the root-level object to be | 91 // Warning: note that JSON officially requires the root-level object to be |
| 95 // an object (e.g. {foo:3}) or an array, while here we're serializing | 92 // an object (e.g. {foo:3}) or an array, while here we're serializing |
| 96 // strings, bools, etc. to "JSON". This only works because (a) the JSON | 93 // strings, bools, etc. to "JSON". This only works because (a) the JSON |
| 97 // writer is lenient, and (b) on the receiving side we wrap the JSON string | 94 // writer is lenient, and (b) on the receiving side we wrap the JSON string |
| 98 // in square brackets, converting it to an array, then parsing it and | 95 // in square brackets, converting it to an array, then parsing it and |
| 99 // grabbing the 0th element to get the value out. | 96 // grabbing the 0th element to get the value out. |
| 100 if (!args.PeekNext().IsEmpty()) { | 97 if (!args.PeekNext().IsEmpty()) { |
| 101 V8ValueConverterImpl conv; | 98 V8ValueConverterImpl conv; |
| 102 value = | 99 value = |
| 103 conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext()); | 100 conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext()); |
| 104 } else { | 101 } else { |
| 105 NOTREACHED() << "No arguments passed to domAutomationController.send"; | 102 NOTREACHED() << "No arguments passed to domAutomationController.send"; |
| 106 return false; | 103 return false; |
| 107 } | 104 } |
| 108 | 105 |
| 109 if (!value || !serializer.Serialize(*value)) | 106 if (!value || !serializer.Serialize(*value)) |
| 110 return false; | 107 return false; |
| 111 | 108 |
| 112 bool succeeded = Send(new FrameHostMsg_DomOperationResponse( | 109 bool succeeded = Send(new FrameHostMsg_DomOperationResponse( |
| 113 routing_id(), json)); | 110 routing_id(), json)); |
| 114 | 111 |
| 115 automation_id_ = MSG_ROUTING_NONE; | |
| 116 return succeeded; | 112 return succeeded; |
| 117 } | 113 } |
| 118 | 114 |
| 119 bool DomAutomationController::SendJSON(const std::string& json) { | 115 bool DomAutomationController::SendJSON(const std::string& json) { |
| 120 if (!render_frame()) | 116 if (!render_frame()) |
| 121 return false; | 117 return false; |
| 122 | 118 |
| 123 if (automation_id_ == MSG_ROUTING_NONE) | |
| 124 return false; | |
| 125 bool result = Send(new FrameHostMsg_DomOperationResponse( | 119 bool result = Send(new FrameHostMsg_DomOperationResponse( |
| 126 routing_id(), json)); | 120 routing_id(), json)); |
| 127 | 121 |
| 128 automation_id_ = MSG_ROUTING_NONE; | |
| 129 return result; | 122 return result; |
| 130 } | 123 } |
| 131 | 124 |
| 132 bool DomAutomationController::SendWithId(int automation_id, | 125 bool DomAutomationController::SendWithId(int automation_id, |
| 133 const std::string& str) { | 126 const std::string& str) { |
| 134 if (!render_frame()) | 127 if (!render_frame()) |
| 135 return false; | 128 return false; |
| 136 return Send( | 129 return Send( |
| 137 new FrameHostMsg_DomOperationResponse(routing_id(), str)); | 130 new FrameHostMsg_DomOperationResponse(routing_id(), str)); |
| 138 } | 131 } |
| 139 | 132 |
| 140 bool DomAutomationController::SetAutomationId(int automation_id) { | 133 bool DomAutomationController::SetAutomationId(int automation_id) { |
| 141 automation_id_ = automation_id; | |
| 142 return true; | 134 return true; |
| 143 } | 135 } |
| 144 | 136 |
| 145 } // namespace content | 137 } // namespace content |
| OLD | NEW |