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