| 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" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 // This function loads the application library, sets the application context and | 28 // This function loads the application library, sets the application context and |
| 29 // thunks and calls into the application MojoMain. To ensure that the thunks are | 29 // thunks and calls into the application MojoMain. To ensure that the thunks are |
| 30 // set correctly we keep it in the Mojo shell .so and pass the function pointer | 30 // set correctly we keep it in the Mojo shell .so and pass the function pointer |
| 31 // to the helper libbootstrap.so. | 31 // to the helper libbootstrap.so. |
| 32 void RunAndroidApplication(JNIEnv* env, | 32 void RunAndroidApplication(JNIEnv* env, |
| 33 jobject j_context, | 33 jobject j_context, |
| 34 const base::FilePath& app_path, | 34 const base::FilePath& app_path, |
| 35 jint j_handle) { | 35 jint j_handle) { |
| 36 ScopedMessagePipeHandle handle((mojo::MessagePipeHandle(j_handle))); | 36 ScopedMessagePipeHandle handle((MessagePipeHandle(j_handle))); |
| 37 | 37 |
| 38 // Load the library, so that we can set the application context there if | 38 // Load the library, so that we can set the application context there if |
| 39 // needed. | 39 // needed. |
| 40 base::NativeLibraryLoadError error; | 40 base::NativeLibraryLoadError error; |
| 41 base::ScopedNativeLibrary app_library( | 41 base::ScopedNativeLibrary app_library( |
| 42 base::LoadNativeLibrary(app_path, &error)); | 42 base::LoadNativeLibrary(app_path, &error)); |
| 43 if (!app_library.is_valid()) { | 43 if (!app_library.is_valid()) { |
| 44 LOG(ERROR) << "Failed to load app library (error: " << error.ToString() | 44 LOG(ERROR) << "Failed to load app library (error: " << error.ToString() |
| 45 << ")"; | 45 << ")"; |
| 46 return; | 46 return; |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Set the application context if needed. Most applications will need to | 49 // Set the application context if needed. Most applications will need to |
| 50 // access the Android ApplicationContext in which they are run. If the | 50 // access the Android ApplicationContext in which they are run. If the |
| 51 // application library exports the InitApplicationContext function, we will | 51 // application library exports the InitApplicationContext function, we will |
| 52 // set it there. | 52 // set it there. |
| 53 const char* init_application_context_name = "InitApplicationContext"; | 53 const char* init_application_context_name = "InitApplicationContext"; |
| 54 typedef void (*InitApplicationContextFn)( | 54 typedef void (*InitApplicationContextFn)( |
| 55 const base::android::JavaRef<jobject>&); | 55 const base::android::JavaRef<jobject>&); |
| 56 InitApplicationContextFn init_application_context = | 56 InitApplicationContextFn init_application_context = |
| 57 reinterpret_cast<InitApplicationContextFn>( | 57 reinterpret_cast<InitApplicationContextFn>( |
| 58 app_library.GetFunctionPointer(init_application_context_name)); | 58 app_library.GetFunctionPointer(init_application_context_name)); |
| 59 if (init_application_context) { | 59 if (init_application_context) { |
| 60 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); | 60 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, j_context); |
| 61 init_application_context(scoped_context); | 61 init_application_context(scoped_context); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Run the application. | 64 // Run the application. |
| 65 base::ScopedNativeLibrary app_library_from_runner( | 65 base::ScopedNativeLibrary app_library_from_runner( |
| 66 shell::DynamicServiceRunner::LoadAndRunService(app_path, handle.Pass())); | 66 shell::DynamicServiceRunner::LoadAndRunService( |
| 67 app_path, shell::DynamicServiceRunner::DeleteAppPath, handle.Pass())); |
| 67 } | 68 } |
| 68 } // namespace | 69 } // namespace |
| 69 | 70 |
| 70 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { | 71 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { |
| 71 } | 72 } |
| 72 | 73 |
| 73 AndroidHandler::~AndroidHandler() { | 74 AndroidHandler::~AndroidHandler() { |
| 74 } | 75 } |
| 75 | 76 |
| 76 void AndroidHandler::RunApplication(ShellPtr shell, URLResponsePtr response) { | 77 void AndroidHandler::RunApplication(ShellPtr shell, URLResponsePtr response) { |
| 77 JNIEnv* env = AttachCurrentThread(); | 78 JNIEnv* env = AttachCurrentThread(); |
| 78 ScopedJavaLocalRef<jstring> j_archive_path = | 79 ScopedJavaLocalRef<jstring> j_archive_path = |
| 79 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); | 80 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); |
| 80 base::FilePath archive_path( | 81 base::FilePath archive_path( |
| 81 ConvertJavaStringToUTF8(env, j_archive_path.obj())); | 82 ConvertJavaStringToUTF8(env, j_archive_path.obj())); |
| 82 | 83 |
| 83 mojo::common::BlockingCopyToFile(response->body.Pass(), archive_path); | 84 common::BlockingCopyToFile(response->body.Pass(), archive_path); |
| 84 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; | 85 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; |
| 85 Java_AndroidHandler_bootstrap( | 86 Java_AndroidHandler_bootstrap( |
| 86 env, GetApplicationContext(), j_archive_path.obj(), | 87 env, GetApplicationContext(), j_archive_path.obj(), |
| 87 shell.PassMessagePipe().release().value(), | 88 shell.PassMessagePipe().release().value(), |
| 88 reinterpret_cast<jlong>(run_android_application_fn)); | 89 reinterpret_cast<jlong>(run_android_application_fn)); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void AndroidHandler::Initialize(ApplicationImpl* app) { | 92 void AndroidHandler::Initialize(ApplicationImpl* app) { |
| 92 } | 93 } |
| 93 | 94 |
| 94 bool AndroidHandler::ConfigureIncomingConnection( | 95 bool AndroidHandler::ConfigureIncomingConnection( |
| 95 ApplicationConnection* connection) { | 96 ApplicationConnection* connection) { |
| 96 connection->AddService(&content_handler_factory_); | 97 connection->AddService(&content_handler_factory_); |
| 97 return true; | 98 return true; |
| 98 } | 99 } |
| 99 | 100 |
| 100 bool RegisterAndroidHandlerJni(JNIEnv* env) { | 101 bool RegisterAndroidHandlerJni(JNIEnv* env) { |
| 101 return RegisterNativesImpl(env); | 102 return RegisterNativesImpl(env); |
| 102 } | 103 } |
| 103 | 104 |
| 104 } // namespace mojo | 105 } // namespace mojo |
| OLD | NEW |