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

Side by Side Diff: mojo/shell/desktop/launcher_process.cc

Issue 1065433002: Simplify some mojo_shell code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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"
12 #include "base/base_switches.h" 11 #include "base/base_switches.h"
13 #include "base/bind.h" 12 #include "base/bind.h"
14 #include "base/command_line.h" 13 #include "base/command_line.h"
15 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
16 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
17 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
18 #include "base/trace_event/trace_event.h" 17 #include "base/trace_event/trace_event.h"
19 #include "mojo/shell/child_process.h"
20 #include "mojo/shell/command_line_util.h" 18 #include "mojo/shell/command_line_util.h"
21 #include "mojo/shell/context.h" 19 #include "mojo/shell/context.h"
22 #include "mojo/shell/init.h"
23 #include "mojo/shell/switches.h" 20 #include "mojo/shell/switches.h"
24 21
22 namespace mojo {
23 namespace shell {
25 namespace { 24 namespace {
26 25
27 void Usage() { 26 void Usage() {
28 std::cerr << "Launch Mojo applications.\n"; 27 std::cerr << "Launch Mojo applications.\n";
29 std::cerr 28 std::cerr
30 << "Usage: mojo_shell" 29 << "Usage: mojo_shell"
31 << " [--" << switches::kArgsFor << "=<mojo-app>]" 30 << " [--" << switches::kArgsFor << "=<mojo-app>]"
32 << " [--" << switches::kContentHandlers << "=<handlers>]" 31 << " [--" << switches::kContentHandlers << "=<handlers>]"
33 << " [--" << switches::kEnableExternalApplications << "]"
34 << " [--" << switches::kDisableCache << "]" 32 << " [--" << switches::kDisableCache << "]"
35 << " [--" << switches::kEnableMultiprocess << "]" 33 << " [--" << switches::kEnableMultiprocess << "]"
36 << " [--" << switches::kOrigin << "=<url-lib-path>]" 34 << " [--" << switches::kOrigin << "=<url-lib-path>]"
37 << " [--" << switches::kTraceStartup << "]" 35 << " [--" << switches::kTraceStartup << "]"
38 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" 36 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]"
39 << " [--" << switches::kPredictableAppFilenames << "]" 37 << " [--" << switches::kPredictableAppFilenames << "]"
40 << " [--" << switches::kWaitForDebugger << "]" 38 << " [--" << switches::kWaitForDebugger << "]"
41 << " <mojo-app> ...\n\n" 39 << " <mojo-app> ...\n\n"
42 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " 40 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within "
43 << "quotes.\n" 41 << "quotes.\n"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 base::Thread flush_thread("mojo_shell_trace_event_flush"); 94 base::Thread flush_thread("mojo_shell_trace_event_flush");
97 flush_thread.Start(); 95 flush_thread.Start();
98 flush_thread.message_loop()->PostTask( 96 flush_thread.message_loop()->PostTask(
99 FROM_HERE, 97 FROM_HERE,
100 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event))); 98 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event)));
101 flush_complete_event.Wait(); 99 flush_complete_event.Wait();
102 } 100 }
103 101
104 } // namespace 102 } // namespace
105 103
106 int main(int argc, char** argv) { 104 int LauncherProcessMain(int argc, char** argv) {
107 base::AtExitManager at_exit; 105 const base::CommandLine& command_line =
108 base::CommandLine::Init(argc, argv); 106 *base::CommandLine::ForCurrentProcess();
109 107
110 mojo::shell::InitializeLogging(); 108 const std::set<std::string> all_switches = switches::GetAllSwitches();
109 const base::CommandLine::SwitchMap switches = command_line.GetSwitches();
110 bool found_unknown_switch = false;
111 for (const auto& s : switches) {
112 if (all_switches.find(s.first) == all_switches.end()) {
113 std::cerr << "unknown switch: " << s.first << std::endl;
114 found_unknown_switch = true;
115 }
116 }
111 117
112 // TODO(vtl): Unify parent and child process cases to the extent possible. 118 if (found_unknown_switch || command_line.HasSwitch(switches::kHelp) ||
113 if (scoped_ptr<mojo::shell::ChildProcess> child_process = 119 command_line.GetArgs().empty()) {
114 mojo::shell::ChildProcess::Create( 120 Usage();
115 *base::CommandLine::ForCurrentProcess())) { 121 return 0;
116 child_process->Main(); 122 }
117 } else {
118 // Only check the command line for the main process.
119 const base::CommandLine& command_line =
120 *base::CommandLine::ForCurrentProcess();
121 123
122 const std::set<std::string> all_switches = switches::GetAllSwitches(); 124 if (command_line.HasSwitch(switches::kTraceStartup)) {
123 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); 125 g_tracing = true;
124 bool found_unknown_switch = false; 126 base::trace_event::CategoryFilter category_filter(
125 for (const auto& s : switches) { 127 command_line.GetSwitchValueASCII(switches::kTraceStartup));
126 if (all_switches.find(s.first) == all_switches.end()) { 128 base::trace_event::TraceLog::GetInstance()->SetEnabled(
127 std::cerr << "unknown switch: " << s.first << std::endl; 129 category_filter, base::trace_event::TraceLog::RECORDING_MODE,
128 found_unknown_switch = true; 130 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL));
129 } 131 }
130 }
131 132
132 if (found_unknown_switch || 133 // We want the shell::Context to outlive the MessageLoop so that pipes are
133 (!command_line.HasSwitch(switches::kEnableExternalApplications) && 134 // all gracefully closed / error-out before we try to shut the Context down.
134 (command_line.HasSwitch(switches::kHelp) || 135 mojo::shell::Context shell_context;
135 command_line.GetArgs().empty()))) { 136 {
137 base::MessageLoop message_loop;
138 if (!shell_context.Init()) {
136 Usage(); 139 Usage();
137 return 0; 140 return 0;
138 } 141 }
139 142 if (g_tracing) {
140 if (command_line.HasSwitch(switches::kTraceStartup)) { 143 message_loop.PostDelayedTask(FROM_HERE,
141 g_tracing = true; 144 base::Bind(StopTracingAndFlushToDisk),
142 base::trace_event::CategoryFilter category_filter( 145 base::TimeDelta::FromSeconds(5));
143 command_line.GetSwitchValueASCII(switches::kTraceStartup));
144 base::trace_event::TraceLog::GetInstance()->SetEnabled(
145 category_filter, base::trace_event::TraceLog::RECORDING_MODE,
146 base::trace_event::TraceOptions(
147 base::trace_event::RECORD_UNTIL_FULL));
148 } 146 }
149 147
150 // We want the shell::Context to outlive the MessageLoop so that pipes are 148 // The mojo_shell --args-for command-line switch is handled specially
151 // all gracefully closed / error-out before we try to shut the Context down. 149 // because it can appear more than once. The base::CommandLine class
152 mojo::shell::Context shell_context; 150 // collapses multiple occurrences of the same switch.
153 { 151 for (int i = 1; i < argc; i++) {
154 base::MessageLoop message_loop; 152 ApplyApplicationArgs(&shell_context, argv[i]);
155 if (!shell_context.Init()) { 153 }
156 Usage();
157 return 0;
158 }
159 if (g_tracing) {
160 message_loop.PostDelayedTask(FROM_HERE,
161 base::Bind(StopTracingAndFlushToDisk),
162 base::TimeDelta::FromSeconds(5));
163 }
164 154
165 // The mojo_shell --args-for command-line switch is handled specially 155 message_loop.PostTask(
166 // because it can appear more than once. The base::CommandLine class 156 FROM_HERE,
167 // collapses multiple occurrences of the same switch. 157 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context));
168 for (int i = 1; i < argc; i++) { 158 message_loop.Run();
169 ApplyApplicationArgs(&shell_context, argv[i]);
170 }
171 159
172 message_loop.PostTask( 160 // Must be called before |message_loop| is destroyed.
173 FROM_HERE, 161 shell_context.Shutdown();
174 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context));
175 message_loop.Run();
176
177 // Must be called before |message_loop| is destroyed.
178 shell_context.Shutdown();
179 }
180 } 162 }
181 163
182 if (g_tracing) 164 if (g_tracing)
183 StopTracingAndFlushToDisk(); 165 StopTracingAndFlushToDisk();
184 return 0; 166 return 0;
185 } 167 }
168
169 } // namespace shell
170 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698