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