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 "shell/android/android_handler.h" | 5 #include "shell/android/android_handler.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
11 #include "base/scoped_native_library.h" | 13 #include "base/scoped_native_library.h" |
12 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
13 #include "jni/AndroidHandler_jni.h" | 15 #include "jni/AndroidHandler_jni.h" |
14 #include "mojo/common/data_pipe_utils.h" | 16 #include "mojo/common/data_pipe_utils.h" |
15 #include "mojo/public/c/system/main.h" | 17 #include "mojo/public/c/system/main.h" |
16 #include "mojo/public/cpp/application/application_impl.h" | 18 #include "mojo/public/cpp/application/application_impl.h" |
17 #include "shell/android/run_android_application_function.h" | 19 #include "shell/android/run_android_application_function.h" |
18 #include "shell/native_application_support.h" | 20 #include "shell/native_application_support.h" |
19 | 21 |
20 using base::android::AttachCurrentThread; | 22 using base::android::AttachCurrentThread; |
(...skipping 15 matching lines...) Expand all Loading... |
36 uintptr_t tracing_id, | 38 uintptr_t tracing_id, |
37 const base::FilePath& app_path, | 39 const base::FilePath& app_path, |
38 jint j_handle) { | 40 jint j_handle) { |
39 mojo::InterfaceRequest<mojo::Application> application_request = | 41 mojo::InterfaceRequest<mojo::Application> application_request = |
40 mojo::MakeRequest<mojo::Application>( | 42 mojo::MakeRequest<mojo::Application>( |
41 mojo::MakeScopedHandle(mojo::MessagePipeHandle(j_handle))); | 43 mojo::MakeScopedHandle(mojo::MessagePipeHandle(j_handle))); |
42 | 44 |
43 // Load the library, so that we can set the application context there if | 45 // Load the library, so that we can set the application context there if |
44 // needed. | 46 // needed. |
45 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! | 47 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! |
46 base::NativeLibrary app_library = | 48 base::NativeLibrary app_library = LoadNativeApplication(app_path); |
47 LoadNativeApplication(app_path, NativeApplicationCleanup::DELETE); | |
48 if (!app_library) | 49 if (!app_library) |
49 return; | 50 return; |
50 | 51 |
51 // Set the application context if needed. Most applications will need to | 52 // Set the application context if needed. Most applications will need to |
52 // access the Android ApplicationContext in which they are run. If the | 53 // access the Android ApplicationContext in which they are run. If the |
53 // application library exports the InitApplicationContext function, we will | 54 // application library exports the InitApplicationContext function, we will |
54 // set it there. | 55 // set it there. |
55 const char* init_application_context_name = "InitApplicationContext"; | 56 const char* init_application_context_name = "InitApplicationContext"; |
56 typedef void (*InitApplicationContextFn)( | 57 typedef void (*InitApplicationContextFn)( |
57 const base::android::JavaRef<jobject>&); | 58 const base::android::JavaRef<jobject>&); |
(...skipping 25 matching lines...) Expand all Loading... |
83 AndroidHandler::~AndroidHandler() { | 84 AndroidHandler::~AndroidHandler() { |
84 } | 85 } |
85 | 86 |
86 void AndroidHandler::RunApplication( | 87 void AndroidHandler::RunApplication( |
87 mojo::InterfaceRequest<mojo::Application> application_request, | 88 mojo::InterfaceRequest<mojo::Application> application_request, |
88 mojo::URLResponsePtr response) { | 89 mojo::URLResponsePtr response) { |
89 JNIEnv* env = AttachCurrentThread(); | 90 JNIEnv* env = AttachCurrentThread(); |
90 uintptr_t tracing_id = reinterpret_cast<uintptr_t>(this); | 91 uintptr_t tracing_id = reinterpret_cast<uintptr_t>(this); |
91 TRACE_EVENT_ASYNC_BEGIN1("android_handler", "AndroidHandler::RunApplication", | 92 TRACE_EVENT_ASYNC_BEGIN1("android_handler", "AndroidHandler::RunApplication", |
92 tracing_id, "url", std::string(response->url)); | 93 tracing_id, "url", std::string(response->url)); |
93 ScopedJavaLocalRef<jstring> j_archive_path = | 94 base::FilePath extracted_dir; |
94 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | 95 base::FilePath cache_dir; |
95 base::FilePath archive_path( | 96 { |
96 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | 97 base::MessageLoop loop; |
| 98 handler_task_runner_->PostTask( |
| 99 FROM_HERE, |
| 100 base::Bind(&AndroidHandler::ExtractApplication, base::Unretained(this), |
| 101 base::Unretained(&extracted_dir), |
| 102 base::Unretained(&cache_dir), base::Passed(response.Pass()), |
| 103 base::Bind(base::IgnoreResult( |
| 104 &base::SingleThreadTaskRunner::PostTask), |
| 105 loop.task_runner(), FROM_HERE, |
| 106 base::MessageLoop::QuitWhenIdleClosure()))); |
| 107 base::RunLoop().Run(); |
| 108 } |
97 | 109 |
98 mojo::common::BlockingCopyToFile(response->body.Pass(), archive_path); | 110 ScopedJavaLocalRef<jstring> j_extracted_dir = |
| 111 ConvertUTF8ToJavaString(env, extracted_dir.value()); |
| 112 ScopedJavaLocalRef<jstring> j_cache_dir = |
| 113 ConvertUTF8ToJavaString(env, cache_dir.value()); |
99 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; | 114 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; |
100 Java_AndroidHandler_bootstrap( | 115 Java_AndroidHandler_bootstrap( |
101 env, GetApplicationContext(), tracing_id, j_archive_path.obj(), | 116 env, GetApplicationContext(), tracing_id, j_extracted_dir.obj(), |
| 117 j_cache_dir.obj(), |
102 application_request.PassMessagePipe().release().value(), | 118 application_request.PassMessagePipe().release().value(), |
103 reinterpret_cast<jlong>(run_android_application_fn)); | 119 reinterpret_cast<jlong>(run_android_application_fn)); |
104 } | 120 } |
105 | 121 |
106 void AndroidHandler::Initialize(mojo::ApplicationImpl* app) { | 122 void AndroidHandler::Initialize(mojo::ApplicationImpl* app) { |
| 123 handler_task_runner_ = base::MessageLoop::current()->task_runner(); |
| 124 app->ConnectToService("mojo:url_response_disk_cache", |
| 125 &url_response_disk_cache_); |
107 } | 126 } |
108 | 127 |
109 bool AndroidHandler::ConfigureIncomingConnection( | 128 bool AndroidHandler::ConfigureIncomingConnection( |
110 mojo::ApplicationConnection* connection) { | 129 mojo::ApplicationConnection* connection) { |
111 connection->AddService(&content_handler_factory_); | 130 connection->AddService(&content_handler_factory_); |
112 connection->AddService(&intent_receiver_manager_factory_); | 131 connection->AddService(&intent_receiver_manager_factory_); |
113 return true; | 132 return true; |
114 } | 133 } |
115 | 134 |
| 135 void AndroidHandler::ExtractApplication(base::FilePath* extracted_dir, |
| 136 base::FilePath* cache_dir, |
| 137 mojo::URLResponsePtr response, |
| 138 const base::Closure& callback) { |
| 139 url_response_disk_cache_->GetExtractedContent( |
| 140 response.Pass(), |
| 141 [extracted_dir, cache_dir, callback](mojo::Array<uint8_t> extracted_path, |
| 142 mojo::Array<uint8_t> cache_path) { |
| 143 if (extracted_path.is_null()) { |
| 144 *extracted_dir = base::FilePath(); |
| 145 *cache_dir = base::FilePath(); |
| 146 } else { |
| 147 *extracted_dir = base::FilePath( |
| 148 std::string(reinterpret_cast<char*>(&extracted_path.front()), |
| 149 extracted_path.size())); |
| 150 *cache_dir = base::FilePath(std::string( |
| 151 reinterpret_cast<char*>(&cache_path.front()), cache_path.size())); |
| 152 } |
| 153 callback.Run(); |
| 154 }); |
| 155 } |
| 156 |
116 bool RegisterAndroidHandlerJni(JNIEnv* env) { | 157 bool RegisterAndroidHandlerJni(JNIEnv* env) { |
117 return RegisterNativesImpl(env); | 158 return RegisterNativesImpl(env); |
118 } | 159 } |
119 | 160 |
120 } // namespace shell | 161 } // namespace shell |
OLD | NEW |