1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/renderer/ipc_echo.h"
7 #include "base/logging.h"
8 #include "content/shell/common/shell_messages.h"
9 #include "content/shell/renderer/binding_helpers.h"
10 #include "gin/object_template_builder.h"
11 #include "gin/wrappable.h"
12 #include "ipc/ipc_sender.h"
13 #include "third_party/WebKit/public/web/WebDOMCustomEvent.h"
14 #include "third_party/WebKit/public/web/WebDOMEvent.h"
15 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
19 class IPCEchoBindings
: public gin::Wrappable
<IPCEchoBindings
> {
21 static gin::WrapperInfo kWrapperInfo
;
22 static void Install(base::WeakPtr
<IPCEcho
> echo
, blink::WebFrame
* frame
);
24 explicit IPCEchoBindings(base::WeakPtr
<IPCEcho
>);
26 void RequestEcho(int id
, int size
);
27 int GetLastEchoId() const;
28 int GetLastEchoSize() const;
31 gin::ObjectTemplateBuilder
GetObjectTemplateBuilder(v8::Isolate
*) override
;
33 base::WeakPtr
<IPCEcho
> native_
;
36 IPCEchoBindings::IPCEchoBindings(base::WeakPtr
<IPCEcho
> native
)
40 void IPCEchoBindings::RequestEcho(int id
, int size
) {
43 native_
->RequestEcho(id
, size
);
46 int IPCEchoBindings::GetLastEchoId() const {
49 return native_
->last_echo_id();
52 int IPCEchoBindings::GetLastEchoSize() const {
55 return native_
->last_echo_size();
58 gin::WrapperInfo
IPCEchoBindings::kWrapperInfo
= {
59 gin::kEmbedderNativeGin
};
61 gin::ObjectTemplateBuilder
IPCEchoBindings::GetObjectTemplateBuilder(
62 v8::Isolate
* isolate
) {
63 return gin::Wrappable
<IPCEchoBindings
>::GetObjectTemplateBuilder(isolate
)
64 .SetMethod("requestEcho",
65 &IPCEchoBindings::RequestEcho
)
66 .SetProperty("lastEchoId",
67 &IPCEchoBindings::GetLastEchoId
)
68 .SetProperty("lastEchoSize",
69 &IPCEchoBindings::GetLastEchoSize
);
73 void IPCEchoBindings::Install(base::WeakPtr
<IPCEcho
> echo
,
74 blink::WebFrame
* frame
) {
75 std::vector
<std::string
> names
;
76 names
.push_back("ipcEcho");
77 return InstallAsWindowProperties(
78 new IPCEchoBindings(echo
), frame
, names
);
81 IPCEcho::IPCEcho(blink::WebDocument document
,
84 : document_(document
), sender_(sender
), routing_id_(routing_id
),
92 void IPCEcho::RequestEcho(int id
, int size
) {
93 sender_
->Send(new ShellViewHostMsg_EchoPing(
94 routing_id_
, id
, std::string(size
, '*')));
97 void IPCEcho::DidRespondEcho(int id
, int size
) {
99 last_echo_size_
= size
;
100 blink::WebString eventName
= blink::WebString::fromUTF8("CustomEvent");
101 blink::WebString eventType
= blink::WebString::fromUTF8("pong");
102 blink::WebDOMEvent event
= document_
.createEvent(eventName
);
103 event
.to
<blink::WebDOMCustomEvent
>().initCustomEvent(
104 eventType
, false, false, blink::WebSerializedScriptValue());
105 document_
.dispatchEvent(event
);
108 void IPCEcho::Install(blink::WebFrame
* frame
) {
109 IPCEchoBindings::Install(weak_factory_
.GetWeakPtr(), frame
);
112 } // namespace content