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.
5 #ifndef REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_
6 #define REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_
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_context.h"
16 #include "remoting/client/client_user_interface.h"
17 #include "remoting/client/frame_consumer_proxy.h"
18 #include "remoting/client/jni/jni_frame_consumer.h"
19 #include "remoting/protocol/clipboard_stub.h"
20 #include "remoting/protocol/cursor_shape_stub.h"
21 #include "remoting/signaling/xmpp_signal_strategy.h"
27 class CursorShapeInfo
;
28 } // namespace protocol
30 class ClientStatusLogger
;
32 class TokenFetcherProxy
;
34 // ClientUserInterface that indirectly makes and receives JNI calls.
35 class ChromotingJniInstance
36 : public ClientUserInterface
,
37 public protocol::ClipboardStub
,
38 public protocol::CursorShapeStub
,
39 public base::RefCountedThreadSafe
<ChromotingJniInstance
> {
41 // Initiates a connection with the specified host. Call from the UI thread.
42 // The instance does not take ownership of |jni_runtime|. To connect with an
43 // unpaired host, pass in |pairing_id| and |pairing_secret| as empty strings.
44 ChromotingJniInstance(ChromotingJniRuntime
* jni_runtime
,
46 const char* auth_token
,
49 const char* host_pubkey
,
50 const char* pairing_id
,
51 const char* pairing_secret
,
52 const char* capabilities
);
54 // Terminates the current connection (if it hasn't already failed) and cleans
55 // up. Must be called before destruction.
58 // Requests the android app to fetch a third-party token.
59 void FetchThirdPartyToken(
60 const GURL
& token_url
,
61 const std::string
& client_id
,
62 const std::string
& scope
,
63 const base::WeakPtr
<TokenFetcherProxy
> token_fetcher_proxy
);
65 // Called by the android app when the token is fetched.
66 void HandleOnThirdPartyTokenFetched(const std::string
& token
,
67 const std::string
& shared_secret
);
69 // Provides the user's PIN and resumes the host authentication attempt. Call
70 // on the UI thread once the user has finished entering this PIN into the UI,
71 // but only after the UI has been asked to provide a PIN (via FetchSecret()).
72 void ProvideSecret(const std::string
& pin
, bool create_pair
,
73 const std::string
& device_name
);
75 // Schedules a redraw on the display thread. May be called from any thread.
78 // Moves the host's cursor to the specified coordinates, optionally with some
79 // mouse button depressed. If |button| is BUTTON_UNDEFINED, no click is made.
80 void SendMouseEvent(int x
, int y
,
81 protocol::MouseEvent_MouseButton button
,
83 void SendMouseWheelEvent(int delta_x
, int delta_y
);
85 // Sends the provided keyboard scan code to the host.
86 bool SendKeyEvent(int key_code
, bool key_down
);
88 void SendTextEvent(const std::string
& text
);
90 // Enables or disables the video channel. May be called from any thread.
91 void EnableVideoChannel(bool enable
);
93 void SendClientMessage(const std::string
& type
, const std::string
& data
);
95 // Records paint time for statistics logging, if enabled. May be called from
97 void RecordPaintTime(int64 paint_time_ms
);
99 // ClientUserInterface implementation.
100 void OnConnectionState(protocol::ConnectionToHost::State state
,
101 protocol::ErrorCode error
) override
;
102 void OnConnectionReady(bool ready
) override
;
103 void OnRouteChanged(const std::string
& channel_name
,
104 const protocol::TransportRoute
& route
) override
;
105 void SetCapabilities(const std::string
& capabilities
) override
;
106 void SetPairingResponse(const protocol::PairingResponse
& response
) override
;
107 void DeliverHostMessage(const protocol::ExtensionMessage
& message
) override
;
108 protocol::ClipboardStub
* GetClipboardStub() override
;
109 protocol::CursorShapeStub
* GetCursorShapeStub() override
;
111 // CursorShapeStub implementation.
112 void InjectClipboardEvent(const protocol::ClipboardEvent
& event
) override
;
114 // ClipboardStub implementation.
115 void SetCursorShape(const protocol::CursorShapeInfo
& shape
) override
;
118 // This object is ref-counted, so it cleans itself up.
119 ~ChromotingJniInstance() override
;
121 void ConnectToHostOnDisplayThread();
122 void ConnectToHostOnNetworkThread();
123 void DisconnectFromHostOnNetworkThread();
125 // Notifies the user interface that the user needs to enter a PIN. The
126 // current authentication attempt is put on hold until |callback| is invoked.
127 // May be called on any thread.
128 void FetchSecret(bool pairable
,
129 const protocol::SecretFetchedCallback
& callback
);
131 // Sets the device name. Can be called on any thread.
132 void SetDeviceName(const std::string
& device_name
);
134 void SendKeyEventInternal(int usb_key_code
, bool key_down
);
136 // Enables or disables periodic logging of performance statistics. Called on
137 // the network thread.
138 void EnableStatsLogging(bool enabled
);
140 // If logging is enabled, logs the current connection statistics, and
141 // triggers another call to this function after the logging time interval.
142 // Called on the network thread.
145 // Used to obtain task runner references and make calls to Java methods.
146 ChromotingJniRuntime
* jni_runtime_
;
148 // ID of the host we are connecting to.
149 std::string host_id_
;
150 std::string host_jid_
;
152 // This group of variables is to be used on the display thread.
153 scoped_refptr
<FrameConsumerProxy
> frame_consumer_
;
154 scoped_ptr
<JniFrameConsumer
> view_
;
155 scoped_ptr
<base::WeakPtrFactory
<JniFrameConsumer
> > view_weak_factory_
;
157 // This group of variables is to be used on the network thread.
158 scoped_ptr
<ClientContext
> client_context_
;
159 scoped_ptr
<VideoRenderer
> video_renderer_
;
160 scoped_ptr
<protocol::Authenticator
> authenticator_
;
161 scoped_ptr
<ChromotingClient
> client_
;
162 XmppSignalStrategy::XmppServerConfig xmpp_config_
;
163 scoped_ptr
<XmppSignalStrategy
> signaling_
; // Must outlive client_
164 scoped_ptr
<ClientStatusLogger
> client_status_logger_
;
165 base::WeakPtr
<TokenFetcherProxy
> token_fetcher_proxy_
;
167 // Pass this the user's PIN once we have it. To be assigned and accessed on
168 // the UI thread, but must be posted to the network thread to call it.
169 protocol::SecretFetchedCallback pin_callback_
;
171 // Indicates whether to establish a new pairing with this host. This is
172 // modified in ProvideSecret(), but thereafter to be used only from the
173 // network thread. (This is safe because ProvideSecret() is invoked at most
174 // once per run, and always before any reference to this flag.)
175 bool create_pairing_
;
177 // The device name to appear in the paired-clients list. Accessed on the
179 std::string device_name_
;
181 // If this is true, performance statistics will be periodically written to
182 // the Android log. Used on the network thread.
183 bool stats_logging_enabled_
;
185 // The set of capabilities supported by the client. Accessed on the network
186 // thread. Once SetCapabilities() is called, this will contain the negotiated
187 // set of capabilities for this remoting session.
188 std::string capabilities_
;
190 friend class base::RefCountedThreadSafe
<ChromotingJniInstance
>;
192 base::WeakPtrFactory
<ChromotingJniInstance
> weak_factory_
;
194 DISALLOW_COPY_AND_ASSIGN(ChromotingJniInstance
);
197 } // namespace remoting