blob: c52b3403868c532c2383aae0dc4c476932bbd3ff [file] [log] [blame]
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +01001// 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
James Robinsonb4b7af22014-12-05 11:21:01 -08005#include "shell/android/android_handler.h"
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +01006
Benjamin Lerman01022292015-05-13 12:27:35 +02007#include <fcntl.h>
8
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +01009#include "base/android/jni_android.h"
10#include "base/android/jni_string.h"
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010011#include "base/files/file_path.h"
12#include "base/logging.h"
Benjamin Lerman5d429aa2015-05-07 16:21:00 +020013#include "base/message_loop/message_loop.h"
Benjamin Lerman01022292015-05-13 12:27:35 +020014#include "base/rand_util.h"
Benjamin Lerman5d429aa2015-05-07 16:21:00 +020015#include "base/run_loop.h"
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010016#include "base/scoped_native_library.h"
Benjamin Lerman01022292015-05-13 12:27:35 +020017#include "base/strings/string_number_conversions.h"
Benjamin Lerman762aa8e2015-04-10 13:22:59 +020018#include "base/trace_event/trace_event.h"
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010019#include "jni/AndroidHandler_jni.h"
James Robinson94ade6b2015-08-25 13:02:06 -070020#include "mojo/data_pipe_utils/data_pipe_utils.h"
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010021#include "mojo/public/c/system/main.h"
22#include "mojo/public/cpp/application/application_impl.h"
Viet-Trung Luuc1248b22016-04-26 13:41:55 -070023#include "mojo/public/cpp/application/connect.h"
James Robinsonb4b7af22014-12-05 11:21:01 -080024#include "shell/android/run_android_application_function.h"
Viet-Trung Luue126c862015-03-30 19:20:45 -070025#include "shell/native_application_support.h"
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010026
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010027using base::android::AttachCurrentThread;
28using base::android::ScopedJavaLocalRef;
29using base::android::ConvertJavaStringToUTF8;
30using base::android::ConvertUTF8ToJavaString;
31using base::android::GetApplicationContext;
32
Viet-Trung Luu36faa4d2015-03-04 18:08:18 -080033namespace shell {
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010034
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010035namespace {
Viet-Trung Luu47e2e352015-03-04 16:38:18 -080036
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010037// This function loads the application library, sets the application context and
38// thunks and calls into the application MojoMain. To ensure that the thunks are
39// set correctly we keep it in the Mojo shell .so and pass the function pointer
40// to the helper libbootstrap.so.
41void RunAndroidApplication(JNIEnv* env,
42 jobject j_context,
Benjamin Lerman762aa8e2015-04-10 13:22:59 +020043 uintptr_t tracing_id,
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010044 const base::FilePath& app_path,
45 jint j_handle) {
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070046 mojo::InterfaceRequest<mojo::Application> application_request =
Viet-Trung Luua5315942016-04-28 11:02:49 -070047 mojo::InterfaceRequest<mojo::Application>(
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070048 mojo::MakeScopedHandle(mojo::MessagePipeHandle(j_handle)));
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010049
50 // Load the library, so that we can set the application context there if
51 // needed.
Viet-Trung Luue126c862015-03-30 19:20:45 -070052 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()!
Benjamin Lerman5d429aa2015-05-07 16:21:00 +020053 base::NativeLibrary app_library = LoadNativeApplication(app_path);
Viet-Trung Luue126c862015-03-30 19:20:45 -070054 if (!app_library)
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010055 return;
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010056
57 // Set the application context if needed. Most applications will need to
58 // access the Android ApplicationContext in which they are run. If the
59 // application library exports the InitApplicationContext function, we will
60 // set it there.
61 const char* init_application_context_name = "InitApplicationContext";
62 typedef void (*InitApplicationContextFn)(
63 const base::android::JavaRef<jobject>&);
64 InitApplicationContextFn init_application_context =
65 reinterpret_cast<InitApplicationContextFn>(
Viet-Trung Luue126c862015-03-30 19:20:45 -070066 base::GetFunctionPointerFromNativeLibrary(
67 app_library, init_application_context_name));
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010068 if (init_application_context) {
69 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context);
70 init_application_context(scoped_context);
71 }
72
Benjamin Lerman762aa8e2015-04-10 13:22:59 +020073 // The application is ready to be run.
74 TRACE_EVENT_ASYNC_END0("android_handler", "AndroidHandler::RunApplication",
75 tracing_id);
76
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010077 // Run the application.
Viet-Trung Luue126c862015-03-30 19:20:45 -070078 RunNativeApplication(app_library, application_request.Pass());
79 // TODO(vtl): See note about unloading and thread-local destructors above
80 // declaration of |LoadNativeApplication()|.
81 base::UnloadNativeLibrary(app_library);
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010082}
Viet-Trung Luu47e2e352015-03-04 16:38:18 -080083
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +010084} // namespace
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010085
Benjamin Lerman6939a5a2014-12-16 16:55:42 +010086AndroidHandler::AndroidHandler() : content_handler_factory_(this) {
87}
88
89AndroidHandler::~AndroidHandler() {
90}
91
James Robinsone5ae9e42015-01-26 17:53:08 -080092void AndroidHandler::RunApplication(
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070093 mojo::InterfaceRequest<mojo::Application> application_request,
94 mojo::URLResponsePtr response) {
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +010095 JNIEnv* env = AttachCurrentThread();
Benjamin Lerman762aa8e2015-04-10 13:22:59 +020096 uintptr_t tracing_id = reinterpret_cast<uintptr_t>(this);
97 TRACE_EVENT_ASYNC_BEGIN1("android_handler", "AndroidHandler::RunApplication",
98 tracing_id, "url", std::string(response->url));
Benjamin Lerman5d429aa2015-05-07 16:21:00 +020099 base::FilePath extracted_dir;
100 base::FilePath cache_dir;
101 {
102 base::MessageLoop loop;
103 handler_task_runner_->PostTask(
104 FROM_HERE,
105 base::Bind(&AndroidHandler::ExtractApplication, base::Unretained(this),
106 base::Unretained(&extracted_dir),
107 base::Unretained(&cache_dir), base::Passed(response.Pass()),
108 base::Bind(base::IgnoreResult(
109 &base::SingleThreadTaskRunner::PostTask),
110 loop.task_runner(), FROM_HERE,
111 base::MessageLoop::QuitWhenIdleClosure())));
112 base::RunLoop().Run();
113 }
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +0100114
Benjamin Lerman5d429aa2015-05-07 16:21:00 +0200115 ScopedJavaLocalRef<jstring> j_extracted_dir =
116 ConvertUTF8ToJavaString(env, extracted_dir.value());
117 ScopedJavaLocalRef<jstring> j_cache_dir =
118 ConvertUTF8ToJavaString(env, cache_dir.value());
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +0100119 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication;
120 Java_AndroidHandler_bootstrap(
Benjamin Lerman5d429aa2015-05-07 16:21:00 +0200121 env, GetApplicationContext(), tracing_id, j_extracted_dir.obj(),
122 j_cache_dir.obj(),
James Robinsone5ae9e42015-01-26 17:53:08 -0800123 application_request.PassMessagePipe().release().value(),
Przemyslaw Pietrzkiewicz78ab4132014-12-03 16:49:32 +0100124 reinterpret_cast<jlong>(run_android_application_fn));
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +0100125}
126
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700127void AndroidHandler::Initialize(mojo::ApplicationImpl* app) {
Benjamin Lerman5d429aa2015-05-07 16:21:00 +0200128 handler_task_runner_ = base::MessageLoop::current()->task_runner();
Viet-Trung Luuc1248b22016-04-26 13:41:55 -0700129 mojo::ConnectToService(app->shell(), "mojo:url_response_disk_cache",
130 GetProxy(&url_response_disk_cache_));
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +0100131}
132
133bool AndroidHandler::ConfigureIncomingConnection(
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700134 mojo::ApplicationConnection* connection) {
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +0100135 connection->AddService(&content_handler_factory_);
136 return true;
137}
138
Benjamin Lerman5d429aa2015-05-07 16:21:00 +0200139void AndroidHandler::ExtractApplication(base::FilePath* extracted_dir,
140 base::FilePath* cache_dir,
141 mojo::URLResponsePtr response,
142 const base::Closure& callback) {
Benjamin Lermand15e3f42015-09-17 11:26:18 +0200143 url_response_disk_cache_->UpdateAndGetExtracted(
Benjamin Lerman5d429aa2015-05-07 16:21:00 +0200144 response.Pass(),
145 [extracted_dir, cache_dir, callback](mojo::Array<uint8_t> extracted_path,
146 mojo::Array<uint8_t> cache_path) {
147 if (extracted_path.is_null()) {
148 *extracted_dir = base::FilePath();
149 *cache_dir = base::FilePath();
150 } else {
151 *extracted_dir = base::FilePath(
152 std::string(reinterpret_cast<char*>(&extracted_path.front()),
153 extracted_path.size()));
154 *cache_dir = base::FilePath(std::string(
155 reinterpret_cast<char*>(&cache_path.front()), cache_path.size()));
156 }
157 callback.Run();
158 });
159}
160
Benjamin Lerman01022292015-05-13 12:27:35 +0200161jstring CreateTemporaryFile(JNIEnv* env,
162 jclass jcaller,
163 jstring j_directory,
164 jstring j_basename,
165 jstring j_extension) {
166 std::string basename(ConvertJavaStringToUTF8(env, j_basename));
167 std::string extension(ConvertJavaStringToUTF8(env, j_extension));
168 base::FilePath directory(ConvertJavaStringToUTF8(env, j_directory));
169
170 for (;;) {
171 std::string random = base::RandBytesAsString(16);
172 std::string filename =
173 basename + base::HexEncode(random.data(), random.size()) + extension;
174 base::FilePath temporary_file = directory.Append(filename);
175 int fd = open(temporary_file.value().c_str(), O_CREAT | O_EXCL, 0600);
176 if (fd != -1) {
177 close(fd);
178 return ConvertUTF8ToJavaString(env, temporary_file.value()).Release();
179 }
180 }
181}
182
Przemyslaw Pietrzkiewicz168bb122014-11-25 20:15:15 +0100183bool RegisterAndroidHandlerJni(JNIEnv* env) {
184 return RegisterNativesImpl(env);
185}
186
Viet-Trung Luu36faa4d2015-03-04 18:08:18 -0800187} // namespace shell