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.
5 #ifndef REMOTING_CLIENT_JNI_CHROMOTING_JNI_RUNTIME_H_
6 #define REMOTING_CLIENT_JNI_CHROMOTING_JNI_RUNTIME_H_
11 #include "base/android/scoped_java_ref.h"
12 #include "net/url_request/url_request_context_getter.h"
13 #include "remoting/base/auto_thread.h"
14 #include "remoting/client/jni/chromoting_jni_instance.h"
15 #include "remoting/protocol/connection_to_host.h"
18 template<typename T
> struct DefaultSingletonTraits
;
23 bool RegisterChromotingJniRuntime(JNIEnv
* env
);
25 // Houses the global resources on which the Chromoting components run
26 // (e.g. message loops and task runners). Proxies outgoing JNI calls from its
27 // ChromotingJniInstance member to Java. All its methods should be invoked
28 // exclusively from the UI thread unless otherwise noted.
29 class ChromotingJniRuntime
{
31 // This class is instantiated at process initialization and persists until
32 // we close. Its components are reused across |ChromotingJniInstance|s.
33 static ChromotingJniRuntime
* GetInstance();
35 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner() {
36 return ui_task_runner_
;
39 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner() {
40 return network_task_runner_
;
43 scoped_refptr
<AutoThreadTaskRunner
> display_task_runner() {
44 return display_task_runner_
;
47 scoped_refptr
<net::URLRequestContextGetter
> url_requester() {
48 return url_requester_
;
51 // Initiates a connection with the specified host. Only call when a host
52 // connection is active (i.e. between a call to Connect() and the
53 // corresponding call to Disconnect()). To skip the attempt at pair-based
54 // authentication, leave |pairing_id| and |pairing_secret| as empty strings.
55 void ConnectToHost(const char* username
,
56 const char* auth_token
,
59 const char* host_pubkey
,
60 const char* pairing_id
,
61 const char* pairing_secret
,
62 const char* capabilities
);
64 // Terminates any ongoing connection attempt and cleans up by nullifying
65 // |session_|. This is a no-op unless |session| is currently non-null.
66 void DisconnectFromHost();
68 // Returns the client for the currently-active session. Do not call if
70 scoped_refptr
<ChromotingJniInstance
> session() {
71 DCHECK(session_
.get());
75 // Notifies Java code of the current connection status. Call on UI thread.
76 void OnConnectionState(protocol::ConnectionToHost::State state
,
77 protocol::ErrorCode error
);
79 // Pops up a dialog box asking the user to enter a PIN. Call on UI thread.
80 void DisplayAuthenticationPrompt(bool pairing_supported
);
82 // Saves new pairing credentials to permanent storage. Call on UI thread.
83 void CommitPairingCredentials(const std::string
& host
,
84 const std::string
& id
,
85 const std::string
& secret
);
87 // Pops up a third party login page to fetch token required for
88 // authentication. Call on UI thread.
89 void FetchThirdPartyToken(const GURL
& token_url
,
90 const std::string
& client_id
,
91 const std::string
& scope
);
93 // Pass on the set of negotiated capabilities to the client.
94 void SetCapabilities(const std::string
& capabilities
);
96 // Passes on the deconstructed ExtensionMessage to the client to handle
98 void HandleExtensionMessage(const std::string
& type
,
99 const std::string
& message
);
101 // Creates a new Bitmap object to store a video frame.
102 base::android::ScopedJavaLocalRef
<jobject
> NewBitmap(int width
, int height
);
104 // Updates video frame bitmap. |bitmap| must be an instance of
105 // android.graphics.Bitmap. Call on the display thread.
106 void UpdateFrameBitmap(jobject bitmap
);
108 // Updates cursor shape. Call on display thread.
109 void UpdateCursorShape(const protocol::CursorShapeInfo
& cursor_shape
);
111 // Draws the latest image buffer onto the canvas. Call on the display thread.
115 ChromotingJniRuntime();
117 // Forces a DisconnectFromHost() in case there is any active or failed
118 // connection, then proceeds to tear down the Chromium dependencies on which
119 // all sessions depended. Because destruction only occurs at application exit
120 // after all connections have terminated, it is safe to make unretained
121 // cross-thread calls on the class.
122 virtual ~ChromotingJniRuntime();
124 // Detaches JVM from the current thread, then signals. Doesn't own |waiter|.
125 void DetachFromVmAndSignal(base::WaitableEvent
* waiter
);
127 // Chromium code's connection to the Java message loop.
128 scoped_ptr
<base::MessageLoopForUI
> ui_loop_
;
130 // References to native threads.
131 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner_
;
132 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner_
;
133 scoped_refptr
<AutoThreadTaskRunner
> display_task_runner_
;
135 scoped_refptr
<net::URLRequestContextGetter
> url_requester_
;
137 // Contains all connection-specific state.
138 scoped_refptr
<ChromotingJniInstance
> session_
;
140 friend struct base::DefaultSingletonTraits
<ChromotingJniRuntime
>;
142 DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime
);
145 } // namespace remoting