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/view_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(blink::WebFrame* frame) { | 24 void DomAutomationController::Install(RenderViewImpl* render_view, |
| 25 blink::WebFrame* frame) { |
25 v8::Isolate* isolate = blink::mainThreadIsolate(); | 26 v8::Isolate* isolate = blink::mainThreadIsolate(); |
26 v8::HandleScope handle_scope(isolate); | 27 v8::HandleScope handle_scope(isolate); |
27 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 28 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
28 if (context.IsEmpty()) | 29 if (context.IsEmpty()) |
29 return; | 30 return; |
30 | 31 |
31 v8::Context::Scope context_scope(context); | 32 v8::Context::Scope context_scope(context); |
32 | 33 |
33 gin::Handle<DomAutomationController> controller = | 34 gin::Handle<DomAutomationController> controller = |
34 gin::CreateHandle(isolate, new DomAutomationController(frame)); | 35 gin::CreateHandle(isolate, new DomAutomationController(render_view)); |
35 v8::Handle<v8::Object> global = context->Global(); | 36 v8::Handle<v8::Object> global = context->Global(); |
36 global->Set(gin::StringToV8(isolate, "domAutomationController"), | 37 global->Set(gin::StringToV8(isolate, "domAutomationController"), |
37 controller.ToV8()); | 38 controller.ToV8()); |
38 } | 39 } |
39 | 40 |
40 DomAutomationController::DomAutomationController(blink::WebFrame* frame) | 41 DomAutomationController::DomAutomationController(RenderViewImpl* render_view) |
41 : frame_(frame), automation_id_(MSG_ROUTING_NONE) {} | 42 : RenderViewObserver(render_view), automation_id_(MSG_ROUTING_NONE) {} |
42 | 43 |
43 DomAutomationController::~DomAutomationController() {} | 44 DomAutomationController::~DomAutomationController() {} |
44 | 45 |
45 gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( | 46 gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( |
46 v8::Isolate* isolate) { | 47 v8::Isolate* isolate) { |
47 return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( | 48 return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( |
48 isolate) | 49 isolate) |
49 .SetMethod("send", &DomAutomationController::Send) | 50 .SetMethod("send", &DomAutomationController::SendMsg) |
50 .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) | 51 .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) |
51 .SetMethod("sendJSON", &DomAutomationController::SendJSON) | 52 .SetMethod("sendJSON", &DomAutomationController::SendJSON) |
52 .SetMethod("sendWithId", &DomAutomationController::SendWithId); | 53 .SetMethod("sendWithId", &DomAutomationController::SendWithId); |
53 } | 54 } |
54 | 55 |
55 bool DomAutomationController::Send(const gin::Arguments& args) { | 56 void DomAutomationController::OnDestruct() {} |
| 57 |
| 58 bool DomAutomationController::SendMsg(const gin::Arguments& args) { |
| 59 if (!render_view()) |
| 60 return false; |
| 61 |
56 if (automation_id_ == MSG_ROUTING_NONE) | 62 if (automation_id_ == MSG_ROUTING_NONE) |
57 return false; | 63 return false; |
58 | 64 |
59 std::string json; | 65 std::string json; |
60 JSONStringValueSerializer serializer(&json); | 66 JSONStringValueSerializer serializer(&json); |
61 scoped_ptr<base::Value> value; | 67 scoped_ptr<base::Value> value; |
62 | 68 |
63 // 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 |
64 // 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 |
65 // 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 |
66 // 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 |
67 // 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 |
68 // grabbing the 0th element to get the value out. | 74 // grabbing the 0th element to get the value out. |
69 if (args.PeekNext()->IsString() || args.PeekNext()->IsBoolean() || | 75 if (args.PeekNext()->IsString() || args.PeekNext()->IsBoolean() || |
70 args.PeekNext()->IsNumber()) { | 76 args.PeekNext()->IsNumber()) { |
71 V8ValueConverterImpl conv; | 77 V8ValueConverterImpl conv; |
72 value.reset( | 78 value.reset( |
73 conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext())); | 79 conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext())); |
74 } else { | 80 } else { |
75 return false; | 81 return false; |
76 } | 82 } |
77 | 83 |
78 if (!serializer.Serialize(*value)) | 84 if (!serializer.Serialize(*value)) |
79 return false; | 85 return false; |
80 | 86 |
81 RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); | 87 bool succeeded = Send( |
82 bool succeeded = render_view->Send(new ViewHostMsg_DomOperationResponse( | 88 new ViewHostMsg_DomOperationResponse(routing_id(), json, automation_id_)); |
83 render_view->GetRoutingID(), json, automation_id_)); | |
84 | 89 |
85 automation_id_ = MSG_ROUTING_NONE; | 90 automation_id_ = MSG_ROUTING_NONE; |
86 return succeeded; | 91 return succeeded; |
87 } | 92 } |
88 | 93 |
89 bool DomAutomationController::SendJSON(const std::string& json) { | 94 bool DomAutomationController::SendJSON(const std::string& json) { |
| 95 if (!render_view()) |
| 96 return false; |
| 97 |
90 if (automation_id_ == MSG_ROUTING_NONE) | 98 if (automation_id_ == MSG_ROUTING_NONE) |
91 return false; | 99 return false; |
92 RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); | 100 bool result = Send( |
93 bool result = render_view->Send(new ViewHostMsg_DomOperationResponse( | 101 new ViewHostMsg_DomOperationResponse(routing_id(), json, automation_id_)); |
94 render_view->GetRoutingID(), json, automation_id_)); | |
95 | 102 |
96 automation_id_ = MSG_ROUTING_NONE; | 103 automation_id_ = MSG_ROUTING_NONE; |
97 return result; | 104 return result; |
98 } | 105 } |
99 | 106 |
100 bool DomAutomationController::SendWithId(int automation_id, | 107 bool DomAutomationController::SendWithId(int automation_id, |
101 const std::string& str) { | 108 const std::string& str) { |
102 RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); | 109 if (!render_view()) |
103 return render_view->Send(new ViewHostMsg_DomOperationResponse( | 110 return false; |
104 render_view->GetRoutingID(), str, automation_id)); | 111 return Send( |
| 112 new ViewHostMsg_DomOperationResponse(routing_id(), str, automation_id)); |
105 } | 113 } |
106 | 114 |
107 bool DomAutomationController::SetAutomationId(int automation_id) { | 115 bool DomAutomationController::SetAutomationId(int automation_id) { |
108 automation_id_ = automation_id; | 116 automation_id_ = automation_id; |
109 return true; | 117 return true; |
110 } | 118 } |
111 | 119 |
112 } // namespace content | 120 } // namespace content |
OLD | NEW |