| OLD | NEW |
| (Empty) |
| 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_JNI_CHROMOTING_JNI_INSTANCE_H_ | |
| 6 #define REMOTING_CLIENT_JNI_CHROMOTING_JNI_INSTANCE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "remoting/client/chromoting_client.h" | |
| 16 #include "remoting/client/client_context.h" | |
| 17 #include "remoting/client/client_telemetry_logger.h" | |
| 18 #include "remoting/client/client_user_interface.h" | |
| 19 #include "remoting/client/jni/connect_to_host_info.h" | |
| 20 #include "remoting/proto/control.pb.h" | |
| 21 #include "remoting/proto/event.pb.h" | |
| 22 #include "remoting/protocol/clipboard_stub.h" | |
| 23 #include "remoting/protocol/cursor_shape_stub.h" | |
| 24 #include "remoting/signaling/xmpp_signal_strategy.h" | |
| 25 | |
| 26 namespace remoting { | |
| 27 | |
| 28 namespace protocol { | |
| 29 class ClipboardEvent; | |
| 30 class PerformanceTracker; | |
| 31 class VideoRenderer; | |
| 32 } // namespace protocol | |
| 33 | |
| 34 class AudioPlayerAndroid; | |
| 35 class ChromotingClientRuntime; | |
| 36 class JniClient; | |
| 37 class JniPairingSecretFetcher; | |
| 38 | |
| 39 // ChromotingJniInstance is scoped to the session. | |
| 40 // This class is Created on the UI thread but thereafter it is used and | |
| 41 // destroyed on the network thread. Except where indicated, all methods are | |
| 42 // called on the network thread. | |
| 43 class ChromotingJniInstance | |
| 44 : public ClientUserInterface, | |
| 45 public protocol::ClipboardStub { | |
| 46 public: | |
| 47 // Initiates a connection with the specified host. Call from the UI thread. | |
| 48 // The instance does not take ownership of |jni_runtime|. To connect with an | |
| 49 // unpaired host, pass in |pairing_id| and |pairing_secret| as empty strings. | |
| 50 ChromotingJniInstance(base::WeakPtr<JniClient> jni_client, | |
| 51 base::WeakPtr<JniPairingSecretFetcher> secret_fetcher, | |
| 52 std::unique_ptr<protocol::CursorShapeStub> cursor_stub, | |
| 53 std::unique_ptr<protocol::VideoRenderer> video_renderer, | |
| 54 const ConnectToHostInfo& info); | |
| 55 | |
| 56 ~ChromotingJniInstance() override; | |
| 57 | |
| 58 // Starts the connection. Can be called on any thread. | |
| 59 void Connect(); | |
| 60 | |
| 61 // Terminates the current connection (if it hasn't already failed) and cleans | |
| 62 // up. The instance will no longer be valid after calling this function. | |
| 63 // Must be called before destruction. | |
| 64 void Disconnect(); | |
| 65 | |
| 66 // Requests the android app to fetch a third-party token. | |
| 67 void FetchThirdPartyToken( | |
| 68 const std::string& host_public_key, | |
| 69 const std::string& token_url, | |
| 70 const std::string& scope, | |
| 71 const protocol::ThirdPartyTokenFetchedCallback& token_fetched_callback); | |
| 72 | |
| 73 // Called by the android app when the token is fetched. | |
| 74 void HandleOnThirdPartyTokenFetched(const std::string& token, | |
| 75 const std::string& shared_secret); | |
| 76 | |
| 77 // Provides the user's PIN and resumes the host authentication attempt. Call | |
| 78 // on the UI thread once the user has finished entering this PIN into the UI, | |
| 79 // but only after the UI has been asked to provide a PIN (via FetchSecret()). | |
| 80 void ProvideSecret(const std::string& pin, bool create_pair, | |
| 81 const std::string& device_name); | |
| 82 | |
| 83 // Moves the host's cursor to the specified coordinates, optionally with some | |
| 84 // mouse button depressed. If |button| is BUTTON_UNDEFINED, no click is made. | |
| 85 void SendMouseEvent(int x, int y, | |
| 86 protocol::MouseEvent_MouseButton button, | |
| 87 bool button_down); | |
| 88 void SendMouseWheelEvent(int delta_x, int delta_y); | |
| 89 | |
| 90 // Sends the provided keyboard scan code to the host. | |
| 91 bool SendKeyEvent(int scan_code, int key_code, bool key_down); | |
| 92 | |
| 93 void SendTextEvent(const std::string& text); | |
| 94 | |
| 95 // Sends the provided touch event payload to the host. | |
| 96 void SendTouchEvent(const protocol::TouchEvent& touch_event); | |
| 97 | |
| 98 // Enables or disables the video channel. May be called from any thread. | |
| 99 void EnableVideoChannel(bool enable); | |
| 100 | |
| 101 void SendClientMessage(const std::string& type, const std::string& data); | |
| 102 | |
| 103 // ClientUserInterface implementation. | |
| 104 void OnConnectionState(protocol::ConnectionToHost::State state, | |
| 105 protocol::ErrorCode error) override; | |
| 106 void OnConnectionReady(bool ready) override; | |
| 107 void OnRouteChanged(const std::string& channel_name, | |
| 108 const protocol::TransportRoute& route) override; | |
| 109 void SetCapabilities(const std::string& capabilities) override; | |
| 110 void SetPairingResponse(const protocol::PairingResponse& response) override; | |
| 111 void DeliverHostMessage(const protocol::ExtensionMessage& message) override; | |
| 112 void SetDesktopSize(const webrtc::DesktopSize& size, | |
| 113 const webrtc::DesktopVector& dpi) override; | |
| 114 protocol::ClipboardStub* GetClipboardStub() override; | |
| 115 protocol::CursorShapeStub* GetCursorShapeStub() override; | |
| 116 | |
| 117 // CursorShapeStub implementation. | |
| 118 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override; | |
| 119 | |
| 120 // Get the weak pointer of the instance. Please only use it on the network | |
| 121 // thread. | |
| 122 base::WeakPtr<ChromotingJniInstance> GetWeakPtr(); | |
| 123 | |
| 124 private: | |
| 125 void ConnectToHostOnNetworkThread(); | |
| 126 | |
| 127 // Sets the device name. Can be called on any thread. | |
| 128 void SetDeviceName(const std::string& device_name); | |
| 129 | |
| 130 void SendKeyEventInternal(int usb_key_code, bool key_down); | |
| 131 | |
| 132 // Enables or disables periodic logging of performance statistics. Called on | |
| 133 // the network thread. | |
| 134 void EnableStatsLogging(bool enabled); | |
| 135 | |
| 136 // If logging is enabled, logs the current connection statistics, and | |
| 137 // triggers another call to this function after the logging time interval. | |
| 138 // Called on the network thread. | |
| 139 void LogPerfStats(); | |
| 140 | |
| 141 // Releases the resource in the right order. | |
| 142 void ReleaseResources(); | |
| 143 | |
| 144 // Used to obtain task runner references. | |
| 145 ChromotingClientRuntime* runtime_; | |
| 146 | |
| 147 base::WeakPtr<JniClient> jni_client_; | |
| 148 | |
| 149 base::WeakPtr<JniPairingSecretFetcher> secret_fetcher_; | |
| 150 | |
| 151 ConnectToHostInfo connection_info_; | |
| 152 | |
| 153 protocol::ClientAuthenticationConfig client_auth_config_; | |
| 154 | |
| 155 // This group of variables is to be used on the network thread. | |
| 156 std::unique_ptr<ClientContext> client_context_; | |
| 157 std::unique_ptr<protocol::PerformanceTracker> perf_tracker_; | |
| 158 std::unique_ptr<protocol::CursorShapeStub> cursor_shape_stub_; | |
| 159 std::unique_ptr<protocol::VideoRenderer> video_renderer_; | |
| 160 std::unique_ptr<ChromotingClient> client_; | |
| 161 XmppSignalStrategy::XmppServerConfig xmpp_config_; | |
| 162 std::unique_ptr<XmppSignalStrategy> signaling_; // Must outlive client_ | |
| 163 protocol::ThirdPartyTokenFetchedCallback third_party_token_fetched_callback_; | |
| 164 | |
| 165 std::unique_ptr<AudioPlayerAndroid> audio_player_; | |
| 166 | |
| 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_ = false; | |
| 172 | |
| 173 // The device name to appear in the paired-clients list. Accessed on the | |
| 174 // network thread. | |
| 175 std::string device_name_; | |
| 176 | |
| 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_ = false; | |
| 180 | |
| 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_; | |
| 185 | |
| 186 // Indicates whether the client is connected to the host. Used on network | |
| 187 // thread. | |
| 188 bool connected_ = false; | |
| 189 | |
| 190 std::unique_ptr<ClientTelemetryLogger> logger_; | |
| 191 | |
| 192 base::WeakPtr<ChromotingJniInstance> weak_ptr_; | |
| 193 base::WeakPtrFactory<ChromotingJniInstance> weak_factory_; | |
| 194 | |
| 195 DISALLOW_COPY_AND_ASSIGN(ChromotingJniInstance); | |
| 196 }; | |
| 197 | |
| 198 } // namespace remoting | |
| 199 | |
| 200 #endif // REMOTING_CLIENT_JNI_CHROMOTING_JNI_INSTANCE_H_ | |
| OLD | NEW |