Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / client / jni / chromoting_jni_instance.h
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"
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"
22
23 namespace remoting {
24
25 namespace protocol {
26 class ClipboardEvent;
27 class CursorShapeInfo;
28 }  // namespace protocol
29
30 class ClientStatusLogger;
31 class VideoRenderer;
32 class TokenFetcherProxy;
33
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> {
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                         const char* capabilities);
53
54   // Terminates the current connection (if it hasn't already failed) and cleans
55   // up. Must be called before destruction.
56   void Disconnect();
57
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);
64
65   // Called by the android app when the token is fetched.
66   void HandleOnThirdPartyTokenFetched(const std::string& token,
67                                       const std::string& shared_secret);
68
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);
74
75   // Schedules a redraw on the display thread. May be called from any thread.
76   void RedrawDesktop();
77
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,
82                       bool button_down);
83   void SendMouseWheelEvent(int delta_x, int delta_y);
84
85   // Sends the provided keyboard scan code to the host.
86   bool SendKeyEvent(int key_code, bool key_down);
87
88   void SendTextEvent(const std::string& text);
89
90   void SendClientMessage(const std::string& type, const std::string& data);
91
92   // Records paint time for statistics logging, if enabled. May be called from
93   // any thread.
94   void RecordPaintTime(int64 paint_time_ms);
95
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;
110
111   // CursorShapeStub implementation.
112   virtual void InjectClipboardEvent(
113       const protocol::ClipboardEvent& event) override;
114
115   // ClipboardStub implementation.
116   virtual void SetCursorShape(const protocol::CursorShapeInfo& shape) override;
117
118  private:
119   // This object is ref-counted, so it cleans itself up.
120   virtual ~ChromotingJniInstance();
121
122   void ConnectToHostOnDisplayThread();
123   void ConnectToHostOnNetworkThread();
124   void DisconnectFromHostOnNetworkThread();
125
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);
131
132   // Sets the device name. Can be called on any thread.
133   void SetDeviceName(const std::string& device_name);
134
135   void SendKeyEventInternal(int usb_key_code, bool key_down);
136
137   // Enables or disables periodic logging of performance statistics. Called on
138   // the network thread.
139   void EnableStatsLogging(bool enabled);
140
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.
144   void LogPerfStats();
145
146   // Used to obtain task runner references and make calls to Java methods.
147   ChromotingJniRuntime* jni_runtime_;
148
149   // ID of the host we are connecting to.
150   std::string host_id_;
151   std::string host_jid_;
152
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_;
157
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_;
167
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_;
171
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_;
177
178   // The device name to appear in the paired-clients list. Accessed on the
179   // network thread.
180   std::string device_name_;
181
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_;
185
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_;
190
191   friend class base::RefCountedThreadSafe<ChromotingJniInstance>;
192
193   base::WeakPtrFactory<ChromotingJniInstance> weak_factory_;
194
195   DISALLOW_COPY_AND_ASSIGN(ChromotingJniInstance);
196 };
197
198 }  // namespace remoting
199
200 #endif