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_
12 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/threading/thread_checker.h"
15 #include "chrome/browser/media/webrtc_logging_handler_host.h"
16 #include "net/url_request/url_fetcher_delegate.h"
28 typedef struct z_stream_s z_stream
;
30 // Used when uploading is done to perform post-upload actions. |log_path| is
31 // also used pre-upload.
32 struct WebRtcLogUploadDoneData
: public WebRtcLogPaths
{
33 WebRtcLogUploadDoneData();
34 ~WebRtcLogUploadDoneData();
36 WebRtcLoggingHandlerHost::UploadDoneCallback callback
;
37 scoped_refptr
<WebRtcLoggingHandlerHost
> host
;
38 std::string local_log_id
;
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 ~WebRtcLogUploader() override
;
50 // net::URLFetcherDelegate implementation.
51 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
52 void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
54 int64 total
) override
;
56 // Returns true is number of logs limit is not reached yet. Increases log
57 // count if true is returned. Must be called before UploadLog().
58 bool ApplyForStartLogging();
60 // Notifies that logging has stopped and that the log should not be uploaded.
61 // Decreases log count. May only be called if permission to log has been
62 // granted by calling ApplyForStartLogging() and getting true in return.
63 // After this function has been called, a new permission must be granted.
64 // Call either this function or LoggingStoppedDoUpload().
65 void LoggingStoppedDontUpload();
67 // Notifies that that logging has stopped and that the log should be uploaded.
68 // Decreases log count. May only be called if permission to log has been
69 // granted by calling ApplyForStartLogging() and getting true in return. After
70 // this function has been called, a new permission must be granted. Call
71 // either this function or LoggingStoppedDontUpload().
72 // |upload_done_data.local_log_id| is set and used internally and should be
74 void LoggingStoppedDoUpload(
75 scoped_ptr
<WebRtcLogBuffer
> log_buffer
,
76 scoped_ptr
<MetaDataMap
> meta_data
,
77 const WebRtcLogUploadDoneData
& upload_done_data
);
79 // Uploads a previously stored log (see LoggingStoppedDoStore()).
80 void UploadStoredLog(const WebRtcLogUploadDoneData
& upload_data
);
82 // Similarly to LoggingStoppedDoUpload(), we store the log in compressed
83 // format on disk but add the option to specify a unique |log_id| for later
84 // identification and potential upload.
85 void LoggingStoppedDoStore(
86 const WebRtcLogPaths
& log_paths
,
87 const std::string
& log_id
,
88 scoped_ptr
<WebRtcLogBuffer
> log_buffer
,
89 scoped_ptr
<MetaDataMap
> meta_data
,
90 const WebRtcLoggingHandlerHost::GenericDoneCallback
& done_callback
);
92 // Cancels URL fetcher operation by deleting all URL fetchers. This cancels
93 // any pending uploads and releases SystemURLRequestContextGetter references.
94 // Sets |shutting_down_| which prevent new fetchers to be created.
97 // For testing purposes. If called, the multipart will not be uploaded, but
98 // written to |post_data_| instead.
99 void OverrideUploadWithBufferForTesting(std::string
* post_data
) {
100 DCHECK((post_data
&& !post_data_
) || (!post_data
&& post_data_
));
101 post_data_
= post_data
;
105 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest
,
106 AddLocallyStoredLogInfoToUploadListFile
);
107 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest
,
108 AddUploadedLogInfoToUploadListFile
);
110 // Sets up a multipart body to be uploaded. The body is produced according
112 void SetupMultipart(std::string
* post_data
,
113 const std::string
& compressed_log
,
114 const base::FilePath
& incoming_rtp_dump
,
115 const base::FilePath
& outgoing_rtp_dump
,
116 const std::map
<std::string
, std::string
>& meta_data
);
118 void CompressLog(std::string
* compressed_log
,
119 WebRtcLogBuffer
* buffer
);
121 void ResizeForNextOutput(std::string
* compressed_log
,
124 void CreateAndStartURLFetcher(
125 const WebRtcLogUploadDoneData
& upload_done_data
,
126 scoped_ptr
<std::string
> post_data
);
128 void DecreaseLogCount();
130 // Must be called on the FILE thread.
131 void WriteCompressedLogToFile(const std::string
& compressed_log
,
132 const base::FilePath
& log_file_path
);
134 void UploadCompressedLog(
135 const std::string
& compressed_log
,
136 scoped_ptr
<MetaDataMap
> meta_data
,
137 const WebRtcLogUploadDoneData
& upload_done_data
);
139 // Append information (upload time, report ID and local ID) about a log to a
140 // log list file, limited to |kLogListLimitLines| entries. This list is used
141 // for viewing the logs under chrome://webrtc-logs, see WebRtcLogUploadList.
142 // The list has the format
143 // upload_time,report_id,local_id
144 // upload_time,report_id,local_id
146 // where each line represents a log. "upload_time" is the time when the log
147 // was uploaded in Unix time. "report_id" is the ID reported back by the
148 // server. "local_id" is the ID for the locally stored log. It's the time
149 // stored in Unix time and it's also used as file name.
150 // AddLocallyStoredLogInfoToUploadListFile() will first be called,
151 // "upload_time" and "report_id" is the left empty in the entry written to the
152 // list file. If uploading is successful, AddUploadedLogInfoToUploadListFile()
153 // is called and those empty items are filled out.
154 // Must be called on the FILE thread.
155 void AddLocallyStoredLogInfoToUploadListFile(
156 const base::FilePath
& upload_list_path
,
157 const std::string
& local_log_id
);
158 void AddUploadedLogInfoToUploadListFile(
159 const base::FilePath
& upload_list_path
,
160 const std::string
& local_log_id
,
161 const std::string
& report_id
);
163 void NotifyUploadDone(int response_code
,
164 const std::string
& report_id
,
165 const WebRtcLogUploadDoneData
& upload_done_data
);
167 // This is the UI thread for Chromium. Some other thread for tests.
168 base::ThreadChecker create_thread_checker_
;
170 // This is the FILE thread for Chromium. Some other thread for tests.
171 base::ThreadChecker file_thread_checker_
;
173 // Keeps track of number of currently open logs. Must be accessed on the UI
177 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed
178 // on the FILE thread.
179 std::string
* post_data_
;
181 typedef std::map
<const net::URLFetcher
*, WebRtcLogUploadDoneData
>
183 // Only accessed on the UI thread.
184 UploadDoneDataMap upload_done_data_
;
186 // When shutting down, don't create new URLFetchers.
189 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader
);
192 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_