chromoting_jni_instance.h revision 558790d6acca3451cf3a6b497803a5f07d0bec58
1// Copyright 2013 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#ifndef REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_
6#define REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_
7
8#include <string>
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "base/message_loop/message_loop.h"
14#include "remoting/client/chromoting_client.h"
15#include "remoting/client/client_config.h"
16#include "remoting/client/client_context.h"
17#include "remoting/client/client_user_interface.h"
18#include "remoting/client/frame_consumer_proxy.h"
19#include "remoting/client/jni/jni_frame_consumer.h"
20#include "remoting/jingle_glue/network_settings.h"
21#include "remoting/jingle_glue/xmpp_signal_strategy.h"
22#include "remoting/protocol/clipboard_stub.h"
23#include "remoting/protocol/connection_to_host.h"
24#include "remoting/protocol/cursor_shape_stub.h"
25
26namespace remoting {
27namespace protocol {
28  class ClipboardEvent;
29  class CursorShapeInfo;
30}  // namespace protocol
31
32// ClientUserInterface that indirectly makes and receives JNI calls.
33class ChromotingJniInstance
34  : public ClientUserInterface,
35    public protocol::ClipboardStub,
36    public protocol::CursorShapeStub,
37    public base::RefCountedThreadSafe<ChromotingJniInstance> {
38 public:
39  // Initiates a connection with the specified host. Call from the UI thread.
40  // The instance does not take ownership of |jni_runtime|.
41  ChromotingJniInstance(ChromotingJniRuntime* jni_runtime,
42                        const char* username,
43                        const char* auth_token,
44                        const char* host_jid,
45                        const char* host_id,
46                        const char* host_pubkey);
47
48  // Terminates the current connection (if it hasn't already failed) and cleans
49  // up. Must be called before destruction.
50  void Cleanup();
51
52  // Provides the user's PIN and resumes the host authentication attempt. Call
53  // on the UI thread once the user has finished entering this PIN into the UI,
54  // but only after the UI has been asked to provide a PIN (via FetchSecret()).
55  void ProvideSecret(const std::string& pin);
56
57  // Schedules a redraw on the display thread. May be called from any thread.
58  void RedrawDesktop();
59
60  // Moves the host's cursor to the specified coordinates, optionally with some
61  // mouse button depressed. If |button| is BUTTON_UNDEFINED, no click is made.
62  void PerformMouseAction(int x,
63                          int y,
64                          protocol::MouseEvent_MouseButton button,
65                          bool buttonDown);
66
67  // ClientUserInterface implementation.
68  virtual void OnConnectionState(
69      protocol::ConnectionToHost::State state,
70      protocol::ErrorCode error) OVERRIDE;
71  virtual void OnConnectionReady(bool ready) OVERRIDE;
72  virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
73  virtual void SetPairingResponse(
74      const protocol::PairingResponse& response) OVERRIDE;
75  virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
76  virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
77  virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
78      GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
79
80  // CursorShapeStub implementation.
81  virtual void InjectClipboardEvent(
82      const protocol::ClipboardEvent& event) OVERRIDE;
83
84  // ClipboardStub implementation.
85  virtual void SetCursorShape(const protocol::CursorShapeInfo& shape) OVERRIDE;
86
87 private:
88  // This object is ref-counted, so it cleans itself up.
89  virtual ~ChromotingJniInstance();
90
91  void ConnectToHostOnDisplayThread();
92  void ConnectToHostOnNetworkThread();
93  void DisconnectFromHostOnNetworkThread();
94
95  // Notifies the user interface that the user needs to enter a PIN. The
96  // current authentication attempt is put on hold until |callback| is invoked.
97  // May be called on any thread.
98  void FetchSecret(bool pairable,
99                   const protocol::SecretFetchedCallback& callback);
100
101  // Used to obtain task runner references and make calls to Java methods.
102  ChromotingJniRuntime* jni_runtime_;
103
104  // This group of variables is to be used on the display thread.
105  scoped_refptr<FrameConsumerProxy> frame_consumer_;
106  scoped_ptr<JniFrameConsumer> view_;
107  scoped_ptr<base::WeakPtrFactory<JniFrameConsumer> > view_weak_factory_;
108
109  // This group of variables is to be used on the network thread.
110  scoped_ptr<ClientConfig> client_config_;
111  scoped_ptr<ClientContext> client_context_;
112  scoped_ptr<protocol::ConnectionToHost> connection_;
113  scoped_ptr<ChromotingClient> client_;
114  scoped_ptr<XmppSignalStrategy::XmppServerConfig> signaling_config_;
115  scoped_ptr<XmppSignalStrategy> signaling_;  // Must outlive client_
116  scoped_ptr<NetworkSettings> network_settings_;
117
118  // Pass this the user's PIN once we have it. To be assigned and accessed on
119  // the UI thread, but must be posted to the network thread to call it.
120  protocol::SecretFetchedCallback pin_callback_;
121
122  // These strings describe the current connection, and are not reused. They
123  // are initialized in ConnectionToHost(), but thereafter are only to be used
124  // on the network thread. (This is safe because ConnectionToHost()'s finishes
125  // using them on the UI thread before they are ever touched from network.)
126  std::string username_;
127  std::string auth_token_;
128  std::string host_jid_;
129  std::string host_id_;
130  std::string host_pubkey_;
131
132  friend class base::RefCountedThreadSafe<ChromotingJniInstance>;
133
134  DISALLOW_COPY_AND_ASSIGN(ChromotingJniInstance);
135};
136
137}  // namespace remoting
138
139#endif
140