OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdio.h> |
| 6 #include <string.h> |
| 7 |
| 8 #include <algorithm> |
| 9 #include <iostream> |
| 10 |
| 11 #include "base/at_exit.h" |
| 12 #include "base/base_switches.h" |
| 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" |
| 15 #include "base/files/file_util.h" |
| 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/trace_event/trace_event.h" |
| 19 #include "mojo/shell/child_process.h" |
| 20 #include "mojo/shell/command_line_util.h" |
| 21 #include "mojo/shell/context.h" |
| 22 #include "mojo/shell/init.h" |
| 23 #include "mojo/shell/switches.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 void Usage() { |
| 28 std::cerr << "Launch Mojo applications.\n"; |
| 29 std::cerr |
| 30 << "Usage: mojo_shell" |
| 31 << " [--" << switches::kArgsFor << "=<mojo-app>]" |
| 32 << " [--" << switches::kContentHandlers << "=<handlers>]" |
| 33 << " [--" << switches::kEnableExternalApplications << "]" |
| 34 << " [--" << switches::kDisableCache << "]" |
| 35 << " [--" << switches::kEnableMultiprocess << "]" |
| 36 << " [--" << switches::kOrigin << "=<url-lib-path>]" |
| 37 << " [--" << switches::kTraceStartup << "]" |
| 38 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" |
| 39 << " [--" << switches::kPredictableAppFilenames << "]" |
| 40 << " [--" << switches::kWaitForDebugger << "]" |
| 41 << " <mojo-app> ...\n\n" |
| 42 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " |
| 43 << "quotes.\n" |
| 44 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n" |
| 45 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" |
| 46 << "The value of <handlers> is a comma separated list like:\n" |
| 47 << "text/html,mojo:html_viewer," |
| 48 << "application/javascript,mojo:js_content_handler\n"; |
| 49 } |
| 50 |
| 51 // Whether we're currently tracing. |
| 52 bool g_tracing = false; |
| 53 |
| 54 // Number of tracing blocks written. |
| 55 uint32_t g_blocks = 0; |
| 56 |
| 57 // Trace file, if open. |
| 58 FILE* g_trace_file = nullptr; |
| 59 |
| 60 void WriteTraceDataCollected( |
| 61 base::WaitableEvent* event, |
| 62 const scoped_refptr<base::RefCountedString>& events_str, |
| 63 bool has_more_events) { |
| 64 if (g_blocks) { |
| 65 fwrite(",", 1, 1, g_trace_file); |
| 66 } |
| 67 |
| 68 ++g_blocks; |
| 69 fwrite(events_str->data().c_str(), 1, events_str->data().length(), |
| 70 g_trace_file); |
| 71 if (!has_more_events) { |
| 72 static const char kEnd[] = "]}"; |
| 73 fwrite(kEnd, 1, strlen(kEnd), g_trace_file); |
| 74 PCHECK(fclose(g_trace_file) == 0); |
| 75 g_trace_file = nullptr; |
| 76 event->Signal(); |
| 77 } |
| 78 } |
| 79 |
| 80 void EndTraceAndFlush(base::WaitableEvent* event) { |
| 81 g_trace_file = fopen("mojo_shell.trace", "w+"); |
| 82 PCHECK(g_trace_file); |
| 83 static const char kStart[] = "{\"traceEvents\":["; |
| 84 fwrite(kStart, 1, strlen(kStart), g_trace_file); |
| 85 base::trace_event::TraceLog::GetInstance()->SetDisabled(); |
| 86 base::trace_event::TraceLog::GetInstance()->Flush( |
| 87 base::Bind(&WriteTraceDataCollected, base::Unretained(event))); |
| 88 } |
| 89 |
| 90 void StopTracingAndFlushToDisk() { |
| 91 g_tracing = false; |
| 92 base::trace_event::TraceLog::GetInstance()->SetDisabled(); |
| 93 base::WaitableEvent flush_complete_event(false, false); |
| 94 // TraceLog::Flush requires a message loop but we've already shut ours down. |
| 95 // Spin up a new thread to flush things out. |
| 96 base::Thread flush_thread("mojo_shell_trace_event_flush"); |
| 97 flush_thread.Start(); |
| 98 flush_thread.message_loop()->PostTask( |
| 99 FROM_HERE, |
| 100 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event))); |
| 101 flush_complete_event.Wait(); |
| 102 } |
| 103 |
| 104 } // namespace |
| 105 |
| 106 int main(int argc, char** argv) { |
| 107 base::AtExitManager at_exit; |
| 108 base::CommandLine::Init(argc, argv); |
| 109 |
| 110 mojo::shell::InitializeLogging(); |
| 111 |
| 112 // TODO(vtl): Unify parent and child process cases to the extent possible. |
| 113 if (scoped_ptr<mojo::shell::ChildProcess> child_process = |
| 114 mojo::shell::ChildProcess::Create( |
| 115 *base::CommandLine::ForCurrentProcess())) { |
| 116 child_process->Main(); |
| 117 } else { |
| 118 // Only check the command line for the main process. |
| 119 const base::CommandLine& command_line = |
| 120 *base::CommandLine::ForCurrentProcess(); |
| 121 |
| 122 const std::set<std::string> all_switches = switches::GetAllSwitches(); |
| 123 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); |
| 124 bool found_unknown_switch = false; |
| 125 for (const auto& s : switches) { |
| 126 if (all_switches.find(s.first) == all_switches.end()) { |
| 127 std::cerr << "unknown switch: " << s.first << std::endl; |
| 128 found_unknown_switch = true; |
| 129 } |
| 130 } |
| 131 |
| 132 if (found_unknown_switch || |
| 133 (!command_line.HasSwitch(switches::kEnableExternalApplications) && |
| 134 (command_line.HasSwitch(switches::kHelp) || |
| 135 command_line.GetArgs().empty()))) { |
| 136 Usage(); |
| 137 return 0; |
| 138 } |
| 139 |
| 140 if (command_line.HasSwitch(switches::kTraceStartup)) { |
| 141 g_tracing = true; |
| 142 base::trace_event::CategoryFilter category_filter( |
| 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 } |
| 149 |
| 150 // We want the shell::Context to outlive the MessageLoop so that pipes are |
| 151 // all gracefully closed / error-out before we try to shut the Context down. |
| 152 mojo::shell::Context shell_context; |
| 153 { |
| 154 base::MessageLoop message_loop; |
| 155 if (!shell_context.Init()) { |
| 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 |
| 165 // The mojo_shell --args-for command-line switch is handled specially |
| 166 // because it can appear more than once. The base::CommandLine class |
| 167 // collapses multiple occurrences of the same switch. |
| 168 for (int i = 1; i < argc; i++) { |
| 169 ApplyApplicationArgs(&shell_context, argv[i]); |
| 170 } |
| 171 |
| 172 message_loop.PostTask( |
| 173 FROM_HERE, |
| 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 } |
| 181 |
| 182 if (g_tracing) |
| 183 StopTracingAndFlushToDisk(); |
| 184 return 0; |
| 185 } |
OLD | NEW |