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" |
18 #include "mojo/shell/context.h" | 19 #include "mojo/shell/context.h" |
19 #include "mojo/shell/switches.h" | 20 #include "mojo/shell/switches.h" |
20 | 21 |
21 namespace mojo { | 22 namespace mojo { |
22 namespace shell { | 23 namespace shell { |
23 namespace { | 24 namespace { |
24 | 25 |
| 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 |
25 // Whether we're currently tracing. | 49 // Whether we're currently tracing. |
26 bool g_tracing = false; | 50 bool g_tracing = false; |
27 | 51 |
28 // Number of tracing blocks written. | 52 // Number of tracing blocks written. |
29 uint32_t g_blocks = 0; | 53 uint32_t g_blocks = 0; |
30 | 54 |
31 // Trace file, if open. | 55 // Trace file, if open. |
32 FILE* g_trace_file = nullptr; | 56 FILE* g_trace_file = nullptr; |
33 | 57 |
34 void WriteTraceDataCollected( | 58 void WriteTraceDataCollected( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // TraceLog::Flush requires a message loop but we've already shut ours down. | 92 // TraceLog::Flush requires a message loop but we've already shut ours down. |
69 // Spin up a new thread to flush things out. | 93 // Spin up a new thread to flush things out. |
70 base::Thread flush_thread("mojo_shell_trace_event_flush"); | 94 base::Thread flush_thread("mojo_shell_trace_event_flush"); |
71 flush_thread.Start(); | 95 flush_thread.Start(); |
72 flush_thread.message_loop()->PostTask( | 96 flush_thread.message_loop()->PostTask( |
73 FROM_HERE, | 97 FROM_HERE, |
74 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event))); | 98 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event))); |
75 flush_complete_event.Wait(); | 99 flush_complete_event.Wait(); |
76 } | 100 } |
77 | 101 |
78 void StartWindowManager(mojo::shell::Context* context) { | |
79 context->Run(GURL("mojo:window_manager")); | |
80 } | |
81 | |
82 } // namespace | 102 } // namespace |
83 | 103 |
84 int LauncherProcessMain(int argc, char** argv) { | 104 int LauncherProcessMain(int argc, char** argv) { |
85 const base::CommandLine& command_line = | 105 const base::CommandLine& command_line = |
86 *base::CommandLine::ForCurrentProcess(); | 106 *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 |
87 if (command_line.HasSwitch(switches::kTraceStartup)) { | 124 if (command_line.HasSwitch(switches::kTraceStartup)) { |
88 g_tracing = true; | 125 g_tracing = true; |
89 base::trace_event::CategoryFilter category_filter( | 126 base::trace_event::CategoryFilter category_filter( |
90 command_line.GetSwitchValueASCII(switches::kTraceStartup)); | 127 command_line.GetSwitchValueASCII(switches::kTraceStartup)); |
91 base::trace_event::TraceLog::GetInstance()->SetEnabled( | 128 base::trace_event::TraceLog::GetInstance()->SetEnabled( |
92 category_filter, base::trace_event::TraceLog::RECORDING_MODE, | 129 category_filter, base::trace_event::TraceLog::RECORDING_MODE, |
93 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); | 130 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); |
94 } | 131 } |
95 | 132 |
96 // We want the shell::Context to outlive the MessageLoop so that pipes are | 133 // We want the shell::Context to outlive the MessageLoop so that pipes are |
97 // all gracefully closed / error-out before we try to shut the Context down. | 134 // all gracefully closed / error-out before we try to shut the Context down. |
98 mojo::shell::Context shell_context; | 135 mojo::shell::Context shell_context; |
99 { | 136 { |
100 base::MessageLoop message_loop; | 137 base::MessageLoop message_loop; |
101 if (!shell_context.Init()) { | 138 if (!shell_context.Init()) { |
| 139 Usage(); |
102 return 0; | 140 return 0; |
103 } | 141 } |
104 if (g_tracing) { | 142 if (g_tracing) { |
105 message_loop.PostDelayedTask(FROM_HERE, | 143 message_loop.PostDelayedTask(FROM_HERE, |
106 base::Bind(StopTracingAndFlushToDisk), | 144 base::Bind(StopTracingAndFlushToDisk), |
107 base::TimeDelta::FromSeconds(5)); | 145 base::TimeDelta::FromSeconds(5)); |
108 } | 146 } |
109 | 147 |
110 message_loop.PostTask(FROM_HERE, | 148 // The mojo_shell --args-for command-line switch is handled specially |
111 base::Bind(&StartWindowManager, &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)); |
112 message_loop.Run(); | 158 message_loop.Run(); |
113 | 159 |
114 // Must be called before |message_loop| is destroyed. | 160 // Must be called before |message_loop| is destroyed. |
115 shell_context.Shutdown(); | 161 shell_context.Shutdown(); |
116 } | 162 } |
117 | 163 |
118 if (g_tracing) | 164 if (g_tracing) |
119 StopTracingAndFlushToDisk(); | 165 StopTracingAndFlushToDisk(); |
120 return 0; | 166 return 0; |
121 } | 167 } |
122 | 168 |
123 } // namespace shell | 169 } // namespace shell |
124 } // namespace mojo | 170 } // namespace mojo |
OLD | NEW |