OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ | |
6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/macros.h" | |
16 #include "base/threading/thread_checker.h" | |
17 #include "chrome/browser/media/webrtc_logging_handler_host.h" | |
18 #include "net/url_request/url_fetcher_delegate.h" | |
19 | |
20 class Profile; | |
21 | |
22 namespace base { | |
23 class SharedMemory; | |
24 } | |
25 | |
26 namespace net { | |
27 class URLFetcher; | |
28 } | |
29 | |
30 typedef struct z_stream_s z_stream; | |
31 | |
32 // Used when uploading is done to perform post-upload actions. |log_path| is | |
33 // also used pre-upload. | |
34 struct WebRtcLogUploadDoneData : public WebRtcLogPaths { | |
35 WebRtcLogUploadDoneData(); | |
36 WebRtcLogUploadDoneData(const WebRtcLogUploadDoneData& other); | |
37 ~WebRtcLogUploadDoneData(); | |
38 | |
39 WebRtcLoggingHandlerHost::UploadDoneCallback callback; | |
40 scoped_refptr<WebRtcLoggingHandlerHost> host; | |
41 std::string local_log_id; | |
42 }; | |
43 | |
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 { | |
49 public: | |
50 WebRtcLogUploader(); | |
51 ~WebRtcLogUploader() override; | |
52 | |
53 // Returns true is number of logs limit is not reached yet. Increases log | |
54 // count if true is returned. Must be called before UploadLog(). | |
55 bool ApplyForStartLogging(); | |
56 | |
57 // Notifies that logging has stopped and that the log should not be uploaded. | |
58 // Decreases log count. May only be called if permission to log has been | |
59 // granted by calling ApplyForStartLogging() and getting true in return. | |
60 // After this function has been called, a new permission must be granted. | |
61 // Call either this function or LoggingStoppedDoUpload(). | |
62 void LoggingStoppedDontUpload(); | |
63 | |
64 // Notifies that that logging has stopped and that the log should be uploaded. | |
65 // Decreases log count. May only be called if permission to log has been | |
66 // granted by calling ApplyForStartLogging() and getting true in return. After | |
67 // this function has been called, a new permission must be granted. Call | |
68 // either this function or LoggingStoppedDontUpload(). | |
69 // |upload_done_data.local_log_id| is set and used internally and should be | |
70 // left empty. | |
71 void LoggingStoppedDoUpload(std::unique_ptr<WebRtcLogBuffer> log_buffer, | |
72 std::unique_ptr<MetaDataMap> meta_data, | |
73 const WebRtcLogUploadDoneData& upload_done_data); | |
74 | |
75 // Uploads a previously stored log (see LoggingStoppedDoStore()). | |
76 void UploadStoredLog(const WebRtcLogUploadDoneData& upload_data); | |
77 | |
78 // Similarly to LoggingStoppedDoUpload(), we store the log in compressed | |
79 // format on disk but add the option to specify a unique |log_id| for later | |
80 // identification and potential upload. | |
81 void LoggingStoppedDoStore( | |
82 const WebRtcLogPaths& log_paths, | |
83 const std::string& log_id, | |
84 std::unique_ptr<WebRtcLogBuffer> log_buffer, | |
85 std::unique_ptr<MetaDataMap> meta_data, | |
86 const WebRtcLoggingHandlerHost::GenericDoneCallback& done_callback); | |
87 | |
88 // Cancels URL fetcher operation by deleting all URL fetchers. This cancels | |
89 // any pending uploads and releases SystemURLRequestContextGetter references. | |
90 // Sets |shutting_down_| which prevent new fetchers to be created. | |
91 void StartShutdown(); | |
92 | |
93 // For testing purposes. If called, the multipart will not be uploaded, but | |
94 // written to |post_data_| instead. | |
95 void OverrideUploadWithBufferForTesting(std::string* post_data) { | |
96 DCHECK((post_data && !post_data_) || (!post_data && post_data_)); | |
97 post_data_ = post_data; | |
98 } | |
99 | |
100 private: | |
101 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest, | |
102 AddLocallyStoredLogInfoToUploadListFile); | |
103 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest, | |
104 AddUploadedLogInfoToUploadListFile); | |
105 | |
106 // net::URLFetcherDelegate implementation. | |
107 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
108 void OnURLFetchUploadProgress(const net::URLFetcher* source, | |
109 int64_t current, | |
110 int64_t total) override; | |
111 | |
112 // Sets up a multipart body to be uploaded. The body is produced according | |
113 // to RFC 2046. | |
114 void SetupMultipart(std::string* post_data, | |
115 const std::string& compressed_log, | |
116 const base::FilePath& incoming_rtp_dump, | |
117 const base::FilePath& outgoing_rtp_dump, | |
118 const std::map<std::string, std::string>& meta_data); | |
119 | |
120 void CompressLog(std::string* compressed_log, | |
121 WebRtcLogBuffer* buffer); | |
122 | |
123 void ResizeForNextOutput(std::string* compressed_log, | |
124 z_stream* stream); | |
125 | |
126 void UploadCompressedLog(const WebRtcLogUploadDoneData& upload_done_data, | |
127 std::unique_ptr<std::string> post_data); | |
128 | |
129 // A couple of helper functions due to having to hop to the UI thread | |
130 // to fetch the system_request_context and back again to the IO thread. | |
131 void SetRequestContextOnUIThread( | |
132 net::URLFetcher* url_fetcher, const WebRtcLogUploadDoneData& data); | |
133 void StartAndTrackRequestContext( | |
134 net::URLFetcher* url_fetcher, const WebRtcLogUploadDoneData& data); | |
135 | |
136 void DecreaseLogCount(); | |
137 | |
138 void ShutdownOnIOThread(); | |
139 | |
140 // Must be called on the FILE thread. | |
141 void WriteCompressedLogToFile(const std::string& compressed_log, | |
142 const base::FilePath& log_file_path); | |
143 | |
144 void PrepareMultipartPostData( | |
145 const std::string& compressed_log, | |
146 std::unique_ptr<MetaDataMap> meta_data, | |
147 const WebRtcLogUploadDoneData& upload_done_data); | |
148 | |
149 // Append information (upload time, report ID and local ID) about a log to a | |
150 // log list file, limited to |kLogListLimitLines| entries. This list is used | |
151 // for viewing the logs under chrome://webrtc-logs, see WebRtcLogUploadList. | |
152 // The list has the format | |
153 // upload_time,report_id,local_id | |
154 // upload_time,report_id,local_id | |
155 // etc. | |
156 // where each line represents a log. "upload_time" is the time when the log | |
157 // was uploaded in Unix time. "report_id" is the ID reported back by the | |
158 // server. "local_id" is the ID for the locally stored log. It's the time | |
159 // stored in Unix time and it's also used as file name. | |
160 // AddLocallyStoredLogInfoToUploadListFile() will first be called, | |
161 // "upload_time" and "report_id" is the left empty in the entry written to the | |
162 // list file. If uploading is successful, AddUploadedLogInfoToUploadListFile() | |
163 // is called and those empty items are filled out. | |
164 // Must be called on the FILE thread. | |
165 void AddLocallyStoredLogInfoToUploadListFile( | |
166 const base::FilePath& upload_list_path, | |
167 const std::string& local_log_id); | |
168 void AddUploadedLogInfoToUploadListFile( | |
169 const base::FilePath& upload_list_path, | |
170 const std::string& local_log_id, | |
171 const std::string& report_id); | |
172 | |
173 void NotifyUploadDone(int response_code, | |
174 const std::string& report_id, | |
175 const WebRtcLogUploadDoneData& upload_done_data); | |
176 | |
177 // This is the UI thread for Chromium. Some other thread for tests. | |
178 base::ThreadChecker create_thread_checker_; | |
179 | |
180 // This is the FILE thread for Chromium. Some other thread for tests. | |
181 base::ThreadChecker file_thread_checker_; | |
182 | |
183 // Keeps track of number of currently open logs. Must be accessed on the IO | |
184 // thread. | |
185 int log_count_; | |
186 | |
187 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed | |
188 // on the FILE thread. | |
189 std::string* post_data_; | |
190 | |
191 typedef std::map<const net::URLFetcher*, WebRtcLogUploadDoneData> | |
192 UploadDoneDataMap; | |
193 // Only accessed on the IO thread. | |
194 UploadDoneDataMap upload_done_data_; | |
195 | |
196 // When shutting down, don't create new URLFetchers. | |
197 bool shutting_down_; | |
198 | |
199 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader); | |
200 }; | |
201 | |
202 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ | |
OLD | NEW |