1 // Copyright 2013 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 CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_
6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/platform_file.h"
15 #include "base/threading/thread_checker.h"
16 #include "chrome/browser/media/webrtc_logging_handler_host.h"
17 #include "net/url_request/url_fetcher_delegate.h"
27 class URLRequestContextGetter
;
30 typedef struct z_stream_s z_stream
;
32 // Used when uploading is done to perform post-upload actions.
34 base::FilePath upload_list_path
;
35 WebRtcLoggingHandlerHost::UploadDoneCallback callback
;
36 scoped_refptr
<WebRtcLoggingHandlerHost
> host
;
37 } WebRtcLogUploadDoneData
;
39 class WebRtcLogURLRequestContextGetter
;
41 // WebRtcLogUploader uploads WebRTC logs, keeps count of how many logs have
42 // been started and denies further logs if a limit is reached. It also adds
43 // the timestamp and report ID of the uploded log to a text file. There must
44 // only be one object of this type.
45 class WebRtcLogUploader
: public net::URLFetcherDelegate
{
48 virtual ~WebRtcLogUploader();
50 // net::URLFetcherDelegate implementation.
51 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
52 virtual void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
53 int64 current
, int64 total
) OVERRIDE
;
55 // Returns true is number of logs limit is not reached yet. Increases log
56 // count if true is returned. Must be called before UploadLog().
57 bool ApplyForStartLogging();
59 // Notifies that logging has stopped and that the log should not be uploaded.
60 // Decreases log count. May only be called if permission to log has been
61 // granted by calling ApplyForStartLogging() and getting true in return.
62 // After this function has been called, a new permission must be granted.
63 // Call either this function or LoggingStoppedDoUpload().
64 void LoggingStoppedDontUpload();
66 // Notifies that that logging has stopped and that the log should be uploaded.
67 // Decreases log count. May only be called if permission to log has been
68 // granted by calling ApplyForStartLogging() and getting true in return. After
69 // this function has been called, a new permission must be granted. Call
70 // either this function or LoggingStoppedDontUpload().
71 void LoggingStoppedDoUpload(
72 net::URLRequestContextGetter
* request_context
,
73 scoped_ptr
<unsigned char[]> log_buffer
,
75 const std::map
<std::string
, std::string
>& meta_data
,
76 const WebRtcLogUploadDoneData
& upload_done_data
);
78 // For testing purposes. If called, the multipart will not be uploaded, but
79 // written to |post_data_| instead.
80 void OverrideUploadWithBufferForTesting(std::string
* post_data
) {
81 post_data_
= post_data
;
85 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest
,
86 AddUploadedLogInfoToUploadListFile
);
88 // Sets up a multipart body to be uploaded. The body is produced according
90 void SetupMultipart(std::string
* post_data
, uint8
* log_buffer
,
91 uint32 log_buffer_length
,
92 const std::map
<std::string
, std::string
>& meta_data
);
94 void AddLogData(std::string
* post_data
, uint8
* log_buffer
,
95 uint32 log_buffer_length
);
96 void CompressLog(std::string
* post_data
, uint8
* input
, uint32 input_size
);
97 void ResizeForNextOutput(std::string
* post_data
, z_stream
* stream
);
99 void CreateAndStartURLFetcher(
100 scoped_refptr
<net::URLRequestContextGetter
> request_context
,
101 const WebRtcLogUploadDoneData
& upload_done_data
,
102 scoped_ptr
<std::string
> post_data
);
104 void DecreaseLogCount();
106 // Append information (time and report ID) about this uploaded log to a log
107 // list file, limited to |kLogListLimitLines| entries. This list is used for
108 // viewing the uploaded logs under chrome://webrtc-logs, see
109 // WebRtcLogUploadList. The list has the format
113 // where each line represents an uploaded log and "time" is Unix time.
114 void AddUploadedLogInfoToUploadListFile(
115 const base::FilePath
& upload_list_path
,
116 const std::string
& report_id
);
118 void NotifyUploadDone(int response_code
,
119 const std::string
& report_id
,
120 const WebRtcLogUploadDoneData
& upload_done_data
);
122 // This is the UI thread for Chromium. Some other thread for tests.
123 base::ThreadChecker create_thread_checker_
;
125 // This is the FILE thread for Chromium. Some other thread for tests.
126 base::ThreadChecker file_thread_checker_
;
130 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed
131 // on the FILE thread.
132 std::string
* post_data_
;
134 typedef std::map
<const net::URLFetcher
*, WebRtcLogUploadDoneData
>
136 // Only accessed on the UI thread.
137 UploadDoneDataMap upload_done_data_
;
139 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader
);
142 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_