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
18template<typename T> struct DefaultSingletonTraits;
19
20namespace remoting {
21
22bool 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.
28class 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                     const char* capabilities);
62
63  // Terminates any ongoing connection attempt and cleans up by nullifying
64  // |session_|. This is a no-op unless |session| is currently non-null.
65  void DisconnectFromHost();
66
67  // Returns the client for the currently-active session. Do not call if
68  // |session| is null.
69  scoped_refptr<ChromotingJniInstance> session() {
70    DCHECK(session_);
71    return session_;
72  }
73
74  // Notifies Java code of the current connection status. Call on UI thread.
75  void OnConnectionState(protocol::ConnectionToHost::State state,
76                         protocol::ErrorCode error);
77
78  // Pops up a dialog box asking the user to enter a PIN. Call on UI thread.
79  void DisplayAuthenticationPrompt(bool pairing_supported);
80
81  // Saves new pairing credentials to permanent storage. Call on UI thread.
82  void CommitPairingCredentials(const std::string& host,
83                                const std::string& id,
84                                const std::string& secret);
85
86  // Pops up a third party login page to fetch token required for
87  // authentication. Call on UI thread.
88  void FetchThirdPartyToken(const GURL& token_url,
89                            const std::string& client_id,
90                            const std::string& scope);
91
92  // Pass on the set of negotiated capabilities to the client.
93  void SetCapabilities(const std::string& capabilities);
94
95  // Passes on the deconstructed ExtensionMessage to the client to handle
96  // appropriately.
97  void HandleExtensionMessage(const std::string& type,
98                              const std::string& message);
99
100  // Creates a new Bitmap object to store a video frame.
101  base::android::ScopedJavaLocalRef<jobject> NewBitmap(
102      webrtc::DesktopSize size);
103
104  // Updates video frame bitmap. |bitmap| must be an instance of
105  // android.graphics.Bitmap. Call on the display thread.
106  void UpdateFrameBitmap(jobject bitmap);
107
108  // Updates cursor shape. Call on display thread.
109  void UpdateCursorShape(const protocol::CursorShapeInfo& cursor_shape);
110
111  // Draws the latest image buffer onto the canvas. Call on the display thread.
112  void RedrawCanvas();
113
114 private:
115  ChromotingJniRuntime();
116
117  // Forces a DisconnectFromHost() in case there is any active or failed
118  // connection, then proceeds to tear down the Chromium dependencies on which
119  // all sessions depended. Because destruction only occurs at application exit
120  // after all connections have terminated, it is safe to make unretained
121  // cross-thread calls on the class.
122  virtual ~ChromotingJniRuntime();
123
124  // Detaches JVM from the current thread, then signals. Doesn't own |waiter|.
125  void DetachFromVmAndSignal(base::WaitableEvent* waiter);
126
127  // Used by the Chromium libraries to clean up the base and net libraries' JNI
128  // bindings. It must persist for the lifetime of the singleton.
129  scoped_ptr<base::AtExitManager> at_exit_manager_;
130
131  // Chromium code's connection to the Java message loop.
132  scoped_ptr<base::MessageLoopForUI> ui_loop_;
133
134  // References to native threads.
135  scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
136  scoped_refptr<AutoThreadTaskRunner> network_task_runner_;
137  scoped_refptr<AutoThreadTaskRunner> display_task_runner_;
138
139  scoped_refptr<net::URLRequestContextGetter> url_requester_;
140
141  // Contains all connection-specific state.
142  scoped_refptr<ChromotingJniInstance> session_;
143
144  friend struct DefaultSingletonTraits<ChromotingJniRuntime>;
145
146  DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime);
147};
148
149}  // namespace remoting
150
151#endif
152