Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: services/android/java_handler.cc

Issue 1464313002: Move //service/android to //service/java_handler (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Follow review Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/android/java_handler.h ('k') | services/java_handler/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/base_jni_onload.h"
8 #include "base/android/base_jni_registrar.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/library_loader/library_loader_hooks.h"
12 #include "base/bind.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/run_loop.h"
16 #include "base/scoped_native_library.h"
17 #include "base/trace_event/trace_event.h"
18 #include "jni/JavaHandler_jni.h"
19 #include "mojo/android/system/base_run_loop.h"
20 #include "mojo/android/system/core_impl.h"
21 #include "mojo/application/application_runner_chromium.h"
22 #include "mojo/application/content_handler_factory.h"
23 #include "mojo/public/c/system/main.h"
24 #include "mojo/public/cpp/application/application_impl.h"
25
26 using base::android::AttachCurrentThread;
27 using base::android::ScopedJavaLocalRef;
28 using base::android::ConvertJavaStringToUTF8;
29 using base::android::ConvertUTF8ToJavaString;
30 using base::android::GetApplicationContext;
31
32 namespace {
33
34 bool RegisterJNI(JNIEnv* env) {
35 if (!base::android::RegisterJni(env))
36 return false;
37
38 if (!services::android::RegisterNativesImpl(env))
39 return false;
40
41 if (!mojo::android::RegisterCoreImpl(env))
42 return false;
43
44 if (!mojo::android::RegisterBaseRunLoop(env))
45 return false;
46
47 return true;
48 }
49
50 } // namespace
51
52 namespace services {
53 namespace android {
54
55 JavaHandler::JavaHandler() : content_handler_factory_(this) {
56 }
57
58 JavaHandler::~JavaHandler() {
59 }
60
61 void JavaHandler::RunApplication(
62 mojo::InterfaceRequest<mojo::Application> application_request,
63 mojo::URLResponsePtr response) {
64 TRACE_EVENT_BEGIN1("java_handler", "JavaHandler::RunApplication", "url",
65 std::string(response->url));
66 JNIEnv* env = base::android::AttachCurrentThread();
67 base::FilePath archive_path;
68 base::FilePath cache_dir;
69 {
70 base::MessageLoop loop;
71 handler_task_runner_->PostTask(
72 FROM_HERE,
73 base::Bind(&JavaHandler::GetApplication, base::Unretained(this),
74 base::Unretained(&archive_path),
75 base::Unretained(&cache_dir), base::Passed(response.Pass()),
76 base::Bind(base::IgnoreResult(
77 &base::SingleThreadTaskRunner::PostTask),
78 loop.task_runner(), FROM_HERE,
79 base::MessageLoop::QuitWhenIdleClosure())));
80 base::RunLoop().Run();
81 }
82
83
84 jobject context = base::android::GetApplicationContext();
85 ScopedJavaLocalRef<jstring> j_archive_path =
86 ConvertUTF8ToJavaString(env, archive_path.value());
87 ScopedJavaLocalRef<jstring> j_cache_dir =
88 ConvertUTF8ToJavaString(env, cache_dir.value());
89 Java_JavaHandler_bootstrap(
90 env, context, j_archive_path.obj(), j_cache_dir.obj(),
91 application_request.PassMessagePipe().release().value());
92 }
93
94 void JavaHandler::Initialize(mojo::ApplicationImpl* app) {
95 tracing_.Initialize(app);
96 handler_task_runner_ = base::MessageLoop::current()->task_runner();
97 app->ConnectToService("mojo:url_response_disk_cache",
98 &url_response_disk_cache_);
99 }
100
101 void JavaHandler::GetApplication(base::FilePath* archive_path,
102 base::FilePath* cache_dir,
103 mojo::URLResponsePtr response,
104 const base::Closure& callback) {
105 url_response_disk_cache_->UpdateAndGet(
106 response.Pass(),
107 [archive_path, cache_dir, callback](mojo::Array<uint8_t> extracted_path,
108 mojo::Array<uint8_t> cache_path) {
109 if (extracted_path.is_null()) {
110 *archive_path = base::FilePath();
111 *cache_dir = base::FilePath();
112 } else {
113 *archive_path = base::FilePath(
114 std::string(reinterpret_cast<char*>(&extracted_path.front()),
115 extracted_path.size()));
116 *cache_dir = base::FilePath(std::string(
117 reinterpret_cast<char*>(&cache_path.front()), cache_path.size()));
118 }
119 callback.Run();
120 });
121 }
122
123 bool JavaHandler::ConfigureIncomingConnection(
124 mojo::ApplicationConnection* connection) {
125 connection->AddService(&content_handler_factory_);
126 return true;
127 }
128
129 void PreInvokeEvent(JNIEnv* env, jclass jcaller) {
130 TRACE_EVENT_END0("java_handler", "JavaHandler::RunApplication");
131 }
132
133 } // namespace android
134 } // namespace services
135
136 MojoResult MojoMain(MojoHandle application_request) {
137 mojo::ApplicationRunnerChromium runner(new services::android::JavaHandler());
138 return runner.Run(application_request);
139 }
140
141 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
142 std::vector<base::android::RegisterCallback> register_callbacks;
143 register_callbacks.push_back(base::Bind(&RegisterJNI));
144 if (!base::android::OnJNIOnLoadRegisterJNI(vm, register_callbacks) ||
145 !base::android::OnJNIOnLoadInit(
146 std::vector<base::android::InitCallback>())) {
147 return -1;
148 }
149
150 // There cannot be two AtExit objects triggering at the same time. Remove the
151 // one from LibraryLoader as ApplicationRunnerChromium also uses one.
152 base::android::LibraryLoaderExitHook();
153
154 return JNI_VERSION_1_4;
155 }
156
157 // This is needed because the application needs to access the application
158 // context.
159 extern "C" JNI_EXPORT void InitApplicationContext(
160 const base::android::JavaRef<jobject>& context) {
161 JNIEnv* env = base::android::AttachCurrentThread();
162 base::android::InitApplicationContext(env, context);
163 }
164
OLDNEW
« no previous file with comments | « services/android/java_handler.h ('k') | services/java_handler/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698