| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "mojo/shell/command_line_util.h" | |
| 6 | |
| 7 #include <functional> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/strings/string_split.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "mojo/shell/context.h" | |
| 14 #include "mojo/shell/switches.h" | |
| 15 | |
| 16 namespace mojo { | |
| 17 namespace shell { | |
| 18 | |
| 19 namespace { | |
| 20 GURL GetAppURLAndSetArgs(const std::string& app_url_and_args, | |
| 21 Context* context) { | |
| 22 std::vector<std::string> args; | |
| 23 GURL app_url = GetAppURLAndArgs(context, app_url_and_args, &args); | |
| 24 | |
| 25 if (args.size() > 1) | |
| 26 context->application_manager()->SetArgsForURL(args, app_url); | |
| 27 return app_url; | |
| 28 } | |
| 29 } // namespace | |
| 30 | |
| 31 bool ParseArgsFor(const std::string& arg, std::string* value) { | |
| 32 const std::string kArgsForSwitches[] = { | |
| 33 "-" + std::string(switches::kArgsFor) + "=", | |
| 34 "--" + std::string(switches::kArgsFor) + "=", | |
| 35 }; | |
| 36 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { | |
| 37 const std::string& argsfor_switch = kArgsForSwitches[i]; | |
| 38 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { | |
| 39 *value = arg.substr(argsfor_switch.size(), std::string::npos); | |
| 40 return true; | |
| 41 } | |
| 42 } | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 GURL GetAppURLAndArgs(Context* context, | |
| 47 const std::string& app_url_and_args, | |
| 48 std::vector<std::string>* args) { | |
| 49 // SplitString() returns empty strings for extra delimeter characters (' '). | |
| 50 base::SplitString(app_url_and_args, ' ', args); | |
| 51 args->erase(std::remove_if(args->begin(), args->end(), | |
| 52 std::mem_fun_ref(&std::string::empty)), | |
| 53 args->end()); | |
| 54 | |
| 55 if (args->empty()) | |
| 56 return GURL(); | |
| 57 GURL app_url = context->ResolveCommandLineURL((*args)[0]); | |
| 58 if (!app_url.is_valid()) { | |
| 59 LOG(ERROR) << "Error: invalid URL: " << (*args)[0]; | |
| 60 return app_url; | |
| 61 } | |
| 62 if (args->size() == 1) | |
| 63 args->clear(); | |
| 64 return app_url; | |
| 65 } | |
| 66 | |
| 67 void ApplyApplicationArgs(Context* context, const std::string& args) { | |
| 68 std::string args_for_value; | |
| 69 if (ParseArgsFor(args, &args_for_value)) | |
| 70 GetAppURLAndSetArgs(args_for_value, context); | |
| 71 } | |
| 72 | |
| 73 void RunCommandLineApps(Context* context) { | |
| 74 const auto& command_line = *base::CommandLine::ForCurrentProcess(); | |
| 75 for (const auto& arg : command_line.GetArgs()) { | |
| 76 std::string arg2; | |
| 77 #if defined(OS_WIN) | |
| 78 arg2 = base::UTF16ToUTF8(arg); | |
| 79 #else | |
| 80 arg2 = arg; | |
| 81 #endif | |
| 82 GURL url = GetAppURLAndSetArgs(arg2, context); | |
| 83 if (!url.is_valid()) | |
| 84 return; | |
| 85 context->Run(url); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 } // namespace shell | |
| 90 } // namespace mojo | |
| OLD | NEW |