Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / shell / renderer / ipc_echo.cc
blob5eb48007ccf19bc55150445e4d9f5da5bdffedd0
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"
17 namespace content {
19 class IPCEchoBindings : public gin::Wrappable<IPCEchoBindings> {
20 public:
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;
30 private:
31 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
32 v8::Isolate*) OVERRIDE;
34 base::WeakPtr<IPCEcho> native_;
37 IPCEchoBindings::IPCEchoBindings(base::WeakPtr<IPCEcho> native)
38 : native_(native) {
41 void IPCEchoBindings::RequestEcho(int id, int size) {
42 if (!native_)
43 return;
44 native_->RequestEcho(id, size);
47 int IPCEchoBindings::GetLastEchoId() const {
48 if (!native_)
49 return 0;
50 return native_->last_echo_id();
53 int IPCEchoBindings::GetLastEchoSize() const {
54 if (!native_)
55 return 0;
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);
73 // static
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,
83 IPC::Sender* sender,
84 int routing_id)
85 : weak_factory_(this),
86 document_(document), sender_(sender), routing_id_(routing_id),
87 last_echo_id_(0) {
90 IPCEcho::~IPCEcho() {
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) {
99 last_echo_id_ = id;
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