Home | History | Annotate | Download | only in jni
      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   // Pops up a third party login page to fetch token required for
     86   // authentication. Call on UI thread.
     87   void FetchThirdPartyToken(const GURL& token_url,
     88                             const std::string& client_id,
     89                             const std::string& scope);
     90 
     91   // Creates a new Bitmap object to store a video frame.
     92   base::android::ScopedJavaLocalRef<jobject> NewBitmap(
     93       webrtc::DesktopSize size);
     94 
     95   // Updates video frame bitmap. |bitmap| must be an instance of
     96   // android.graphics.Bitmap. Call on the display thread.
     97   void UpdateFrameBitmap(jobject bitmap);
     98 
     99   // Updates cursor shape. Call on display thread.
    100   void UpdateCursorShape(const protocol::CursorShapeInfo& cursor_shape);
    101 
    102   // Draws the latest image buffer onto the canvas. Call on the display thread.
    103   void RedrawCanvas();
    104 
    105  private:
    106   ChromotingJniRuntime();
    107 
    108   // Forces a DisconnectFromHost() in case there is any active or failed
    109   // connection, then proceeds to tear down the Chromium dependencies on which
    110   // all sessions depended. Because destruction only occurs at application exit
    111   // after all connections have terminated, it is safe to make unretained
    112   // cross-thread calls on the class.
    113   virtual ~ChromotingJniRuntime();
    114 
    115   // Detaches JVM from the current thread, then signals. Doesn't own |waiter|.
    116   void DetachFromVmAndSignal(base::WaitableEvent* waiter);
    117 
    118   // Used by the Chromium libraries to clean up the base and net libraries' JNI
    119   // bindings. It must persist for the lifetime of the singleton.
    120   scoped_ptr<base::AtExitManager> at_exit_manager_;
    121 
    122   // Chromium code's connection to the Java message loop.
    123   scoped_ptr<base::MessageLoopForUI> ui_loop_;
    124 
    125   // References to native threads.
    126   scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
    127   scoped_refptr<AutoThreadTaskRunner> network_task_runner_;
    128   scoped_refptr<AutoThreadTaskRunner> display_task_runner_;
    129 
    130   scoped_refptr<net::URLRequestContextGetter> url_requester_;
    131 
    132   // Contains all connection-specific state.
    133   scoped_refptr<ChromotingJniInstance> session_;
    134 
    135   friend struct DefaultSingletonTraits<ChromotingJniRuntime>;
    136 
    137   DISALLOW_COPY_AND_ASSIGN(ChromotingJniRuntime);
    138 };
    139 
    140 }  // namespace remoting
    141 
    142 #endif
    143