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

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

Issue 1067023003: Add shell::Tracer object and wire up to --trace-startup on Android (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: rebase Created 5 years, 8 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
« no previous file with comments | « shell/BUILD.gn ('k') | shell/desktop/main.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/main.h" 5 #include "shell/android/main.h"
6 6
7 #include "base/android/fifo_utils.h" 7 #include "base/android/fifo_utils.h"
8 #include "base/android/jni_android.h" 8 #include "base/android/jni_android.h"
9 #include "base/android/jni_array.h" 9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
(...skipping 11 matching lines...) Expand all
22 #include "jni/ShellMain_jni.h" 22 #include "jni/ShellMain_jni.h"
23 #include "mojo/common/message_pump_mojo.h" 23 #include "mojo/common/message_pump_mojo.h"
24 #include "shell/android/android_handler_loader.h" 24 #include "shell/android/android_handler_loader.h"
25 #include "shell/android/background_application_loader.h" 25 #include "shell/android/background_application_loader.h"
26 #include "shell/android/native_viewport_application_loader.h" 26 #include "shell/android/native_viewport_application_loader.h"
27 #include "shell/android/ui_application_loader_android.h" 27 #include "shell/android/ui_application_loader_android.h"
28 #include "shell/application_manager/application_loader.h" 28 #include "shell/application_manager/application_loader.h"
29 #include "shell/command_line_util.h" 29 #include "shell/command_line_util.h"
30 #include "shell/context.h" 30 #include "shell/context.h"
31 #include "shell/init.h" 31 #include "shell/init.h"
32 #include "shell/switches.h"
33 #include "shell/tracer.h"
32 #include "ui/gl/gl_surface_egl.h" 34 #include "ui/gl/gl_surface_egl.h"
33 35
34 using base::LazyInstance; 36 using base::LazyInstance;
35 37
36 namespace shell { 38 namespace shell {
37 39
38 namespace { 40 namespace {
39 41
40 // Tag for logging. 42 // Tag for logging.
41 const char kLogTag[] = "chromium"; 43 const char kLogTag[] = "chromium";
(...skipping 17 matching lines...) Expand all
59 const base::FilePath mojo_shell_path_; 61 const base::FilePath mojo_shell_path_;
60 const base::FilePath mojo_shell_child_path_; 62 const base::FilePath mojo_shell_child_path_;
61 const std::vector<std::string> parameters_; 63 const std::vector<std::string> parameters_;
62 64
63 DISALLOW_COPY_AND_ASSIGN(MojoShellRunner); 65 DISALLOW_COPY_AND_ASSIGN(MojoShellRunner);
64 }; 66 };
65 67
66 LazyInstance<scoped_ptr<base::MessageLoop>> g_java_message_loop = 68 LazyInstance<scoped_ptr<base::MessageLoop>> g_java_message_loop =
67 LAZY_INSTANCE_INITIALIZER; 69 LAZY_INSTANCE_INITIALIZER;
68 70
71 LazyInstance<scoped_ptr<Tracer>> g_tracer = LAZY_INSTANCE_INITIALIZER;
69 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER; 72 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER;
70 73
71 LazyInstance<scoped_ptr<MojoShellRunner>> g_shell_runner = 74 LazyInstance<scoped_ptr<MojoShellRunner>> g_shell_runner =
72 LAZY_INSTANCE_INITIALIZER; 75 LAZY_INSTANCE_INITIALIZER;
73 76
74 LazyInstance<scoped_ptr<base::DelegateSimpleThread>> g_shell_thread = 77 LazyInstance<scoped_ptr<base::DelegateSimpleThread>> g_shell_thread =
75 LAZY_INSTANCE_INITIALIZER; 78 LAZY_INSTANCE_INITIALIZER;
76 79
77 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activiy = 80 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activity =
78 LAZY_INSTANCE_INITIALIZER; 81 LAZY_INSTANCE_INITIALIZER;
79 82
80 void ConfigureAndroidServices(Context* context) { 83 void ConfigureAndroidServices(Context* context) {
81 context->application_manager()->SetLoaderForURL( 84 context->application_manager()->SetLoaderForURL(
82 make_scoped_ptr(new UIApplicationLoader( 85 make_scoped_ptr(new UIApplicationLoader(
83 make_scoped_ptr(new NativeViewportApplicationLoader()), 86 make_scoped_ptr(new NativeViewportApplicationLoader()),
84 g_java_message_loop.Get().get())), 87 g_java_message_loop.Get().get())),
85 GURL("mojo:native_viewport_service")); 88 GURL("mojo:native_viewport_service"));
86 89
87 // Android handler is bundled with the Mojo shell, because it uses the 90 // Android handler is bundled with the Mojo shell, because it uses the
88 // MojoShell application as the JNI bridge to bootstrap execution of other 91 // MojoShell application as the JNI bridge to bootstrap execution of other
89 // Android Mojo apps that need JNI. 92 // Android Mojo apps that need JNI.
90 context->application_manager()->SetLoaderForURL( 93 context->application_manager()->SetLoaderForURL(
91 make_scoped_ptr(new BackgroundApplicationLoader( 94 make_scoped_ptr(new BackgroundApplicationLoader(
92 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", 95 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
93 base::MessageLoop::TYPE_DEFAULT)), 96 base::MessageLoop::TYPE_DEFAULT)),
94 GURL("mojo:android_handler")); 97 GURL("mojo:android_handler"));
95 98
96 // By default, the keyboard is handled by the native_viewport_service. 99 // By default, the keyboard is handled by the native_viewport_service.
97 context->url_resolver()->AddURLMapping(GURL("mojo:keyboard"), 100 context->url_resolver()->AddURLMapping(GURL("mojo:keyboard"),
98 GURL("mojo:native_viewport_service")); 101 GURL("mojo:native_viewport_service"));
99 } 102 }
100 103
101 void QuitShellThread() { 104 void QuitShellThread() {
102 g_shell_thread.Get()->Join(); 105 g_shell_thread.Get()->Join();
103 g_shell_thread.Pointer()->reset(); 106 g_shell_thread.Pointer()->reset();
104 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(), 107 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(),
105 g_main_activiy.Get().obj()); 108 g_main_activity.Get().obj());
106 exit(0); 109 exit(0);
107 } 110 }
108 111
109 void MojoShellRunner::Run() { 112 void MojoShellRunner::Run() {
110 base::MessageLoop loop(mojo::common::MessagePumpMojo::Create()); 113 base::MessageLoop loop(mojo::common::MessagePumpMojo::Create());
111 Context* context = g_context.Pointer()->get(); 114 Context* context = g_context.Pointer()->get();
112 ConfigureAndroidServices(context); 115 ConfigureAndroidServices(context);
113 context->InitWithPaths(mojo_shell_path_, mojo_shell_child_path_); 116 context->InitWithPaths(mojo_shell_path_, mojo_shell_child_path_);
114 117
115 for (const auto& args : parameters_) 118 for (const auto& args : parameters_)
(...skipping 28 matching lines...) Expand all
144 } // namespace 147 } // namespace
145 148
146 static void Init(JNIEnv* env, 149 static void Init(JNIEnv* env,
147 jclass clazz, 150 jclass clazz,
148 jobject activity, 151 jobject activity,
149 jstring mojo_shell_path, 152 jstring mojo_shell_path,
150 jstring mojo_shell_child_path, 153 jstring mojo_shell_child_path,
151 jobjectArray jparameters, 154 jobjectArray jparameters,
152 jstring j_local_apps_directory, 155 jstring j_local_apps_directory,
153 jstring j_tmp_dir) { 156 jstring j_tmp_dir) {
154 g_main_activiy.Get().Reset(env, activity); 157 g_main_activity.Get().Reset(env, activity);
155 158
159 std::string tmp_dir = base::android::ConvertJavaStringToUTF8(env, j_tmp_dir);
156 // Setting the TMPDIR environment variable so that applications can use it. 160 // Setting the TMPDIR environment variable so that applications can use it.
157 // TODO(qsr) We will need our subprocesses to inherit this. 161 // TODO(qsr) We will need our subprocesses to inherit this.
158 int return_value = 162 int return_value = setenv("TMPDIR", tmp_dir.c_str(), 1);
159 setenv("TMPDIR",
160 base::android::ConvertJavaStringToUTF8(env, j_tmp_dir).c_str(), 1);
161 DCHECK_EQ(return_value, 0); 163 DCHECK_EQ(return_value, 0);
162 164
163 base::android::ScopedJavaLocalRef<jobject> scoped_activity(env, activity); 165 base::android::ScopedJavaLocalRef<jobject> scoped_activity(env, activity);
164 base::android::InitApplicationContext(env, scoped_activity); 166 base::android::InitApplicationContext(env, scoped_activity);
165 167
166 std::vector<std::string> parameters; 168 std::vector<std::string> parameters;
167 parameters.push_back( 169 parameters.push_back(
168 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); 170 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path));
169 base::android::AppendJavaStringArrayToStringVector(env, jparameters, 171 base::android::AppendJavaStringArrayToStringVector(env, jparameters,
170 &parameters); 172 &parameters);
171 base::CommandLine::Init(0, nullptr); 173 base::CommandLine::Init(0, nullptr);
172 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters); 174 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
175 command_line->InitFromArgv(parameters);
176 Tracer* tracer = new Tracer;
177 g_tracer.Get().reset(tracer);
178 bool trace_startup = command_line->HasSwitch(switches::kTraceStartup);
179 if (trace_startup)
180 tracer->Start(command_line->GetSwitchValueASCII(switches::kTraceStartup));
181
173 g_shell_runner.Get().reset(new MojoShellRunner( 182 g_shell_runner.Get().reset(new MojoShellRunner(
174 base::FilePath( 183 base::FilePath(
175 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)), 184 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)),
176 base::FilePath( 185 base::FilePath(
177 base::android::ConvertJavaStringToUTF8(env, mojo_shell_child_path)), 186 base::android::ConvertJavaStringToUTF8(env, mojo_shell_child_path)),
178 parameters)); 187 parameters));
179 188
180 InitializeLogging(); 189 InitializeLogging();
181 190
182 InitializeRedirection(); 191 InitializeRedirection();
183 192
184 // We want ~MessageLoop to happen prior to ~Context. Initializing 193 // We want ~MessageLoop to happen prior to ~Context. Initializing
185 // LazyInstances is akin to stack-allocating objects; their destructors 194 // LazyInstances is akin to stack-allocating objects; their destructors
186 // will be invoked first-in-last-out. 195 // will be invoked first-in-last-out.
187 Context* shell_context = new Context(); 196 Context* shell_context = new Context();
188 shell_context->SetShellFileRoot(base::FilePath( 197 shell_context->SetShellFileRoot(base::FilePath(
189 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); 198 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory)));
190 g_context.Get().reset(shell_context); 199 g_context.Get().reset(shell_context);
191 200
192 g_java_message_loop.Get().reset(new base::MessageLoopForUI); 201 g_java_message_loop.Get().reset(new base::MessageLoopForUI);
193 base::MessageLoopForUI::current()->Start(); 202 base::MessageLoopForUI::current()->Start();
194 203
204 if (trace_startup) {
205 g_java_message_loop.Get()->PostDelayedTask(
206 FROM_HERE,
207 base::Bind(&Tracer::StopAndFlushToFile, base::Unretained(tracer),
208 tmp_dir + "/mojo_shell.trace"),
209 base::TimeDelta::FromSeconds(5));
210 }
211
195 // TODO(abarth): At which point should we switch to cross-platform 212 // TODO(abarth): At which point should we switch to cross-platform
196 // initialization? 213 // initialization?
197 214
198 gfx::GLSurface::InitializeOneOff(); 215 gfx::GLSurface::InitializeOneOff();
199 } 216 }
200 217
201 static jboolean Start(JNIEnv* env, jclass clazz) { 218 static jboolean Start(JNIEnv* env, jclass clazz) {
202 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size()) 219 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size())
203 return false; 220 return false;
204 221
(...skipping 18 matching lines...) Expand all
223 return RegisterNativesImpl(env); 240 return RegisterNativesImpl(env);
224 } 241 }
225 242
226 } // namespace shell 243 } // namespace shell
227 244
228 // TODO(vtl): We need a main(), even though it should never be called. 245 // TODO(vtl): We need a main(), even though it should never be called.
229 int main(int argc, char** argv) { 246 int main(int argc, char** argv) {
230 NOTREACHED(); 247 NOTREACHED();
231 return 1; 248 return 1;
232 } 249 }
OLDNEW
« no previous file with comments | « shell/BUILD.gn ('k') | shell/desktop/main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698