- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / webrtc_log_uploader.h
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 <map>
9 #include <string>
10
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"
17
18 class Profile;
19
20 namespace base {
21 class SharedMemory;
22 }
23
24 namespace net {
25 class URLFetcher;
26 class URLRequestContextGetter;
27 }
28
29 typedef struct z_stream_s z_stream;
30
31 // Used when uploading is done to perform post-upload actions.
32 typedef struct {
33   Profile* profile;
34   WebRtcLoggingHandlerHost::UploadDoneCallback callback;
35   scoped_refptr<WebRtcLoggingHandlerHost> host;
36 } WebRtcLogUploadDoneData;
37
38 class WebRtcLogURLRequestContextGetter;
39
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 {
45  public:
46   WebRtcLogUploader();
47   virtual ~WebRtcLogUploader();
48
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;
53
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();
57
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();
64
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,
73       uint32 length,
74       const std::map<std::string, std::string>& meta_data,
75       const WebRtcLogUploadDoneData& upload_done_data);
76
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;
81   }
82
83  private:
84   FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest,
85                            AddUploadedLogInfoToUploadListFile);
86
87   // Sets up a multipart body to be uploaded. The body is produced according
88   // to RFC 2046.
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);
92
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();
98
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
103   // time,id
104   // time,id
105   // etc.
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);
110
111   void NotifyUploadDone(int response_code,
112                         const std::string& report_id,
113                         const WebRtcLogUploadDoneData& upload_done_data);
114
115   int log_count_;
116
117   // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed
118   // on the FILE thread.
119   std::string* post_data_;
120
121   typedef std::map<const net::URLFetcher*, WebRtcLogUploadDoneData>
122       UploadDoneDataMap;
123   UploadDoneDataMap upload_done_data_;
124
125   DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader);
126 };
127
128 #endif  // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_