1 // Copyright 2015 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.
11 #include "base/base_switches.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/files/file_util.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/trace_event/trace_event.h"
18 #include "mandoline/app/desktop/launcher_process.h"
19 #include "mojo/runner/context.h"
20 #include "mojo/runner/switches.h"
25 // Whether we're currently tracing.
26 bool g_tracing
= false;
28 // Number of tracing blocks written.
29 uint32_t g_blocks
= 0;
31 // Trace file, if open.
32 FILE* g_trace_file
= nullptr;
34 void WriteTraceDataCollected(
35 base::WaitableEvent
* event
,
36 const scoped_refptr
<base::RefCountedString
>& events_str
,
37 bool has_more_events
) {
39 fwrite(",", 1, 1, g_trace_file
);
43 fwrite(events_str
->data().c_str(), 1, events_str
->data().length(),
45 if (!has_more_events
) {
46 static const char kEnd
[] = "]}";
47 fwrite(kEnd
, 1, strlen(kEnd
), g_trace_file
);
48 PCHECK(fclose(g_trace_file
) == 0);
49 g_trace_file
= nullptr;
54 void EndTraceAndFlush(base::WaitableEvent
* event
) {
55 g_trace_file
= fopen("mojo_shell.trace", "w+");
57 static const char kStart
[] = "{\"traceEvents\":[";
58 fwrite(kStart
, 1, strlen(kStart
), g_trace_file
);
59 base::trace_event::TraceLog::GetInstance()->SetDisabled();
60 base::trace_event::TraceLog::GetInstance()->Flush(
61 base::Bind(&WriteTraceDataCollected
, base::Unretained(event
)));
64 void StopTracingAndFlushToDisk() {
66 base::trace_event::TraceLog::GetInstance()->SetDisabled();
67 base::WaitableEvent
flush_complete_event(false, false);
68 // TraceLog::Flush requires a message loop but we've already shut ours down.
69 // Spin up a new thread to flush things out.
70 base::Thread
flush_thread("mojo_shell_trace_event_flush");
72 flush_thread
.message_loop()->PostTask(
74 base::Bind(EndTraceAndFlush
, base::Unretained(&flush_complete_event
)));
75 flush_complete_event
.Wait();
78 void StartApp(mojo::runner::Context
* context
) {
79 // If a mojo app isn't specified (i.e. for an apptest), run the mojo shell's
81 GURL
app_url(GURL("mojo:window_manager"));
82 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
83 base::CommandLine::StringVector args
= command_line
->GetArgs();
84 for (size_t i
= 0; i
< args
.size(); ++i
) {
85 GURL
possible_app(args
[i
]);
86 if (possible_app
.SchemeIs("mojo")) {
87 app_url
= possible_app
;
92 context
->Run(app_url
, service_provider
.Pass());
97 int LauncherProcessMain(int argc
, char** argv
) {
98 const base::CommandLine
& command_line
=
99 *base::CommandLine::ForCurrentProcess();
100 if (command_line
.HasSwitch(switches::kTraceStartup
)) {
102 base::trace_event::CategoryFilter
category_filter(
103 command_line
.GetSwitchValueASCII(switches::kTraceStartup
));
104 base::trace_event::TraceLog::GetInstance()->SetEnabled(
105 category_filter
, base::trace_event::TraceLog::RECORDING_MODE
,
106 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL
));
109 // We want the runner::Context to outlive the MessageLoop so that pipes are
110 // all gracefully closed / error-out before we try to shut the Context down.
111 mojo::runner::Context shell_context
;
113 base::MessageLoop message_loop
;
114 if (!shell_context
.Init()) {
118 message_loop
.PostDelayedTask(FROM_HERE
,
119 base::Bind(StopTracingAndFlushToDisk
),
120 base::TimeDelta::FromSeconds(5));
123 message_loop
.PostTask(FROM_HERE
, base::Bind(&StartApp
, &shell_context
));
126 // Must be called before |message_loop| is destroyed.
127 shell_context
.Shutdown();
131 StopTracingAndFlushToDisk();
135 } // namespace mandoline