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/standalone/android/android_handler.h" | 5 #include "mojo/shell/standalone/android/android_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/android/context_utils.h" | 10 #include "base/android/context_utils.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 namespace { | 34 namespace { |
35 | 35 |
36 // This function loads the application library, sets the application context and | 36 // This function loads the application library, sets the application context and |
37 // thunks and calls into the application MojoMain. To ensure that the thunks are | 37 // thunks and calls into the application MojoMain. To ensure that the thunks are |
38 // set correctly we keep it in the Mojo shell .so and pass the function pointer | 38 // set correctly we keep it in the Mojo shell .so and pass the function pointer |
39 // to the helper libbootstrap.so. | 39 // to the helper libbootstrap.so. |
40 void RunAndroidApplication(JNIEnv* env, | 40 void RunAndroidApplication(JNIEnv* env, |
41 jobject j_context, | 41 jobject j_context, |
42 const base::FilePath& app_path, | 42 const base::FilePath& app_path, |
43 jint j_handle) { | 43 jint j_handle) { |
44 InterfaceRequest<mojom::Application> application_request = | 44 InterfaceRequest<mojom::ShellClient> request = |
45 MakeRequest<mojom::Application>( | 45 MakeRequest<mojom::ShellClient>( |
46 MakeScopedHandle(MessagePipeHandle(j_handle))); | 46 MakeScopedHandle(MessagePipeHandle(j_handle))); |
47 | 47 |
48 // Load the library, so that we can set the application context there if | 48 // Load the library, so that we can set the application context there if |
49 // needed. | 49 // needed. |
50 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! | 50 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! |
51 base::NativeLibrary app_library = LoadNativeApplication(app_path); | 51 base::NativeLibrary app_library = LoadNativeApplication(app_path); |
52 if (!app_library) | 52 if (!app_library) |
53 return; | 53 return; |
54 | 54 |
55 // Set the application context if needed. Most applications will need to | 55 // Set the application context if needed. Most applications will need to |
56 // access the Android ApplicationContext in which they are run. If the | 56 // access the Android ApplicationContext in which they are run. If the |
57 // application library exports the InitApplicationContext function, we will | 57 // application library exports the InitApplicationContext function, we will |
58 // set it there. | 58 // set it there. |
59 const char* init_application_context_name = "InitApplicationContext"; | 59 const char* init_application_context_name = "InitApplicationContext"; |
60 typedef void (*InitApplicationContextFn)( | 60 typedef void (*InitApplicationContextFn)( |
61 const base::android::JavaRef<jobject>&); | 61 const base::android::JavaRef<jobject>&); |
62 InitApplicationContextFn init_application_context = | 62 InitApplicationContextFn init_application_context = |
63 reinterpret_cast<InitApplicationContextFn>( | 63 reinterpret_cast<InitApplicationContextFn>( |
64 base::GetFunctionPointerFromNativeLibrary( | 64 base::GetFunctionPointerFromNativeLibrary( |
65 app_library, init_application_context_name)); | 65 app_library, init_application_context_name)); |
66 if (init_application_context) { | 66 if (init_application_context) { |
67 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); | 67 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); |
68 init_application_context(scoped_context); | 68 init_application_context(scoped_context); |
69 } | 69 } |
70 | 70 |
71 // Run the application. | 71 // Run the application. |
72 RunNativeApplication(app_library, std::move(application_request)); | 72 RunNativeApplication(app_library, std::move(request)); |
73 // TODO(vtl): See note about unloading and thread-local destructors above | 73 // TODO(vtl): See note about unloading and thread-local destructors above |
74 // declaration of |LoadNativeApplication()|. | 74 // declaration of |LoadNativeApplication()|. |
75 base::UnloadNativeLibrary(app_library); | 75 base::UnloadNativeLibrary(app_library); |
76 } | 76 } |
77 | 77 |
78 // Returns true if |url| denotes a cached app. If true |app_dir| is set to the | 78 // Returns true if |url| denotes a cached app. If true |app_dir| is set to the |
79 // path of the directory for the app and |path_to_mojo| the path of the app's | 79 // path of the directory for the app and |path_to_mojo| the path of the app's |
80 // .mojo file. | 80 // .mojo file. |
81 bool IsCachedApp(JNIEnv* env, | 81 bool IsCachedApp(JNIEnv* env, |
82 const GURL& url, | 82 const GURL& url, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 return true; | 126 return true; |
127 } | 127 } |
128 | 128 |
129 } // namespace | 129 } // namespace |
130 | 130 |
131 AndroidHandler::AndroidHandler() : content_handler_factory_(this) {} | 131 AndroidHandler::AndroidHandler() : content_handler_factory_(this) {} |
132 | 132 |
133 AndroidHandler::~AndroidHandler() {} | 133 AndroidHandler::~AndroidHandler() {} |
134 | 134 |
135 void AndroidHandler::RunApplication( | 135 void AndroidHandler::RunApplication( |
136 InterfaceRequest<mojom::Application> application_request, | 136 InterfaceRequest<mojom::ShellClient> request, |
137 URLResponsePtr response) { | 137 URLResponsePtr response) { |
138 JNIEnv* env = AttachCurrentThread(); | 138 JNIEnv* env = AttachCurrentThread(); |
139 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; | 139 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; |
140 if (!response->url.is_null()) { | 140 if (!response->url.is_null()) { |
141 base::FilePath internal_app_path; | 141 base::FilePath internal_app_path; |
142 base::FilePath path_to_mojo; | 142 base::FilePath path_to_mojo; |
143 if (IsCachedApp(env, GURL(response->url.get()), &internal_app_path, | 143 if (IsCachedApp(env, GURL(response->url.get()), &internal_app_path, |
144 &path_to_mojo)) { | 144 &path_to_mojo)) { |
145 ScopedJavaLocalRef<jstring> j_internal_app_path( | 145 ScopedJavaLocalRef<jstring> j_internal_app_path( |
146 ConvertUTF8ToJavaString(env, internal_app_path.value())); | 146 ConvertUTF8ToJavaString(env, internal_app_path.value())); |
147 ScopedJavaLocalRef<jstring> j_path_to_mojo( | 147 ScopedJavaLocalRef<jstring> j_path_to_mojo( |
148 ConvertUTF8ToJavaString(env, path_to_mojo.value())); | 148 ConvertUTF8ToJavaString(env, path_to_mojo.value())); |
149 Java_AndroidHandler_bootstrapCachedApp( | 149 Java_AndroidHandler_bootstrapCachedApp( |
150 env, GetApplicationContext(), j_path_to_mojo.obj(), | 150 env, GetApplicationContext(), j_path_to_mojo.obj(), |
151 j_internal_app_path.obj(), | 151 j_internal_app_path.obj(), |
152 application_request.PassMessagePipe().release().value(), | 152 request.PassMessagePipe().release().value(), |
153 reinterpret_cast<jlong>(run_android_application_fn)); | 153 reinterpret_cast<jlong>(run_android_application_fn)); |
154 return; | 154 return; |
155 } | 155 } |
156 } | 156 } |
157 ScopedJavaLocalRef<jstring> j_archive_path = | 157 ScopedJavaLocalRef<jstring> j_archive_path = |
158 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | 158 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); |
159 base::FilePath archive_path( | 159 base::FilePath archive_path( |
160 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | 160 ConvertJavaStringToUTF8(env, j_archive_path.obj())); |
161 | 161 |
162 common::BlockingCopyToFile(std::move(response->body), archive_path); | 162 common::BlockingCopyToFile(std::move(response->body), archive_path); |
163 Java_AndroidHandler_bootstrap( | 163 Java_AndroidHandler_bootstrap( |
164 env, GetApplicationContext(), j_archive_path.obj(), | 164 env, GetApplicationContext(), j_archive_path.obj(), |
165 application_request.PassMessagePipe().release().value(), | 165 request.PassMessagePipe().release().value(), |
166 reinterpret_cast<jlong>(run_android_application_fn)); | 166 reinterpret_cast<jlong>(run_android_application_fn)); |
167 } | 167 } |
168 | 168 |
169 void AndroidHandler::Initialize(Shell* shell, | 169 void AndroidHandler::Initialize(Shell* shell, |
170 const std::string& url, | 170 const std::string& url, |
171 uint32_t id) {} | 171 uint32_t id) {} |
172 | 172 |
173 bool AndroidHandler::AcceptConnection(Connection* connection) { | 173 bool AndroidHandler::AcceptConnection(Connection* connection) { |
174 connection->AddService(&content_handler_factory_); | 174 connection->AddService(&content_handler_factory_); |
175 return true; | 175 return true; |
176 } | 176 } |
177 | 177 |
178 bool RegisterAndroidHandlerJni(JNIEnv* env) { | 178 bool RegisterAndroidHandlerJni(JNIEnv* env) { |
179 return RegisterNativesImpl(env); | 179 return RegisterNativesImpl(env); |
180 } | 180 } |
181 | 181 |
182 } // namespace shell | 182 } // namespace shell |
183 } // namespace mojo | 183 } // namespace mojo |
OLD | NEW |