Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 5 | #ifndef REMOTING_CLIENT_JNI_CHROMOTING_JNI_RUNTIME_H_ |
| 6 | #define REMOTING_CLIENT_JNI_CHROMOTING_JNI_RUNTIME_H_ |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 7 | |
| 8 | #include <jni.h> |
| 9 | |
| 10 | #include "base/at_exit.h" |
| 11 | #include "net/url_request/url_request_context_getter.h" |
| 12 | #include "remoting/base/auto_thread.h" |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 13 | #include "remoting/client/jni/chromoting_jni_instance.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 14 | #include "remoting/protocol/connection_to_host.h" |
| 15 | |
| 16 | template<typename T> struct DefaultSingletonTraits; |
| 17 | |
| 18 | namespace remoting { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 19 | |
| 20 | // Houses the global resources on which the Chromoting components run |
| 21 | // (e.g. message loops and task runners). Proxies outgoing JNI calls from its |
| 22 | // ChromotingJniInstance member to Java. All its methods should be invoked |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 23 | // exclusively from the UI thread unless otherwise noted. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 24 | class ChromotingJniRuntime { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 25 | public: |
| 26 | // This class is instantiated at process initialization and persists until |
| 27 | // we close. Its components are reused across |ChromotingJniInstance|s. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 28 | static ChromotingJniRuntime* GetInstance(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 29 | |
| 30 | scoped_refptr<AutoThreadTaskRunner> ui_task_runner() { |
| 31 | return ui_task_runner_; |
| 32 | } |
| 33 | |
| 34 | scoped_refptr<AutoThreadTaskRunner> network_task_runner() { |
| 35 | return network_task_runner_; |
| 36 | } |
| 37 | |
| 38 | scoped_refptr<AutoThreadTaskRunner> display_task_runner() { |
| 39 | return display_task_runner_; |
| 40 | } |
| 41 | |
| 42 | scoped_refptr<net::URLRequestContextGetter> url_requester() { |
| 43 | return url_requester_; |
| 44 | } |
| 45 | |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 46 | // Initiates a connection with the specified host. Only call when a host |
| 47 | // connection is active (i.e. between a call to Connect() and the |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 48 | // corresponding call to Disconnect()). To skip the attempt at pair-based |
| 49 | // authentication, leave |pairing_id| and |pairing_secret| as empty strings. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 50 | void ConnectToHost(const char* username, |
| 51 | const char* auth_token, |
| 52 | const char* host_jid, |
| 53 | const char* host_id, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 54 | const char* host_pubkey, |
| 55 | const char* pairing_id, |
| 56 | const char* pairing_secret); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 57 | |
| 58 | // Terminates any ongoing connection attempt and cleans up by nullifying |
| 59 | // |session_|. This is a no-op unless |session| is currently non-null. |
| 60 | void DisconnectFromHost(); |
| 61 | |
| 62 | // Returns the client for the currently-active session. Do not call if |
| 63 | // |session| is null. |
| 64 | scoped_refptr<ChromotingJniInstance> session() { |
| 65 | DCHECK(session_); |
| 66 | return session_; |
| 67 | } |
| 68 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 69 | // Notifies the user of the current connection status. Call on UI thread. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 70 | void ReportConnectionStatus(protocol::ConnectionToHost::State state, |
| 71 | protocol::ErrorCode error); |
| 72 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 73 | // Pops up a dialog box asking the user to enter a PIN. Call on UI thread. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 74 | void DisplayAuthenticationPrompt(); |
| 75 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 76 | // Saves new pairing credentials to permanent storage. Call on UI thread. |
| 77 | void CommitPairingCredentials(const std::string& host, |
| 78 | const std::string& id, |
| 79 | const std::string& secret); |
| 80 | |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 81 | // Updates image dimensions and canvas memory space. Call on display thread. |
| 82 | void UpdateImageBuffer(int width, int height, jobject buffer); |
| 83 | |
| 84 | // Draws the latest image buffer onto the canvas. Call on the display thread. |
| 85 | void RedrawCanvas(); |
| 86 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 87 | private: |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 88 | ChromotingJniRuntime(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 89 | |
| 90 | // Forces a DisconnectFromHost() in case there is any active or failed |
| 91 | // connection, then proceeds to tear down the Chromium dependencies on which |
| 92 | // all sessions depended. Because destruction only occurs at application exit |
| 93 | // after all connections have terminated, it is safe to make unretained |
| 94 | // cross-thread calls on the class. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 95 | virtual ~ChromotingJniRuntime(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 96 | |
| 97 | // Reference to the Java class into which we make JNI calls. |
| 98 | jclass class_; |
| 99 | |
| 100 | // Used by the Chromium libraries to clean up the base and net libraries' JNI |
| 101 | // bindings. It must persist for the lifetime of the singleton. |
| 102 | scoped_ptr<base::AtExitManager> at_exit_manager_; |
| 103 | |
| 104 | // Chromium code's connection to the Java message loop. |
| 105 | scoped_ptr<base::MessageLoopForUI> ui_loop_; |
| 106 | |
| 107 | // References to native threads. |
| 108 | scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; |
| 109 | scoped_refptr<AutoThreadTaskRunner> network_task_runner_; |
| 110 | scoped_refptr<AutoThreadTaskRunner> display_task_runner_; |
| 111 | |
| 112 | scoped_refptr<net::URLRequestContextGetter> url_requester_; |
| 113 | |
| 114 | // Contains all connection-specific state. |
| 115 | scoped_refptr<ChromotingJniInstance> session_; |
| 116 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 117 | friend struct DefaultSingletonTraits<ChromotingJniRuntime>; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 118 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace remoting |
| 123 | |
| 124 | #endif |