summaryrefslogtreecommitdiffstats
path: root/remoting/client/chromoting_client.cc
blob: cad19933250f281aae4cdcf5d06c9cd79535c662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/client/chromoting_client.h"

#include "base/message_loop.h"
#include "remoting/client/chromoting_view.h"
#include "remoting/client/host_connection.h"

static const uint32 kCreatedColor = 0xff0000ff;
static const uint32 kDisconnectedColor = 0xff00ff00;
static const uint32 kFailedColor = 0xffff0000;

namespace remoting {

ChromotingClient::ChromotingClient(MessageLoop* message_loop,
                                   HostConnection* connection,
                                   ChromotingView* view)
    : message_loop_(message_loop),
      state_(CREATED),
      host_connection_(connection),
      view_(view) {
}

ChromotingClient::~ChromotingClient() {
}

void ChromotingClient::Repaint() {
  message_loop()->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &ChromotingClient::DoRepaint));
}

void ChromotingClient::SetViewport(int x, int y, int width, int height) {
  message_loop()->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &ChromotingClient::DoSetViewport,
                        x, y, width, height));
}

void ChromotingClient::HandleMessages(HostConnection* conn,
                                      HostMessageList* messages) {
  for (size_t i = 0; i < messages->size(); ++i) {
    HostMessage* msg = (*messages)[i];
    // TODO(ajwong): Consider creating a macro similar to the IPC message
    // mappings.  Also reconsider the lifetime of the message object.
    if (msg->has_init_client()) {
      message_loop()->PostTask(
          FROM_HERE,
          NewRunnableMethod(this, &ChromotingClient::DoInitClient, msg));
    } else if (msg->has_begin_update_stream()) {
      message_loop()->PostTask(
          FROM_HERE,
          NewRunnableMethod(this, &ChromotingClient::DoBeginUpdate, msg));
    } else if (msg->has_update_stream_packet()) {
      message_loop()->PostTask(
          FROM_HERE,
          NewRunnableMethod(this, &ChromotingClient::DoHandleUpdate, msg));
    } else if (msg->has_end_update_stream()) {
      message_loop()->PostTask(
          FROM_HERE,
          NewRunnableMethod(this, &ChromotingClient::DoEndUpdate, msg));
    } else {
      NOTREACHED() << "Unknown message received";
    }
  }
  // Assume we have processed all the messages.
  messages->clear();
}

void ChromotingClient::OnConnectionOpened(HostConnection* conn) {
  LOG(INFO) << "ChromotingClient::OnConnectionOpened";
  SetState(CONNECTED);
}

void ChromotingClient::OnConnectionClosed(HostConnection* conn) {
  LOG(INFO) << "ChromotingClient::OnConnectionClosed";
  SetState(DISCONNECTED);
}

void ChromotingClient::OnConnectionFailed(HostConnection* conn) {
  LOG(INFO) << "ChromotingClient::OnConnectionFailed";
  SetState(FAILED);
}

MessageLoop* ChromotingClient::message_loop() {
  return message_loop_;
}

void ChromotingClient::SetState(State s) {
  // TODO(ajwong): We actually may want state to be a shared variable. Think
  // through later.
  message_loop()->PostTask(
      FROM_HERE,
      NewRunnableMethod(this, &ChromotingClient::DoSetState, s));
}

void ChromotingClient::DoSetState(State s) {
  DCHECK_EQ(message_loop(), MessageLoop::current());

  state_ = s;
  switch (state_) {
    case CREATED:
      view_->SetSolidFill(kCreatedColor);
      break;

    case CONNECTED:
      view_->UnsetSolidFill();
      break;

    case DISCONNECTED:
      view_->SetSolidFill(kDisconnectedColor);
      break;

    case FAILED:
      view_->SetSolidFill(kFailedColor);
      break;
  }

  DoRepaint();
}

void ChromotingClient::DoRepaint() {
  DCHECK_EQ(message_loop(), MessageLoop::current());

  view_->Paint();
}

void ChromotingClient::DoSetViewport(int x, int y, int width, int height) {
  DCHECK_EQ(message_loop(), MessageLoop::current());

  view_->SetViewport(x, y, width, height);
}

void ChromotingClient::DoInitClient(HostMessage* msg) {
  DCHECK_EQ(message_loop(), MessageLoop::current());
  DCHECK(msg->has_init_client());
  scoped_ptr<HostMessage> deleter(msg);

  // Saves the dimension and resize the window.
  int width = msg->init_client().width();
  int height = msg->init_client().height();
  LOG(INFO) << "Init client received geometry: " << width << "x" << height;

  view_->SetBackingStoreSize(width, height);
}

void ChromotingClient::DoBeginUpdate(HostMessage* msg) {
  DCHECK_EQ(message_loop(), MessageLoop::current());
  DCHECK(msg->has_begin_update_stream());

  view_->HandleBeginUpdateStream(msg);
}

void ChromotingClient::DoHandleUpdate(HostMessage* msg) {
  DCHECK_EQ(message_loop(), MessageLoop::current());
  DCHECK(msg->has_update_stream_packet());

  view_->HandleUpdateStreamPacket(msg);
}

void ChromotingClient::DoEndUpdate(HostMessage* msg) {
  DCHECK_EQ(message_loop(), MessageLoop::current());
  DCHECK(msg->has_update_stream_packet());

  view_->HandleUpdateStreamPacket(msg);
}

}  // namespace remoting