1 // Copyright 2018 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_WEBRTC_EVENT_LOG_HISTORY_H_ 6 #define CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_HISTORY_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/files/file.h" 12 #include "base/files/file_path.h" 13 #include "base/macros.h" 14 #include "base/time/time.h" 15 16 namespace webrtc_event_logging { 17 18 // Writes a small history file to disk, which allows us to remember what logs 19 // were captured and uploaded, after they are uploaded (whether successfully or 20 // not), or after they ware pruned (if they expire before an upload opportunity 21 // presents itself). 22 class WebRtcEventLogHistoryFileWriter final { 23 public: 24 // Creates and initializes a WebRtcEventLogHistoryFileWriter object. 25 // Overwrites existing files on disk, if any. 26 // If initialization fails (e.g. couldn't create the file), an empty 27 // unique_ptr is returned. 28 static std::unique_ptr<WebRtcEventLogHistoryFileWriter> Create( 29 const base::FilePath& path); 30 31 // The capture time must be later than UNIX epoch start. 32 bool WriteCaptureTime(base::Time capture_time); 33 34 // The upload time must be later than UNIX epoch start. 35 // Writing an upload time earlier than the capture time is not prevented, 36 // but an invalid history file will be produced. 37 bool WriteUploadTime(base::Time upload_time); 38 39 // If |upload_id| is empty, it means the upload was not successful. In that 40 // case, the |upload_time| still denotes the time when the upload started. 41 // |upload_id|'s length must not exceed kWebRtcEventLogMaxUploadIdBytes. 42 bool WriteUploadId(const std::string& upload_id); 43 44 // Deletes the file being written to, and invalidates this object. 45 void Delete(); 46 47 // May only be called on a valid object. 48 base::FilePath path() const; 49 50 private: 51 explicit WebRtcEventLogHistoryFileWriter(const base::FilePath& path); 52 53 // Returns true if initialization was successful; false otherwise. 54 // Overwrites existing files on disk, if any. 55 bool Init(); 56 57 // Returns true if and only if the entire string was written to the file. 58 bool Write(const std::string& str); 59 60 const base::FilePath path_; 61 base::File file_; 62 bool valid_; 63 64 DISALLOW_COPY_AND_ASSIGN(WebRtcEventLogHistoryFileWriter); 65 }; 66 67 // Reads from disk a small history file and recovers the data from it. 68 class WebRtcEventLogHistoryFileReader final { 69 public: 70 // Creates and initializes a WebRtcEventLogHistoryFileReader object. 71 // If initialization fails (e.g. couldn't parse the file), an empty 72 // unique_ptr is returned. 73 static std::unique_ptr<WebRtcEventLogHistoryFileReader> Create( 74 const base::FilePath& path); 75 76 WebRtcEventLogHistoryFileReader(WebRtcEventLogHistoryFileReader&& other); 77 78 // Mandatory fields. 79 std::string LocalId() const; // Must return a non-empty ID. 80 base::Time CaptureTime() const; // Must return a non-null base::Time. 81 82 // Optional fields. 83 base::Time UploadTime() const; // Non-null only if upload was attempted. 84 std::string UploadId() const; // Non-null only if upload was successful. 85 86 // May only be performed on a valid object. 87 base::FilePath path() const; 88 89 // Compares by capture time. 90 bool operator<(const WebRtcEventLogHistoryFileReader& other) const; 91 92 private: 93 explicit WebRtcEventLogHistoryFileReader(const base::FilePath& path); 94 95 // Returns true if initialization was successful; false otherwise. 96 // If true is returned, |this| is now valid, and will remain so until 97 // the object is destroyed or std::move()-ed away from. 98 bool Init(); 99 100 // Returns true if parsing succeeded; false otherwise. 101 bool Parse(const std::string& file_contents); 102 103 const base::FilePath path_; 104 105 const std::string local_id_; 106 107 // Mandatory field; must be non-null (and therefore also non-zero). 108 base::Time capture_time_; 109 110 // Optional fields; may appear 0 or 1 times in the file. 111 base::Time upload_time_; // Nullness/zero-ness indicates "unset". 112 std::string upload_id_; // Empty string indicates "unset". 113 114 bool valid_; 115 116 DISALLOW_COPY_AND_ASSIGN(WebRtcEventLogHistoryFileReader); 117 }; 118 119 } // namespace webrtc_event_logging 120 121 #endif // CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_HISTORY_H_ 122