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/client/compositor/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 "cc/proto/compositor_message.pb.h" |
| 13 #include "net/base/net_errors.h" |
| 14 |
| 15 namespace blimp { |
| 16 |
| 17 RenderWidgetMessageProcessor::RenderWidgetMessageProcessor( |
| 18 BlimpMessageProcessor* input_message_processor, |
| 19 BlimpMessageProcessor* compositor_message_processor) |
| 20 : input_message_processor_(input_message_processor), |
| 21 compositor_message_processor_(compositor_message_processor) { |
| 22 // TODO(dtrainor): Register as a BlimpMessageProcessor for |
| 23 // BlimpMessage::RENDER_WIDGET and BlimpMessage::COMPOSITOR messages. |
| 24 } |
| 25 |
| 26 RenderWidgetMessageProcessor::~RenderWidgetMessageProcessor() { |
| 27 // TODO(dtrainor): Unregister as a BlimpMessageProcessor for |
| 28 // BlimpMessage::RENDER_WIDGET and BlimpMessage::COMPOSITOR messages. |
| 29 } |
| 30 |
| 31 void RenderWidgetMessageProcessor::SendInputEvent( |
| 32 const int tab_id, const blink::WebInputEvent& event) { |
| 33 scoped_ptr<BlimpMessage> blimp_message = |
| 34 input_message_generator_.GenerateMessage(event); |
| 35 |
| 36 // Don't send unsupported WebInputEvents. |
| 37 if (!blimp_message) |
| 38 return; |
| 39 |
| 40 blimp_message->set_target_tab_id(tab_id); |
| 41 blimp_message->mutable_input()->set_render_widget_id( |
| 42 GetRenderWidgetId(tab_id)); |
| 43 |
| 44 input_message_processor_->ProcessMessage(std::move(blimp_message), |
| 45 net::CompletionCallback()); |
| 46 } |
| 47 |
| 48 void RenderWidgetMessageProcessor::SendCompositorMessage( |
| 49 const int tab_id, |
| 50 const cc::proto::CompositorMessage& 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 compositor_message->mutable_payload()->resize( |
| 59 base::checked_cast<size_t>(message.ByteSize())); |
| 60 if (message.SerializeToString(compositor_message->mutable_payload())) { |
| 61 compositor_message_processor_->ProcessMessage(std::move(blimp_message), |
| 62 net::CompletionCallback()); |
| 63 } else { |
| 64 LOG(ERROR) << "Unable to serialize compositor proto."; |
| 65 } |
| 66 } |
| 67 |
| 68 void RenderWidgetMessageProcessor::SetDelegate( |
| 69 const int tab_id, RenderWidgetMessageDelegate* delegate) { |
| 70 DCHECK(!FindDelegate(tab_id)); |
| 71 delegates_[tab_id] = delegate; |
| 72 } |
| 73 |
| 74 void RenderWidgetMessageProcessor::RemoveDelegate(const int tab_id) { |
| 75 DelegateMap::iterator it = delegates_.find(tab_id); |
| 76 if (it != delegates_.end()) |
| 77 delegates_.erase(it); |
| 78 } |
| 79 |
| 80 void RenderWidgetMessageProcessor::ProcessMessage( |
| 81 scoped_ptr<BlimpMessage> message, |
| 82 const net::CompletionCallback& callback) { |
| 83 DCHECK(message->type() == BlimpMessage::RENDER_WIDGET || |
| 84 message->type() == BlimpMessage::COMPOSITOR); |
| 85 |
| 86 int target_tab_id = message->target_tab_id(); |
| 87 RenderWidgetMessageDelegate* delegate = FindDelegate(target_tab_id); |
| 88 DCHECK(delegate); |
| 89 |
| 90 switch (message->type()) { |
| 91 case BlimpMessage::RENDER_WIDGET: |
| 92 if (message->render_widget().type() == RenderWidgetMessage::INITIALIZE) { |
| 93 render_widget_ids_[target_tab_id] = |
| 94 message->render_widget().render_widget_id(); |
| 95 delegate->OnRenderWidgetInitialized(); |
| 96 } |
| 97 break; |
| 98 case BlimpMessage::COMPOSITOR: |
| 99 { |
| 100 DCHECK_EQ(message->compositor().render_widget_id(), |
| 101 GetRenderWidgetId(target_tab_id)); |
| 102 scoped_ptr<cc::proto::CompositorMessage> payload( |
| 103 new cc::proto::CompositorMessage); |
| 104 if (payload->ParseFromString(message->compositor().payload())) { |
| 105 delegate->OnCompositorMessageReceived(std::move(payload)); |
| 106 } |
| 107 } |
| 108 break; |
| 109 default: |
| 110 NOTIMPLEMENTED(); |
| 111 } |
| 112 |
| 113 if (!callback.is_null()) { |
| 114 callback.Run(net::OK); |
| 115 } |
| 116 } |
| 117 |
| 118 RenderWidgetMessageProcessor::RenderWidgetMessageDelegate* |
| 119 RenderWidgetMessageProcessor::FindDelegate( |
| 120 const int tab_id) { |
| 121 DelegateMap::const_iterator it = delegates_.find(tab_id); |
| 122 if (it != delegates_.end()) |
| 123 return it->second; |
| 124 return nullptr; |
| 125 } |
| 126 |
| 127 uint32_t RenderWidgetMessageProcessor::GetRenderWidgetId(const int tab_id) { |
| 128 RenderWidgetIdMap::const_iterator it = render_widget_ids_.find(tab_id); |
| 129 if (it != render_widget_ids_.end()) |
| 130 return it->second; |
| 131 return 0U; |
| 132 } |
| 133 |
| 134 } // namespace blimp |
OLD | NEW |