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_JNI_CHROMOTING_JNI_RUNTIME_H_ 6 #define REMOTING_CLIENT_JNI_CHROMOTING_JNI_RUNTIME_H_ 7 8 #include <jni.h> 9 #include <string> 10 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" 17 18 template<typename T> struct DefaultSingletonTraits; 19 20 namespace remoting { 21 22 bool RegisterJni(JNIEnv* env); 23 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 { 29 public: 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(); 33 34 scoped_refptr<AutoThreadTaskRunner> ui_task_runner() { 35 return ui_task_runner_; 36 } 37 38 scoped_refptr<AutoThreadTaskRunner> network_task_runner() { 39 return network_task_runner_; 40 } 41 42 scoped_refptr<AutoThreadTaskRunner> display_task_runner() { 43 return display_task_runner_; 44 } 45 46 scoped_refptr<net::URLRequestContextGetter> url_requester() { 47 return url_requester_; 48 } 49 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, 56 const char* host_jid, 57 const char* host_id, 58 const char* host_pubkey, 59 const char* pairing_id, 60 const char* pairing_secret); 61 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(); 65 66 // Returns the client for the currently-active session. Do not call if 67 // |session| is null. 68 scoped_refptr<ChromotingJniInstance> session() { 69 DCHECK(session_); 70 return session_; 71 } 72 73 // Notifies the user of the current connection status. Call on UI thread. 74 void ReportConnectionStatus(protocol::ConnectionToHost::State state, 75 protocol::ErrorCode error); 76 77 // Pops up a dialog box asking the user to enter a PIN. Call on UI thread. 78 void DisplayAuthenticationPrompt(bool pairing_supported); 79 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); 84 85 // Creates a new Bitmap object to store a video frame. 86 base::android::ScopedJavaLocalRef<jobject> NewBitmap( 87 webrtc::DesktopSize size); 88 89 // Updates video frame bitmap. |bitmap| must be an instance of 90 // android.graphics.Bitmap. Call on the display thread. 91 void UpdateFrameBitmap(jobject bitmap); 92 93 // Updates cursor shape. Call on display thread. 94 void UpdateCursorShape(const protocol::CursorShapeInfo& cursor_shape); 95 96 // Draws the latest image buffer onto the canvas. Call on the display thread. 97 void RedrawCanvas(); 98 99 private: 100 ChromotingJniRuntime(); 101 102 // Forces a DisconnectFromHost() in case there is any active or failed 103 // connection, then proceeds to tear down the Chromium dependencies on which 104 // all sessions depended. Because destruction only occurs at application exit 105 // after all connections have terminated, it is safe to make unretained 106 // cross-thread calls on the class. 107 virtual ~ChromotingJniRuntime(); 108 109 // Detaches JVM from the current thread, then signals. Doesn't own |waiter|. 110 void DetachFromVmAndSignal(base::WaitableEvent* waiter); 111 112 // Used by the Chromium libraries to clean up the base and net libraries' JNI 113 // bindings. It must persist for the lifetime of the singleton. 114 scoped_ptr<base::AtExitManager> at_exit_manager_; 115 116 // Chromium code's connection to the Java message loop. 117 scoped_ptr<base::MessageLoopForUI> ui_loop_; 118 119 // References to native threads. 120 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; 121 scoped_refptr<AutoThreadTaskRunner> network_task_runner_; 122 scoped_refptr<AutoThreadTaskRunner> display_task_runner_; 123 124 scoped_refptr<net::URLRequestContextGetter> url_requester_; 125 126 // Contains all connection-specific state. 127 scoped_refptr<ChromotingJniInstance> session_; 128 129 friend struct DefaultSingletonTraits<ChromotingJniRuntime>; 130 131 DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime); 132 }; 133 134 } // namespace remoting 135 136 #endif 137