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/common/child_process_messages.h" | 9 #include "content/common/child_process_messages.h" |
10 #include "content/common/view_messages.h" | 10 #include "content/common/frame_messages.h" |
11 #include "content/renderer/render_view_impl.h" | 11 #include "content/renderer/render_view_impl.h" |
12 #include "content/renderer/v8_value_converter_impl.h" | 12 #include "content/renderer/v8_value_converter_impl.h" |
13 #include "gin/handle.h" | 13 #include "gin/handle.h" |
14 #include "gin/object_template_builder.h" | 14 #include "gin/object_template_builder.h" |
15 #include "third_party/WebKit/public/web/WebFrame.h" | 15 #include "third_party/WebKit/public/web/WebFrame.h" |
16 #include "third_party/WebKit/public/web/WebKit.h" | 16 #include "third_party/WebKit/public/web/WebKit.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 gin::WrapperInfo DomAutomationController::kWrapperInfo = { | 20 gin::WrapperInfo DomAutomationController::kWrapperInfo = { |
21 gin::kEmbedderNativeGin}; | 21 gin::kEmbedderNativeGin}; |
22 | 22 |
23 // static | 23 // static |
24 void DomAutomationController::Install(RenderViewImpl* render_view, | 24 void DomAutomationController::Install(RenderFrame* render_frame, |
25 blink::WebFrame* frame) { | 25 blink::WebFrame* frame) { |
26 v8::Isolate* isolate = blink::mainThreadIsolate(); | 26 v8::Isolate* isolate = blink::mainThreadIsolate(); |
27 v8::HandleScope handle_scope(isolate); | 27 v8::HandleScope handle_scope(isolate); |
28 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 28 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
29 if (context.IsEmpty()) | 29 if (context.IsEmpty()) |
30 return; | 30 return; |
31 | 31 |
32 v8::Context::Scope context_scope(context); | 32 v8::Context::Scope context_scope(context); |
33 | 33 |
34 gin::Handle<DomAutomationController> controller = | 34 gin::Handle<DomAutomationController> controller = |
35 gin::CreateHandle(isolate, new DomAutomationController(render_view)); | 35 gin::CreateHandle(isolate, new DomAutomationController(render_frame)); |
36 v8::Handle<v8::Object> global = context->Global(); | 36 v8::Handle<v8::Object> global = context->Global(); |
37 global->Set(gin::StringToV8(isolate, "domAutomationController"), | 37 global->Set(gin::StringToV8(isolate, "domAutomationController"), |
38 controller.ToV8()); | 38 controller.ToV8()); |
39 } | 39 } |
40 | 40 |
41 DomAutomationController::DomAutomationController(RenderViewImpl* render_view) | 41 DomAutomationController::DomAutomationController(RenderFrame* render_frame) |
42 : RenderViewObserver(render_view), automation_id_(MSG_ROUTING_NONE) {} | 42 : RenderFrameObserver(render_frame), automation_id_(MSG_ROUTING_NONE) {} |
43 | 43 |
44 DomAutomationController::~DomAutomationController() {} | 44 DomAutomationController::~DomAutomationController() {} |
45 | 45 |
46 gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( | 46 gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( |
47 v8::Isolate* isolate) { | 47 v8::Isolate* isolate) { |
48 return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( | 48 return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( |
49 isolate) | 49 isolate) |
50 .SetMethod("send", &DomAutomationController::SendMsg) | 50 .SetMethod("send", &DomAutomationController::SendMsg) |
51 .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) | 51 .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) |
52 .SetMethod("sendJSON", &DomAutomationController::SendJSON) | 52 .SetMethod("sendJSON", &DomAutomationController::SendJSON) |
53 .SetMethod("sendWithId", &DomAutomationController::SendWithId); | 53 .SetMethod("sendWithId", &DomAutomationController::SendWithId); |
54 } | 54 } |
55 | 55 |
56 void DomAutomationController::OnDestruct() {} | 56 void DomAutomationController::OnDestruct() {} |
57 | 57 |
58 bool DomAutomationController::SendMsg(const gin::Arguments& args) { | 58 bool DomAutomationController::SendMsg(const gin::Arguments& args) { |
59 if (!render_view()) | 59 if (!render_frame()) |
60 return false; | 60 return false; |
61 | 61 |
62 if (automation_id_ == MSG_ROUTING_NONE) | 62 if (automation_id_ == MSG_ROUTING_NONE) |
63 return false; | 63 return false; |
64 | 64 |
65 std::string json; | 65 std::string json; |
66 JSONStringValueSerializer serializer(&json); | 66 JSONStringValueSerializer serializer(&json); |
67 scoped_ptr<base::Value> value; | 67 scoped_ptr<base::Value> value; |
68 | 68 |
69 // Warning: note that JSON officially requires the root-level object to be | 69 // Warning: note that JSON officially requires the root-level object to be |
70 // an object (e.g. {foo:3}) or an array, while here we're serializing | 70 // an object (e.g. {foo:3}) or an array, while here we're serializing |
71 // strings, bools, etc. to "JSON". This only works because (a) the JSON | 71 // strings, bools, etc. to "JSON". This only works because (a) the JSON |
72 // writer is lenient, and (b) on the receiving side we wrap the JSON string | 72 // writer is lenient, and (b) on the receiving side we wrap the JSON string |
73 // in square brackets, converting it to an array, then parsing it and | 73 // in square brackets, converting it to an array, then parsing it and |
74 // grabbing the 0th element to get the value out. | 74 // grabbing the 0th element to get the value out. |
75 if (args.PeekNext()->IsString() || args.PeekNext()->IsBoolean() || | 75 if (args.PeekNext()->IsString() || args.PeekNext()->IsBoolean() || |
76 args.PeekNext()->IsNumber()) { | 76 args.PeekNext()->IsNumber()) { |
77 V8ValueConverterImpl conv; | 77 V8ValueConverterImpl conv; |
78 value.reset( | 78 value.reset( |
79 conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext())); | 79 conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext())); |
80 } else { | 80 } else { |
81 return false; | 81 return false; |
82 } | 82 } |
83 | 83 |
84 if (!serializer.Serialize(*value)) | 84 if (!serializer.Serialize(*value)) |
85 return false; | 85 return false; |
86 | 86 |
87 bool succeeded = Send( | 87 bool succeeded = Send(new FrameHostMsg_DomOperationResponse( |
88 new ViewHostMsg_DomOperationResponse(routing_id(), json, automation_id_)); | 88 routing_id(), json, automation_id_)); |
89 | 89 |
90 automation_id_ = MSG_ROUTING_NONE; | 90 automation_id_ = MSG_ROUTING_NONE; |
91 return succeeded; | 91 return succeeded; |
92 } | 92 } |
93 | 93 |
94 bool DomAutomationController::SendJSON(const std::string& json) { | 94 bool DomAutomationController::SendJSON(const std::string& json) { |
95 if (!render_view()) | 95 if (!render_frame()) |
96 return false; | 96 return false; |
97 | 97 |
98 if (automation_id_ == MSG_ROUTING_NONE) | 98 if (automation_id_ == MSG_ROUTING_NONE) |
99 return false; | 99 return false; |
100 bool result = Send( | 100 bool result = Send(new FrameHostMsg_DomOperationResponse( |
101 new ViewHostMsg_DomOperationResponse(routing_id(), json, automation_id_)); | 101 routing_id(), json, automation_id_)); |
102 | 102 |
103 automation_id_ = MSG_ROUTING_NONE; | 103 automation_id_ = MSG_ROUTING_NONE; |
104 return result; | 104 return result; |
105 } | 105 } |
106 | 106 |
107 bool DomAutomationController::SendWithId(int automation_id, | 107 bool DomAutomationController::SendWithId(int automation_id, |
108 const std::string& str) { | 108 const std::string& str) { |
109 if (!render_view()) | 109 if (!render_frame()) |
110 return false; | 110 return false; |
111 return Send( | 111 return Send( |
112 new ViewHostMsg_DomOperationResponse(routing_id(), str, automation_id)); | 112 new FrameHostMsg_DomOperationResponse(routing_id(), str, automation_id)); |
113 } | 113 } |
114 | 114 |
115 bool DomAutomationController::SetAutomationId(int automation_id) { | 115 bool DomAutomationController::SetAutomationId(int automation_id) { |
116 automation_id_ = automation_id; | 116 automation_id_ = automation_id; |
117 return true; | 117 return true; |
118 } | 118 } |
119 | 119 |
120 } // namespace content | 120 } // namespace content |
OLD | NEW |