blob: b1288686672bff26ea771aa2f985f398903c541e [file] [log] [blame]
Benjamin Lerman4fc210c2014-12-18 18:18:06 +01001// 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 Membrivesa6278342016-04-22 15:46:03 +02008#include <string>
9#include <vector>
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010010
11#include "base/command_line.h"
12#include "base/logging.h"
Etienne Membrivesa6278342016-04-22 15:46:03 +020013#include "base/strings/string_tokenizer.h"
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010014#include "base/strings/utf_string_conversions.h"
15#include "shell/context.h"
16#include "shell/switches.h"
17
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010018namespace shell {
19
20namespace {
21GURL GetAppURLAndSetArgs(const std::string& app_url_and_args,
22 Context* context) {
23 std::vector<std::string> args;
Nick Bray86363432015-01-27 12:55:35 -080024 GURL app_url = GetAppURLAndArgs(context, app_url_and_args, &args);
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010025
Benjamin Lermanf3b7f562015-11-23 18:18:03 +010026 if (!args.empty())
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010027 context->application_manager()->SetArgsForURL(args, app_url);
28 return app_url;
29}
30} // namespace
31
32bool 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 Bray86363432015-01-27 12:55:35 -080047GURL GetAppURLAndArgs(Context* context,
48 const std::string& app_url_and_args,
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010049 std::vector<std::string>* args) {
50 // SplitString() returns empty strings for extra delimeter characters (' ').
Etienne Membrivesa6278342016-04-22 15:46:03 +020051 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 Lerman4fc210c2014-12-18 18:18:06 +010057 args->erase(std::remove_if(args->begin(), args->end(),
James Robinsoncfeebe52015-09-09 14:17:53 -070058 [](const std::string& a) { return a.empty(); }),
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010059 args->end());
60
61 if (args->empty())
62 return GURL();
Nick Bray86363432015-01-27 12:55:35 -080063 GURL app_url = context->ResolveCommandLineURL((*args)[0]);
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010064 if (!app_url.is_valid()) {
65 LOG(ERROR) << "Error: invalid URL: " << (*args)[0];
66 return app_url;
67 }
Benjamin Lermanf3b7f562015-11-23 18:18:03 +010068 args->erase(args->begin());
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010069 return app_url;
70}
71
72void 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
78void RunCommandLineApps(Context* context) {
79 const auto& command_line = *base::CommandLine::ForCurrentProcess();
80 for (const auto& arg : command_line.GetArgs()) {
James Robinson66db2322015-04-02 12:56:26 -070081 GURL url = GetAppURLAndSetArgs(arg, context);
Benjamin Lerman4fc210c2014-12-18 18:18:06 +010082 if (!url.is_valid())
83 return;
84 context->Run(url);
85 }
86}
87
88} // namespace shell