| 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/runner/android/android_handler.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/android/context_utils.h" | |
| 11 #include "base/android/jni_android.h" | |
| 12 #include "base/android/jni_string.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/scoped_native_library.h" | |
| 16 #include "jni/AndroidHandler_jni.h" | |
| 17 #include "mojo/common/data_pipe_utils.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" | |
| 21 #include "mojo/shell/runner/host/native_application_support.h" | |
| 22 #include "mojo/util/filename_util.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 using base::android::AttachCurrentThread; | |
| 26 using base::android::ScopedJavaLocalRef; | |
| 27 using base::android::ConvertJavaStringToUTF8; | |
| 28 using base::android::ConvertUTF8ToJavaString; | |
| 29 using base::android::GetApplicationContext; | |
| 30 | |
| 31 namespace mojo { | |
| 32 namespace runner { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 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 | |
| 38 // set correctly we keep it in the Mojo shell .so and pass the function pointer | |
| 39 // to the helper libbootstrap.so. | |
| 40 void RunAndroidApplication(JNIEnv* env, | |
| 41 jobject j_context, | |
| 42 const base::FilePath& app_path, | |
| 43 jint j_handle) { | |
| 44 InterfaceRequest<Application> application_request = | |
| 45 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle))); | |
| 46 | |
| 47 // Load the library, so that we can set the application context there if | |
| 48 // needed. | |
| 49 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! | |
| 50 base::NativeLibrary app_library = shell::LoadNativeApplication(app_path); | |
| 51 if (!app_library) | |
| 52 return; | |
| 53 | |
| 54 // Set the application context if needed. Most applications will need to | |
| 55 // access the Android ApplicationContext in which they are run. If the | |
| 56 // application library exports the InitApplicationContext function, we will | |
| 57 // set it there. | |
| 58 const char* init_application_context_name = "InitApplicationContext"; | |
| 59 typedef void (*InitApplicationContextFn)( | |
| 60 const base::android::JavaRef<jobject>&); | |
| 61 InitApplicationContextFn init_application_context = | |
| 62 reinterpret_cast<InitApplicationContextFn>( | |
| 63 base::GetFunctionPointerFromNativeLibrary( | |
| 64 app_library, init_application_context_name)); | |
| 65 if (init_application_context) { | |
| 66 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); | |
| 67 init_application_context(scoped_context); | |
| 68 } | |
| 69 | |
| 70 // Run the application. | |
| 71 shell::RunNativeApplication(app_library, std::move(application_request)); | |
| 72 // TODO(vtl): See note about unloading and thread-local destructors above | |
| 73 // declaration of |LoadNativeApplication()|. | |
| 74 base::UnloadNativeLibrary(app_library); | |
| 75 } | |
| 76 | |
| 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 | |
| 79 // .mojo file. | |
| 80 bool IsCachedApp(JNIEnv* env, | |
| 81 const GURL& url, | |
| 82 base::FilePath* app_dir, | |
| 83 base::FilePath* path_to_mojo) { | |
| 84 ScopedJavaLocalRef<jstring> j_cached_apps_dir = | |
| 85 Java_AndroidHandler_getCachedAppsDir(env, GetApplicationContext()); | |
| 86 const base::FilePath cached_apps_fp( | |
| 87 ConvertJavaStringToUTF8(env, j_cached_apps_dir.obj())); | |
| 88 const std::string cached_apps(util::FilePathToFileURL(cached_apps_fp).spec()); | |
| 89 const std::string response_url(GURL(url).spec()); | |
| 90 if (response_url.size() <= cached_apps.size() || | |
| 91 cached_apps.compare(0u, cached_apps.size(), response_url, 0u, | |
| 92 cached_apps.size()) != 0) { | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 const std::string mojo_suffix(".mojo"); | |
| 97 // app_rel_path is either something like html_viewer/html_viewer.mojo, or | |
| 98 // html_viewer.mojo, depending upon whether the app has a package. | |
| 99 const std::string app_rel_path(response_url.substr(cached_apps.size() + 1)); | |
| 100 const size_t slash_index = app_rel_path.find('/'); | |
| 101 if (slash_index != std::string::npos) { | |
| 102 const std::string tail = | |
| 103 app_rel_path.substr(slash_index + 1, std::string::npos); | |
| 104 const std::string head = app_rel_path.substr(0, slash_index); | |
| 105 if (head.find('/') != std::string::npos || | |
| 106 tail.size() <= mojo_suffix.size() || | |
| 107 tail.compare(tail.size() - mojo_suffix.size(), tail.size(), | |
| 108 mojo_suffix) != 0) { | |
| 109 return false; | |
| 110 } | |
| 111 *app_dir = cached_apps_fp.Append(head); | |
| 112 *path_to_mojo = app_dir->Append(tail); | |
| 113 return true; | |
| 114 } | |
| 115 if (app_rel_path.find('/') != std::string::npos || | |
| 116 app_rel_path.size() <= mojo_suffix.size() || | |
| 117 app_rel_path.compare(app_rel_path.size() - mojo_suffix.size(), | |
| 118 mojo_suffix.size(), mojo_suffix) != 0) { | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 *app_dir = cached_apps_fp.Append( | |
| 123 app_rel_path.substr(0, app_rel_path.size() - mojo_suffix.size())); | |
| 124 *path_to_mojo = cached_apps_fp.Append(app_rel_path); | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| 129 | |
| 130 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { | |
| 131 } | |
| 132 | |
| 133 AndroidHandler::~AndroidHandler() { | |
| 134 } | |
| 135 | |
| 136 void AndroidHandler::RunApplication( | |
| 137 InterfaceRequest<Application> application_request, | |
| 138 URLResponsePtr response) { | |
| 139 JNIEnv* env = AttachCurrentThread(); | |
| 140 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; | |
| 141 if (!response->url.is_null()) { | |
| 142 base::FilePath internal_app_path; | |
| 143 base::FilePath path_to_mojo; | |
| 144 if (IsCachedApp(env, GURL(response->url.get()), &internal_app_path, | |
| 145 &path_to_mojo)) { | |
| 146 ScopedJavaLocalRef<jstring> j_internal_app_path( | |
| 147 ConvertUTF8ToJavaString(env, internal_app_path.value())); | |
| 148 ScopedJavaLocalRef<jstring> j_path_to_mojo( | |
| 149 ConvertUTF8ToJavaString(env, path_to_mojo.value())); | |
| 150 Java_AndroidHandler_bootstrapCachedApp( | |
| 151 env, GetApplicationContext(), j_path_to_mojo.obj(), | |
| 152 j_internal_app_path.obj(), | |
| 153 application_request.PassMessagePipe().release().value(), | |
| 154 reinterpret_cast<jlong>(run_android_application_fn)); | |
| 155 return; | |
| 156 } | |
| 157 } | |
| 158 ScopedJavaLocalRef<jstring> j_archive_path = | |
| 159 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | |
| 160 base::FilePath archive_path( | |
| 161 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | |
| 162 | |
| 163 common::BlockingCopyToFile(std::move(response->body), archive_path); | |
| 164 Java_AndroidHandler_bootstrap( | |
| 165 env, GetApplicationContext(), j_archive_path.obj(), | |
| 166 application_request.PassMessagePipe().release().value(), | |
| 167 reinterpret_cast<jlong>(run_android_application_fn)); | |
| 168 } | |
| 169 | |
| 170 void AndroidHandler::Initialize(ApplicationImpl* app) { | |
| 171 } | |
| 172 | |
| 173 bool AndroidHandler::ConfigureIncomingConnection( | |
| 174 ApplicationConnection* connection) { | |
| 175 connection->AddService(&content_handler_factory_); | |
| 176 return true; | |
| 177 } | |
| 178 | |
| 179 bool RegisterAndroidHandlerJni(JNIEnv* env) { | |
| 180 return RegisterNativesImpl(env); | |
| 181 } | |
| 182 | |
| 183 } // namespace runner | |
| 184 } // namespace mojo | |
| OLD | NEW |