| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <iostream> | 6 #include <iostream> |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | |
| 12 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 13 #include "base/strings/string_split.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "shell/child_process.h" | 12 #include "shell/child_process.h" |
| 13 #include "shell/command_line_util.h" |
| 16 #include "shell/context.h" | 14 #include "shell/context.h" |
| 17 #include "shell/init.h" | 15 #include "shell/init.h" |
| 18 #include "shell/mojo_url_resolver.h" | |
| 19 #include "shell/switches.h" | 16 #include "shell/switches.h" |
| 20 | 17 |
| 21 namespace { | 18 namespace { |
| 22 | 19 |
| 23 #if defined(OS_LINUX) | 20 #if defined(OS_LINUX) |
| 24 // Copied from ui/gfx/switches.cc to avoid a dependency on //ui/gfx | 21 // Copied from ui/gfx/switches.cc to avoid a dependency on //ui/gfx |
| 25 const char kEnableHarfBuzzRenderText[] = "enable-harfbuzz-rendertext"; | 22 const char kEnableHarfBuzzRenderText[] = "enable-harfbuzz-rendertext"; |
| 26 #endif | 23 #endif |
| 27 | 24 |
| 28 bool IsEmpty(const std::string& s) { | |
| 29 return s.empty(); | |
| 30 } | |
| 31 | |
| 32 // The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args | |
| 33 // is a list of "configuration" arguments separated by spaces. If one or more | |
| 34 // arguments are specified they will be available when the Mojo application | |
| 35 // is initialized. See ApplicationImpl::args(). | |
| 36 GURL GetAppURLAndSetArgs(const std::string& app_url_and_args, | |
| 37 mojo::shell::Context* context) { | |
| 38 // SplitString() returns empty strings for extra delimeter characters (' '). | |
| 39 std::vector<std::string> argv; | |
| 40 base::SplitString(app_url_and_args, ' ', &argv); | |
| 41 argv.erase(std::remove_if(argv.begin(), argv.end(), IsEmpty), argv.end()); | |
| 42 | |
| 43 if (argv.empty()) | |
| 44 return GURL::EmptyGURL(); | |
| 45 GURL app_url(argv[0]); | |
| 46 if (!app_url.is_valid()) { | |
| 47 LOG(ERROR) << "Error: invalid URL: " << argv[0]; | |
| 48 return app_url; | |
| 49 } | |
| 50 if (argv.size() > 1) | |
| 51 context->application_manager()->SetArgsForURL(argv, app_url); | |
| 52 return app_url; | |
| 53 } | |
| 54 | |
| 55 void RunApps(mojo::shell::Context* context) { | |
| 56 const auto& command_line = *base::CommandLine::ForCurrentProcess(); | |
| 57 for (const auto& arg : command_line.GetArgs()) { | |
| 58 std::string arg2; | |
| 59 #if defined(OS_WIN) | |
| 60 arg2 = base::UTF16ToUTF8(arg); | |
| 61 #else | |
| 62 arg2 = arg; | |
| 63 #endif | |
| 64 GURL url = GetAppURLAndSetArgs(arg2, context); | |
| 65 if (!url.is_valid()) | |
| 66 return; | |
| 67 context->Run(GetAppURLAndSetArgs(arg2, context)); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void Usage() { | 25 void Usage() { |
| 72 std::cerr << "Launch Mojo applications.\n"; | 26 std::cerr << "Launch Mojo applications.\n"; |
| 73 std::cerr | 27 std::cerr |
| 74 << "Usage: mojo_shell" | 28 << "Usage: mojo_shell" |
| 75 << " [--" << switches::kArgsFor << "=<mojo-app>]" | 29 << " [--" << switches::kArgsFor << "=<mojo-app>]" |
| 76 << " [--" << switches::kContentHandlers << "=<handlers>]" | 30 << " [--" << switches::kContentHandlers << "=<handlers>]" |
| 77 << " [--" << switches::kEnableExternalApplications << "]" | 31 << " [--" << switches::kEnableExternalApplications << "]" |
| 78 << " [--" << switches::kDisableCache << "]" | 32 << " [--" << switches::kDisableCache << "]" |
| 79 << " [--" << switches::kEnableMultiprocess << "]" | 33 << " [--" << switches::kEnableMultiprocess << "]" |
| 80 << " [--" << switches::kOrigin << "=<url-lib-path>]" | 34 << " [--" << switches::kOrigin << "=<url-lib-path>]" |
| 81 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" | 35 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" |
| 82 << " <mojo-app> ...\n\n" | 36 << " <mojo-app> ...\n\n" |
| 83 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " | 37 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " |
| 84 << "quotes.\n" | 38 << "quotes.\n" |
| 85 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n" | 39 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n" |
| 86 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" | 40 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" |
| 87 << "The value of <handlers> is a comma separated list like:\n" | 41 << "The value of <handlers> is a comma separated list like:\n" |
| 88 << "text/html,mojo:html_viewer," | 42 << "text/html,mojo:html_viewer," |
| 89 << "application/javascript,mojo:js_content_handler\n"; | 43 << "application/javascript,mojo:js_content_handler\n"; |
| 90 } | 44 } |
| 91 | 45 |
| 92 bool IsArgsFor(const std::string& arg, std::string* value) { | |
| 93 const std::string kArgsForSwitches[] = { | |
| 94 "-" + std::string(switches::kArgsFor), | |
| 95 "--" + std::string(switches::kArgsFor), | |
| 96 }; | |
| 97 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { | |
| 98 std::string argsfor_switch(kArgsForSwitches[i]); | |
| 99 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { | |
| 100 *value = arg.substr(argsfor_switch.size() + 1, std::string::npos); | |
| 101 return true; | |
| 102 } | |
| 103 } | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 } // namespace | 46 } // namespace |
| 108 | 47 |
| 109 int main(int argc, char** argv) { | 48 int main(int argc, char** argv) { |
| 110 base::AtExitManager at_exit; | 49 base::AtExitManager at_exit; |
| 111 base::CommandLine::Init(argc, argv); | 50 base::CommandLine::Init(argc, argv); |
| 112 | 51 |
| 113 const base::CommandLine& command_line = | 52 const base::CommandLine& command_line = |
| 114 *base::CommandLine::ForCurrentProcess(); | 53 *base::CommandLine::ForCurrentProcess(); |
| 115 | 54 |
| 116 const std::set<std::string> all_switches = switches::GetAllSwitches(); | 55 const std::set<std::string> all_switches = switches::GetAllSwitches(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 base::MessageLoop message_loop; | 92 base::MessageLoop message_loop; |
| 154 if (!shell_context.Init()) { | 93 if (!shell_context.Init()) { |
| 155 Usage(); | 94 Usage(); |
| 156 return 0; | 95 return 0; |
| 157 } | 96 } |
| 158 | 97 |
| 159 // The mojo_shell --args-for command-line switch is handled specially | 98 // The mojo_shell --args-for command-line switch is handled specially |
| 160 // because it can appear more than once. The base::CommandLine class | 99 // because it can appear more than once. The base::CommandLine class |
| 161 // collapses multiple occurrences of the same switch. | 100 // collapses multiple occurrences of the same switch. |
| 162 for (int i = 1; i < argc; i++) { | 101 for (int i = 1; i < argc; i++) { |
| 163 std::string args_for_value; | 102 ApplyApplicationArgs(&shell_context, argv[i]); |
| 164 if (IsArgsFor(argv[i], &args_for_value)) | |
| 165 GetAppURLAndSetArgs(args_for_value, &shell_context); | |
| 166 } | 103 } |
| 167 | 104 |
| 168 message_loop.PostTask(FROM_HERE, base::Bind(RunApps, &shell_context)); | 105 message_loop.PostTask( |
| 106 FROM_HERE, |
| 107 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context)); |
| 169 message_loop.Run(); | 108 message_loop.Run(); |
| 170 } | 109 } |
| 171 } | 110 } |
| 172 return 0; | 111 return 0; |
| 173 } | 112 } |
| OLD | NEW |