Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / runner / android / android_handler.cc
blob3338b56cea244e39b9e40d779bdcbfb4048f884f
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 bool is_cached_app) {
41 InterfaceRequest<Application> application_request =
42 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle)));
44 // Load the library, so that we can set the application context there if
45 // needed.
46 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()!
47 base::NativeLibrary app_library = LoadNativeApplication(
48 app_path, is_cached_app ? shell::NativeApplicationCleanup::DONT_DELETE
49 : shell::NativeApplicationCleanup::DELETE);
50 if (!app_library)
51 return;
53 // Set the application context if needed. Most applications will need to
54 // access the Android ApplicationContext in which they are run. If the
55 // application library exports the InitApplicationContext function, we will
56 // set it there.
57 const char* init_application_context_name = "InitApplicationContext";
58 typedef void (*InitApplicationContextFn)(
59 const base::android::JavaRef<jobject>&);
60 InitApplicationContextFn init_application_context =
61 reinterpret_cast<InitApplicationContextFn>(
62 base::GetFunctionPointerFromNativeLibrary(
63 app_library, init_application_context_name));
64 if (init_application_context) {
65 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context);
66 init_application_context(scoped_context);
69 // Run the application.
70 RunNativeApplication(app_library, application_request.Pass());
71 // TODO(vtl): See note about unloading and thread-local destructors above
72 // declaration of |LoadNativeApplication()|.
73 base::UnloadNativeLibrary(app_library);
76 // Returns true if |url| denotes a cached app. If true |app_dir| is set to the
77 // path of the directory for the app and |path_to_mojo| the path of the app's
78 // .mojo file.
79 bool IsCachedApp(JNIEnv* env,
80 const GURL& url,
81 base::FilePath* app_dir,
82 base::FilePath* path_to_mojo) {
83 ScopedJavaLocalRef<jstring> j_cached_apps_dir =
84 Java_AndroidHandler_getCachedAppsDir(env, GetApplicationContext());
85 const base::FilePath cached_apps_fp(
86 ConvertJavaStringToUTF8(env, j_cached_apps_dir.obj()));
87 const std::string cached_apps(util::FilePathToFileURL(cached_apps_fp).spec());
88 const std::string response_url(GURL(url).spec());
89 if (response_url.size() <= cached_apps.size() ||
90 cached_apps.compare(0u, cached_apps.size(), response_url, 0u,
91 cached_apps.size()) != 0) {
92 return false;
95 const std::string mojo_suffix(".mojo");
96 // app_rel_path is either something like html_viewer/html_viewer.mojo, or
97 // html_viewer.mojo, depending upon whether the app has a package.
98 const std::string app_rel_path(response_url.substr(cached_apps.size() + 1));
99 const size_t slash_index = app_rel_path.find('/');
100 if (slash_index != std::string::npos) {
101 const std::string tail =
102 app_rel_path.substr(slash_index + 1, std::string::npos);
103 const std::string head = app_rel_path.substr(0, slash_index);
104 if (head.find('/') != std::string::npos ||
105 tail.size() <= mojo_suffix.size() ||
106 tail.compare(tail.size() - mojo_suffix.size(), tail.size(),
107 mojo_suffix) != 0) {
108 return false;
110 *app_dir = cached_apps_fp.Append(head);
111 *path_to_mojo = app_dir->Append(tail);
112 return true;
114 if (app_rel_path.find('/') != std::string::npos ||
115 app_rel_path.size() <= mojo_suffix.size() ||
116 app_rel_path.compare(app_rel_path.size() - mojo_suffix.size(),
117 mojo_suffix.size(), mojo_suffix) != 0) {
118 return false;
121 *app_dir = cached_apps_fp.Append(
122 app_rel_path.substr(0, app_rel_path.size() - mojo_suffix.size()));
123 *path_to_mojo = cached_apps_fp.Append(app_rel_path);
124 return true;
127 } // namespace
129 AndroidHandler::AndroidHandler() : content_handler_factory_(this) {
132 AndroidHandler::~AndroidHandler() {
135 void AndroidHandler::RunApplication(
136 InterfaceRequest<Application> application_request,
137 URLResponsePtr response) {
138 JNIEnv* env = AttachCurrentThread();
139 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication;
140 if (!response->url.is_null()) {
141 base::FilePath internal_app_path;
142 base::FilePath path_to_mojo;
143 if (IsCachedApp(env, GURL(response->url), &internal_app_path,
144 &path_to_mojo)) {
145 ScopedJavaLocalRef<jstring> j_internal_app_path(
146 ConvertUTF8ToJavaString(env, internal_app_path.value()));
147 ScopedJavaLocalRef<jstring> j_path_to_mojo(
148 ConvertUTF8ToJavaString(env, path_to_mojo.value()));
149 Java_AndroidHandler_bootstrapCachedApp(
150 env, GetApplicationContext(), j_path_to_mojo.obj(),
151 j_internal_app_path.obj(),
152 application_request.PassMessagePipe().release().value(),
153 reinterpret_cast<jlong>(run_android_application_fn));
154 return;
157 ScopedJavaLocalRef<jstring> j_archive_path =
158 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext());
159 base::FilePath archive_path(
160 ConvertJavaStringToUTF8(env, j_archive_path.obj()));
162 common::BlockingCopyToFile(response->body.Pass(), archive_path);
163 Java_AndroidHandler_bootstrap(
164 env, GetApplicationContext(), j_archive_path.obj(),
165 application_request.PassMessagePipe().release().value(),
166 reinterpret_cast<jlong>(run_android_application_fn));
169 void AndroidHandler::Initialize(ApplicationImpl* app) {
172 bool AndroidHandler::ConfigureIncomingConnection(
173 ApplicationConnection* connection) {
174 connection->AddService(&content_handler_factory_);
175 return true;
178 bool RegisterAndroidHandlerJni(JNIEnv* env) {
179 return RegisterNativesImpl(env);
182 } // namespace runner
183 } // namespace mojo