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 void SendClientMessage(const std::string
& type
, const std::string
& data
);
92 // Records paint time for statistics logging, if enabled. May be called from
94 void RecordPaintTime(int64 paint_time_ms
);
96 // ClientUserInterface implementation.
97 virtual void OnConnectionState(
98 protocol::ConnectionToHost::State state
,
99 protocol::ErrorCode error
) override
;
100 virtual void OnConnectionReady(bool ready
) override
;
101 virtual void OnRouteChanged(const std::string
& channel_name
,
102 const protocol::TransportRoute
& route
) override
;
103 virtual void SetCapabilities(const std::string
& capabilities
) override
;
104 virtual void SetPairingResponse(
105 const protocol::PairingResponse
& response
) override
;
106 virtual void DeliverHostMessage(
107 const protocol::ExtensionMessage
& message
) override
;
108 virtual protocol::ClipboardStub
* GetClipboardStub() override
;
109 virtual protocol::CursorShapeStub
* GetCursorShapeStub() override
;
111 // CursorShapeStub implementation.
112 virtual void InjectClipboardEvent(
113 const protocol::ClipboardEvent
& event
) override
;
115 // ClipboardStub implementation.
116 virtual void SetCursorShape(const protocol::CursorShapeInfo
& shape
) override
;
119 // This object is ref-counted, so it cleans itself up.
120 virtual ~ChromotingJniInstance();
122 void ConnectToHostOnDisplayThread();
123 void ConnectToHostOnNetworkThread();
124 void DisconnectFromHostOnNetworkThread();
126 // Notifies the user interface that the user needs to enter a PIN. The
127 // current authentication attempt is put on hold until |callback| is invoked.
128 // May be called on any thread.
129 void FetchSecret(bool pairable
,
130 const protocol::SecretFetchedCallback
& callback
);
132 // Sets the device name. Can be called on any thread.
133 void SetDeviceName(const std::string
& device_name
);
135 void SendKeyEventInternal(int usb_key_code
, bool key_down
);
137 // Enables or disables periodic logging of performance statistics. Called on
138 // the network thread.
139 void EnableStatsLogging(bool enabled
);
141 // If logging is enabled, logs the current connection statistics, and
142 // triggers another call to this function after the logging time interval.
143 // Called on the network thread.
146 // Used to obtain task runner references and make calls to Java methods.
147 ChromotingJniRuntime
* jni_runtime_
;
149 // ID of the host we are connecting to.
150 std::string host_id_
;
151 std::string host_jid_
;
153 // This group of variables is to be used on the display thread.
154 scoped_refptr
<FrameConsumerProxy
> frame_consumer_
;
155 scoped_ptr
<JniFrameConsumer
> view_
;
156 scoped_ptr
<base::WeakPtrFactory
<JniFrameConsumer
> > view_weak_factory_
;
158 // This group of variables is to be used on the network thread.
159 scoped_ptr
<ClientContext
> client_context_
;
160 scoped_ptr
<VideoRenderer
> video_renderer_
;
161 scoped_ptr
<protocol::Authenticator
> authenticator_
;
162 scoped_ptr
<ChromotingClient
> client_
;
163 XmppSignalStrategy::XmppServerConfig xmpp_config_
;
164 scoped_ptr
<XmppSignalStrategy
> signaling_
; // Must outlive client_
165 scoped_ptr
<ClientStatusLogger
> client_status_logger_
;
166 base::WeakPtr
<TokenFetcherProxy
> token_fetcher_proxy_
;
168 // Pass this the user's PIN once we have it. To be assigned and accessed on
169 // the UI thread, but must be posted to the network thread to call it.
170 protocol::SecretFetchedCallback pin_callback_
;
172 // Indicates whether to establish a new pairing with this host. This is
173 // modified in ProvideSecret(), but thereafter to be used only from the
174 // network thread. (This is safe because ProvideSecret() is invoked at most
175 // once per run, and always before any reference to this flag.)
176 bool create_pairing_
;
178 // The device name to appear in the paired-clients list. Accessed on the
180 std::string device_name_
;
182 // If this is true, performance statistics will be periodically written to
183 // the Android log. Used on the network thread.
184 bool stats_logging_enabled_
;
186 // The set of capabilities supported by the client. Accessed on the network
187 // thread. Once SetCapabilities() is called, this will contain the negotiated
188 // set of capabilities for this remoting session.
189 std::string capabilities_
;
191 friend class base::RefCountedThreadSafe
<ChromotingJniInstance
>;
193 base::WeakPtrFactory
<ChromotingJniInstance
> weak_factory_
;
195 DISALLOW_COPY_AND_ASSIGN(ChromotingJniInstance
);
198 } // namespace remoting