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