summaryrefslogtreecommitdiffstats
path: root/content/shell/renderer/ipc_echo.cc
blob: fe60c65f873fb445fc39dc227ec616c4bd8a09cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/shell/renderer/ipc_echo.h"

#include "base/logging.h"
#include "content/shell/common/shell_messages.h"
#include "content/shell/renderer/binding_helpers.h"
#include "gin/object_template_builder.h"
#include "gin/wrappable.h"
#include "ipc/ipc_sender.h"
#include "third_party/WebKit/public/web/WebDOMCustomEvent.h"
#include "third_party/WebKit/public/web/WebDOMEvent.h"
#include "third_party/WebKit/public/web/WebSerializedScriptValue.h"

namespace content {

class IPCEchoBindings : public gin::Wrappable<IPCEchoBindings> {
public:
  static gin::WrapperInfo kWrapperInfo;
  static void Install(base::WeakPtr<IPCEcho> echo, blink::WebFrame* frame);

  explicit IPCEchoBindings(base::WeakPtr<IPCEcho>);

  void RequestEcho(int id, int size);
  int GetLastEchoId() const;
  int GetLastEchoSize() const;

private:
 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate*) override;

  base::WeakPtr<IPCEcho> native_;
};

IPCEchoBindings::IPCEchoBindings(base::WeakPtr<IPCEcho> native)
    : native_(native) {
}

void IPCEchoBindings::RequestEcho(int id, int size) {
  if (!native_)
    return;
  native_->RequestEcho(id, size);
}

int IPCEchoBindings::GetLastEchoId() const {
  if (!native_)
    return 0;
  return native_->last_echo_id();
}

int IPCEchoBindings::GetLastEchoSize() const {
  if (!native_)
    return 0;
  return native_->last_echo_size();
}

gin::WrapperInfo IPCEchoBindings::kWrapperInfo = {
    gin::kEmbedderNativeGin };

gin::ObjectTemplateBuilder IPCEchoBindings::GetObjectTemplateBuilder(
    v8::Isolate* isolate) {
  return gin::Wrappable<IPCEchoBindings>::GetObjectTemplateBuilder(isolate)
      .SetMethod("requestEcho",
                 &IPCEchoBindings::RequestEcho)
      .SetProperty("lastEchoId",
                   &IPCEchoBindings::GetLastEchoId)
      .SetProperty("lastEchoSize",
                   &IPCEchoBindings::GetLastEchoSize);
}

// static
void IPCEchoBindings::Install(base::WeakPtr<IPCEcho> echo,
                              blink::WebFrame* frame) {
  std::vector<std::string> names;
  names.push_back("ipcEcho");
  return InstallAsWindowProperties(
      new IPCEchoBindings(echo), frame, names);
}

IPCEcho::IPCEcho(blink::WebDocument document,
                 IPC::Sender* sender,
                 int routing_id)
    : document_(document), sender_(sender), routing_id_(routing_id),
      last_echo_id_(0),
      weak_factory_(this) {
}

IPCEcho::~IPCEcho() {
}

void IPCEcho::RequestEcho(int id, int size) {
  sender_->Send(new ShellViewHostMsg_EchoPing(
      routing_id_, id, std::string(size, '*')));
}

void IPCEcho::DidRespondEcho(int id, int size) {
  last_echo_id_ = id;
  last_echo_size_ = size;
  blink::WebString eventName = blink::WebString::fromUTF8("CustomEvent");
  blink::WebString eventType = blink::WebString::fromUTF8("pong");
  blink::WebDOMEvent event = document_.createEvent(eventName);
  event.to<blink::WebDOMCustomEvent>().initCustomEvent(
      eventType, false, false, blink::WebSerializedScriptValue());
  document_.dispatchEvent(event);
}

void IPCEcho::Install(blink::WebFrame* frame) {
  IPCEchoBindings::Install(weak_factory_.GetWeakPtr(), frame);
}

}  // namespace content