OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_ | 5 #ifndef REMOTING_IOS_BRIDGE_CLIENT_INSTANCE_H_ |
6 #define REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_ | 6 #define REMOTING_IOS_BRIDGE_CLIENT_INSTANCE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "remoting/base/auto_thread.h" |
14 #include "remoting/client/chromoting_client.h" | 16 #include "remoting/client/chromoting_client.h" |
15 #include "remoting/client/client_config.h" | 17 #include "remoting/client/client_config.h" |
16 #include "remoting/client/client_context.h" | 18 #include "remoting/client/client_context.h" |
17 #include "remoting/client/client_user_interface.h" | 19 #include "remoting/client/client_user_interface.h" |
18 #include "remoting/client/frame_consumer_proxy.h" | 20 #include "remoting/client/frame_consumer_proxy.h" |
19 #include "remoting/client/jni/jni_frame_consumer.h" | 21 #include "remoting/client/software_video_renderer.h" |
| 22 |
| 23 #include "remoting/ios/bridge/frame_consumer_bridge.h" |
| 24 |
| 25 #include "remoting/jingle_glue/network_settings.h" |
20 #include "remoting/jingle_glue/xmpp_signal_strategy.h" | 26 #include "remoting/jingle_glue/xmpp_signal_strategy.h" |
21 #include "remoting/protocol/clipboard_stub.h" | 27 #include "remoting/protocol/clipboard_stub.h" |
22 #include "remoting/protocol/connection_to_host.h" | 28 #include "remoting/protocol/connection_to_host.h" |
23 #include "remoting/protocol/cursor_shape_stub.h" | 29 #include "remoting/protocol/cursor_shape_stub.h" |
24 | 30 |
25 namespace remoting { | 31 namespace remoting { |
26 | 32 |
27 namespace protocol { | 33 class ClientProxy; |
28 class ClipboardEvent; | |
29 class CursorShapeInfo; | |
30 } // namespace protocol | |
31 | 34 |
32 class VideoRenderer; | 35 // ClientUserInterface that indirectly makes and receives OBJ_C calls from the |
| 36 // UI application |
| 37 class ClientInstance : public ClientUserInterface, |
| 38 public protocol::ClipboardStub, |
| 39 public protocol::CursorShapeStub, |
| 40 public base::RefCountedThreadSafe<ClientInstance> { |
| 41 public: |
| 42 // Initiates a connection with the specified host. Call from the UI thread. To |
| 43 // connect with an unpaired host, pass in |pairing_id| and |pairing_secret| as |
| 44 // empty strings. |
| 45 ClientInstance(const base::WeakPtr<ClientProxy>& proxy, |
| 46 const std::string& username, |
| 47 const std::string& auth_token, |
| 48 const std::string& host_jid, |
| 49 const std::string& host_id, |
| 50 const std::string& host_pubkey, |
| 51 const std::string& pairing_id, |
| 52 const std::string& pairing_secret); |
33 | 53 |
34 // ClientUserInterface that indirectly makes and receives JNI calls. | 54 // Begins the connection process. Should not be called again until after |
35 class ChromotingJniInstance | 55 // |CleanUp| |
36 : public ClientUserInterface, | 56 void Start(); |
37 public protocol::ClipboardStub, | |
38 public protocol::CursorShapeStub, | |
39 public base::RefCountedThreadSafe<ChromotingJniInstance> { | |
40 public: | |
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, | |
45 const char* username, | |
46 const char* auth_token, | |
47 const char* host_jid, | |
48 const char* host_id, | |
49 const char* host_pubkey, | |
50 const char* pairing_id, | |
51 const char* pairing_secret); | |
52 | 57 |
53 // Terminates the current connection (if it hasn't already failed) and cleans | 58 // Terminates the current connection (if it hasn't already failed) and cleans |
54 // up. Must be called before destruction. | 59 // up. Must be called before destruction can occur or a memory leak may occur. |
55 void Cleanup(); | 60 void Cleanup(); |
56 | 61 |
57 // Provides the user's PIN and resumes the host authentication attempt. Call | |
58 // on the UI thread once the user has finished entering this PIN into the UI, | |
59 // but only after the UI has been asked to provide a PIN (via FetchSecret()). | |
60 void ProvideSecret(const std::string& pin, bool create_pair, | |
61 const std::string& device_name); | |
62 | |
63 // Schedules a redraw on the display thread. May be called from any thread. | |
64 void RedrawDesktop(); | |
65 | |
66 // Moves the host's cursor to the specified coordinates, optionally with some | |
67 // mouse button depressed. If |button| is BUTTON_UNDEFINED, no click is made. | |
68 void PerformMouseAction(int x, int y, | |
69 protocol::MouseEvent_MouseButton button, | |
70 bool button_down); | |
71 | |
72 void PerformMouseWheelDeltaAction(int delta_x, int delta_y); | |
73 | |
74 // Sends the provided keyboard scan code to the host. | |
75 void PerformKeyboardAction(int key_code, bool key_down); | |
76 | |
77 // Records paint time for statistics logging, if enabled. May be called from | |
78 // any thread. | |
79 void RecordPaintTime(int64 paint_time_ms); | |
80 | |
81 // ClientUserInterface implementation. | |
82 virtual void OnConnectionState( | |
83 protocol::ConnectionToHost::State state, | |
84 protocol::ErrorCode error) OVERRIDE; | |
85 virtual void OnConnectionReady(bool ready) OVERRIDE; | |
86 virtual void OnRouteChanged(const std::string& channel_name, | |
87 const protocol::TransportRoute& route) OVERRIDE; | |
88 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; | |
89 virtual void SetPairingResponse( | |
90 const protocol::PairingResponse& response) OVERRIDE; | |
91 virtual void DeliverHostMessage( | |
92 const protocol::ExtensionMessage& message) OVERRIDE; | |
93 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE; | |
94 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE; | |
95 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> | |
96 GetTokenFetcher(const std::string& host_public_key) OVERRIDE; | |
97 | |
98 // CursorShapeStub implementation. | |
99 virtual void InjectClipboardEvent( | |
100 const protocol::ClipboardEvent& event) OVERRIDE; | |
101 | |
102 // ClipboardStub implementation. | |
103 virtual void SetCursorShape(const protocol::CursorShapeInfo& shape) OVERRIDE; | |
104 | |
105 private: | |
106 // This object is ref-counted, so it cleans itself up. | |
107 virtual ~ChromotingJniInstance(); | |
108 | |
109 void ConnectToHostOnDisplayThread(); | |
110 void ConnectToHostOnNetworkThread(); | |
111 void DisconnectFromHostOnNetworkThread(); | |
112 | |
113 // Notifies the user interface that the user needs to enter a PIN. The | 62 // Notifies the user interface that the user needs to enter a PIN. The |
114 // current authentication attempt is put on hold until |callback| is invoked. | 63 // current authentication attempt is put on hold until |callback| is invoked. |
115 // May be called on any thread. | 64 // May be called on any thread. |
116 void FetchSecret(bool pairable, | 65 void FetchSecret(bool pairable, |
117 const protocol::SecretFetchedCallback& callback); | 66 const protocol::SecretFetchedCallback& callback); |
118 | 67 |
119 // Sets the device name. Can be called on any thread. | 68 // Provides the user's PIN and resumes the host authentication attempt. Call |
120 void SetDeviceName(const std::string& device_name); | 69 // on the UI thread once the user has finished entering this PIN into the UI, |
| 70 // but only after the UI has been asked to provide a PIN (via FetchSecret()). |
| 71 void ProvideSecret(const std::string& pin, bool create_pair); |
121 | 72 |
122 // Enables or disables periodic logging of performance statistics. Called on | 73 // Moves the host's cursor to the specified coordinates, optionally with some |
123 // the network thread. | 74 // mouse button depressed. If |button| is BUTTON_UNDEFINED, no click is made. |
124 void EnableStatsLogging(bool enabled); | 75 void PerformMouseAction( |
| 76 const webrtc::DesktopVector& position, |
| 77 const webrtc::DesktopVector& wheel_delta, |
| 78 int /* protocol::MouseEvent_MouseButton */ whichButton, |
| 79 bool button_down); |
125 | 80 |
126 // If logging is enabled, logs the current connection statistics, and | 81 // Sends the provided keyboard scan code to the host. |
127 // triggers another call to this function after the logging time interval. | 82 void PerformKeyboardAction(int key_code, bool key_down); |
128 // Called on the network thread. | |
129 void LogPerfStats(); | |
130 | 83 |
131 // Used to obtain task runner references and make calls to Java methods. | 84 // ClientUserInterface implementation. |
132 ChromotingJniRuntime* jni_runtime_; | 85 virtual void OnConnectionState(protocol::ConnectionToHost::State state, |
| 86 protocol::ErrorCode error) OVERRIDE; |
| 87 virtual void OnConnectionReady(bool ready) OVERRIDE; |
| 88 virtual void OnRouteChanged(const std::string& channel_name, |
| 89 const protocol::TransportRoute& route) OVERRIDE; |
| 90 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; |
| 91 virtual void SetPairingResponse(const protocol::PairingResponse& response) |
| 92 OVERRIDE; |
| 93 virtual void DeliverHostMessage(const protocol::ExtensionMessage& message) |
| 94 OVERRIDE; |
| 95 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE; |
| 96 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE; |
| 97 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> |
| 98 GetTokenFetcher(const std::string& host_public_key) OVERRIDE; |
| 99 |
| 100 // CursorShapeStub implementation. |
| 101 virtual void InjectClipboardEvent(const protocol::ClipboardEvent& event) |
| 102 OVERRIDE; |
| 103 |
| 104 // ClipboardStub implementation. |
| 105 virtual void SetCursorShape(const protocol::CursorShapeInfo& shape) OVERRIDE; |
| 106 |
| 107 private: |
| 108 // This object is ref-counted, so it cleans itself up. |
| 109 virtual ~ClientInstance(); |
| 110 |
| 111 void ConnectToHostOnNetworkThread( |
| 112 scoped_refptr<FrameConsumerProxy> consumer_proxy, |
| 113 const base::Closure& done); |
| 114 void DisconnectFromHostOnNetworkThread(const base::Closure& done); |
| 115 |
| 116 // Proxy to exchange messages between the |
| 117 // common Chromoting protocol and UI Application. |
| 118 base::WeakPtr<ClientProxy> proxyToClient_; |
133 | 119 |
134 // ID of the host we are connecting to. | 120 // ID of the host we are connecting to. |
135 std::string host_id_; | 121 std::string host_id_; |
136 | 122 |
137 // This group of variables is to be used on the display thread. | 123 // This group of variables is to be used on the display thread. |
138 scoped_refptr<FrameConsumerProxy> frame_consumer_; | 124 scoped_ptr<SoftwareVideoRenderer> video_renderer_; |
139 scoped_ptr<JniFrameConsumer> view_; | 125 scoped_ptr<FrameConsumerBridge> view_; |
140 scoped_ptr<base::WeakPtrFactory<JniFrameConsumer> > view_weak_factory_; | |
141 | 126 |
142 // This group of variables is to be used on the network thread. | 127 // This group of variables is to be used on the network thread. |
143 ClientConfig client_config_; | 128 ClientConfig client_config_; |
144 scoped_ptr<ClientContext> client_context_; | 129 scoped_ptr<ClientContext> client_context_; |
145 scoped_ptr<VideoRenderer> video_renderer_; | |
146 scoped_ptr<protocol::ConnectionToHost> connection_; | 130 scoped_ptr<protocol::ConnectionToHost> connection_; |
147 scoped_ptr<ChromotingClient> client_; | 131 scoped_ptr<ChromotingClient> client_; |
148 XmppSignalStrategy::XmppServerConfig xmpp_config_; | 132 XmppSignalStrategy::XmppServerConfig xmpp_config_; |
149 scoped_ptr<XmppSignalStrategy> signaling_; // Must outlive client_ | 133 scoped_ptr<XmppSignalStrategy> signaling_; // Must outlive client_ |
150 | 134 |
151 // Pass this the user's PIN once we have it. To be assigned and accessed on | 135 // Pass this the user's PIN once we have it. To be assigned and accessed on |
152 // the UI thread, but must be posted to the network thread to call it. | 136 // the UI thread, but must be posted to the network thread to call it. |
153 protocol::SecretFetchedCallback pin_callback_; | 137 protocol::SecretFetchedCallback pin_callback_; |
154 | 138 |
155 // Indicates whether to establish a new pairing with this host. This is | 139 // Indicates whether to establish a new pairing with this host. This is |
156 // modified in ProvideSecret(), but thereafter to be used only from the | 140 // modified in ProvideSecret(), but thereafter to be used only from the |
157 // network thread. (This is safe because ProvideSecret() is invoked at most | 141 // network thread. (This is safe because ProvideSecret() is invoked at most |
158 // once per run, and always before any reference to this flag.) | 142 // once per run, and always before any reference to this flag.) |
159 bool create_pairing_; | 143 bool create_pairing_; |
160 | 144 |
161 // The device name to appear in the paired-clients list. Accessed on the | 145 // Chromium code's connection to the OBJ_C message loop. Once created the |
162 // network thread. | 146 // MessageLoop will live for the life of the program. An attempt was made to |
163 std::string device_name_; | 147 // create the primary message loop earlier in the programs life, but a |
| 148 // MessageLoop requires ARC to be disabled. |
| 149 base::MessageLoopForUI* ui_loop_; |
164 | 150 |
165 // If this is true, performance statistics will be periodically written to | 151 // References to native threads. |
166 // the Android log. Used on the network thread. | 152 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; |
167 bool stats_logging_enabled_; | 153 scoped_refptr<AutoThreadTaskRunner> network_task_runner_; |
168 | 154 |
169 friend class base::RefCountedThreadSafe<ChromotingJniInstance>; | 155 scoped_refptr<net::URLRequestContextGetter> url_requester_; |
170 | 156 |
171 DISALLOW_COPY_AND_ASSIGN(ChromotingJniInstance); | 157 friend class base::RefCountedThreadSafe<ClientInstance>; |
| 158 |
| 159 DISALLOW_COPY_AND_ASSIGN(ClientInstance); |
172 }; | 160 }; |
173 | 161 |
174 } // namespace remoting | 162 } // namespace remoting |
175 | 163 |
176 #endif | 164 #endif // REMOTING_IOS_BRIDGE_CLIENT_INSTANCE_H_ |
OLD | NEW |