Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / mojo / runner / android / android_handler.cc
blob354720a4968621bd71506983ee8b0cab1d7e936c
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.
5 #include "mojo/runner/android/android_handler.h"
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/application/public/cpp/application_impl.h"
14 #include "mojo/common/data_pipe_utils.h"
15 #include "mojo/public/c/system/main.h"
16 #include "mojo/runner/android/run_android_application_function.h"
17 #include "mojo/runner/native_application_support.h"
18 #include "mojo/util/filename_util.h"
19 #include "url/gurl.h"
21 using base::android::AttachCurrentThread;
22 using base::android::ScopedJavaLocalRef;
23 using base::android::ConvertJavaStringToUTF8;
24 using base::android::ConvertUTF8ToJavaString;
25 using base::android::GetApplicationContext;
27 namespace mojo {
28 namespace runner {
30 namespace {
32 // This function loads the application library, sets the application context and
33 // thunks and calls into the application MojoMain. To ensure that the thunks are
34 // set correctly we keep it in the Mojo shell .so and pass the function pointer
35 // to the helper libbootstrap.so.
36 void RunAndroidApplication(JNIEnv* env,
37 jobject j_context,
38 const base::FilePath& app_path,
39 jint j_handle) {
40 InterfaceRequest<Application> application_request =
41 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle)));
43 // Load the library, so that we can set the application context there if
44 // needed.
45 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()!
46 base::NativeLibrary app_library = LoadNativeApplication(app_path);
47 if (!app_library)
48 return;
50 // Set the application context if needed. Most applications will need to
51 // access the Android ApplicationContext in which they are run. If the
52 // application library exports the InitApplicationContext function, we will
53 // set it there.
54 const char* init_application_context_name = "InitApplicationContext";
55 typedef void (*InitApplicationContextFn)(
56 const base::android::JavaRef<jobject>&);
57 InitApplicationContextFn init_application_context =
58 reinterpret_cast<InitApplicationContextFn>(
59 base::GetFunctionPointerFromNativeLibrary(
60 app_library, init_application_context_name));
61 if (init_application_context) {
62 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context);
63 init_application_context(scoped_context);
66 // Run the application.
67 RunNativeApplication(app_library, application_request.Pass());
68 // TODO(vtl): See note about unloading and thread-local destructors above
69 // declaration of |LoadNativeApplication()|.
70 base::UnloadNativeLibrary(app_library);
73 // Returns true if |url| denotes a cached app. If true |app_dir| is set to the
74 // path of the directory for the app and |path_to_mojo| the path of the app's
75 // .mojo file.
76 bool IsCachedApp(JNIEnv* env,
77 const GURL& url,
78 base::FilePath* app_dir,
79 base::FilePath* path_to_mojo) {
80 ScopedJavaLocalRef<jstring> j_cached_apps_dir =
81 Java_AndroidHandler_getCachedAppsDir(env, GetApplicationContext());
82 const base::FilePath cached_apps_fp(
83 ConvertJavaStringToUTF8(env, j_cached_apps_dir.obj()));
84 const std::string cached_apps(util::FilePathToFileURL(cached_apps_fp).spec());
85 const std::string response_url(GURL(url).spec());
86 if (response_url.size() <= cached_apps.size() ||
87 cached_apps.compare(0u, cached_apps.size(), response_url, 0u,
88 cached_apps.size()) != 0) {
89 return false;
92 const std::string mojo_suffix(".mojo");
93 // app_rel_path is either something like html_viewer/html_viewer.mojo, or
94 // html_viewer.mojo, depending upon whether the app has a package.
95 const std::string app_rel_path(response_url.substr(cached_apps.size() + 1));
96 const size_t slash_index = app_rel_path.find('/');
97 if (slash_index != std::string::npos) {
98 const std::string tail =
99 app_rel_path.substr(slash_index + 1, std::string::npos);
100 const std::string head = app_rel_path.substr(0, slash_index);
101 if (head.find('/') != std::string::npos ||
102 tail.size() <= mojo_suffix.size() ||
103 tail.compare(tail.size() - mojo_suffix.size(), tail.size(),
104 mojo_suffix) != 0) {
105 return false;
107 *app_dir = cached_apps_fp.Append(head);
108 *path_to_mojo = app_dir->Append(tail);
109 return true;
111 if (app_rel_path.find('/') != std::string::npos ||
112 app_rel_path.size() <= mojo_suffix.size() ||
113 app_rel_path.compare(app_rel_path.size() - mojo_suffix.size(),
114 mojo_suffix.size(), mojo_suffix) != 0) {
115 return false;
118 *app_dir = cached_apps_fp.Append(
119 app_rel_path.substr(0, app_rel_path.size() - mojo_suffix.size()));
120 *path_to_mojo = cached_apps_fp.Append(app_rel_path);
121 return true;
124 } // namespace
126 AndroidHandler::AndroidHandler() : content_handler_factory_(this) {
129 AndroidHandler::~AndroidHandler() {
132 void AndroidHandler::RunApplication(
133 InterfaceRequest<Application> application_request,
134 URLResponsePtr response) {
135 JNIEnv* env = AttachCurrentThread();
136 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication;
137 if (!response->url.is_null()) {
138 base::FilePath internal_app_path;
139 base::FilePath path_to_mojo;
140 if (IsCachedApp(env, GURL(response->url), &internal_app_path,
141 &path_to_mojo)) {
142 ScopedJavaLocalRef<jstring> j_internal_app_path(
143 ConvertUTF8ToJavaString(env, internal_app_path.value()));
144 ScopedJavaLocalRef<jstring> j_path_to_mojo(
145 ConvertUTF8ToJavaString(env, path_to_mojo.value()));
146 Java_AndroidHandler_bootstrapCachedApp(
147 env, GetApplicationContext(), j_path_to_mojo.obj(),
148 j_internal_app_path.obj(),
149 application_request.PassMessagePipe().release().value(),
150 reinterpret_cast<jlong>(run_android_application_fn));
151 return;
154 ScopedJavaLocalRef<jstring> j_archive_path =
155 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext());
156 base::FilePath archive_path(
157 ConvertJavaStringToUTF8(env, j_archive_path.obj()));
159 common::BlockingCopyToFile(response->body.Pass(), archive_path);
160 Java_AndroidHandler_bootstrap(
161 env, GetApplicationContext(), j_archive_path.obj(),
162 application_request.PassMessagePipe().release().value(),
163 reinterpret_cast<jlong>(run_android_application_fn));
166 void AndroidHandler::Initialize(ApplicationImpl* app) {
169 bool AndroidHandler::ConfigureIncomingConnection(
170 ApplicationConnection* connection) {
171 connection->AddService(&content_handler_factory_);
172 return true;
175 bool RegisterAndroidHandlerJni(JNIEnv* env) {
176 return RegisterNativesImpl(env);
179 } // namespace runner
180 } // namespace mojo