Chromium Code Reviews| 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 // This is an example of an Android Mojo app that is composed of native code and | 5 // This is an example of an Android Mojo app that is composed of native code and |
| 6 // Java code that the native code calls into. In particular, we call to Java | 6 // Java code that the native code calls into. In particular, we call to Java |
| 7 // upon entering MojoMain to retrieve the name of the device that the | 7 // upon entering MojoMain to retrieve the name of the device that the |
| 8 // application is running on and print it out to the log. | 8 // application is running on and print it out to the log. |
| 9 // | 9 // |
| 10 // To run the example: | 10 // To run the example: |
| 11 // - build and install the MojoShell on the device: | 11 // - build and install the MojoShell on the device: |
| 12 // ninja -C some_place mojo_shell_apk | 12 // ninja -C some_place mojo_shell_apk |
| 13 // adb install -r some_place/apks/MojoShell.apk | 13 // adb install -r some_place/apks/MojoShell.apk |
| 14 // - install forwarder on the device: | 14 // - install forwarder on the device: |
| 15 // ninja -C some_place forwarder | 15 // ninja -C some_place forwarder |
| 16 // adb push some_place/forwarder /data/tmp/forwarder | 16 // adb push some_place/forwarder /data/tmp/forwarder |
| 17 // - run the forwarder on the device: | 17 // - run the forwarder on the device: |
| 18 // adb shell /data/tmp/forwarder 4444:4444 | 18 // adb shell /data/tmp/forwarder 4444:4444 |
| 19 // - build the example: | 19 // - build the example: |
| 20 // ninja -C some_place examples/device_name | 20 // ninja -C some_place examples/device_name |
| 21 // - set up the http server in your output directory: | 21 // - set up the http server in your output directory: |
| 22 // python -m SimpleHTTPServer 4444 | 22 // python -m SimpleHTTPServer 4444 |
| 23 // - build/android/adb_run_mojo_shell | 23 // - build/android/adb_run_mojo_shell |
| 24 // http://127.0.0.1:4444/obj/examples/device_name/device_name.mojo | 24 // http://127.0.0.1:4444/obj/examples/device_name/device_name.mojo |
| 25 | 25 |
| 26 #include "base/android/jni_string.h" | 26 #include "base/android/jni_string.h" |
| 27 #include "jni/DeviceName_jni.h" | 27 #include "jni/DeviceName_jni.h" |
| 28 #include "mojo/public/c/system/main.h" | 28 #include "mojo/public/c/system/main.h" |
| 29 #include "mojo/public/cpp/system/core.h" | |
| 29 | 30 |
| 30 namespace mojo { | 31 namespace mojo { |
| 31 | 32 |
| 32 namespace examples { | 33 namespace examples { |
| 33 | 34 |
| 34 bool RegisterDeviceJni(JNIEnv* env) { | 35 bool RegisterDeviceJni(JNIEnv* env) { |
| 35 return RegisterNativesImpl(env); | 36 return RegisterNativesImpl(env); |
| 36 } | 37 } |
| 37 | 38 |
| 38 // Simplest example of a function that needs to call into Java. | 39 // Simplest example of a function that needs to call into Java. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 51 env, Java_DeviceName_getApplicationClassName(env, context).obj()); | 52 env, Java_DeviceName_getApplicationClassName(env, context).obj()); |
| 52 } | 53 } |
| 53 | 54 |
| 54 } // namespace examples | 55 } // namespace examples |
| 55 } // namespace mojo | 56 } // namespace mojo |
| 56 | 57 |
| 57 MojoResult MojoMain(MojoHandle shell_handle) { | 58 MojoResult MojoMain(MojoHandle shell_handle) { |
| 58 LOG(INFO) << "Device name: " << mojo::examples::GetDeviceName(); | 59 LOG(INFO) << "Device name: " << mojo::examples::GetDeviceName(); |
| 59 LOG(INFO) << "Application class: " | 60 LOG(INFO) << "Application class: " |
| 60 << mojo::examples::GetApplicationClassName(); | 61 << mojo::examples::GetApplicationClassName(); |
| 62 | |
| 63 // Call a core function to demonstrate that the thunks were properly set. | |
|
ppi
2014/12/03 12:30:24
Ben, is it possible to do it more concisely than i
qsr
2014/12/03 12:38:33
Yes, and no. I would use MojoGetTimeTicksNow() ins
ppi
2014/12/03 13:06:05
Thanks, done.
| |
| 64 mojo::ScopedMessagePipeHandle handle_a; | |
| 65 mojo::ScopedMessagePipeHandle handle_b; | |
| 66 CreateMessagePipe(NULL, &handle_a, &handle_b); | |
| 67 LOG(INFO) << "All looks good."; | |
| 61 return MOJO_RESULT_OK; | 68 return MOJO_RESULT_OK; |
| 62 } | 69 } |
| 63 | 70 |
| 64 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 71 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 65 base::android::InitVM(vm); | 72 base::android::InitVM(vm); |
| 66 JNIEnv* env = base::android::AttachCurrentThread(); | 73 JNIEnv* env = base::android::AttachCurrentThread(); |
| 67 | 74 |
| 68 if (!mojo::examples::RegisterDeviceJni(env)) | 75 if (!mojo::examples::RegisterDeviceJni(env)) |
| 69 return -1; | 76 return -1; |
| 70 | 77 |
| 71 return JNI_VERSION_1_4; | 78 return JNI_VERSION_1_4; |
| 72 } | 79 } |
| 73 | 80 |
| 74 // This is needed only if the application actually needs to access the | 81 // This is needed only if the application actually needs to access the |
| 75 // application context. For instance, GetDeviceName() itself doesn't need it, | 82 // application context. For instance, GetDeviceName() itself doesn't need it, |
| 76 // but we use it for demonstrative purposes in GetApplicationClassName(). | 83 // but we use it for demonstrative purposes in GetApplicationClassName(). |
| 77 extern "C" JNI_EXPORT void InitApplicationContext( | 84 extern "C" JNI_EXPORT void InitApplicationContext( |
| 78 const base::android::JavaRef<jobject>& context) { | 85 const base::android::JavaRef<jobject>& context) { |
| 79 JNIEnv* env = base::android::AttachCurrentThread(); | 86 JNIEnv* env = base::android::AttachCurrentThread(); |
| 80 base::android::InitApplicationContext(env, context); | 87 base::android::InitApplicationContext(env, context); |
| 81 } | 88 } |
| OLD | NEW |