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

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

Issue 1061413002: Shell: Make a separate binary for child processes. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased 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
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 <stdio.h> 5 #include <stdio.h>
6 #include <string.h> 6 #include <string.h>
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <iostream> 9 #include <iostream>
10 10
11 #include "base/at_exit.h" 11 #include "base/at_exit.h"
12 #include "base/base_switches.h" 12 #include "base/base_switches.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/debug/profiler.h" 15 #include "base/debug/profiler.h"
16 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
17 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/synchronization/waitable_event.h" 18 #include "base/synchronization/waitable_event.h"
19 #include "base/trace_event/trace_event.h" 19 #include "base/trace_event/trace_event.h"
20 #include "shell/child_main.h"
21 #include "shell/command_line_util.h" 20 #include "shell/command_line_util.h"
22 #include "shell/context.h" 21 #include "shell/context.h"
23 #include "shell/init.h" 22 #include "shell/init.h"
24 #include "shell/switches.h" 23 #include "shell/switches.h"
25 24
26 namespace { 25 namespace {
27 26
28 void Usage() { 27 void Usage() {
29 std::cerr << "Launch Mojo applications.\n"; 28 std::cerr << "Launch Mojo applications.\n";
30 std::cerr 29 std::cerr
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 } // namespace 105 } // namespace
107 106
108 int main(int argc, char** argv) { 107 int main(int argc, char** argv) {
109 base::AtExitManager at_exit; 108 base::AtExitManager at_exit;
110 base::CommandLine::Init(argc, argv); 109 base::CommandLine::Init(argc, argv);
111 110
112 mojo::shell::InitializeLogging(); 111 mojo::shell::InitializeLogging();
113 112
114 const base::CommandLine& command_line = 113 const base::CommandLine& command_line =
115 *base::CommandLine::ForCurrentProcess(); 114 *base::CommandLine::ForCurrentProcess();
115 CHECK(!command_line.HasSwitch(switches::kChildProcess));
DaveMoore 2015/04/07 22:27:30 Seems like we can drop this too. If you're running
116 116
117 // TODO(vtl): Unify parent and child process cases to the extent possible. 117 // Only check the command line for the main process.
118 int exit_code = 0; 118 const std::set<std::string> all_switches = switches::GetAllSwitches();
119 if (command_line.HasSwitch(switches::kChildProcess)) { 119 const base::CommandLine::SwitchMap switches = command_line.GetSwitches();
120 exit_code = mojo::shell::ChildMain(); 120 bool found_unknown_switch = false;
121 } else { 121 for (const auto& s : switches) {
122 // Only check the command line for the main process. 122 if (all_switches.find(s.first) == all_switches.end()) {
123 const std::set<std::string> all_switches = switches::GetAllSwitches(); 123 std::cerr << "unknown switch: " << s.first << std::endl;
124 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); 124 found_unknown_switch = true;
125 bool found_unknown_switch = false;
126 for (const auto& s : switches) {
127 if (all_switches.find(s.first) == all_switches.end()) {
128 std::cerr << "unknown switch: " << s.first << std::endl;
129 found_unknown_switch = true;
130 }
131 } 125 }
126 }
132 127
133 if (found_unknown_switch || 128 if (found_unknown_switch ||
134 (!command_line.HasSwitch(switches::kEnableExternalApplications) && 129 (!command_line.HasSwitch(switches::kEnableExternalApplications) &&
135 (command_line.HasSwitch(switches::kHelp) || 130 (command_line.HasSwitch(switches::kHelp) ||
136 command_line.GetArgs().empty()))) { 131 command_line.GetArgs().empty()))) {
132 Usage();
133 return 0;
134 }
135
136 if (command_line.HasSwitch(switches::kTraceStartup)) {
137 g_tracing = true;
138 base::trace_event::CategoryFilter category_filter(
139 command_line.GetSwitchValueASCII(switches::kTraceStartup));
140 base::trace_event::TraceLog::GetInstance()->SetEnabled(
141 category_filter, base::trace_event::TraceLog::RECORDING_MODE,
142 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL));
143 }
144
145 if (command_line.HasSwitch(switches::kCPUProfile)) {
146 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING)
147 LOG(ERROR) << "Profiling requires is_debug=false and "
148 << "enable_profiling=true. Continuing without profiling.";
149 // StartProfiling() and StopProfiling() are a no-op.
150 #endif
151 base::debug::StartProfiling("mojo_shell.pprof");
152 }
153
154 // We want the shell::Context to outlive the MessageLoop so that pipes are all
155 // gracefully closed / error-out before we try to shut the Context down.
156 mojo::shell::Context shell_context;
157 {
158 base::MessageLoop message_loop;
159 if (!shell_context.Init()) {
137 Usage(); 160 Usage();
138 return 0; 161 return 0;
139 } 162 }
140 163 if (g_tracing) {
141 if (command_line.HasSwitch(switches::kTraceStartup)) { 164 message_loop.PostDelayedTask(FROM_HERE,
142 g_tracing = true; 165 base::Bind(StopTracingAndFlushToDisk),
143 base::trace_event::CategoryFilter category_filter( 166 base::TimeDelta::FromSeconds(5));
144 command_line.GetSwitchValueASCII(switches::kTraceStartup));
145 base::trace_event::TraceLog::GetInstance()->SetEnabled(
146 category_filter, base::trace_event::TraceLog::RECORDING_MODE,
147 base::trace_event::TraceOptions(
148 base::trace_event::RECORD_UNTIL_FULL));
149 } 167 }
150 168
151 if (command_line.HasSwitch(switches::kCPUProfile)) { 169 // The mojo_shell --args-for command-line switch is handled specially
152 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) 170 // because it can appear more than once. The base::CommandLine class
153 LOG(ERROR) << "Profiling requires is_debug=false and " 171 // collapses multiple occurrences of the same switch.
154 << "enable_profiling=true. Continuing without profiling."; 172 for (int i = 1; i < argc; i++)
155 // StartProfiling() and StopProfiling() are a no-op. 173 ApplyApplicationArgs(&shell_context, argv[i]);
156 #endif
157 base::debug::StartProfiling("mojo_shell.pprof");
158 }
159 174
160 // We want the shell::Context to outlive the MessageLoop so that pipes are 175 message_loop.PostTask(
161 // all gracefully closed / error-out before we try to shut the Context down. 176 FROM_HERE,
162 mojo::shell::Context shell_context; 177 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context));
163 { 178 message_loop.Run();
164 base::MessageLoop message_loop;
165 if (!shell_context.Init()) {
166 Usage();
167 return 0;
168 }
169 if (g_tracing) {
170 message_loop.PostDelayedTask(FROM_HERE,
171 base::Bind(StopTracingAndFlushToDisk),
172 base::TimeDelta::FromSeconds(5));
173 }
174 179
175 // The mojo_shell --args-for command-line switch is handled specially 180 // Must be called before |message_loop| is destroyed.
176 // because it can appear more than once. The base::CommandLine class 181 shell_context.Shutdown();
177 // collapses multiple occurrences of the same switch.
178 for (int i = 1; i < argc; i++) {
179 ApplyApplicationArgs(&shell_context, argv[i]);
180 }
181
182 message_loop.PostTask(
183 FROM_HERE,
184 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context));
185 message_loop.Run();
186
187 // Must be called before |message_loop| is destroyed.
188 shell_context.Shutdown();
189 }
190
191 if (command_line.HasSwitch(switches::kCPUProfile)) {
192 base::debug::StopProfiling();
193 }
194 } 182 }
195 183
184 if (command_line.HasSwitch(switches::kCPUProfile))
185 base::debug::StopProfiling();
196 if (g_tracing) 186 if (g_tracing)
197 StopTracingAndFlushToDisk(); 187 StopTracingAndFlushToDisk();
198 return exit_code; 188 return 0;
199 } 189 }
OLDNEW
« shell/child_main.cc ('K') | « shell/context.cc ('k') | shell/shell_test_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698