Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / media / webrtc_log_uploader.h
blob40d95547bf78a001dc88f6b36d3810394249ea10
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
18 class Profile;
20 namespace base {
21 class SharedMemory;
24 namespace net {
25 class URLFetcher;
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 WebRtcLoggingHandlerHost::UploadDoneCallback callback;
38 scoped_refptr<WebRtcLoggingHandlerHost> host;
39 std::string local_log_id;
42 // WebRtcLogUploader uploads WebRTC logs, keeps count of how many logs have
43 // been started and denies further logs if a limit is reached. It also adds
44 // the timestamp and report ID of the uploded log to a text file. There must
45 // only be one object of this type.
46 class WebRtcLogUploader : public net::URLFetcherDelegate {
47 public:
48 WebRtcLogUploader();
49 virtual ~WebRtcLogUploader();
51 // net::URLFetcherDelegate implementation.
52 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
53 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source,
54 int64 current, 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
73 // left empty.
74 void LoggingStoppedDoUpload(
75 scoped_ptr<unsigned char[]> log_buffer,
76 uint32 length,
77 const std::map<std::string, std::string>& meta_data,
78 const WebRtcLogUploadDoneData& upload_done_data);
80 // Cancels URL fetcher operation by deleting all URL fetchers. This cancels
81 // any pending uploads and releases SystemURLRequestContextGetter references.
82 // Sets |shutting_down_| which prevent new fetchers to be created.
83 void StartShutdown();
85 // For testing purposes. If called, the multipart will not be uploaded, but
86 // written to |post_data_| instead.
87 void OverrideUploadWithBufferForTesting(std::string* post_data) {
88 DCHECK((post_data && !post_data_) || (!post_data && post_data_));
89 post_data_ = post_data;
92 private:
93 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest,
94 AddLocallyStoredLogInfoToUploadListFile);
95 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest,
96 AddUploadedLogInfoToUploadListFile);
98 // Sets up a multipart body to be uploaded. The body is produced according
99 // to RFC 2046.
100 void SetupMultipart(std::string* post_data,
101 const std::vector<uint8>& compressed_log,
102 const std::map<std::string, std::string>& meta_data);
104 // Adds |compressed_log| to |post_data|.
105 void AddLogData(std::string* post_data,
106 const std::vector<uint8>& compressed_log);
108 void CompressLog(std::vector<uint8>* compressed_log,
109 uint8* input,
110 uint32 input_size);
112 void ResizeForNextOutput(std::vector<uint8>* compressed_log,
113 z_stream* stream);
115 void CreateAndStartURLFetcher(
116 const WebRtcLogUploadDoneData& upload_done_data,
117 scoped_ptr<std::string> post_data);
119 void DecreaseLogCount();
121 // Must be called on the FILE thread.
122 void WriteCompressedLogToFile(const std::vector<uint8>& compressed_log,
123 const base::FilePath& log_file_path);
125 // Append information (upload time, report ID and local ID) about a log to a
126 // log list file, limited to |kLogListLimitLines| entries. This list is used
127 // for viewing the logs under chrome://webrtc-logs, see WebRtcLogUploadList.
128 // The list has the format
129 // upload_time,report_id,local_id
130 // upload_time,report_id,local_id
131 // etc.
132 // where each line represents a log. "upload_time" is the time when the log
133 // was uploaded in Unix time. "report_id" is the ID reported back by the
134 // server. "local_id" is the ID for the locally stored log. It's the time
135 // stored in Unix time and it's also used as file name.
136 // AddLocallyStoredLogInfoToUploadListFile() will first be called,
137 // "upload_time" and "report_id" is the left empty in the entry written to the
138 // list file. If uploading is successful, AddUploadedLogInfoToUploadListFile()
139 // is called and those empty items are filled out.
140 // Must be called on the FILE thread.
141 void AddLocallyStoredLogInfoToUploadListFile(
142 const base::FilePath& upload_list_path,
143 const std::string& local_log_id);
144 void AddUploadedLogInfoToUploadListFile(
145 const base::FilePath& upload_list_path,
146 const std::string& local_log_id,
147 const std::string& report_id);
149 void NotifyUploadDone(int response_code,
150 const std::string& report_id,
151 const WebRtcLogUploadDoneData& upload_done_data);
153 // This is the UI thread for Chromium. Some other thread for tests.
154 base::ThreadChecker create_thread_checker_;
156 // This is the FILE thread for Chromium. Some other thread for tests.
157 base::ThreadChecker file_thread_checker_;
159 // Keeps track of number of currently open logs. Must be accessed on the UI
160 // thread.
161 int log_count_;
163 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed
164 // on the FILE thread.
165 std::string* post_data_;
167 typedef std::map<const net::URLFetcher*, WebRtcLogUploadDoneData>
168 UploadDoneDataMap;
169 // Only accessed on the UI thread.
170 UploadDoneDataMap upload_done_data_;
172 // When shutting down, don't create new URLFetchers.
173 bool shutting_down_;
175 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader);
178 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_