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 "chrome/browser/media/webrtc_logging_handler_host.h"
16 #include "net/url_request/url_fetcher_delegate.h"
26 class URLRequestContextGetter;
29 typedef struct z_stream_s z_stream;
31 // Used when uploading is done to perform post-upload actions.
34 WebRtcLoggingHandlerHost::UploadDoneCallback callback;
35 scoped_refptr<WebRtcLoggingHandlerHost> host;
36 } WebRtcLogUploadDoneData;
38 class WebRtcLogURLRequestContextGetter;
40 // WebRtcLogUploader uploads WebRTC logs, keeps count of how many logs have
41 // been started and denies further logs if a limit is reached. It also adds
42 // the timestamp and report ID of the uploded log to a text file. There must
43 // only be one object of this type.
44 class WebRtcLogUploader : public net::URLFetcherDelegate {
47 virtual ~WebRtcLogUploader();
49 // net::URLFetcherDelegate implementation.
50 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
51 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source,
52 int64 current, int64 total) OVERRIDE;
54 // Returns true is number of logs limit is not reached yet. Increases log
55 // count if true is returned. Must be called before UploadLog().
56 bool ApplyForStartLogging();
58 // Notifies that logging has stopped and that the log should not be uploaded.
59 // Decreases log count. May only be called if permission to log has been
60 // granted by calling ApplyForStartLogging() and getting true in return.
61 // After this function has been called, a new permission must be granted.
62 // Call either this function or LoggingStoppedDoUpload().
63 void LoggingStoppedDontUpload();
65 // Notifies that that logging has stopped and that the log should be uploaded.
66 // Decreases log count. May only be called if permission to log has been
67 // granted by calling ApplyForStartLogging() and getting true in return. After
68 // this function has been called, a new permission must be granted. Call
69 // either this function or LoggingStoppedDontUpload().
70 void LoggingStoppedDoUpload(
71 net::URLRequestContextGetter* request_context,
72 scoped_ptr<base::SharedMemory> shared_memory,
74 const std::map<std::string, std::string>& meta_data,
75 const WebRtcLogUploadDoneData& upload_done_data);
77 // For testing purposes. If called, the multipart will not be uploaded, but
78 // written to |post_data_| instead.
79 void OverrideUploadWithBufferForTesting(std::string* post_data) {
80 post_data_ = post_data;
84 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest,
85 AddUploadedLogInfoToUploadListFile);
87 // Sets up a multipart body to be uploaded. The body is produced according
89 void SetupMultipart(std::string* post_data, uint8* log_buffer,
90 uint32 log_buffer_length,
91 const std::map<std::string, std::string>& meta_data);
93 void AddLogData(std::string* post_data, uint8* log_buffer,
94 uint32 log_buffer_length);
95 void CompressLog(std::string* post_data, uint8* input, uint32 input_size);
96 void ResizeForNextOutput(std::string* post_data, z_stream* stream);
97 void DecreaseLogCount();
99 // Append information (time and report ID) about this uploaded log to a log
100 // list file, limited to |kLogListLimitLines| entries. This list is used for
101 // viewing the uploaded logs under chrome://webrtc-logs, see
102 // WebRtcLogUploadList. The list has the format
106 // where each line represents an uploaded log and "time" is Unix time.
107 void AddUploadedLogInfoToUploadListFile(
108 const base::FilePath& upload_list_path,
109 const std::string& report_id);
111 void NotifyUploadDone(int response_code,
112 const std::string& report_id,
113 const WebRtcLogUploadDoneData& upload_done_data);
117 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed
118 // on the FILE thread.
119 std::string* post_data_;
121 typedef std::map<const net::URLFetcher*, WebRtcLogUploadDoneData>
123 UploadDoneDataMap upload_done_data_;
125 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader);
128 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_