OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 #include "content/shell/renderer/ipc_echo.h" |
| 6 |
| 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" |
| 16 |
| 17 namespace content { |
| 18 |
| 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); |
| 23 |
| 24 explicit IPCEchoBindings(base::WeakPtr<IPCEcho>); |
| 25 |
| 26 void RequestEcho(int id, int size); |
| 27 int GetLastEchoId() const; |
| 28 int GetLastEchoSize() const; |
| 29 |
| 30 private: |
| 31 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 32 v8::Isolate*) OVERRIDE; |
| 33 |
| 34 base::WeakPtr<IPCEcho> native_; |
| 35 }; |
| 36 |
| 37 IPCEchoBindings::IPCEchoBindings(base::WeakPtr<IPCEcho> native) |
| 38 : native_(native) { |
| 39 } |
| 40 |
| 41 void IPCEchoBindings::RequestEcho(int id, int size) { |
| 42 if (!native_) |
| 43 return; |
| 44 native_->RequestEcho(id, size); |
| 45 } |
| 46 |
| 47 int IPCEchoBindings::GetLastEchoId() const { |
| 48 if (!native_) |
| 49 return 0; |
| 50 return native_->last_echo_id(); |
| 51 } |
| 52 |
| 53 int IPCEchoBindings::GetLastEchoSize() const { |
| 54 if (!native_) |
| 55 return 0; |
| 56 return native_->last_echo_size(); |
| 57 } |
| 58 |
| 59 gin::WrapperInfo IPCEchoBindings::kWrapperInfo = { |
| 60 gin::kEmbedderNativeGin }; |
| 61 |
| 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); |
| 71 } |
| 72 |
| 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); |
| 80 } |
| 81 |
| 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) { |
| 88 } |
| 89 |
| 90 IPCEcho::~IPCEcho() { |
| 91 } |
| 92 |
| 93 void IPCEcho::RequestEcho(int id, int size) { |
| 94 sender_->Send(new ShellViewHostMsg_EchoPing( |
| 95 routing_id_, id, std::string(size, '*'))); |
| 96 } |
| 97 |
| 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); |
| 107 } |
| 108 |
| 109 void IPCEcho::Install(blink::WebFrame* frame) { |
| 110 IPCEchoBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 111 } |
| 112 |
| 113 } // namespace content |
OLD | NEW |