Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / mandoline / app / desktop / launcher_process.cc
blob132d0333a2873dc2d1edb16d373942c9f698037b
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.
5 #include <stdio.h>
6 #include <string.h>
8 #include <algorithm>
9 #include <iostream>
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 "components/tracing/trace_config_file.h"
19 #include "components/tracing/tracing_switches.h"
20 #include "mandoline/app/core_services_initialization.h"
21 #include "mandoline/app/desktop/launcher_process.h"
22 #include "mojo/runner/context.h"
23 #include "mojo/runner/switches.h"
25 namespace mandoline {
26 namespace {
28 // Whether we're currently tracing.
29 bool g_tracing = false;
31 // Number of tracing blocks written.
32 uint32_t g_blocks = 0;
34 // Trace file, if open.
35 FILE* g_trace_file = nullptr;
37 void WriteTraceDataCollected(
38 base::WaitableEvent* event,
39 const scoped_refptr<base::RefCountedString>& events_str,
40 bool has_more_events) {
41 if (g_blocks) {
42 fwrite(",", 1, 1, g_trace_file);
45 ++g_blocks;
46 fwrite(events_str->data().c_str(), 1, events_str->data().length(),
47 g_trace_file);
48 if (!has_more_events) {
49 static const char kEnd[] = "]}";
50 fwrite(kEnd, 1, strlen(kEnd), g_trace_file);
51 PCHECK(fclose(g_trace_file) == 0);
52 g_trace_file = nullptr;
53 event->Signal();
57 void EndTraceAndFlush(base::WaitableEvent* event) {
58 g_trace_file = fopen("mojo_shell.trace", "w+");
59 PCHECK(g_trace_file);
60 static const char kStart[] = "{\"traceEvents\":[";
61 fwrite(kStart, 1, strlen(kStart), g_trace_file);
62 base::trace_event::TraceLog::GetInstance()->SetDisabled();
63 base::trace_event::TraceLog::GetInstance()->Flush(
64 base::Bind(&WriteTraceDataCollected, base::Unretained(event)));
67 void StopTracingAndFlushToDisk() {
68 g_tracing = false;
69 base::trace_event::TraceLog::GetInstance()->SetDisabled();
70 base::WaitableEvent flush_complete_event(false, false);
71 // TraceLog::Flush requires a message loop but we've already shut ours down.
72 // Spin up a new thread to flush things out.
73 base::Thread flush_thread("mojo_shell_trace_event_flush");
74 flush_thread.Start();
75 flush_thread.message_loop()->PostTask(
76 FROM_HERE,
77 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event)));
78 flush_complete_event.Wait();
81 } // namespace
83 int LauncherProcessMain(int argc, char** argv) {
84 const base::CommandLine& command_line =
85 *base::CommandLine::ForCurrentProcess();
86 if (command_line.HasSwitch(switches::kTraceStartup)) {
87 g_tracing = true;
88 base::trace_event::TraceConfig trace_config(
89 command_line.GetSwitchValueASCII(switches::kTraceStartup),
90 base::trace_event::RECORD_UNTIL_FULL);
91 base::trace_event::TraceLog::GetInstance()->SetEnabled(
92 trace_config, base::trace_event::TraceLog::RECORDING_MODE);
93 } else if (tracing::TraceConfigFile::GetInstance()->IsEnabled()) {
94 g_tracing = true;
95 base::trace_event::TraceLog::GetInstance()->SetEnabled(
96 tracing::TraceConfigFile::GetInstance()->GetTraceConfig(),
97 base::trace_event::TraceLog::RECORDING_MODE);
100 // We want the runner::Context to outlive the MessageLoop so that pipes are
101 // all gracefully closed / error-out before we try to shut the Context down.
102 mojo::runner::Context shell_context;
103 InitCoreServicesForContext(&shell_context);
105 base::MessageLoop message_loop;
106 if (!shell_context.Init()) {
107 return 0;
109 if (g_tracing) {
110 message_loop.PostDelayedTask(FROM_HERE,
111 base::Bind(StopTracingAndFlushToDisk),
112 base::TimeDelta::FromSeconds(5));
115 message_loop.PostTask(FROM_HERE,
116 base::Bind(&mojo::runner::Context::Run,
117 base::Unretained(&shell_context),
118 GURL("mojo:desktop_ui")));
119 message_loop.Run();
121 // Must be called before |message_loop| is destroyed.
122 shell_context.Shutdown();
125 if (g_tracing)
126 StopTracingAndFlushToDisk();
127 return 0;
130 } // namespace mandoline