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

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

Issue 816473002: Update mojo shell so that --args-for can be used on android (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Refactor and add unit tests Created 6 years 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/mojo_main.h" 5 #include "shell/android/mojo_main.h"
6 6
7 #include "base/android/command_line_android.h"
8 #include "base/android/java_handler_thread.h" 7 #include "base/android/java_handler_thread.h"
9 #include "base/android/jni_android.h" 8 #include "base/android/jni_android.h"
9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/at_exit.h" 11 #include "base/at_exit.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
19 #include "jni/MojoMain_jni.h" 19 #include "jni/MojoMain_jni.h"
20 #include "mojo/application_manager/application_loader.h" 20 #include "mojo/application_manager/application_loader.h"
21 #include "mojo/application_manager/background_shell_application_loader.h" 21 #include "mojo/application_manager/background_shell_application_loader.h"
22 #include "shell/android/android_handler_loader.h" 22 #include "shell/android/android_handler_loader.h"
23 #include "shell/android/native_viewport_application_loader.h" 23 #include "shell/android/native_viewport_application_loader.h"
24 #include "shell/android/ui_application_loader_android.h" 24 #include "shell/android/ui_application_loader_android.h"
25 #include "shell/command_line_util.h"
25 #include "shell/context.h" 26 #include "shell/context.h"
26 #include "shell/init.h" 27 #include "shell/init.h"
27 #include "ui/gl/gl_surface_egl.h" 28 #include "ui/gl/gl_surface_egl.h"
28 29
29 using base::LazyInstance; 30 using base::LazyInstance;
30 31
31 namespace mojo { 32 namespace mojo {
32 namespace shell { 33 namespace shell {
33 34
34 namespace { 35 namespace {
(...skipping 16 matching lines...) Expand all
51 // Android handler is bundled with the Mojo shell, because it uses the 52 // Android handler is bundled with the Mojo shell, because it uses the
52 // MojoShell application as the JNI bridge to bootstrap execution of other 53 // MojoShell application as the JNI bridge to bootstrap execution of other
53 // Android Mojo apps that need JNI. 54 // Android Mojo apps that need JNI.
54 context->application_manager()->SetLoaderForURL( 55 context->application_manager()->SetLoaderForURL(
55 make_scoped_ptr(new BackgroundShellApplicationLoader( 56 make_scoped_ptr(new BackgroundShellApplicationLoader(
56 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", 57 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
57 base::MessageLoop::TYPE_DEFAULT)), 58 base::MessageLoop::TYPE_DEFAULT)),
58 GURL("mojo:android_handler")); 59 GURL("mojo:android_handler"));
59 } 60 }
60 61
61 void RunShell(std::vector<GURL> app_urls) { 62 void StartShellOnShellThread() {
62 Context* context = g_context.Pointer()->get(); 63 Context* context = g_context.Pointer()->get();
63 ConfigureAndroidServices(context); 64 ConfigureAndroidServices(context);
64 context->Init(); 65 context->Init();
65 for (std::vector<GURL>::const_iterator it = app_urls.begin(); 66 RunCommandLineApps(context);
66 it != app_urls.end(); ++it) {
67 context->Run(*it);
68 }
69 } 67 }
70 68
71 } // namespace 69 } // namespace
72 70
73 static void Init(JNIEnv* env, 71 static void Init(JNIEnv* env,
74 jclass clazz, 72 jclass clazz,
75 jobject context, 73 jobject context,
76 jstring mojo_shell_path, 74 jstring mojo_shell_path,
77 jobjectArray jparameters, 75 jobjectArray jparameters,
78 jstring j_local_apps_directory) { 76 jstring j_local_apps_directory) {
79 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); 77 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
80 base::android::InitApplicationContext(env, scoped_context); 78 base::android::InitApplicationContext(env, scoped_context);
81 79
82 base::android::InitNativeCommandLineFromJavaArray(env, jparameters); 80 std::vector<std::string> parameters;
83 base::FilePath mojo_shell_file_path( 81 parameters.push_back(
84 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); 82 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path));
85 base::CommandLine::ForCurrentProcess()->SetProgram(mojo_shell_file_path); 83 base::android::AppendJavaStringArrayToStringVector(env, jparameters,
84 &parameters);
85 CommandLine::Init(0, nullptr);
86 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters);
87
86 InitializeLogging(); 88 InitializeLogging();
87 89
88 // We want ~MessageLoop to happen prior to ~Context. Initializing 90 // We want ~MessageLoop to happen prior to ~Context. Initializing
89 // LazyInstances is akin to stack-allocating objects; their destructors 91 // LazyInstances is akin to stack-allocating objects; their destructors
90 // will be invoked first-in-last-out. 92 // will be invoked first-in-last-out.
91 Context* shell_context = new Context(); 93 Context* shell_context = new Context();
92 shell_context->mojo_url_resolver()->SetLocalAppsPath(base::FilePath( 94 shell_context->mojo_url_resolver()->SetLocalAppsPath(base::FilePath(
93 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); 95 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory)));
96 for (auto& args : parameters) {
sky 2014/12/18 17:01:15 nit: no {}
qsr 2014/12/18 17:17:56 Done.
97 ApplyApplicationArgs(shell_context, args);
98 }
99
94 g_context.Get().reset(shell_context); 100 g_context.Get().reset(shell_context);
95 101
96 g_java_message_loop.Get().reset(new base::MessageLoopForUI); 102 g_java_message_loop.Get().reset(new base::MessageLoopForUI);
97 base::MessageLoopForUI::current()->Start(); 103 base::MessageLoopForUI::current()->Start();
98 104
99 // TODO(abarth): At which point should we switch to cross-platform 105 // TODO(abarth): At which point should we switch to cross-platform
100 // initialization? 106 // initialization?
101 107
102 gfx::GLSurface::InitializeOneOff(); 108 gfx::GLSurface::InitializeOneOff();
103 } 109 }
104 110
105 static void Start(JNIEnv* env, jclass clazz, jstring jurl) { 111 static jboolean Start(JNIEnv* env, jclass clazz) {
106 std::vector<GURL> app_urls; 112 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size())
113 return false;
114
107 #if defined(MOJO_SHELL_DEBUG_URL) 115 #if defined(MOJO_SHELL_DEBUG_URL)
108 app_urls.push_back(GURL(MOJO_SHELL_DEBUG_URL)); 116 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL);
109 // Sleep for 5 seconds to give the debugger a chance to attach. 117 // Sleep for 5 seconds to give the debugger a chance to attach.
110 sleep(5); 118 sleep(5);
111 #else
112 if (jurl)
113 app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl)));
114 #endif 119 #endif
115 120
116 g_shell_thread.Get().reset( 121 g_shell_thread.Get().reset(
117 new base::android::JavaHandlerThread("shell_thread")); 122 new base::android::JavaHandlerThread("shell_thread"));
118 g_shell_thread.Get()->Start(); 123 g_shell_thread.Get()->Start();
119 g_shell_thread.Get()->message_loop()->PostTask( 124 g_shell_thread.Get()->message_loop()->PostTask(
120 FROM_HERE, base::Bind(&RunShell, app_urls)); 125 FROM_HERE, base::Bind(&StartShellOnShellThread));
126 return true;
127 }
128
129 static void AddApplicationURL(JNIEnv* env, jclass clazz, jstring jurl) {
130 base::CommandLine::ForCurrentProcess()->AppendArg(
131 base::android::ConvertJavaStringToUTF8(env, jurl));
121 } 132 }
122 133
123 bool RegisterMojoMain(JNIEnv* env) { 134 bool RegisterMojoMain(JNIEnv* env) {
124 return RegisterNativesImpl(env); 135 return RegisterNativesImpl(env);
125 } 136 }
126 137
127 } // namespace shell 138 } // namespace shell
128 } // namespace mojo 139 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698