OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/shell/android/android_handler.h" | 5 #include "mojo/shell/android/android_handler.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/files/file.h" | |
10 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/scoped_native_library.h" |
12 #include "jni/AndroidHandler_jni.h" | 12 #include "jni/AndroidHandler_jni.h" |
13 #include "mojo/common/data_pipe_utils.h" | 13 #include "mojo/common/data_pipe_utils.h" |
14 #include "mojo/public/c/system/main.h" | 14 #include "mojo/public/c/system/main.h" |
15 #include "mojo/public/cpp/application/application_impl.h" | 15 #include "mojo/public/cpp/application/application_impl.h" |
| 16 #include "mojo/shell/android/run_android_application_function.h" |
| 17 #include "mojo/shell/dynamic_service_runner.h" |
16 | 18 |
17 using base::FilePath; | |
18 using base::File; | |
19 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
20 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
21 using base::android::ConvertJavaStringToUTF8; | 21 using base::android::ConvertJavaStringToUTF8; |
22 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
23 using base::android::GetApplicationContext; | 23 using base::android::GetApplicationContext; |
24 | 24 |
25 namespace mojo { | 25 namespace mojo { |
26 | 26 |
27 namespace {} // namespace | 27 namespace { |
| 28 // This function loads the application library, sets the application context and |
| 29 // thunks and calls into the application MojoMain. To ensure that the thunks are |
| 30 // set correctly we keep it in the Mojo shell .so and pass the function pointer |
| 31 // to the helper libbootstrap.so. |
| 32 void RunAndroidApplication(JNIEnv* env, |
| 33 jobject j_context, |
| 34 const base::FilePath& app_path, |
| 35 jint j_handle) { |
| 36 ScopedMessagePipeHandle handle((mojo::MessagePipeHandle(j_handle))); |
| 37 |
| 38 // Load the library, so that we can set the application context there if |
| 39 // needed. |
| 40 base::NativeLibraryLoadError error; |
| 41 base::ScopedNativeLibrary app_library( |
| 42 base::LoadNativeLibrary(app_path, &error)); |
| 43 if (!app_library.is_valid()) { |
| 44 LOG(ERROR) << "Failed to load app library (error: " << error.ToString() |
| 45 << ")"; |
| 46 return; |
| 47 } |
| 48 |
| 49 // Set the application context if needed. Most applications will need to |
| 50 // access the Android ApplicationContext in which they are run. If the |
| 51 // application library exports the InitApplicationContext function, we will |
| 52 // set it there. |
| 53 const char* init_application_context_name = "InitApplicationContext"; |
| 54 typedef void (*InitApplicationContextFn)( |
| 55 const base::android::JavaRef<jobject>&); |
| 56 InitApplicationContextFn init_application_context = |
| 57 reinterpret_cast<InitApplicationContextFn>( |
| 58 app_library.GetFunctionPointer(init_application_context_name)); |
| 59 if (init_application_context) { |
| 60 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); |
| 61 init_application_context(scoped_context); |
| 62 } |
| 63 |
| 64 // Run the application. |
| 65 base::ScopedNativeLibrary app_library_from_runner( |
| 66 shell::DynamicServiceRunner::LoadAndRunService(app_path, handle.Pass())); |
| 67 } |
| 68 } // namespace |
28 | 69 |
29 void AndroidHandler::RunApplication(ShellPtr shell, URLResponsePtr response) { | 70 void AndroidHandler::RunApplication(ShellPtr shell, URLResponsePtr response) { |
30 JNIEnv* env = AttachCurrentThread(); | 71 JNIEnv* env = AttachCurrentThread(); |
31 ScopedJavaLocalRef<jstring> j_archive_path = | 72 ScopedJavaLocalRef<jstring> j_archive_path = |
32 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | 73 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); |
33 FilePath archive_path(ConvertJavaStringToUTF8(env, j_archive_path.obj())); | 74 base::FilePath archive_path( |
| 75 ConvertJavaStringToUTF8(env, j_archive_path.obj())); |
34 | 76 |
35 mojo::common::BlockingCopyToFile(response->body.Pass(), archive_path); | 77 mojo::common::BlockingCopyToFile(response->body.Pass(), archive_path); |
36 Java_AndroidHandler_bootstrap(env, GetApplicationContext(), | 78 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; |
37 j_archive_path.obj(), | 79 Java_AndroidHandler_bootstrap( |
38 shell.PassMessagePipe().release().value()); | 80 env, GetApplicationContext(), j_archive_path.obj(), |
| 81 shell.PassMessagePipe().release().value(), |
| 82 reinterpret_cast<jlong>(run_android_application_fn)); |
39 } | 83 } |
40 | 84 |
41 void AndroidHandler::Initialize(ApplicationImpl* app) { | 85 void AndroidHandler::Initialize(ApplicationImpl* app) { |
42 } | 86 } |
43 | 87 |
44 bool AndroidHandler::ConfigureIncomingConnection( | 88 bool AndroidHandler::ConfigureIncomingConnection( |
45 ApplicationConnection* connection) { | 89 ApplicationConnection* connection) { |
46 connection->AddService(&content_handler_factory_); | 90 connection->AddService(&content_handler_factory_); |
47 return true; | 91 return true; |
48 } | 92 } |
49 | 93 |
50 bool RegisterAndroidHandlerJni(JNIEnv* env) { | 94 bool RegisterAndroidHandlerJni(JNIEnv* env) { |
51 return RegisterNativesImpl(env); | 95 return RegisterNativesImpl(env); |
52 } | 96 } |
53 | 97 |
54 } // namespace mojo | 98 } // namespace mojo |
OLD | NEW |