Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / content / shell / renderer / ipc_echo.cc
blob5bdb320ddad3165d775f8f4ca387b2271a1ee9fe
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 "base/memory/scoped_ptr.h"
9 #include "content/shell/common/shell_messages.h"
10 #include "gin/handle.h"
11 #include "gin/object_template_builder.h"
12 #include "gin/wrappable.h"
13 #include "ipc/ipc_sender.h"
14 #include "third_party/WebKit/public/web/WebDOMCustomEvent.h"
15 #include "third_party/WebKit/public/web/WebDOMEvent.h"
16 #include "third_party/WebKit/public/web/WebFrame.h"
17 #include "third_party/WebKit/public/web/WebKit.h"
18 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
20 namespace content {
22 class IPCEchoBindings : public gin::Wrappable<IPCEchoBindings> {
23 public:
24 static gin::WrapperInfo kWrapperInfo;
25 static void Install(base::WeakPtr<IPCEcho> echo, blink::WebFrame* frame);
27 explicit IPCEchoBindings(base::WeakPtr<IPCEcho>);
29 void RequestEcho(int id, int size);
30 int GetLastEchoId() const;
31 int GetLastEchoSize() const;
33 private:
34 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate*) override;
36 base::WeakPtr<IPCEcho> native_;
39 IPCEchoBindings::IPCEchoBindings(base::WeakPtr<IPCEcho> native)
40 : native_(native) {
43 void IPCEchoBindings::RequestEcho(int id, int size) {
44 if (!native_)
45 return;
46 native_->RequestEcho(id, size);
49 int IPCEchoBindings::GetLastEchoId() const {
50 if (!native_)
51 return 0;
52 return native_->last_echo_id();
55 int IPCEchoBindings::GetLastEchoSize() const {
56 if (!native_)
57 return 0;
58 return native_->last_echo_size();
61 gin::WrapperInfo IPCEchoBindings::kWrapperInfo = {
62 gin::kEmbedderNativeGin };
64 gin::ObjectTemplateBuilder IPCEchoBindings::GetObjectTemplateBuilder(
65 v8::Isolate* isolate) {
66 return gin::Wrappable<IPCEchoBindings>::GetObjectTemplateBuilder(isolate)
67 .SetMethod("requestEcho",
68 &IPCEchoBindings::RequestEcho)
69 .SetProperty("lastEchoId",
70 &IPCEchoBindings::GetLastEchoId)
71 .SetProperty("lastEchoSize",
72 &IPCEchoBindings::GetLastEchoSize);
75 // static
76 void IPCEchoBindings::Install(base::WeakPtr<IPCEcho> echo,
77 blink::WebFrame* frame) {
78 v8::Isolate* isolate = blink::mainThreadIsolate();
79 v8::HandleScope handle_scope(isolate);
80 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
81 if (context.IsEmpty())
82 return;
84 v8::Context::Scope context_scope(context);
86 IPCEchoBindings* wrapped = new IPCEchoBindings(echo);
87 gin::Handle<IPCEchoBindings> bindings = gin::CreateHandle(isolate, wrapped);
88 if (bindings.IsEmpty())
89 return;
90 v8::Local<v8::Object> global = context->Global();
91 v8::Local<v8::Value> v8_bindings = bindings.ToV8();
92 global->Set(gin::StringToV8(isolate, "ipcEcho"), v8_bindings);
95 IPCEcho::IPCEcho(blink::WebDocument document,
96 IPC::Sender* sender,
97 int routing_id)
98 : document_(document), sender_(sender), routing_id_(routing_id),
99 last_echo_id_(0),
100 weak_factory_(this) {
103 IPCEcho::~IPCEcho() {
106 void IPCEcho::RequestEcho(int id, int size) {
107 sender_->Send(new ShellViewHostMsg_EchoPing(
108 routing_id_, id, std::string(size, '*')));
111 void IPCEcho::DidRespondEcho(int id, int size) {
112 last_echo_id_ = id;
113 last_echo_size_ = size;
114 blink::WebString eventName = blink::WebString::fromUTF8("CustomEvent");
115 blink::WebString eventType = blink::WebString::fromUTF8("pong");
116 blink::WebDOMEvent event = document_.createEvent(eventName);
117 event.to<blink::WebDOMCustomEvent>().initCustomEvent(
118 eventType, false, false, blink::WebSerializedScriptValue());
119 document_.dispatchEvent(event);
122 void IPCEcho::Install(blink::WebFrame* frame) {
123 IPCEchoBindings::Install(weak_factory_.GetWeakPtr(), frame);
126 } // namespace content