Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 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 "shell/command_line_util.h" |
| 6 | |
| 7 | #include <functional> |
Etienne Membrives | a627834 | 2016-04-22 15:46:03 +0200 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 10 | |
| 11 | #include "base/command_line.h" |
| 12 | #include "base/logging.h" |
Etienne Membrives | a627834 | 2016-04-22 15:46:03 +0200 | [diff] [blame] | 13 | #include "base/strings/string_tokenizer.h" |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
| 15 | #include "shell/context.h" |
| 16 | #include "shell/switches.h" |
| 17 | |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 18 | namespace shell { |
| 19 | |
| 20 | namespace { |
| 21 | GURL GetAppURLAndSetArgs(const std::string& app_url_and_args, |
| 22 | Context* context) { |
| 23 | std::vector<std::string> args; |
Nick Bray | 8636343 | 2015-01-27 12:55:35 -0800 | [diff] [blame] | 24 | GURL app_url = GetAppURLAndArgs(context, app_url_and_args, &args); |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 25 | |
Benjamin Lerman | f3b7f56 | 2015-11-23 18:18:03 +0100 | [diff] [blame] | 26 | if (!args.empty()) |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 27 | context->application_manager()->SetArgsForURL(args, app_url); |
| 28 | return app_url; |
| 29 | } |
| 30 | } // namespace |
| 31 | |
| 32 | bool ParseArgsFor(const std::string& arg, std::string* value) { |
| 33 | const std::string kArgsForSwitches[] = { |
| 34 | "-" + std::string(switches::kArgsFor) + "=", |
| 35 | "--" + std::string(switches::kArgsFor) + "=", |
| 36 | }; |
| 37 | for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { |
| 38 | const std::string& argsfor_switch = kArgsForSwitches[i]; |
| 39 | if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { |
| 40 | *value = arg.substr(argsfor_switch.size(), std::string::npos); |
| 41 | return true; |
| 42 | } |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
Nick Bray | 8636343 | 2015-01-27 12:55:35 -0800 | [diff] [blame] | 47 | GURL GetAppURLAndArgs(Context* context, |
| 48 | const std::string& app_url_and_args, |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 49 | std::vector<std::string>* args) { |
| 50 | // SplitString() returns empty strings for extra delimeter characters (' '). |
Etienne Membrives | a627834 | 2016-04-22 15:46:03 +0200 | [diff] [blame] | 51 | base::StringTokenizer tokenizer = |
| 52 | base::StringTokenizer(app_url_and_args, " "); |
| 53 | tokenizer.set_quote_chars("'\""); |
| 54 | while (tokenizer.GetNext()) { |
| 55 | args->push_back(tokenizer.token()); |
| 56 | } |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 57 | args->erase(std::remove_if(args->begin(), args->end(), |
James Robinson | cfeebe5 | 2015-09-09 14:17:53 -0700 | [diff] [blame] | 58 | [](const std::string& a) { return a.empty(); }), |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 59 | args->end()); |
| 60 | |
| 61 | if (args->empty()) |
| 62 | return GURL(); |
Nick Bray | 8636343 | 2015-01-27 12:55:35 -0800 | [diff] [blame] | 63 | GURL app_url = context->ResolveCommandLineURL((*args)[0]); |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 64 | if (!app_url.is_valid()) { |
| 65 | LOG(ERROR) << "Error: invalid URL: " << (*args)[0]; |
| 66 | return app_url; |
| 67 | } |
Benjamin Lerman | f3b7f56 | 2015-11-23 18:18:03 +0100 | [diff] [blame] | 68 | args->erase(args->begin()); |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 69 | return app_url; |
| 70 | } |
| 71 | |
| 72 | void ApplyApplicationArgs(Context* context, const std::string& args) { |
| 73 | std::string args_for_value; |
| 74 | if (ParseArgsFor(args, &args_for_value)) |
| 75 | GetAppURLAndSetArgs(args_for_value, context); |
| 76 | } |
| 77 | |
| 78 | void RunCommandLineApps(Context* context) { |
| 79 | const auto& command_line = *base::CommandLine::ForCurrentProcess(); |
| 80 | for (const auto& arg : command_line.GetArgs()) { |
James Robinson | 66db232 | 2015-04-02 12:56:26 -0700 | [diff] [blame] | 81 | GURL url = GetAppURLAndSetArgs(arg, context); |
Benjamin Lerman | 4fc210c | 2014-12-18 18:18:06 +0100 | [diff] [blame] | 82 | if (!url.is_valid()) |
| 83 | return; |
| 84 | context->Run(url); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | } // namespace shell |