OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "blimp/engine/browser/engine_render_widget_message_processor.h" |
| 6 |
| 7 #include "base/numerics/safe_conversions.h" |
| 8 #include "blimp/common/proto/blimp_message.pb.h" |
| 9 #include "blimp/common/proto/compositor.pb.h" |
| 10 #include "blimp/common/proto/input.pb.h" |
| 11 #include "blimp/common/proto/render_widget.pb.h" |
| 12 #include "net/base/net_errors.h" |
| 13 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 14 |
| 15 namespace blimp { |
| 16 |
| 17 EngineRenderWidgetMessageProcessor::EngineRenderWidgetMessageProcessor( |
| 18 BlimpMessageProcessor* render_widget_message_processor, |
| 19 BlimpMessageProcessor* compositor_message_processor) |
| 20 : render_widget_message_processor_(render_widget_message_processor), |
| 21 compositor_message_processor_(compositor_message_processor) { |
| 22 // TODO(dtrainor): Register as a BlimpMessageProcessor for |
| 23 // BlimpMessage::INPUT and BlimpMessage::COMPOSITOR messages. |
| 24 } |
| 25 |
| 26 EngineRenderWidgetMessageProcessor::~EngineRenderWidgetMessageProcessor() { |
| 27 // TODO(dtrainor): Unregister as a BlimpMessageProcessor for |
| 28 // BlimpMessage::INPUT and BlimpMessage::COMPOSITOR messages. |
| 29 } |
| 30 |
| 31 void EngineRenderWidgetMessageProcessor::OnRenderWidgetInitialized( |
| 32 const int tab_id) { |
| 33 render_widget_ids_[tab_id] = GetRenderWidgetId(tab_id) + 1; |
| 34 |
| 35 scoped_ptr<BlimpMessage> blimp_message(new BlimpMessage); |
| 36 blimp_message->set_target_tab_id(tab_id); |
| 37 |
| 38 RenderWidgetMessage* render_widget_message = |
| 39 blimp_message->mutable_render_widget(); |
| 40 |
| 41 render_widget_message->set_type(RenderWidgetMessage::INITIALIZE); |
| 42 render_widget_message->set_render_widget_id(GetRenderWidgetId(tab_id)); |
| 43 |
| 44 render_widget_message_processor_->ProcessMessage(std::move(blimp_message), |
| 45 net::CompletionCallback()); |
| 46 } |
| 47 |
| 48 void EngineRenderWidgetMessageProcessor::SendCompositorMessage( |
| 49 const int tab_id, |
| 50 const std::vector<uint8_t>& message) { |
| 51 scoped_ptr<BlimpMessage> blimp_message(new BlimpMessage); |
| 52 blimp_message->set_target_tab_id(tab_id); |
| 53 |
| 54 CompositorMessage* compositor_message = blimp_message->mutable_compositor(); |
| 55 uint32_t render_widget_id = GetRenderWidgetId(tab_id); |
| 56 DCHECK_LT(0U, render_widget_id); |
| 57 compositor_message->set_render_widget_id(render_widget_id); |
| 58 // TODO(dtrainor): Move the transport medium to std::string* and use |
| 59 // set_allocated_payload. |
| 60 compositor_message->set_payload(message.data(), |
| 61 base::checked_cast<int>(message.size())); |
| 62 |
| 63 render_widget_message_processor_->ProcessMessage(std::move(blimp_message), |
| 64 net::CompletionCallback()); |
| 65 } |
| 66 |
| 67 void EngineRenderWidgetMessageProcessor::SetDelegate( |
| 68 const int tab_id, RenderWidgetMessageDelegate* delegate) { |
| 69 DCHECK(!FindDelegate(tab_id)); |
| 70 delegates_[tab_id] = delegate; |
| 71 } |
| 72 |
| 73 void EngineRenderWidgetMessageProcessor::RemoveDelegate(const int tab_id) { |
| 74 DelegateMap::iterator it = delegates_.find(tab_id); |
| 75 if (it != delegates_.end()) |
| 76 delegates_.erase(it); |
| 77 } |
| 78 |
| 79 void EngineRenderWidgetMessageProcessor::ProcessMessage( |
| 80 scoped_ptr<BlimpMessage> message, |
| 81 const net::CompletionCallback& callback) { |
| 82 DCHECK(message->type() == BlimpMessage::INPUT || |
| 83 message->type() == BlimpMessage::COMPOSITOR); |
| 84 |
| 85 int target_tab_id = message->target_tab_id(); |
| 86 uint32_t render_widget_id = GetRenderWidgetId(target_tab_id); |
| 87 DCHECK_GT(render_widget_id, 0U); |
| 88 |
| 89 RenderWidgetMessageDelegate* delegate = FindDelegate(target_tab_id); |
| 90 DCHECK(delegate); |
| 91 |
| 92 switch (message->type()) { |
| 93 case BlimpMessage::INPUT: |
| 94 if (message->input().render_widget_id() == render_widget_id) { |
| 95 scoped_ptr<blink::WebInputEvent> event = |
| 96 input_message_processor_.ProcessMessage(message->input()); |
| 97 if (event) |
| 98 delegate->OnWebInputEvent(std::move(event)); |
| 99 } |
| 100 break; |
| 101 case BlimpMessage::COMPOSITOR: |
| 102 if (message->compositor().render_widget_id() == render_widget_id) { |
| 103 std::vector<uint8_t> payload(message->compositor().payload().size()); |
| 104 memcpy(payload.data(), |
| 105 message->compositor().payload().data(), |
| 106 payload.size()); |
| 107 delegate->OnCompositorMessageReceived(payload); |
| 108 } |
| 109 break; |
| 110 default: |
| 111 NOTREACHED(); |
| 112 } |
| 113 |
| 114 if (!callback.is_null()) { |
| 115 callback.Run(net::OK); |
| 116 } |
| 117 } |
| 118 |
| 119 EngineRenderWidgetMessageProcessor::RenderWidgetMessageDelegate* |
| 120 EngineRenderWidgetMessageProcessor::FindDelegate( |
| 121 const int tab_id) { |
| 122 DelegateMap::const_iterator it = delegates_.find(tab_id); |
| 123 if (it != delegates_.end()) |
| 124 return it->second; |
| 125 return nullptr; |
| 126 } |
| 127 |
| 128 uint32_t EngineRenderWidgetMessageProcessor::GetRenderWidgetId( |
| 129 const int tab_id) { |
| 130 RenderWidgetIdMap::const_iterator it = render_widget_ids_.find(tab_id); |
| 131 if (it != render_widget_ids_.end()) |
| 132 return it->second; |
| 133 return 0U; |
| 134 } |
| 135 |
| 136 } // namespace blimp |
OLD | NEW |