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