| 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/runner/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" |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/scoped_native_library.h" | 15 #include "base/scoped_native_library.h" |
| 16 #include "jni/AndroidHandler_jni.h" | 16 #include "jni/AndroidHandler_jni.h" |
| 17 #include "mojo/common/data_pipe_utils.h" | 17 #include "mojo/common/data_pipe_utils.h" |
| 18 #include "mojo/public/c/system/main.h" | 18 #include "mojo/public/c/system/main.h" |
| 19 #include "mojo/runner/android/run_android_application_function.h" | |
| 20 #include "mojo/shell/public/cpp/application_impl.h" | 19 #include "mojo/shell/public/cpp/application_impl.h" |
| 21 #include "mojo/shell/runner/host/native_application_support.h" | 20 #include "mojo/shell/runner/host/native_application_support.h" |
| 21 #include "mojo/shell/standalone/android/run_android_application_function.h" |
| 22 #include "mojo/util/filename_util.h" | 22 #include "mojo/util/filename_util.h" |
| 23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 24 | 24 |
| 25 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
| 26 using base::android::ScopedJavaLocalRef; | 26 using base::android::ScopedJavaLocalRef; |
| 27 using base::android::ConvertJavaStringToUTF8; | 27 using base::android::ConvertJavaStringToUTF8; |
| 28 using base::android::ConvertUTF8ToJavaString; | 28 using base::android::ConvertUTF8ToJavaString; |
| 29 using base::android::GetApplicationContext; | 29 using base::android::GetApplicationContext; |
| 30 | 30 |
| 31 namespace mojo { | 31 namespace mojo { |
| 32 namespace runner { | 32 namespace shell { |
| 33 | 33 |
| 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<Application> application_request = | 44 InterfaceRequest<Application> application_request = |
| 45 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle))); | 45 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle))); |
| 46 | 46 |
| 47 // Load the library, so that we can set the application context there if | 47 // Load the library, so that we can set the application context there if |
| 48 // needed. | 48 // needed. |
| 49 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! | 49 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! |
| 50 base::NativeLibrary app_library = shell::LoadNativeApplication(app_path); | 50 base::NativeLibrary app_library = LoadNativeApplication(app_path); |
| 51 if (!app_library) | 51 if (!app_library) |
| 52 return; | 52 return; |
| 53 | 53 |
| 54 // Set the application context if needed. Most applications will need to | 54 // Set the application context if needed. Most applications will need to |
| 55 // access the Android ApplicationContext in which they are run. If the | 55 // access the Android ApplicationContext in which they are run. If the |
| 56 // application library exports the InitApplicationContext function, we will | 56 // application library exports the InitApplicationContext function, we will |
| 57 // set it there. | 57 // set it there. |
| 58 const char* init_application_context_name = "InitApplicationContext"; | 58 const char* init_application_context_name = "InitApplicationContext"; |
| 59 typedef void (*InitApplicationContextFn)( | 59 typedef void (*InitApplicationContextFn)( |
| 60 const base::android::JavaRef<jobject>&); | 60 const base::android::JavaRef<jobject>&); |
| 61 InitApplicationContextFn init_application_context = | 61 InitApplicationContextFn init_application_context = |
| 62 reinterpret_cast<InitApplicationContextFn>( | 62 reinterpret_cast<InitApplicationContextFn>( |
| 63 base::GetFunctionPointerFromNativeLibrary( | 63 base::GetFunctionPointerFromNativeLibrary( |
| 64 app_library, init_application_context_name)); | 64 app_library, init_application_context_name)); |
| 65 if (init_application_context) { | 65 if (init_application_context) { |
| 66 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); | 66 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); |
| 67 init_application_context(scoped_context); | 67 init_application_context(scoped_context); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // Run the application. | 70 // Run the application. |
| 71 shell::RunNativeApplication(app_library, std::move(application_request)); | 71 RunNativeApplication(app_library, std::move(application_request)); |
| 72 // TODO(vtl): See note about unloading and thread-local destructors above | 72 // TODO(vtl): See note about unloading and thread-local destructors above |
| 73 // declaration of |LoadNativeApplication()|. | 73 // declaration of |LoadNativeApplication()|. |
| 74 base::UnloadNativeLibrary(app_library); | 74 base::UnloadNativeLibrary(app_library); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Returns true if |url| denotes a cached app. If true |app_dir| is set to the | 77 // Returns true if |url| denotes a cached app. If true |app_dir| is set to the |
| 78 // path of the directory for the app and |path_to_mojo| the path of the app's | 78 // path of the directory for the app and |path_to_mojo| the path of the app's |
| 79 // .mojo file. | 79 // .mojo file. |
| 80 bool IsCachedApp(JNIEnv* env, | 80 bool IsCachedApp(JNIEnv* env, |
| 81 const GURL& url, | 81 const GURL& url, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 } | 120 } |
| 121 | 121 |
| 122 *app_dir = cached_apps_fp.Append( | 122 *app_dir = cached_apps_fp.Append( |
| 123 app_rel_path.substr(0, app_rel_path.size() - mojo_suffix.size())); | 123 app_rel_path.substr(0, app_rel_path.size() - mojo_suffix.size())); |
| 124 *path_to_mojo = cached_apps_fp.Append(app_rel_path); | 124 *path_to_mojo = cached_apps_fp.Append(app_rel_path); |
| 125 return true; | 125 return true; |
| 126 } | 126 } |
| 127 | 127 |
| 128 } // namespace | 128 } // namespace |
| 129 | 129 |
| 130 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { | 130 AndroidHandler::AndroidHandler() : content_handler_factory_(this) {} |
| 131 } | |
| 132 | 131 |
| 133 AndroidHandler::~AndroidHandler() { | 132 AndroidHandler::~AndroidHandler() {} |
| 134 } | |
| 135 | 133 |
| 136 void AndroidHandler::RunApplication( | 134 void AndroidHandler::RunApplication( |
| 137 InterfaceRequest<Application> application_request, | 135 InterfaceRequest<Application> application_request, |
| 138 URLResponsePtr response) { | 136 URLResponsePtr response) { |
| 139 JNIEnv* env = AttachCurrentThread(); | 137 JNIEnv* env = AttachCurrentThread(); |
| 140 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; | 138 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; |
| 141 if (!response->url.is_null()) { | 139 if (!response->url.is_null()) { |
| 142 base::FilePath internal_app_path; | 140 base::FilePath internal_app_path; |
| 143 base::FilePath path_to_mojo; | 141 base::FilePath path_to_mojo; |
| 144 if (IsCachedApp(env, GURL(response->url.get()), &internal_app_path, | 142 if (IsCachedApp(env, GURL(response->url.get()), &internal_app_path, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 base::FilePath archive_path( | 158 base::FilePath archive_path( |
| 161 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | 159 ConvertJavaStringToUTF8(env, j_archive_path.obj())); |
| 162 | 160 |
| 163 common::BlockingCopyToFile(std::move(response->body), archive_path); | 161 common::BlockingCopyToFile(std::move(response->body), archive_path); |
| 164 Java_AndroidHandler_bootstrap( | 162 Java_AndroidHandler_bootstrap( |
| 165 env, GetApplicationContext(), j_archive_path.obj(), | 163 env, GetApplicationContext(), j_archive_path.obj(), |
| 166 application_request.PassMessagePipe().release().value(), | 164 application_request.PassMessagePipe().release().value(), |
| 167 reinterpret_cast<jlong>(run_android_application_fn)); | 165 reinterpret_cast<jlong>(run_android_application_fn)); |
| 168 } | 166 } |
| 169 | 167 |
| 170 void AndroidHandler::Initialize(ApplicationImpl* app) { | 168 void AndroidHandler::Initialize(ApplicationImpl* app) {} |
| 171 } | |
| 172 | 169 |
| 173 bool AndroidHandler::ConfigureIncomingConnection( | 170 bool AndroidHandler::ConfigureIncomingConnection( |
| 174 ApplicationConnection* connection) { | 171 ApplicationConnection* connection) { |
| 175 connection->AddService(&content_handler_factory_); | 172 connection->AddService(&content_handler_factory_); |
| 176 return true; | 173 return true; |
| 177 } | 174 } |
| 178 | 175 |
| 179 bool RegisterAndroidHandlerJni(JNIEnv* env) { | 176 bool RegisterAndroidHandlerJni(JNIEnv* env) { |
| 180 return RegisterNativesImpl(env); | 177 return RegisterNativesImpl(env); |
| 181 } | 178 } |
| 182 | 179 |
| 183 } // namespace runner | 180 } // namespace shell |
| 184 } // namespace mojo | 181 } // namespace mojo |
| OLD | NEW |