Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Unified Diff: remoting/client/jni/chromoting_jni.cc

Issue 19253003: Separate singleton out of ChromotingJNIInstance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add missing checks and fix class names' CamelCase Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/client/jni/chromoting_jni.cc
diff --git a/remoting/client/jni/chromoting_jni.cc b/remoting/client/jni/chromoting_jni.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1016a49bcaebd3d02c356237ec5b92aed2d14923
--- /dev/null
+++ b/remoting/client/jni/chromoting_jni.cc
@@ -0,0 +1,109 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "remoting/client/jni/chromoting_jni.h"
+
+#include "base/android/base_jni_registrar.h"
+#include "base/android/jni_android.h"
+#include "base/memory/singleton.h"
+#include "net/android/net_jni_registrar.h"
+#include "remoting/base/url_request_context.h"
+#include "remoting/client/jni/chromoting_jni_instance.h"
+
+namespace remoting {
+
+// static
+ChromotingJni* ChromotingJni::GetInstance() {
+ return Singleton<ChromotingJni>::get();
+}
+
+ChromotingJni::ChromotingJni() {
+ JNIEnv* env = base::android::AttachCurrentThread();
Wez 2013/07/16 13:46:56 nit: Add a comment to explain that this is require
solb 2013/07/16 19:23:02 Done.
+
+ // The base and networks stacks must be registered with JNI in order to work
+ // on Android. An AtExitManager cleans this up at world's end.
Wez 2013/07/16 13:46:56 nit: An -> The nit: world's end -> process exit
solb 2013/07/16 19:23:02 Done.
+ collector_.reset(new base::AtExitManager());
+ base::android::RegisterJni(env);
+ net::android::RegisterJni(env);
+
+ // On Android, the UI thread is managed by Java, so we need to attach and
+ // start a special type of message loop to allow Chromium code to run tasks.
+ LOG(INFO) << "Starting main message loop";
+ ui_loop_.reset(new base::MessageLoopForUI());
+ ui_loop_->Start();
+
+ LOG(INFO) << "Spawning additional threads";
+ // TODO(solb) Stop pretending to control the managed UI thread's lifetime.
+ ui_task_runner_ = new AutoThreadTaskRunner(ui_loop_->message_loop_proxy(),
+ base::MessageLoop::QuitClosure());
+ network_task_runner_ = AutoThread::CreateWithType("native_net",
+ ui_task_runner_,
+ base::MessageLoop::TYPE_IO);
+ display_task_runner_ = AutoThread::Create("native_disp",
+ ui_task_runner_);
+
+ url_requester_ = new URLRequestContextGetter(ui_task_runner_,
+ network_task_runner_);
+
+ class_ = static_cast<jclass>(env->NewGlobalRef(env->FindClass(JAVA_CLASS)));
+}
+
+ChromotingJni::~ChromotingJni() {
+ // The singleton should only ever be destroyed on the main thread.
+ DCHECK(ui_task_runner_->BelongsToCurrentThread());
+
+ // The session must be shut down first, since it depends on our other
+ // components' still being alive.
+ DisconnectFromHost();
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ env->DeleteGlobalRef(class_);
+ // TODO(solb): crbug.com/259594 Detach all threads from JVM here.
+}
+
+void ChromotingJni::ConnectToHost(const char* username,
+ const char* auth_token,
+ const char* host_jid,
+ const char* host_id,
+ const char* host_pubkey) {
+ DCHECK(ui_task_runner_->BelongsToCurrentThread());
+ DCHECK(!session_);
+ session_ = new ChromotingJniInstance(username,
+ auth_token,
+ host_jid,
+ host_id,
+ host_pubkey);
+}
+
+void ChromotingJni::DisconnectFromHost() {
+ DCHECK(ui_task_runner_->BelongsToCurrentThread());
+ if (session_) {
+ session_->Cleanup();
+ session_ = NULL;
+ }
+}
+
+void ChromotingJni::ReportConnectionStatus(
+ protocol::ConnectionToHost::State state,
+ protocol::ErrorCode error) {
+ DCHECK(ui_task_runner_->BelongsToCurrentThread());
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ env->CallStaticVoidMethod(
+ class_,
+ env->GetStaticMethodID(class_, "reportConnectionStatus", "(II)V"),
+ state,
+ error);
+}
+
+void ChromotingJni::DisplayAuthenticationPrompt() {
+ DCHECK(ui_task_runner_->BelongsToCurrentThread());
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ env->CallStaticVoidMethod(
+ class_,
+ env->GetStaticMethodID(class_, "displayAuthenticationPrompt", "()V"));
+}
+
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698