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/native_application_support.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 namespace shell { | |
27 | |
28 namespace { | |
29 | |
30 // This function loads the application library, sets the application context and | |
31 // thunks and calls into the application MojoMain. To ensure that the thunks are | |
32 // set correctly we keep it in the Mojo shell .so and pass the function pointer | |
33 // to the helper libbootstrap.so. | |
34 void RunAndroidApplication(JNIEnv* env, | |
35 jobject j_context, | |
36 const base::FilePath& app_path, | |
37 jint j_handle) { | |
38 InterfaceRequest<Application> application_request = | |
39 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle))); | |
40 | |
41 // Load the library, so that we can set the application context there if | |
42 // needed. | |
43 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! | |
44 base::NativeLibrary app_library = | |
45 LoadNativeApplication(app_path, NativeApplicationCleanup::DELETE); | |
46 if (!app_library) | |
47 return; | |
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 base::GetFunctionPointerFromNativeLibrary( | |
59 app_library, init_application_context_name)); | |
60 if (init_application_context) { | |
61 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); | |
62 init_application_context(scoped_context); | |
63 } | |
64 | |
65 // Run the application. | |
66 RunNativeApplication(app_library, application_request.Pass()); | |
67 // TODO(vtl): See note about unloading and thread-local destructors above | |
68 // declaration of |LoadNativeApplication()|. | |
69 base::UnloadNativeLibrary(app_library); | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { | |
75 } | |
76 | |
77 AndroidHandler::~AndroidHandler() { | |
78 } | |
79 | |
80 void AndroidHandler::RunApplication( | |
81 InterfaceRequest<Application> application_request, | |
82 URLResponsePtr response) { | |
83 JNIEnv* env = AttachCurrentThread(); | |
84 ScopedJavaLocalRef<jstring> j_archive_path = | |
85 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | |
86 base::FilePath archive_path( | |
87 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | |
88 | |
89 common::BlockingCopyToFile(response->body.Pass(), archive_path); | |
90 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; | |
91 Java_AndroidHandler_bootstrap( | |
92 env, GetApplicationContext(), j_archive_path.obj(), | |
93 application_request.PassMessagePipe().release().value(), | |
94 reinterpret_cast<jlong>(run_android_application_fn)); | |
95 } | |
96 | |
97 void AndroidHandler::Initialize(ApplicationImpl* app) { | |
98 } | |
99 | |
100 bool AndroidHandler::ConfigureIncomingConnection( | |
101 ApplicationConnection* connection) { | |
102 connection->AddService(&content_handler_factory_); | |
103 return true; | |
104 } | |
105 | |
106 bool RegisterAndroidHandlerJni(JNIEnv* env) { | |
107 return RegisterNativesImpl(env); | |
108 } | |
109 | |
110 } // namespace shell | |
111 } // namespace mojo | |
OLD | NEW |