b5f32c69eb385a38ddc75d6b7f8de6474521188e
[platform/framework/web/crosswalk.git] / src / breakpad / src / common / linux / google_crashdump_uploader.cc
1 // Copyright (c) 2009, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 #include "common/linux/google_crashdump_uploader.h"
32 #include "common/linux/libcurl_wrapper.h"
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37
38 #include <iostream>
39
40 #include "common/using_std_string.h"
41
42 namespace google_breakpad {
43
44 GoogleCrashdumpUploader::GoogleCrashdumpUploader(const string& product,
45                                                  const string& version,
46                                                  const string& guid,
47                                                  const string& ptime,
48                                                  const string& ctime,
49                                                  const string& email,
50                                                  const string& comments,
51                                                  const string& minidump_pathname,
52                                                  const string& crash_server,
53                                                  const string& proxy_host,
54                                                  const string& proxy_userpassword) {
55   LibcurlWrapper* http_layer = new LibcurlWrapper();
56   Init(product,
57        version,
58        guid,
59        ptime,
60        ctime,
61        email,
62        comments,
63        minidump_pathname,
64        crash_server,
65        proxy_host,
66        proxy_userpassword,
67        http_layer);
68 }
69
70 GoogleCrashdumpUploader::GoogleCrashdumpUploader(const string& product,
71                                                  const string& version,
72                                                  const string& guid,
73                                                  const string& ptime,
74                                                  const string& ctime,
75                                                  const string& email,
76                                                  const string& comments,
77                                                  const string& minidump_pathname,
78                                                  const string& crash_server,
79                                                  const string& proxy_host,
80                                                  const string& proxy_userpassword,
81                                                  LibcurlWrapper* http_layer) {
82   Init(product,
83        version,
84        guid,
85        ptime,
86        ctime,
87        email,
88        comments,
89        minidump_pathname,
90        crash_server,
91        proxy_host,
92        proxy_userpassword,
93        http_layer);
94 }
95
96 void GoogleCrashdumpUploader::Init(const string& product,
97                                    const string& version,
98                                    const string& guid,
99                                    const string& ptime,
100                                    const string& ctime,
101                                    const string& email,
102                                    const string& comments,
103                                    const string& minidump_pathname,
104                                    const string& crash_server,
105                                    const string& proxy_host,
106                                    const string& proxy_userpassword,
107                                    LibcurlWrapper* http_layer) {
108   product_ = product;
109   version_ = version;
110   guid_ = guid;
111   ptime_ = ptime;
112   ctime_ = ctime;
113   email_ = email;
114   comments_ = comments;
115   http_layer_ = http_layer;
116
117   crash_server_ = crash_server;
118   proxy_host_ = proxy_host;
119   proxy_userpassword_ = proxy_userpassword;
120   minidump_pathname_ = minidump_pathname;
121   std::cout << "Uploader initializing";
122   std::cout << "\tProduct: " << product_;
123   std::cout << "\tVersion: " << version_;
124   std::cout << "\tGUID: " << guid_;
125   if (!ptime_.empty()) {
126     std::cout << "\tProcess uptime: " << ptime_;
127   }
128   if (!ctime_.empty()) {
129     std::cout << "\tCumulative Process uptime: " << ctime_;
130   }
131   if (!email_.empty()) {
132     std::cout << "\tEmail: " << email_;
133   }
134   if (!comments_.empty()) {
135     std::cout << "\tComments: " << comments_;
136   }
137 }
138
139 bool GoogleCrashdumpUploader::CheckRequiredParametersArePresent() {
140   string error_text;
141   if (product_.empty()) {
142     error_text.append("\nProduct name must be specified.");
143   }
144
145   if (version_.empty()) {
146     error_text.append("\nProduct version must be specified.");
147   }
148
149   if (guid_.empty()) {
150     error_text.append("\nClient ID must be specified.");
151   }
152
153   if (minidump_pathname_.empty()) {
154     error_text.append("\nMinidump pathname must be specified.");
155   }
156
157   if (!error_text.empty()) {
158     std::cout << error_text;
159     return false;
160   }
161   return true;
162
163 }
164
165 bool GoogleCrashdumpUploader::Upload() {
166   bool ok = http_layer_->Init();
167   if (!ok) {
168     std::cout << "http layer init failed";
169     return ok;
170   }
171
172   if (!CheckRequiredParametersArePresent()) {
173     return false;
174   }
175
176   struct stat st;
177   int err = stat(minidump_pathname_.c_str(), &st);
178   if (err) {
179     std::cout << minidump_pathname_ << " could not be found";
180     return false;
181   }
182
183   parameters_["prod"] = product_;
184   parameters_["ver"] = version_;
185   parameters_["guid"] = guid_;
186   parameters_["ptime"] = ptime_;
187   parameters_["ctime"] = ctime_;
188   parameters_["email"] = email_;
189   parameters_["comments_"] = comments_;
190   if (!http_layer_->AddFile(minidump_pathname_,
191                             "upload_file_minidump")) {
192     return false;
193   }
194   std::cout << "Sending request to " << crash_server_;
195   return http_layer_->SendRequest(crash_server_,
196                                   parameters_,
197                                   NULL);
198 }
199 }