OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/android/java_handler.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/logging.h" | |
11 #include "base/scoped_native_library.h" | |
12 #include "jni/JavaHandler_jni.h" | |
13 #include "mojo/android/system/base_run_loop.h" | |
14 #include "mojo/android/system/core_impl.h" | |
15 #include "mojo/application/application_runner_chromium.h" | |
16 #include "mojo/application/content_handler_factory.h" | |
17 #include "mojo/common/data_pipe_utils.h" | |
18 #include "mojo/public/c/system/main.h" | |
19 #include "mojo/public/cpp/application/application_impl.h" | |
20 | |
21 using base::android::AttachCurrentThread; | |
22 using base::android::ScopedJavaLocalRef; | |
23 using base::android::ConvertJavaStringToUTF8; | |
24 using base::android::ConvertUTF8ToJavaString; | |
25 using base::android::GetApplicationContext; | |
26 | |
27 namespace services { | |
28 namespace android { | |
29 JavaHandler::JavaHandler() : content_handler_factory_(this) { | |
30 } | |
31 | |
32 JavaHandler::~JavaHandler() { | |
33 } | |
34 | |
35 void JavaHandler::RunApplication( | |
36 mojo::InterfaceRequest<mojo::Application> application_request, | |
37 mojo::URLResponsePtr response) { | |
38 JNIEnv* env = base::android::AttachCurrentThread(); | |
39 ScopedJavaLocalRef<jstring> j_archive_path = | |
40 Java_JavaHandler_getNewTempLibraryPath(env, GetApplicationContext()); | |
41 base::FilePath archive_path( | |
42 base::android::ConvertJavaStringToUTF8(env, j_archive_path.obj())); | |
43 | |
44 mojo::common::BlockingCopyToFile(response->body.Pass(), archive_path); | |
45 | |
46 jobject context = base::android::GetApplicationContext(); | |
47 Java_JavaHandler_bootstrap( | |
48 env, context, j_archive_path.obj(), | |
49 application_request.PassMessagePipe().release().value()); | |
50 } | |
51 | |
52 void JavaHandler::Initialize(mojo::ApplicationImpl* app) { | |
53 } | |
54 | |
55 bool JavaHandler::ConfigureIncomingConnection( | |
56 mojo::ApplicationConnection* connection) { | |
57 connection->AddService(&content_handler_factory_); | |
58 return true; | |
59 } | |
60 | |
61 bool RegisterJavaHandlerJni(JNIEnv* env) { | |
62 return RegisterNativesImpl(env); | |
63 } | |
64 | |
65 } // namespace android | |
66 } // namespace services | |
67 | |
68 MojoResult MojoMain(MojoHandle shell_handle) { | |
69 mojo::ApplicationRunnerChromium runner(new services::android::JavaHandler()); | |
70 return runner.Run(shell_handle); | |
71 } | |
72 | |
73 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |
74 base::android::InitVM(vm); | |
75 JNIEnv* env = base::android::AttachCurrentThread(); | |
76 | |
77 if (!services::android::RegisterJavaHandlerJni(env)) | |
78 return -1; | |
79 | |
80 if (!mojo::android::RegisterCoreImpl(env)) | |
81 return -1; | |
82 | |
83 if (!mojo::android::RegisterBaseRunLoop(env)) | |
84 return -1; | |
85 | |
86 return JNI_VERSION_1_4; | |
87 } | |
88 | |
89 // This is needed only if the application actually needs to access the | |
90 // application context. | |
qsr
2015/02/05 16:50:08
s/only if/because/
etiennej
2015/02/06 16:22:29
Done.
| |
91 extern "C" JNI_EXPORT void InitApplicationContext( | |
92 const base::android::JavaRef<jobject>& context) { | |
93 JNIEnv* env = base::android::AttachCurrentThread(); | |
94 base::android::InitApplicationContext(env, context); | |
95 } | |
96 | |
OLD | NEW |