| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/tracing/crash_service_uploader.h" | 5 #include "chrome/browser/tracing/crash_service_uploader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 const char kUploadURL[] = "https://clients2.google.com/cr/report"; | 39 const char kUploadURL[] = "https://clients2.google.com/cr/report"; |
| 40 const char kUploadContentType[] = "multipart/form-data"; | 40 const char kUploadContentType[] = "multipart/form-data"; |
| 41 const char kMultipartBoundary[] = | 41 const char kMultipartBoundary[] = |
| 42 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; | 42 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; |
| 43 const int kHttpResponseOk = 200; | 43 const int kHttpResponseOk = 200; |
| 44 | 44 |
| 45 // Allow up to 10MB for trace upload | 45 // Allow up to 10MB for trace upload |
| 46 const int kMaxUploadBytes = 10000000; | 46 const size_t kMaxUploadBytes = 10000000; |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 TraceCrashServiceUploader::TraceCrashServiceUploader( | 50 TraceCrashServiceUploader::TraceCrashServiceUploader( |
| 51 net::URLRequestContextGetter* request_context) | 51 net::URLRequestContextGetter* request_context) |
| 52 : request_context_(request_context) { | 52 : request_context_(request_context), max_upload_bytes_(kMaxUploadBytes) { |
| 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 54 const base::CommandLine& command_line = | 54 const base::CommandLine& command_line = |
| 55 *base::CommandLine::ForCurrentProcess(); | 55 *base::CommandLine::ForCurrentProcess(); |
| 56 std::string upload_url = kUploadURL; | 56 std::string upload_url = kUploadURL; |
| 57 if (command_line.HasSwitch(switches::kTraceUploadURL)) { | 57 if (command_line.HasSwitch(switches::kTraceUploadURL)) { |
| 58 upload_url = command_line.GetSwitchValueASCII(switches::kTraceUploadURL); | 58 upload_url = command_line.GetSwitchValueASCII(switches::kTraceUploadURL); |
| 59 } | 59 } |
| 60 SetUploadURL(upload_url); | 60 SetUploadURL(upload_url); |
| 61 } | 61 } |
| 62 | 62 |
| 63 TraceCrashServiceUploader::~TraceCrashServiceUploader() { | 63 TraceCrashServiceUploader::~TraceCrashServiceUploader() { |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void TraceCrashServiceUploader::SetUploadURL(const std::string& url) { | 67 void TraceCrashServiceUploader::SetUploadURL(const std::string& url) { |
| 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 69 upload_url_ = url; | 69 upload_url_ = url; |
| 70 | 70 |
| 71 if (!GURL(upload_url_).is_valid()) | 71 if (!GURL(upload_url_).is_valid()) |
| 72 upload_url_.clear(); | 72 upload_url_.clear(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void TraceCrashServiceUploader::SetMaxUploadBytes(size_t max_upload_bytes) { |
| 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 77 max_upload_bytes_ = max_upload_bytes; |
| 78 } |
| 79 |
| 75 void TraceCrashServiceUploader::OnURLFetchComplete( | 80 void TraceCrashServiceUploader::OnURLFetchComplete( |
| 76 const net::URLFetcher* source) { | 81 const net::URLFetcher* source) { |
| 77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 78 DCHECK_EQ(source, url_fetcher_.get()); | 83 DCHECK_EQ(source, url_fetcher_.get()); |
| 79 int response_code = source->GetResponseCode(); | 84 int response_code = source->GetResponseCode(); |
| 80 string feedback; | 85 string feedback; |
| 81 bool success = (response_code == kHttpResponseOk); | 86 bool success = (response_code == kHttpResponseOk); |
| 82 if (success) { | 87 if (success) { |
| 83 source->GetResponseAsString(&feedback); | 88 source->GetResponseAsString(&feedback); |
| 84 } else { | 89 } else { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 version = "unknown"; | 172 version = "unknown"; |
| 168 } | 173 } |
| 169 | 174 |
| 170 if (url_fetcher_.get()) { | 175 if (url_fetcher_.get()) { |
| 171 OnUploadError("Already uploading."); | 176 OnUploadError("Already uploading."); |
| 172 return; | 177 return; |
| 173 } | 178 } |
| 174 | 179 |
| 175 std::string compressed_contents; | 180 std::string compressed_contents; |
| 176 if (upload_mode == COMPRESSED_UPLOAD) { | 181 if (upload_mode == COMPRESSED_UPLOAD) { |
| 177 std::unique_ptr<char[]> compressed_buffer(new char[kMaxUploadBytes]); | 182 std::unique_ptr<char[]> compressed_buffer(new char[max_upload_bytes_]); |
| 178 int compressed_bytes; | 183 int compressed_bytes; |
| 179 if (!Compress(file_contents, kMaxUploadBytes, compressed_buffer.get(), | 184 if (!Compress(file_contents, max_upload_bytes_, compressed_buffer.get(), |
| 180 &compressed_bytes)) { | 185 &compressed_bytes)) { |
| 181 OnUploadError("Compressing file failed."); | 186 OnUploadError("Compressing file failed."); |
| 182 return; | 187 return; |
| 183 } | 188 } |
| 184 compressed_contents = | 189 compressed_contents = |
| 185 std::string(compressed_buffer.get(), compressed_bytes); | 190 std::string(compressed_buffer.get(), compressed_bytes); |
| 186 } else { | 191 } else { |
| 187 if (file_contents.size() >= kMaxUploadBytes) { | |
| 188 OnUploadError("File is too large to upload."); | |
| 189 return; | |
| 190 } | |
| 191 compressed_contents = file_contents; | 192 compressed_contents = file_contents; |
| 192 } | 193 } |
| 194 if (compressed_contents.size() > max_upload_bytes_) { |
| 195 OnUploadError("File is too large to upload."); |
| 196 return; |
| 197 } |
| 193 | 198 |
| 194 std::string post_data; | 199 std::string post_data; |
| 195 SetupMultipart(product, version, std::move(metadata), "trace.json.gz", | 200 SetupMultipart(product, version, std::move(metadata), "trace.json.gz", |
| 196 compressed_contents, &post_data); | 201 compressed_contents, &post_data); |
| 197 | 202 |
| 198 content::BrowserThread::PostTask( | 203 content::BrowserThread::PostTask( |
| 199 content::BrowserThread::UI, FROM_HERE, | 204 content::BrowserThread::UI, FROM_HERE, |
| 200 base::Bind(&TraceCrashServiceUploader::CreateAndStartURLFetcher, | 205 base::Bind(&TraceCrashServiceUploader::CreateAndStartURLFetcher, |
| 201 base::Unretained(this), upload_url, post_data)); | 206 base::Unretained(this), upload_url, post_data)); |
| 202 } | 207 } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 std::string content_type = kUploadContentType; | 307 std::string content_type = kUploadContentType; |
| 303 content_type.append("; boundary="); | 308 content_type.append("; boundary="); |
| 304 content_type.append(kMultipartBoundary); | 309 content_type.append(kMultipartBoundary); |
| 305 | 310 |
| 306 url_fetcher_ = | 311 url_fetcher_ = |
| 307 net::URLFetcher::Create(GURL(upload_url), net::URLFetcher::POST, this); | 312 net::URLFetcher::Create(GURL(upload_url), net::URLFetcher::POST, this); |
| 308 url_fetcher_->SetRequestContext(request_context_); | 313 url_fetcher_->SetRequestContext(request_context_); |
| 309 url_fetcher_->SetUploadData(content_type, post_data); | 314 url_fetcher_->SetUploadData(content_type, post_data); |
| 310 url_fetcher_->Start(); | 315 url_fetcher_->Start(); |
| 311 } | 316 } |
| OLD | NEW |