Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / remoting / client / chromoting_client.cc
blobcf13d13e65c75cc1f654b13932b9ed5c5ca4d4a8
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.
5 #include "remoting/client/chromoting_client.h"
7 #include "base/bind.h"
8 #include "remoting/client/audio_decode_scheduler.h"
9 #include "remoting/client/audio_player.h"
10 #include "remoting/client/client_context.h"
11 #include "remoting/client/client_user_interface.h"
12 #include "remoting/client/rectangle_update_decoder.h"
13 #include "remoting/proto/audio.pb.h"
14 #include "remoting/proto/video.pb.h"
15 #include "remoting/protocol/authentication_method.h"
16 #include "remoting/protocol/connection_to_host.h"
17 #include "remoting/protocol/negotiating_client_authenticator.h"
18 #include "remoting/protocol/session_config.h"
19 #include "remoting/protocol/transport.h"
21 namespace remoting {
23 using protocol::AuthenticationMethod;
25 ChromotingClient::ChromotingClient(
26 const ClientConfig& config,
27 ClientContext* client_context,
28 protocol::ConnectionToHost* connection,
29 ClientUserInterface* user_interface,
30 scoped_refptr<FrameConsumerProxy> frame_consumer,
31 scoped_ptr<AudioPlayer> audio_player)
32 : config_(config),
33 task_runner_(client_context->main_task_runner()),
34 connection_(connection),
35 user_interface_(user_interface),
36 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
37 rectangle_decoder_ =
38 new RectangleUpdateDecoder(client_context->main_task_runner(),
39 client_context->decode_task_runner(),
40 frame_consumer);
41 audio_decode_scheduler_.reset(new AudioDecodeScheduler(
42 client_context->main_task_runner(),
43 client_context->audio_decode_task_runner(),
44 audio_player.Pass()));
47 ChromotingClient::~ChromotingClient() {
50 void ChromotingClient::Start(
51 scoped_refptr<XmppProxy> xmpp_proxy,
52 scoped_ptr<protocol::TransportFactory> transport_factory) {
53 DCHECK(task_runner_->BelongsToCurrentThread());
55 scoped_ptr<protocol::Authenticator> authenticator(
56 new protocol::NegotiatingClientAuthenticator(
57 config_.authentication_tag, config_.fetch_secret_callback,
58 user_interface_->GetTokenFetcher(config_.host_public_key),
59 config_.authentication_methods));
61 // Create a WeakPtr to ourself for to use for all posted tasks.
62 weak_ptr_ = weak_factory_.GetWeakPtr();
64 connection_->Connect(xmpp_proxy, config_.local_jid, config_.host_jid,
65 config_.host_public_key, transport_factory.Pass(),
66 authenticator.Pass(), this, this, this,
67 rectangle_decoder_,
68 audio_decode_scheduler_.get());
71 void ChromotingClient::Stop(const base::Closure& shutdown_task) {
72 DCHECK(task_runner_->BelongsToCurrentThread());
74 connection_->Disconnect(base::Bind(&ChromotingClient::OnDisconnected,
75 weak_ptr_, shutdown_task));
78 FrameProducer* ChromotingClient::GetFrameProducer() {
79 return rectangle_decoder_;
82 void ChromotingClient::OnDisconnected(const base::Closure& shutdown_task) {
83 shutdown_task.Run();
86 ChromotingStats* ChromotingClient::GetStats() {
87 DCHECK(task_runner_->BelongsToCurrentThread());
88 return rectangle_decoder_->GetStats();
91 void ChromotingClient::InjectClipboardEvent(
92 const protocol::ClipboardEvent& event) {
93 DCHECK(task_runner_->BelongsToCurrentThread());
94 user_interface_->GetClipboardStub()->InjectClipboardEvent(event);
97 void ChromotingClient::SetCursorShape(
98 const protocol::CursorShapeInfo& cursor_shape) {
99 user_interface_->GetCursorShapeStub()->SetCursorShape(cursor_shape);
102 void ChromotingClient::OnConnectionState(
103 protocol::ConnectionToHost::State state,
104 protocol::ErrorCode error) {
105 DCHECK(task_runner_->BelongsToCurrentThread());
106 VLOG(1) << "ChromotingClient::OnConnectionState(" << state << ")";
107 if (state == protocol::ConnectionToHost::CONNECTED)
108 Initialize();
109 user_interface_->OnConnectionState(state, error);
112 void ChromotingClient::OnConnectionReady(bool ready) {
113 VLOG(1) << "ChromotingClient::OnConnectionReady(" << ready << ")";
114 user_interface_->OnConnectionReady(ready);
117 void ChromotingClient::Initialize() {
118 DCHECK(task_runner_->BelongsToCurrentThread());
120 // Initialize the decoder.
121 rectangle_decoder_->Initialize(connection_->config());
122 if (connection_->config().is_audio_enabled())
123 audio_decode_scheduler_->Initialize(connection_->config());
126 } // namespace remoting