blob: 1a6952b56247070363b69824db1eccabe6391e67 [file] [log] [blame]
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001// 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 Murdochbbcdd452013-07-25 10:06:34 +01005#ifndef REMOTING_CLIENT_JNI_CHROMOTING_JNI_RUNTIME_H_
6#define REMOTING_CLIENT_JNI_CHROMOTING_JNI_RUNTIME_H_
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01007
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 Murdoch9ab55632013-07-18 11:57:30 +010013#include "remoting/client/jni/chromoting_jni_instance.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010014#include "remoting/protocol/connection_to_host.h"
15
16template<typename T> struct DefaultSingletonTraits;
17
18namespace remoting {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010019
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 Murdoch9ab55632013-07-18 11:57:30 +010023// exclusively from the UI thread unless otherwise noted.
Ben Murdochbbcdd452013-07-25 10:06:34 +010024class ChromotingJniRuntime {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010025 public:
26 // This class is instantiated at process initialization and persists until
27 // we close. Its components are reused across |ChromotingJniInstance|s.
Ben Murdochbbcdd452013-07-25 10:06:34 +010028 static ChromotingJniRuntime* GetInstance();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010029
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 Murdoch9ab55632013-07-18 11:57:30 +010046 // 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 Murdochbb1529c2013-08-08 10:24:53 +010048 // corresponding call to Disconnect()). To skip the attempt at pair-based
49 // authentication, leave |pairing_id| and |pairing_secret| as empty strings.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010050 void ConnectToHost(const char* username,
51 const char* auth_token,
52 const char* host_jid,
53 const char* host_id,
Ben Murdochbb1529c2013-08-08 10:24:53 +010054 const char* host_pubkey,
55 const char* pairing_id,
56 const char* pairing_secret);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010057
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 Murdochbb1529c2013-08-08 10:24:53 +010069 // Notifies the user of the current connection status. Call on UI thread.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010070 void ReportConnectionStatus(protocol::ConnectionToHost::State state,
71 protocol::ErrorCode error);
72
Ben Murdochbb1529c2013-08-08 10:24:53 +010073 // Pops up a dialog box asking the user to enter a PIN. Call on UI thread.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010074 void DisplayAuthenticationPrompt();
75
Ben Murdochbb1529c2013-08-08 10:24:53 +010076 // 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 Murdoch9ab55632013-07-18 11:57:30 +010081 // 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 Murdoch7dbb3d52013-07-17 14:55:54 +010087 private:
Ben Murdochbbcdd452013-07-25 10:06:34 +010088 ChromotingJniRuntime();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010089
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 Murdochbbcdd452013-07-25 10:06:34 +010095 virtual ~ChromotingJniRuntime();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010096
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 Murdochbbcdd452013-07-25 10:06:34 +0100117 friend struct DefaultSingletonTraits<ChromotingJniRuntime>;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100118
Ben Murdochbbcdd452013-07-25 10:06:34 +0100119 DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100120};
121
122} // namespace remoting
123
124#endif