• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_BROWSER_TRACING_TRACE_UPLOADER_H_
6 #define CONTENT_BROWSER_TRACING_TRACE_UPLOADER_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
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"
20 
21 namespace net {
22 class URLFetcher;
23 class URLRequestContextGetter;
24 }  // namespace net
25 
26 namespace content {
27 
28 namespace {
29 
30 // Allow up to 10MB for trace upload
31 const int kMaxUploadBytes = 10000000;
32 
33 }  // namespace
34 
35 // TraceUploader uploads traces.
36 class TraceUploader : public net::URLFetcherDelegate {
37  public:
38   typedef base::Callback<void(bool, const std::string&, const std::string&)>
39       UploadDoneCallback;
40   typedef base::Callback<void(int64, int64)> UploadProgressCallback;
41 
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();
47 
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;
52 
53   // Compresses and uploads the given file contents.
54   void DoUpload(const std::string& file_contents,
55                 UploadProgressCallback progress_callback,
56                 UploadDoneCallback done_callback);
57 
58  private:
59   // Sets up a multipart body to be uploaded. The body is produced according
60   // to RFC 2046.
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);
74 
75   std::string product_;
76   std::string version_;
77 
78   std::string upload_url_;
79 
80   scoped_ptr<net::URLFetcher> url_fetcher_;
81   UploadProgressCallback progress_callback_;
82   UploadDoneCallback done_callback_;
83 
84   net::URLRequestContextGetter* request_context_;
85 
86   DISALLOW_COPY_AND_ASSIGN(TraceUploader);
87 };
88 
89 }  // namespace content
90 
91 #endif  // CONTENT_BROWSER_TRACING_TRACE_UPLOADER_H_
92