1 // Copyright 2018 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "client/client_argv_handling.h"
16 
17 #include "base/strings/stringprintf.h"
18 
19 namespace crashpad {
20 
21 namespace {
22 
FormatArgumentString(const std::string & name,const std::string & value)23 std::string FormatArgumentString(const std::string& name,
24                                  const std::string& value) {
25   return base::StringPrintf("--%s=%s", name.c_str(), value.c_str());
26 }
27 
28 }  // namespace
29 
BuildHandlerArgvStrings(const base::FilePath & handler,const base::FilePath & database,const base::FilePath & metrics_dir,const std::string & url,const std::map<std::string,std::string> & annotations,const std::vector<std::string> & arguments,const std::vector<base::FilePath> & attachments)30 std::vector<std::string> BuildHandlerArgvStrings(
31     const base::FilePath& handler,
32     const base::FilePath& database,
33     const base::FilePath& metrics_dir,
34     const std::string& url,
35     const std::map<std::string, std::string>& annotations,
36     const std::vector<std::string>& arguments,
37     const std::vector<base::FilePath>& attachments) {
38   std::vector<std::string> argv_strings(1, handler.value());
39 
40   for (const auto& argument : arguments) {
41     argv_strings.push_back(argument);
42   }
43 
44   if (!database.empty()) {
45     argv_strings.push_back(FormatArgumentString("database", database.value()));
46   }
47 
48   if (!metrics_dir.empty()) {
49     argv_strings.push_back(
50         FormatArgumentString("metrics-dir", metrics_dir.value()));
51   }
52 
53   if (!url.empty()) {
54     argv_strings.push_back(FormatArgumentString("url", url));
55   }
56 
57   for (const auto& kv : annotations) {
58     argv_strings.push_back(
59         FormatArgumentString("annotation", kv.first + '=' + kv.second));
60   }
61 
62   for (const auto& attachment : attachments) {
63     argv_strings.push_back(
64         FormatArgumentString("attachment", attachment.value()));
65   }
66 
67   return argv_strings;
68 }
69 
StringVectorToCStringVector(const std::vector<std::string> & strings,std::vector<const char * > * c_strings)70 void StringVectorToCStringVector(const std::vector<std::string>& strings,
71                                  std::vector<const char*>* c_strings) {
72   c_strings->clear();
73   c_strings->reserve(strings.size() + 1);
74   for (const auto& str : strings) {
75     c_strings->push_back(str.c_str());
76   }
77   c_strings->push_back(nullptr);
78 }
79 
80 }  // namespace crashpad
81