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"
17 template<typename T
> struct DefaultSingletonTraits
;
21 bool RegisterJni(JNIEnv
* env
);
23 // Houses the global resources on which the Chromoting components run
24 // (e.g. message loops and task runners). Proxies outgoing JNI calls from its
25 // ChromotingJniInstance member to Java. All its methods should be invoked
26 // exclusively from the UI thread unless otherwise noted.
27 class ChromotingJniRuntime
{
29 // This class is instantiated at process initialization and persists until
30 // we close. Its components are reused across |ChromotingJniInstance|s.
31 static ChromotingJniRuntime
* GetInstance();
33 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner() {
34 return ui_task_runner_
;
37 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner() {
38 return network_task_runner_
;
41 scoped_refptr
<AutoThreadTaskRunner
> display_task_runner() {
42 return display_task_runner_
;
45 scoped_refptr
<net::URLRequestContextGetter
> url_requester() {
46 return url_requester_
;
49 // Initiates a connection with the specified host. Only call when a host
50 // connection is active (i.e. between a call to Connect() and the
51 // corresponding call to Disconnect()). To skip the attempt at pair-based
52 // authentication, leave |pairing_id| and |pairing_secret| as empty strings.
53 void ConnectToHost(const char* username
,
54 const char* auth_token
,
57 const char* host_pubkey
,
58 const char* pairing_id
,
59 const char* pairing_secret
,
60 const char* capabilities
);
62 // Terminates any ongoing connection attempt and cleans up by nullifying
63 // |session_|. This is a no-op unless |session| is currently non-null.
64 void DisconnectFromHost();
66 // Returns the client for the currently-active session. Do not call if
68 scoped_refptr
<ChromotingJniInstance
> session() {
69 DCHECK(session_
.get());
73 // Notifies Java code of the current connection status. Call on UI thread.
74 void OnConnectionState(protocol::ConnectionToHost::State state
,
75 protocol::ErrorCode error
);
77 // Pops up a dialog box asking the user to enter a PIN. Call on UI thread.
78 void DisplayAuthenticationPrompt(bool pairing_supported
);
80 // Saves new pairing credentials to permanent storage. Call on UI thread.
81 void CommitPairingCredentials(const std::string
& host
,
82 const std::string
& id
,
83 const std::string
& secret
);
85 // Pops up a third party login page to fetch token required for
86 // authentication. Call on UI thread.
87 void FetchThirdPartyToken(const GURL
& token_url
,
88 const std::string
& client_id
,
89 const std::string
& scope
);
91 // Pass on the set of negotiated capabilities to the client.
92 void SetCapabilities(const std::string
& capabilities
);
94 // Passes on the deconstructed ExtensionMessage to the client to handle
96 void HandleExtensionMessage(const std::string
& type
,
97 const std::string
& message
);
99 // Creates a new Bitmap object to store a video frame.
100 base::android::ScopedJavaLocalRef
<jobject
> NewBitmap(
101 webrtc::DesktopSize size
);
103 // Updates video frame bitmap. |bitmap| must be an instance of
104 // android.graphics.Bitmap. Call on the display thread.
105 void UpdateFrameBitmap(jobject bitmap
);
107 // Updates cursor shape. Call on display thread.
108 void UpdateCursorShape(const protocol::CursorShapeInfo
& cursor_shape
);
110 // Draws the latest image buffer onto the canvas. Call on the display thread.
114 ChromotingJniRuntime();
116 // Forces a DisconnectFromHost() in case there is any active or failed
117 // connection, then proceeds to tear down the Chromium dependencies on which
118 // all sessions depended. Because destruction only occurs at application exit
119 // after all connections have terminated, it is safe to make unretained
120 // cross-thread calls on the class.
121 virtual ~ChromotingJniRuntime();
123 // Detaches JVM from the current thread, then signals. Doesn't own |waiter|.
124 void DetachFromVmAndSignal(base::WaitableEvent
* waiter
);
126 // Chromium code's connection to the Java message loop.
127 scoped_ptr
<base::MessageLoopForUI
> ui_loop_
;
129 // References to native threads.
130 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner_
;
131 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner_
;
132 scoped_refptr
<AutoThreadTaskRunner
> display_task_runner_
;
134 scoped_refptr
<net::URLRequestContextGetter
> url_requester_
;
136 // Contains all connection-specific state.
137 scoped_refptr
<ChromotingJniInstance
> session_
;
139 friend struct DefaultSingletonTraits
<ChromotingJniRuntime
>;
141 DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime
);
144 } // namespace remoting