OLD | NEW |
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/base_switches.h" | 11 #include "base/base_switches.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
18 #include "mojo/shell/command_line_util.h" | |
19 #include "mojo/shell/context.h" | 18 #include "mojo/shell/context.h" |
20 #include "mojo/shell/switches.h" | 19 #include "mojo/shell/switches.h" |
21 | 20 |
22 namespace mojo { | 21 namespace mojo { |
23 namespace shell { | 22 namespace shell { |
24 namespace { | 23 namespace { |
25 | 24 |
26 void Usage() { | |
27 std::cerr << "Launch Mojo applications.\n"; | |
28 std::cerr | |
29 << "Usage: mojo_shell" | |
30 << " [--" << switches::kArgsFor << "=<mojo-app>]" | |
31 << " [--" << switches::kContentHandlers << "=<handlers>]" | |
32 << " [--" << switches::kDisableCache << "]" | |
33 << " [--" << switches::kEnableMultiprocess << "]" | |
34 << " [--" << switches::kOrigin << "=<url-lib-path>]" | |
35 << " [--" << switches::kTraceStartup << "]" | |
36 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" | |
37 << " [--" << switches::kPredictableAppFilenames << "]" | |
38 << " [--" << switches::kWaitForDebugger << "]" | |
39 << " <mojo-app> ...\n\n" | |
40 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " | |
41 << "quotes.\n" | |
42 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n" | |
43 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" | |
44 << "The value of <handlers> is a comma separated list like:\n" | |
45 << "text/html,mojo:html_viewer," | |
46 << "application/javascript,mojo:js_content_handler\n"; | |
47 } | |
48 | |
49 // Whether we're currently tracing. | 25 // Whether we're currently tracing. |
50 bool g_tracing = false; | 26 bool g_tracing = false; |
51 | 27 |
52 // Number of tracing blocks written. | 28 // Number of tracing blocks written. |
53 uint32_t g_blocks = 0; | 29 uint32_t g_blocks = 0; |
54 | 30 |
55 // Trace file, if open. | 31 // Trace file, if open. |
56 FILE* g_trace_file = nullptr; | 32 FILE* g_trace_file = nullptr; |
57 | 33 |
58 void WriteTraceDataCollected( | 34 void WriteTraceDataCollected( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 // TraceLog::Flush requires a message loop but we've already shut ours down. | 68 // TraceLog::Flush requires a message loop but we've already shut ours down. |
93 // Spin up a new thread to flush things out. | 69 // Spin up a new thread to flush things out. |
94 base::Thread flush_thread("mojo_shell_trace_event_flush"); | 70 base::Thread flush_thread("mojo_shell_trace_event_flush"); |
95 flush_thread.Start(); | 71 flush_thread.Start(); |
96 flush_thread.message_loop()->PostTask( | 72 flush_thread.message_loop()->PostTask( |
97 FROM_HERE, | 73 FROM_HERE, |
98 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event))); | 74 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event))); |
99 flush_complete_event.Wait(); | 75 flush_complete_event.Wait(); |
100 } | 76 } |
101 | 77 |
| 78 void StartApp(mojo::shell::Context* context) { |
| 79 // If a mojo app isn't specified (i.e. for an apptest), run the mojo shell's |
| 80 // window manager. |
| 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; |
| 88 break; |
| 89 } |
| 90 } |
| 91 |
| 92 context->Run(app_url); |
| 93 } |
| 94 |
102 } // namespace | 95 } // namespace |
103 | 96 |
104 int LauncherProcessMain(int argc, char** argv) { | 97 int LauncherProcessMain(int argc, char** argv) { |
105 const base::CommandLine& command_line = | 98 const base::CommandLine& command_line = |
106 *base::CommandLine::ForCurrentProcess(); | 99 *base::CommandLine::ForCurrentProcess(); |
107 | |
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 } | |
117 | |
118 if (found_unknown_switch || command_line.HasSwitch(switches::kHelp) || | |
119 command_line.GetArgs().empty()) { | |
120 Usage(); | |
121 return 0; | |
122 } | |
123 | |
124 if (command_line.HasSwitch(switches::kTraceStartup)) { | 100 if (command_line.HasSwitch(switches::kTraceStartup)) { |
125 g_tracing = true; | 101 g_tracing = true; |
126 base::trace_event::CategoryFilter category_filter( | 102 base::trace_event::CategoryFilter category_filter( |
127 command_line.GetSwitchValueASCII(switches::kTraceStartup)); | 103 command_line.GetSwitchValueASCII(switches::kTraceStartup)); |
128 base::trace_event::TraceLog::GetInstance()->SetEnabled( | 104 base::trace_event::TraceLog::GetInstance()->SetEnabled( |
129 category_filter, base::trace_event::TraceLog::RECORDING_MODE, | 105 category_filter, base::trace_event::TraceLog::RECORDING_MODE, |
130 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); | 106 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); |
131 } | 107 } |
132 | 108 |
133 // We want the shell::Context to outlive the MessageLoop so that pipes are | 109 // We want the shell::Context to outlive the MessageLoop so that pipes are |
134 // all gracefully closed / error-out before we try to shut the Context down. | 110 // all gracefully closed / error-out before we try to shut the Context down. |
135 mojo::shell::Context shell_context; | 111 mojo::shell::Context shell_context; |
136 { | 112 { |
137 base::MessageLoop message_loop; | 113 base::MessageLoop message_loop; |
138 if (!shell_context.Init()) { | 114 if (!shell_context.Init()) { |
139 Usage(); | |
140 return 0; | 115 return 0; |
141 } | 116 } |
142 if (g_tracing) { | 117 if (g_tracing) { |
143 message_loop.PostDelayedTask(FROM_HERE, | 118 message_loop.PostDelayedTask(FROM_HERE, |
144 base::Bind(StopTracingAndFlushToDisk), | 119 base::Bind(StopTracingAndFlushToDisk), |
145 base::TimeDelta::FromSeconds(5)); | 120 base::TimeDelta::FromSeconds(5)); |
146 } | 121 } |
147 | 122 |
148 // The mojo_shell --args-for command-line switch is handled specially | 123 message_loop.PostTask(FROM_HERE, base::Bind(&StartApp, &shell_context)); |
149 // because it can appear more than once. The base::CommandLine class | |
150 // collapses multiple occurrences of the same switch. | |
151 for (int i = 1; i < argc; i++) { | |
152 ApplyApplicationArgs(&shell_context, argv[i]); | |
153 } | |
154 | |
155 message_loop.PostTask( | |
156 FROM_HERE, | |
157 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context)); | |
158 message_loop.Run(); | 124 message_loop.Run(); |
159 | 125 |
160 // Must be called before |message_loop| is destroyed. | 126 // Must be called before |message_loop| is destroyed. |
161 shell_context.Shutdown(); | 127 shell_context.Shutdown(); |
162 } | 128 } |
163 | 129 |
164 if (g_tracing) | 130 if (g_tracing) |
165 StopTracingAndFlushToDisk(); | 131 StopTracingAndFlushToDisk(); |
166 return 0; | 132 return 0; |
167 } | 133 } |
168 | 134 |
169 } // namespace shell | 135 } // namespace shell |
170 } // namespace mojo | 136 } // namespace mojo |
OLD | NEW |