OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/shell/android/android_handler.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/logging.h" | |
11 #include "base/scoped_native_library.h" | |
12 #include "jni/AndroidHandler_jni.h" | |
13 #include "mojo/common/data_pipe_utils.h" | |
14 #include "mojo/public/c/system/main.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" | |
18 | |
19 using base::android::AttachCurrentThread; | |
20 using base::android::ScopedJavaLocalRef; | |
21 using base::android::ConvertJavaStringToUTF8; | |
22 using base::android::ConvertUTF8ToJavaString; | |
23 using base::android::GetApplicationContext; | |
24 | |
25 namespace mojo { | |
26 | |
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 | |
69 | |
70 void AndroidHandler::RunApplication(ShellPtr shell, URLResponsePtr response) { | |
71 JNIEnv* env = AttachCurrentThread(); | |
72 ScopedJavaLocalRef<jstring> j_archive_path = | |
73 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | |
74 base::FilePath archive_path( | |
75 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | |
76 | |
77 mojo::common::BlockingCopyToFile(response->body.Pass(), archive_path); | |
78 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; | |
79 Java_AndroidHandler_bootstrap( | |
80 env, GetApplicationContext(), j_archive_path.obj(), | |
81 shell.PassMessagePipe().release().value(), | |
82 reinterpret_cast<jlong>(run_android_application_fn)); | |
83 } | |
84 | |
85 void AndroidHandler::Initialize(ApplicationImpl* app) { | |
86 } | |
87 | |
88 bool AndroidHandler::ConfigureIncomingConnection( | |
89 ApplicationConnection* connection) { | |
90 connection->AddService(&content_handler_factory_); | |
91 return true; | |
92 } | |
93 | |
94 bool RegisterAndroidHandlerJni(JNIEnv* env) { | |
95 return RegisterNativesImpl(env); | |
96 } | |
97 | |
98 } // namespace mojo | |
OLD | NEW |