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 "mojo/runner/android/android_handler.h" | 5 #include "mojo/runner/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/scoped_native_library.h" | 11 #include "base/scoped_native_library.h" |
12 #include "jni/AndroidHandler_jni.h" | 12 #include "jni/AndroidHandler_jni.h" |
13 #include "mojo/application/public/cpp/application_impl.h" | 13 #include "mojo/application/public/cpp/application_impl.h" |
14 #include "mojo/common/data_pipe_utils.h" | 14 #include "mojo/common/data_pipe_utils.h" |
15 #include "mojo/public/c/system/main.h" | 15 #include "mojo/public/c/system/main.h" |
16 #include "mojo/runner/android/run_android_application_function.h" | 16 #include "mojo/runner/android/run_android_application_function.h" |
17 #include "mojo/runner/native_application_support.h" | 17 #include "mojo/runner/native_application_support.h" |
18 #include "mojo/util/filename_util.h" | |
19 #include "url/gurl.h" | |
20 | 18 |
21 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
22 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
23 using base::android::ConvertJavaStringToUTF8; | 21 using base::android::ConvertJavaStringToUTF8; |
24 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
25 using base::android::GetApplicationContext; | 23 using base::android::GetApplicationContext; |
26 | 24 |
27 namespace mojo { | 25 namespace mojo { |
28 namespace runner { | 26 namespace runner { |
29 | 27 |
30 namespace { | 28 namespace { |
31 | 29 |
32 // This function loads the application library, sets the application context and | 30 // 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 | 31 // 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 | 32 // set correctly we keep it in the Mojo shell .so and pass the function pointer |
35 // to the helper libbootstrap.so. | 33 // to the helper libbootstrap.so. |
36 void RunAndroidApplication(JNIEnv* env, | 34 void RunAndroidApplication(JNIEnv* env, |
37 jobject j_context, | 35 jobject j_context, |
38 const base::FilePath& app_path, | 36 const base::FilePath& app_path, |
39 jint j_handle, | 37 jint j_handle) { |
40 bool is_cached_app) { | |
41 InterfaceRequest<Application> application_request = | 38 InterfaceRequest<Application> application_request = |
42 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle))); | 39 MakeRequest<Application>(MakeScopedHandle(MessagePipeHandle(j_handle))); |
43 | 40 |
44 // Load the library, so that we can set the application context there if | 41 // Load the library, so that we can set the application context there if |
45 // needed. | 42 // needed. |
46 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! | 43 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! |
47 base::NativeLibrary app_library = LoadNativeApplication( | 44 base::NativeLibrary app_library = |
48 app_path, is_cached_app ? shell::NativeApplicationCleanup::DONT_DELETE | 45 LoadNativeApplication(app_path, shell::NativeApplicationCleanup::DELETE); |
49 : shell::NativeApplicationCleanup::DELETE); | |
50 if (!app_library) | 46 if (!app_library) |
51 return; | 47 return; |
52 | 48 |
53 // Set the application context if needed. Most applications will need to | 49 // Set the application context if needed. Most applications will need to |
54 // access the Android ApplicationContext in which they are run. If the | 50 // access the Android ApplicationContext in which they are run. If the |
55 // application library exports the InitApplicationContext function, we will | 51 // application library exports the InitApplicationContext function, we will |
56 // set it there. | 52 // set it there. |
57 const char* init_application_context_name = "InitApplicationContext"; | 53 const char* init_application_context_name = "InitApplicationContext"; |
58 typedef void (*InitApplicationContextFn)( | 54 typedef void (*InitApplicationContextFn)( |
59 const base::android::JavaRef<jobject>&); | 55 const base::android::JavaRef<jobject>&); |
60 InitApplicationContextFn init_application_context = | 56 InitApplicationContextFn init_application_context = |
61 reinterpret_cast<InitApplicationContextFn>( | 57 reinterpret_cast<InitApplicationContextFn>( |
62 base::GetFunctionPointerFromNativeLibrary( | 58 base::GetFunctionPointerFromNativeLibrary( |
63 app_library, init_application_context_name)); | 59 app_library, init_application_context_name)); |
64 if (init_application_context) { | 60 if (init_application_context) { |
65 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); | 61 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); |
66 init_application_context(scoped_context); | 62 init_application_context(scoped_context); |
67 } | 63 } |
68 | 64 |
69 // Run the application. | 65 // Run the application. |
70 RunNativeApplication(app_library, application_request.Pass()); | 66 RunNativeApplication(app_library, application_request.Pass()); |
71 // TODO(vtl): See note about unloading and thread-local destructors above | 67 // TODO(vtl): See note about unloading and thread-local destructors above |
72 // declaration of |LoadNativeApplication()|. | 68 // declaration of |LoadNativeApplication()|. |
73 base::UnloadNativeLibrary(app_library); | 69 base::UnloadNativeLibrary(app_library); |
74 } | 70 } |
75 | 71 |
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_local_apps_dir = | |
84 Java_AndroidHandler_getLocalAppsDir(env, GetApplicationContext()); | |
85 const base::FilePath local_apps_fp( | |
86 ConvertJavaStringToUTF8(env, j_local_apps_dir.obj())); | |
87 const std::string local_apps(util::FilePathToFileURL(local_apps_fp).spec()); | |
88 const std::string response_url(GURL(url).spec()); | |
89 if (response_url.size() <= local_apps.size() || | |
90 local_apps.compare(0u, local_apps.size(), response_url, 0u, | |
91 local_apps.size()) != 0) { | |
92 return false; | |
93 } | |
94 | |
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(local_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; | |
109 } | |
110 *app_dir = local_apps_fp.Append(head); | |
111 *path_to_mojo = app_dir->Append(tail); | |
112 return true; | |
113 } | |
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; | |
119 } | |
120 | |
121 *app_dir = local_apps_fp.Append( | |
122 app_rel_path.substr(0, app_rel_path.size() - mojo_suffix.size())); | |
123 *path_to_mojo = local_apps_fp.Append(app_rel_path); | |
124 return true; | |
125 } | |
126 | |
127 } // namespace | 72 } // namespace |
128 | 73 |
129 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { | 74 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { |
130 } | 75 } |
131 | 76 |
132 AndroidHandler::~AndroidHandler() { | 77 AndroidHandler::~AndroidHandler() { |
133 } | 78 } |
134 | 79 |
135 void AndroidHandler::RunApplication( | 80 void AndroidHandler::RunApplication( |
136 InterfaceRequest<Application> application_request, | 81 InterfaceRequest<Application> application_request, |
137 URLResponsePtr response) { | 82 URLResponsePtr response) { |
138 JNIEnv* env = AttachCurrentThread(); | 83 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; | |
155 } | |
156 } | |
157 ScopedJavaLocalRef<jstring> j_archive_path = | 84 ScopedJavaLocalRef<jstring> j_archive_path = |
158 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | 85 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); |
159 base::FilePath archive_path( | 86 base::FilePath archive_path( |
160 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | 87 ConvertJavaStringToUTF8(env, j_archive_path.obj())); |
161 | 88 |
162 common::BlockingCopyToFile(response->body.Pass(), archive_path); | 89 common::BlockingCopyToFile(response->body.Pass(), archive_path); |
| 90 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; |
163 Java_AndroidHandler_bootstrap( | 91 Java_AndroidHandler_bootstrap( |
164 env, GetApplicationContext(), j_archive_path.obj(), | 92 env, GetApplicationContext(), j_archive_path.obj(), |
165 application_request.PassMessagePipe().release().value(), | 93 application_request.PassMessagePipe().release().value(), |
166 reinterpret_cast<jlong>(run_android_application_fn)); | 94 reinterpret_cast<jlong>(run_android_application_fn)); |
167 } | 95 } |
168 | 96 |
169 void AndroidHandler::Initialize(ApplicationImpl* app) { | 97 void AndroidHandler::Initialize(ApplicationImpl* app) { |
170 } | 98 } |
171 | 99 |
172 bool AndroidHandler::ConfigureIncomingConnection( | 100 bool AndroidHandler::ConfigureIncomingConnection( |
173 ApplicationConnection* connection) { | 101 ApplicationConnection* connection) { |
174 connection->AddService(&content_handler_factory_); | 102 connection->AddService(&content_handler_factory_); |
175 return true; | 103 return true; |
176 } | 104 } |
177 | 105 |
178 bool RegisterAndroidHandlerJni(JNIEnv* env) { | 106 bool RegisterAndroidHandlerJni(JNIEnv* env) { |
179 return RegisterNativesImpl(env); | 107 return RegisterNativesImpl(env); |
180 } | 108 } |
181 | 109 |
182 } // namespace runner | 110 } // namespace runner |
183 } // namespace mojo | 111 } // namespace mojo |
OLD | NEW |