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.
5 #ifndef CONTENT_BROWSER_TRACING_TRACE_UPLOADER_H_
6 #define CONTENT_BROWSER_TRACING_TRACE_UPLOADER_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/thread_checker.h"
19 #include "net/url_request/url_fetcher_delegate.h"
23 class URLRequestContextGetter
;
30 // Allow up to 10MB for trace upload
31 const int kMaxUploadBytes
= 10000000;
35 // TraceUploader uploads traces.
36 class TraceUploader
: public net::URLFetcherDelegate
{
38 typedef base::Callback
<void(bool, const std::string
&, const std::string
&)>
40 typedef base::Callback
<void(int64
, int64
)> UploadProgressCallback
;
42 TraceUploader(const std::string
& product
,
43 const std::string
& version
,
44 const std::string
& upload_url
,
45 net::URLRequestContextGetter
* request_context
);
46 virtual ~TraceUploader();
48 // net::URLFetcherDelegate implementation.
49 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
50 virtual void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
51 int64 current
, int64 total
) OVERRIDE
;
53 // Compresses and uploads the given file contents.
54 void DoUpload(const std::string
& file_contents
,
55 UploadProgressCallback progress_callback
,
56 UploadDoneCallback done_callback
);
59 // Sets up a multipart body to be uploaded. The body is produced according
61 void SetupMultipart(const std::string
& trace_filename
,
62 const std::string
& trace_contents
,
63 std::string
* post_data
);
64 void AddTraceFile(const std::string
& trace_filename
,
65 const std::string
& trace_contents
,
66 std::string
* post_data
);
67 // Compresses the input and returns whether compression was successful.
68 bool Compress(std::string input
,
69 int max_compressed_bytes
,
70 char* compressed_contents
,
71 int* compressed_bytes
);
72 void CreateAndStartURLFetcher(const std::string
& post_data
);
73 void OnUploadError(std::string error_message
);
78 std::string upload_url_
;
80 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
81 UploadProgressCallback progress_callback_
;
82 UploadDoneCallback done_callback_
;
84 net::URLRequestContextGetter
* request_context_
;
86 DISALLOW_COPY_AND_ASSIGN(TraceUploader
);
89 } // namespace content
91 #endif // CONTENT_BROWSER_TRACING_TRACE_UPLOADER_H_