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 virtual gin::ObjectTemplateBuilder
GetObjectTemplateBuilder(
32 v8::Isolate
*) OVERRIDE
;
34 base::WeakPtr
<IPCEcho
> native_
;
37 IPCEchoBindings::IPCEchoBindings(base::WeakPtr
<IPCEcho
> native
)
41 void IPCEchoBindings::RequestEcho(int id
, int size
) {
44 native_
->RequestEcho(id
, size
);
47 int IPCEchoBindings::GetLastEchoId() const {
50 return native_
->last_echo_id();
53 int IPCEchoBindings::GetLastEchoSize() const {
56 return native_
->last_echo_size();
59 gin::WrapperInfo
IPCEchoBindings::kWrapperInfo
= {
60 gin::kEmbedderNativeGin
};
62 gin::ObjectTemplateBuilder
IPCEchoBindings::GetObjectTemplateBuilder(
63 v8::Isolate
* isolate
) {
64 return gin::Wrappable
<IPCEchoBindings
>::GetObjectTemplateBuilder(isolate
)
65 .SetMethod("requestEcho",
66 &IPCEchoBindings::RequestEcho
)
67 .SetProperty("lastEchoId",
68 &IPCEchoBindings::GetLastEchoId
)
69 .SetProperty("lastEchoSize",
70 &IPCEchoBindings::GetLastEchoSize
);
74 void IPCEchoBindings::Install(base::WeakPtr
<IPCEcho
> echo
,
75 blink::WebFrame
* frame
) {
76 std::vector
<std::string
> names
;
77 names
.push_back("ipcEcho");
78 return InstallAsWindowProperties(
79 new IPCEchoBindings(echo
), frame
, names
);
82 IPCEcho::IPCEcho(blink::WebDocument document
,
85 : weak_factory_(this),
86 document_(document
), sender_(sender
), routing_id_(routing_id
),
93 void IPCEcho::RequestEcho(int id
, int size
) {
94 sender_
->Send(new ShellViewHostMsg_EchoPing(
95 routing_id_
, id
, std::string(size
, '*')));
98 void IPCEcho::DidRespondEcho(int id
, int size
) {
100 last_echo_size_
= size
;
101 blink::WebString eventName
= blink::WebString::fromUTF8("CustomEvent");
102 blink::WebString eventType
= blink::WebString::fromUTF8("pong");
103 blink::WebDOMEvent event
= document_
.createEvent(eventName
);
104 event
.to
<blink::WebDOMCustomEvent
>().initCustomEvent(
105 eventType
, false, false, blink::WebSerializedScriptValue());
106 document_
.dispatchEvent(event
);
109 void IPCEcho::Install(blink::WebFrame
* frame
) {
110 IPCEchoBindings::Install(weak_factory_
.GetWeakPtr(), frame
);
113 } // namespace content