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
{
33 WebRtcLogUploadDoneData();
34 ~WebRtcLogUploadDoneData();
36 base::FilePath log_path
;
37 base::FilePath incoming_rtp_dump
;
38 base::FilePath outgoing_rtp_dump
;
39 WebRtcLoggingHandlerHost::UploadDoneCallback callback
;
40 scoped_refptr
<WebRtcLoggingHandlerHost
> host
;
41 std::string local_log_id
;
44 // WebRtcLogUploader uploads WebRTC logs, keeps count of how many logs have
45 // been started and denies further logs if a limit is reached. It also adds
46 // the timestamp and report ID of the uploded log to a text file. There must
47 // only be one object of this type.
48 class WebRtcLogUploader
: public net::URLFetcherDelegate
{
51 ~WebRtcLogUploader() override
;
53 // net::URLFetcherDelegate implementation.
54 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
55 void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
57 int64 total
) override
;
59 // Returns true is number of logs limit is not reached yet. Increases log
60 // count if true is returned. Must be called before UploadLog().
61 bool ApplyForStartLogging();
63 // Notifies that logging has stopped and that the log should not be uploaded.
64 // Decreases log count. May only be called if permission to log has been
65 // granted by calling ApplyForStartLogging() and getting true in return.
66 // After this function has been called, a new permission must be granted.
67 // Call either this function or LoggingStoppedDoUpload().
68 void LoggingStoppedDontUpload();
70 // Notifies that that logging has stopped and that the log should be uploaded.
71 // Decreases log count. May only be called if permission to log has been
72 // granted by calling ApplyForStartLogging() and getting true in return. After
73 // this function has been called, a new permission must be granted. Call
74 // either this function or LoggingStoppedDontUpload().
75 // |upload_done_data.local_log_id| is set and used internally and should be
77 void LoggingStoppedDoUpload(
78 scoped_ptr
<unsigned char[]> log_buffer
,
80 const std::map
<std::string
, std::string
>& meta_data
,
81 const WebRtcLogUploadDoneData
& upload_done_data
);
83 // Cancels URL fetcher operation by deleting all URL fetchers. This cancels
84 // any pending uploads and releases SystemURLRequestContextGetter references.
85 // Sets |shutting_down_| which prevent new fetchers to be created.
88 // For testing purposes. If called, the multipart will not be uploaded, but
89 // written to |post_data_| instead.
90 void OverrideUploadWithBufferForTesting(std::string
* post_data
) {
91 DCHECK((post_data
&& !post_data_
) || (!post_data
&& post_data_
));
92 post_data_
= post_data
;
96 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest
,
97 AddLocallyStoredLogInfoToUploadListFile
);
98 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest
,
99 AddUploadedLogInfoToUploadListFile
);
101 // Sets up a multipart body to be uploaded. The body is produced according
103 void SetupMultipart(std::string
* post_data
,
104 const std::vector
<uint8
>& compressed_log
,
105 const base::FilePath
& incoming_rtp_dump
,
106 const base::FilePath
& outgoing_rtp_dump
,
107 const std::map
<std::string
, std::string
>& meta_data
);
109 void CompressLog(std::vector
<uint8
>* compressed_log
,
113 void ResizeForNextOutput(std::vector
<uint8
>* compressed_log
,
116 void CreateAndStartURLFetcher(
117 const WebRtcLogUploadDoneData
& upload_done_data
,
118 scoped_ptr
<std::string
> post_data
);
120 void DecreaseLogCount();
122 // Must be called on the FILE thread.
123 void WriteCompressedLogToFile(const std::vector
<uint8
>& compressed_log
,
124 const base::FilePath
& log_file_path
);
126 // Append information (upload time, report ID and local ID) about a log to a
127 // log list file, limited to |kLogListLimitLines| entries. This list is used
128 // for viewing the logs under chrome://webrtc-logs, see WebRtcLogUploadList.
129 // The list has the format
130 // upload_time,report_id,local_id
131 // upload_time,report_id,local_id
133 // where each line represents a log. "upload_time" is the time when the log
134 // was uploaded in Unix time. "report_id" is the ID reported back by the
135 // server. "local_id" is the ID for the locally stored log. It's the time
136 // stored in Unix time and it's also used as file name.
137 // AddLocallyStoredLogInfoToUploadListFile() will first be called,
138 // "upload_time" and "report_id" is the left empty in the entry written to the
139 // list file. If uploading is successful, AddUploadedLogInfoToUploadListFile()
140 // is called and those empty items are filled out.
141 // Must be called on the FILE thread.
142 void AddLocallyStoredLogInfoToUploadListFile(
143 const base::FilePath
& upload_list_path
,
144 const std::string
& local_log_id
);
145 void AddUploadedLogInfoToUploadListFile(
146 const base::FilePath
& upload_list_path
,
147 const std::string
& local_log_id
,
148 const std::string
& report_id
);
150 void NotifyUploadDone(int response_code
,
151 const std::string
& report_id
,
152 const WebRtcLogUploadDoneData
& upload_done_data
);
154 // This is the UI thread for Chromium. Some other thread for tests.
155 base::ThreadChecker create_thread_checker_
;
157 // This is the FILE thread for Chromium. Some other thread for tests.
158 base::ThreadChecker file_thread_checker_
;
160 // Keeps track of number of currently open logs. Must be accessed on the UI
164 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed
165 // on the FILE thread.
166 std::string
* post_data_
;
168 typedef std::map
<const net::URLFetcher
*, WebRtcLogUploadDoneData
>
170 // Only accessed on the UI thread.
171 UploadDoneDataMap upload_done_data_
;
173 // When shutting down, don't create new URLFetchers.
176 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader
);
179 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_