| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ | 6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ |
| 7 | 7 |
| 8 #include <map> |
| 8 #include <string> | 9 #include <string> |
| 9 #include <vector> | |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
| 15 #include "chrome/browser/media/webrtc_logging_handler_host.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 class SharedMemory; | 19 class SharedMemory; |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 class URLFetcher; | 23 class URLFetcher; |
| 23 class URLRequestContextGetter; | 24 class URLRequestContextGetter; |
| 24 } | 25 } |
| 25 | 26 |
| 26 typedef struct z_stream_s z_stream; | 27 typedef struct z_stream_s z_stream; |
| 27 | 28 |
| 29 // Used when uploading is done to inform about that it's done. |
| 30 typedef struct { |
| 31 WebRtcLoggingHandlerHost::UploadDoneCallback callback; |
| 32 scoped_refptr<WebRtcLoggingHandlerHost> host; |
| 33 } WebRtcLogUploadDoneData; |
| 34 |
| 28 class WebRtcLogURLRequestContextGetter; | 35 class WebRtcLogURLRequestContextGetter; |
| 29 | 36 |
| 30 // WebRtcLogUploader uploads WebRTC logs, keeps count of how many logs have | 37 // WebRtcLogUploader uploads WebRTC logs, keeps count of how many logs have |
| 31 // been started and denies further logs if a limit is reached. It also adds | 38 // been started and denies further logs if a limit is reached. It also adds |
| 32 // the timestamp and report ID of the uploded log to a text file. There must | 39 // the timestamp and report ID of the uploded log to a text file. There must |
| 33 // only be one object of this type. | 40 // only be one object of this type. |
| 34 class WebRtcLogUploader : public net::URLFetcherDelegate { | 41 class WebRtcLogUploader : public net::URLFetcherDelegate { |
| 35 public: | 42 public: |
| 36 WebRtcLogUploader(); | 43 WebRtcLogUploader(); |
| 37 virtual ~WebRtcLogUploader(); | 44 virtual ~WebRtcLogUploader(); |
| 38 | 45 |
| 39 // net::URLFetcherDelegate implementation. | 46 // net::URLFetcherDelegate implementation. |
| 40 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 47 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 41 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source, | 48 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source, |
| 42 int64 current, int64 total) OVERRIDE; | 49 int64 current, int64 total) OVERRIDE; |
| 43 | 50 |
| 44 // Returns true is number of logs limit is not reached yet. Increases log | 51 // Returns true is number of logs limit is not reached yet. Increases log |
| 45 // count if true is returned. Must be called before UploadLog(). | 52 // count if true is returned. Must be called before UploadLog(). |
| 46 bool ApplyForStartLogging(); | 53 bool ApplyForStartLogging(); |
| 47 | 54 |
| 48 // Uploads log and decreases log count. May only be called if permission to | 55 // Notifies that logging has stopped and that the log should not be uploaded. |
| 49 // to log has been granted by calling ApplyForStartLogging() and getting true | 56 // Decreases log count. May only be called if permission to log has been |
| 50 // in return. After UploadLog has been called, a new permission must be | 57 // granted by calling ApplyForStartLogging() and getting true in return. |
| 51 // granted. | 58 // After this function has been called, a new permission must be granted. |
| 52 void UploadLog(net::URLRequestContextGetter* request_context, | 59 // Call either this function or LoggingStoppedDoUpload(). |
| 53 scoped_ptr<base::SharedMemory> shared_memory, | 60 void LoggingStoppedDontUpload(); |
| 54 uint32 length, | 61 |
| 55 const std::string& app_session_id, | 62 // Notifies that that logging has stopped and that the log should be uploaded. |
| 56 const std::string& app_url); | 63 // Decreases log count. May only be called if permission to log has been |
| 64 // granted by calling ApplyForStartLogging() and getting true in return. After |
| 65 // this function has been called, a new permission must be granted. Call |
| 66 // either this function or LoggingStoppedDontUpload(). |
| 67 void LoggingStoppedDoUpload( |
| 68 net::URLRequestContextGetter* request_context, |
| 69 scoped_ptr<base::SharedMemory> shared_memory, |
| 70 uint32 length, |
| 71 const std::map<std::string, std::string>& meta_data, |
| 72 const WebRtcLogUploadDoneData& upload_done_data); |
| 57 | 73 |
| 58 // For testing purposes. If called, the multipart will not be uploaded, but | 74 // For testing purposes. If called, the multipart will not be uploaded, but |
| 59 // written to |post_data_| instead. | 75 // written to |post_data_| instead. |
| 60 void OverrideUploadWithBufferForTesting(std::string* post_data) { | 76 void OverrideUploadWithBufferForTesting(std::string* post_data) { |
| 61 post_data_ = post_data; | 77 post_data_ = post_data; |
| 62 } | 78 } |
| 63 | 79 |
| 64 private: | 80 private: |
| 65 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest, | 81 FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest, |
| 66 AddUploadedLogInfoToUploadListFile); | 82 AddUploadedLogInfoToUploadListFile); |
| 67 | 83 |
| 68 // Sets up a multipart body to be uploaded. The body is produced according | 84 // Sets up a multipart body to be uploaded. The body is produced according |
| 69 // to RFC 2046. | 85 // to RFC 2046. |
| 70 void SetupMultipart(std::string* post_data, uint8* log_buffer, | 86 void SetupMultipart(std::string* post_data, uint8* log_buffer, |
| 71 uint32 log_buffer_length, | 87 uint32 log_buffer_length, |
| 72 const std::string& app_session_id, | 88 const std::map<std::string, std::string>& meta_data); |
| 73 const std::string& app_url); | |
| 74 | 89 |
| 75 void AddLogData(std::string* post_data, uint8* log_buffer, | 90 void AddLogData(std::string* post_data, uint8* log_buffer, |
| 76 uint32 log_buffer_length); | 91 uint32 log_buffer_length); |
| 77 void CompressLog(std::string* post_data, uint8* input, uint32 input_size); | 92 void CompressLog(std::string* post_data, uint8* input, uint32 input_size); |
| 78 void ResizeForNextOutput(std::string* post_data, z_stream* stream); | 93 void ResizeForNextOutput(std::string* post_data, z_stream* stream); |
| 79 void DecreaseLogCount(); | 94 void DecreaseLogCount(); |
| 80 | 95 |
| 81 // Append information (time and report ID) about this uploaded log to a log | 96 // Append information (time and report ID) about this uploaded log to a log |
| 82 // list file, limited to |kLogListLimitLines| entries. This list is used for | 97 // list file, limited to |kLogListLimitLines| entries. This list is used for |
| 83 // viewing the uploaded logs under chrome://webrtc-logs, see | 98 // viewing the uploaded logs under chrome://webrtc-logs, see |
| 84 // WebRtcLogUploadList. The list has the format | 99 // WebRtcLogUploadList. The list has the format |
| 85 // time,id | 100 // time,id |
| 86 // time,id | 101 // time,id |
| 87 // etc. | 102 // etc. |
| 88 // where each line represents an uploaded log and "time" is Unix time. | 103 // where each line represents an uploaded log and "time" is Unix time. |
| 89 void AddUploadedLogInfoToUploadListFile(const std::string& report_id); | 104 void AddUploadedLogInfoToUploadListFile(const std::string& report_id); |
| 90 | 105 |
| 91 void SetUploadPathForTesting(const base::FilePath& path) { | 106 void SetUploadPathForTesting(const base::FilePath& path) { |
| 92 upload_list_path_ = path; | 107 upload_list_path_ = path; |
| 93 } | 108 } |
| 94 | 109 |
| 95 int log_count_; | 110 int log_count_; |
| 96 base::FilePath upload_list_path_; | 111 base::FilePath upload_list_path_; |
| 97 | 112 |
| 98 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed | 113 // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed |
| 99 // on the FILE thread. | 114 // on the FILE thread. |
| 100 std::string* post_data_; | 115 std::string* post_data_; |
| 101 | 116 |
| 117 typedef std::map<const net::URLFetcher*, WebRtcLogUploadDoneData> |
| 118 UploadDoneDataMap; |
| 119 UploadDoneDataMap upload_done_data_; |
| 120 |
| 102 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader); | 121 DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader); |
| 103 }; | 122 }; |
| 104 | 123 |
| 105 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ | 124 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_ |
| OLD | NEW |