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

Side by Side Diff: shell/android/android_handler.cc

Issue 1088533003: Adding URLResponse Disk Cache to mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: And now with full app tests and a bug fix. Created 5 years, 7 months 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
OLDNEW
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"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
11 #include "base/scoped_native_library.h" 13 #include "base/scoped_native_library.h"
12 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
13 #include "jni/AndroidHandler_jni.h" 15 #include "jni/AndroidHandler_jni.h"
14 #include "mojo/common/data_pipe_utils.h" 16 #include "mojo/common/data_pipe_utils.h"
15 #include "mojo/public/c/system/main.h" 17 #include "mojo/public/c/system/main.h"
16 #include "mojo/public/cpp/application/application_impl.h" 18 #include "mojo/public/cpp/application/application_impl.h"
17 #include "shell/android/run_android_application_function.h" 19 #include "shell/android/run_android_application_function.h"
18 #include "shell/native_application_support.h" 20 #include "shell/native_application_support.h"
19 21
20 using base::android::AttachCurrentThread; 22 using base::android::AttachCurrentThread;
(...skipping 15 matching lines...) Expand all
36 uintptr_t tracing_id, 38 uintptr_t tracing_id,
37 const base::FilePath& app_path, 39 const base::FilePath& app_path,
38 jint j_handle) { 40 jint j_handle) {
39 mojo::InterfaceRequest<mojo::Application> application_request = 41 mojo::InterfaceRequest<mojo::Application> application_request =
40 mojo::MakeRequest<mojo::Application>( 42 mojo::MakeRequest<mojo::Application>(
41 mojo::MakeScopedHandle(mojo::MessagePipeHandle(j_handle))); 43 mojo::MakeScopedHandle(mojo::MessagePipeHandle(j_handle)));
42 44
43 // Load the library, so that we can set the application context there if 45 // Load the library, so that we can set the application context there if
44 // needed. 46 // needed.
45 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()! 47 // TODO(vtl): We'd use a ScopedNativeLibrary, but it doesn't have .get()!
46 base::NativeLibrary app_library = 48 base::NativeLibrary app_library = LoadNativeApplication(app_path);
47 LoadNativeApplication(app_path, NativeApplicationCleanup::DELETE);
48 if (!app_library) 49 if (!app_library)
49 return; 50 return;
50 51
51 // Set the application context if needed. Most applications will need to 52 // Set the application context if needed. Most applications will need to
52 // access the Android ApplicationContext in which they are run. If the 53 // access the Android ApplicationContext in which they are run. If the
53 // application library exports the InitApplicationContext function, we will 54 // application library exports the InitApplicationContext function, we will
54 // set it there. 55 // set it there.
55 const char* init_application_context_name = "InitApplicationContext"; 56 const char* init_application_context_name = "InitApplicationContext";
56 typedef void (*InitApplicationContextFn)( 57 typedef void (*InitApplicationContextFn)(
57 const base::android::JavaRef<jobject>&); 58 const base::android::JavaRef<jobject>&);
(...skipping 18 matching lines...) Expand all
76 } 77 }
77 78
78 } // namespace 79 } // namespace
79 80
80 AndroidHandler::AndroidHandler() : content_handler_factory_(this) { 81 AndroidHandler::AndroidHandler() : content_handler_factory_(this) {
81 } 82 }
82 83
83 AndroidHandler::~AndroidHandler() { 84 AndroidHandler::~AndroidHandler() {
84 } 85 }
85 86
87 void RunClosure(scoped_refptr<base::SingleThreadTaskRunner> runner,
88 const base::Closure& closure) {
89 runner->PostTask(FROM_HERE, closure);
90 }
91
86 void AndroidHandler::RunApplication( 92 void AndroidHandler::RunApplication(
87 mojo::InterfaceRequest<mojo::Application> application_request, 93 mojo::InterfaceRequest<mojo::Application> application_request,
88 mojo::URLResponsePtr response) { 94 mojo::URLResponsePtr response) {
89 JNIEnv* env = AttachCurrentThread(); 95 JNIEnv* env = AttachCurrentThread();
90 uintptr_t tracing_id = reinterpret_cast<uintptr_t>(this); 96 uintptr_t tracing_id = reinterpret_cast<uintptr_t>(this);
91 TRACE_EVENT_ASYNC_BEGIN1("android_handler", "AndroidHandler::RunApplication", 97 TRACE_EVENT_ASYNC_BEGIN1("android_handler", "AndroidHandler::RunApplication",
92 tracing_id, "url", std::string(response->url)); 98 tracing_id, "url", std::string(response->url));
93 ScopedJavaLocalRef<jstring> j_archive_path = 99 base::FilePath extracted_dir;
94 Java_AndroidHandler_getNewTempArchivePath(env, GetApplicationContext()); 100 base::FilePath cache_dir;
95 base::FilePath archive_path( 101 {
96 ConvertJavaStringToUTF8(env, j_archive_path.obj())); 102 base::MessageLoop loop;
103 handler_task_runner_->PostTask(
104 FROM_HERE,
105 base::Bind(&AndroidHandler::ExtractApplication, base::Unretained(this),
106 base::Unretained(&extracted_dir),
107 base::Unretained(&cache_dir), base::Passed(response.Pass()),
108 base::Bind(&RunClosure, loop.task_runner(),
109 base::MessageLoop::QuitWhenIdleClosure())));
110 base::RunLoop().Run();
DaveMoore 2015/05/05 21:20:59 What makes this runloop exit?
qsr 2015/05/06 08:13:49 In the previous line, the callback to ExtractAppli
111 }
97 112
98 mojo::common::BlockingCopyToFile(response->body.Pass(), archive_path); 113 ScopedJavaLocalRef<jstring> j_extracted_dir =
114 ConvertUTF8ToJavaString(env, extracted_dir.value());
115 ScopedJavaLocalRef<jstring> j_cache_dir =
116 ConvertUTF8ToJavaString(env, cache_dir.value());
99 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication; 117 RunAndroidApplicationFn run_android_application_fn = &RunAndroidApplication;
100 Java_AndroidHandler_bootstrap( 118 Java_AndroidHandler_bootstrap(
101 env, GetApplicationContext(), tracing_id, j_archive_path.obj(), 119 env, GetApplicationContext(), tracing_id, j_extracted_dir.obj(),
120 j_cache_dir.obj(),
102 application_request.PassMessagePipe().release().value(), 121 application_request.PassMessagePipe().release().value(),
103 reinterpret_cast<jlong>(run_android_application_fn)); 122 reinterpret_cast<jlong>(run_android_application_fn));
104 } 123 }
105 124
106 void AndroidHandler::Initialize(mojo::ApplicationImpl* app) { 125 void AndroidHandler::Initialize(mojo::ApplicationImpl* app) {
126 handler_task_runner_ = base::MessageLoop::current()->task_runner();
127 app->ConnectToService("mojo:service_cache", &service_cache_);
107 } 128 }
108 129
109 bool AndroidHandler::ConfigureIncomingConnection( 130 bool AndroidHandler::ConfigureIncomingConnection(
110 mojo::ApplicationConnection* connection) { 131 mojo::ApplicationConnection* connection) {
111 connection->AddService(&content_handler_factory_); 132 connection->AddService(&content_handler_factory_);
112 connection->AddService(&intent_receiver_manager_factory_); 133 connection->AddService(&intent_receiver_manager_factory_);
113 return true; 134 return true;
114 } 135 }
115 136
137 void AndroidHandler::ExtractApplication(base::FilePath* extracted_dir,
138 base::FilePath* cache_dir,
139 mojo::URLResponsePtr response,
140 const base::Closure& callback) {
141 service_cache_->GetExtractedContent(
142 response.Pass(),
143 [extracted_dir, cache_dir, callback](mojo::Array<uint8_t> extracted_path,
144 mojo::Array<uint8_t> cache_path) {
145 if (extracted_path.is_null()) {
146 *extracted_dir = base::FilePath();
147 *cache_dir = base::FilePath();
148 } else {
149 *extracted_dir = base::FilePath(
150 std::string(reinterpret_cast<char*>(&extracted_path.front()),
151 extracted_path.size()));
152 *cache_dir = base::FilePath(std::string(
153 reinterpret_cast<char*>(&cache_path.front()), cache_path.size()));
154 }
155 callback.Run();
156 });
157 }
158
116 bool RegisterAndroidHandlerJni(JNIEnv* env) { 159 bool RegisterAndroidHandlerJni(JNIEnv* env) {
117 return RegisterNativesImpl(env); 160 return RegisterNativesImpl(env);
118 } 161 }
119 162
120 } // namespace shell 163 } // namespace shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698