Home | History | Annotate | Download | only in media
      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 <string>
      9 #include <vector>
     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 "net/url_request/url_fetcher_delegate.h"
     16 
     17 namespace base {
     18 class SharedMemory;
     19 }
     20 
     21 namespace net {
     22 class URLFetcher;
     23 class URLRequestContextGetter;
     24 }
     25 
     26 typedef struct z_stream_s z_stream;
     27 
     28 class WebRtcLogURLRequestContextGetter;
     29 
     30 // 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
     32 // the timestamp and report ID of the uploded log to a text file. There must
     33 // only be one object of this type.
     34 class WebRtcLogUploader : public net::URLFetcherDelegate {
     35  public:
     36   WebRtcLogUploader();
     37   virtual ~WebRtcLogUploader();
     38 
     39   // net::URLFetcherDelegate implementation.
     40   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
     41   virtual void OnURLFetchUploadProgress(const net::URLFetcher* source,
     42                                         int64 current, int64 total) OVERRIDE;
     43 
     44   // Returns true is number of logs limit is not reached yet. Increases log
     45   // count if true is returned. Must be called before UploadLog().
     46   bool ApplyForStartLogging();
     47 
     48   // Uploads log and decreases log count. May only be called if permission to
     49   // to log has been granted by calling ApplyForStartLogging() and getting true
     50   // in return. After UploadLog has been called, a new permission must be
     51   // granted.
     52   void UploadLog(net::URLRequestContextGetter* request_context,
     53                  scoped_ptr<base::SharedMemory> shared_memory,
     54                  uint32 length,
     55                  const std::string& app_session_id,
     56                  const std::string& app_url);
     57 
     58   // For testing purposes. If called, the multipart will not be uploaded, but
     59   // written to |post_data_| instead.
     60   void OverrideUploadWithBufferForTesting(std::string* post_data) {
     61     post_data_ = post_data;
     62   }
     63 
     64  private:
     65   FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest,
     66                            AddUploadedLogInfoToUploadListFile);
     67 
     68   // Sets up a multipart body to be uploaded. The body is produced according
     69   // to RFC 2046.
     70   void SetupMultipart(std::string* post_data, uint8* log_buffer,
     71                       uint32 log_buffer_length,
     72                       const std::string& app_session_id,
     73                       const std::string& app_url);
     74 
     75   void AddLogData(std::string* post_data, uint8* log_buffer,
     76                   uint32 log_buffer_length);
     77   void CompressLog(std::string* post_data, uint8* input, uint32 input_size);
     78   void ResizeForNextOutput(std::string* post_data, z_stream* stream);
     79   void DecreaseLogCount();
     80 
     81   // 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
     83   // viewing the uploaded logs under chrome://webrtc-logs, see
     84   // WebRtcLogUploadList. The list has the format
     85   // time,id
     86   // time,id
     87   // etc.
     88   // where each line represents an uploaded log and "time" is Unix time.
     89   void AddUploadedLogInfoToUploadListFile(const std::string& report_id);
     90 
     91   void SetUploadPathForTesting(const base::FilePath& path) {
     92     upload_list_path_ = path;
     93   }
     94 
     95   int log_count_;
     96   base::FilePath upload_list_path_;
     97 
     98   // For testing purposes, see OverrideUploadWithBufferForTesting. Only accessed
     99   // on the FILE thread.
    100   std::string* post_data_;
    101 
    102   DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader);
    103 };
    104 
    105 #endif  // CHROME_BROWSER_MEDIA_WEBRTC_LOG_UPLOADER_H_
    106