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 "base/at_exit.h"
13 #include "net/url_request/url_request_context_getter.h"
14 #include "remoting/base/auto_thread.h"
15 #include "remoting/client/jni/chromoting_jni_instance.h"
16 #include "remoting/protocol/connection_to_host.h"
18 template<typename T
> struct DefaultSingletonTraits
;
22 bool RegisterJni(JNIEnv
* env
);
24 // Houses the global resources on which the Chromoting components run
25 // (e.g. message loops and task runners). Proxies outgoing JNI calls from its
26 // ChromotingJniInstance member to Java. All its methods should be invoked
27 // exclusively from the UI thread unless otherwise noted.
28 class ChromotingJniRuntime
{
30 // This class is instantiated at process initialization and persists until
31 // we close. Its components are reused across |ChromotingJniInstance|s.
32 static ChromotingJniRuntime
* GetInstance();
34 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner() {
35 return ui_task_runner_
;
38 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner() {
39 return network_task_runner_
;
42 scoped_refptr
<AutoThreadTaskRunner
> display_task_runner() {
43 return display_task_runner_
;
46 scoped_refptr
<net::URLRequestContextGetter
> url_requester() {
47 return url_requester_
;
50 // Initiates a connection with the specified host. Only call when a host
51 // connection is active (i.e. between a call to Connect() and the
52 // corresponding call to Disconnect()). To skip the attempt at pair-based
53 // authentication, leave |pairing_id| and |pairing_secret| as empty strings.
54 void ConnectToHost(const char* username
,
55 const char* auth_token
,
58 const char* host_pubkey
,
59 const char* pairing_id
,
60 const char* pairing_secret
,
61 const char* capabilities
);
63 // Terminates any ongoing connection attempt and cleans up by nullifying
64 // |session_|. This is a no-op unless |session| is currently non-null.
65 void DisconnectFromHost();
67 // Returns the client for the currently-active session. Do not call if
69 scoped_refptr
<ChromotingJniInstance
> session() {
70 DCHECK(session_
.get());
74 // Notifies Java code of the current connection status. Call on UI thread.
75 void OnConnectionState(protocol::ConnectionToHost::State state
,
76 protocol::ErrorCode error
);
78 // Pops up a dialog box asking the user to enter a PIN. Call on UI thread.
79 void DisplayAuthenticationPrompt(bool pairing_supported
);
81 // Saves new pairing credentials to permanent storage. Call on UI thread.
82 void CommitPairingCredentials(const std::string
& host
,
83 const std::string
& id
,
84 const std::string
& secret
);
86 // Pops up a third party login page to fetch token required for
87 // authentication. Call on UI thread.
88 void FetchThirdPartyToken(const GURL
& token_url
,
89 const std::string
& client_id
,
90 const std::string
& scope
);
92 // Pass on the set of negotiated capabilities to the client.
93 void SetCapabilities(const std::string
& capabilities
);
95 // Passes on the deconstructed ExtensionMessage to the client to handle
97 void HandleExtensionMessage(const std::string
& type
,
98 const std::string
& message
);
100 // Creates a new Bitmap object to store a video frame.
101 base::android::ScopedJavaLocalRef
<jobject
> NewBitmap(
102 webrtc::DesktopSize size
);
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 // Used by the Chromium libraries to clean up the base and net libraries' JNI
128 // bindings. It must persist for the lifetime of the singleton.
129 scoped_ptr
<base::AtExitManager
> at_exit_manager_
;
131 // Chromium code's connection to the Java message loop.
132 scoped_ptr
<base::MessageLoopForUI
> ui_loop_
;
134 // References to native threads.
135 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner_
;
136 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner_
;
137 scoped_refptr
<AutoThreadTaskRunner
> display_task_runner_
;
139 scoped_refptr
<net::URLRequestContextGetter
> url_requester_
;
141 // Contains all connection-specific state.
142 scoped_refptr
<ChromotingJniInstance
> session_
;
144 friend struct DefaultSingletonTraits
<ChromotingJniRuntime
>;
146 DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime
);
149 } // namespace remoting