Home | History | Annotate | Download | only in protocol
      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 "remoting/protocol/host_control_dispatcher.h"
      6 
      7 #include "base/callback_helpers.h"
      8 #include "base/message_loop/message_loop_proxy.h"
      9 #include "net/socket/stream_socket.h"
     10 #include "remoting/base/constants.h"
     11 #include "remoting/proto/control.pb.h"
     12 #include "remoting/proto/internal.pb.h"
     13 #include "remoting/protocol/clipboard_stub.h"
     14 #include "remoting/protocol/host_stub.h"
     15 #include "remoting/protocol/util.h"
     16 
     17 namespace remoting {
     18 namespace protocol {
     19 
     20 HostControlDispatcher::HostControlDispatcher()
     21     : ChannelDispatcherBase(kControlChannelName),
     22       clipboard_stub_(NULL),
     23       host_stub_(NULL) {
     24 }
     25 
     26 HostControlDispatcher::~HostControlDispatcher() {
     27   writer_.Close();
     28 }
     29 
     30 void HostControlDispatcher::OnInitialized() {
     31   reader_.Init(channel(), base::Bind(
     32       &HostControlDispatcher::OnMessageReceived, base::Unretained(this)));
     33   writer_.Init(channel(), BufferedSocketWriter::WriteFailedCallback());
     34 }
     35 
     36 void HostControlDispatcher::SetCapabilities(
     37     const Capabilities& capabilities) {
     38   ControlMessage message;
     39   message.mutable_capabilities()->CopyFrom(capabilities);
     40   writer_.Write(SerializeAndFrameMessage(message), base::Closure());
     41 }
     42 
     43 void HostControlDispatcher::SetPairingResponse(
     44     const PairingResponse& pairing_response) {
     45   ControlMessage message;
     46   message.mutable_pairing_response()->CopyFrom(pairing_response);
     47   writer_.Write(SerializeAndFrameMessage(message), base::Closure());
     48 }
     49 
     50 void HostControlDispatcher::DeliverHostMessage(
     51     const ExtensionMessage& message) {
     52   ControlMessage control_message;
     53   control_message.mutable_extension_message()->CopyFrom(message);
     54   writer_.Write(SerializeAndFrameMessage(control_message), base::Closure());
     55 }
     56 
     57 void HostControlDispatcher::InjectClipboardEvent(const ClipboardEvent& event) {
     58   ControlMessage message;
     59   message.mutable_clipboard_event()->CopyFrom(event);
     60   writer_.Write(SerializeAndFrameMessage(message), base::Closure());
     61 }
     62 
     63 void HostControlDispatcher::SetCursorShape(
     64     const CursorShapeInfo& cursor_shape) {
     65   ControlMessage message;
     66   message.mutable_cursor_shape()->CopyFrom(cursor_shape);
     67   writer_.Write(SerializeAndFrameMessage(message), base::Closure());
     68 }
     69 
     70 void HostControlDispatcher::OnMessageReceived(
     71     scoped_ptr<ControlMessage> message, const base::Closure& done_task) {
     72   DCHECK(clipboard_stub_);
     73   DCHECK(host_stub_);
     74 
     75   base::ScopedClosureRunner done_runner(done_task);
     76 
     77   if (message->has_clipboard_event()) {
     78     clipboard_stub_->InjectClipboardEvent(message->clipboard_event());
     79   } else if (message->has_client_resolution()) {
     80     host_stub_->NotifyClientResolution(message->client_resolution());
     81   } else if (message->has_video_control()) {
     82     host_stub_->ControlVideo(message->video_control());
     83   } else if (message->has_audio_control()) {
     84     host_stub_->ControlAudio(message->audio_control());
     85   } else if (message->has_capabilities()) {
     86     host_stub_->SetCapabilities(message->capabilities());
     87   } else if (message->has_pairing_request()) {
     88     host_stub_->RequestPairing(message->pairing_request());
     89   } else if (message->has_extension_message()) {
     90     host_stub_->DeliverClientMessage(message->extension_message());
     91   } else {
     92     LOG(WARNING) << "Unknown control message received.";
     93   }
     94 }
     95 
     96 }  // namespace protocol
     97 }  // namespace remoting
     98