| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 #include "chrome/browser/media/webrtc_log_util.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/files/file_enumerator.h" | |
| 13 #include "base/files/file_util.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "chrome/browser/browser_process.h" | |
| 18 #include "chrome/browser/media/webrtc_log_list.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/browser/profiles/profile_attributes_entry.h" | |
| 21 #include "chrome/browser/profiles/profile_attributes_storage.h" | |
| 22 #include "chrome/browser/profiles/profile_manager.h" | |
| 23 #include "content/public/browser/browser_thread.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const int kDaysToKeepLogs = 5; | |
| 28 | |
| 29 // Remove any empty entries from the log list. One line is one log entry, see | |
| 30 // WebRtcLogUploader::AddLocallyStoredLogInfoToUploadListFile for more | |
| 31 // information about the format. | |
| 32 void RemoveEmptyEntriesInLogList(std::string* log_list) { | |
| 33 static const char kEmptyLine[] = ",,\n"; | |
| 34 size_t pos = 0; | |
| 35 do { | |
| 36 pos = log_list->find(kEmptyLine, pos); | |
| 37 if (pos == std::string::npos) | |
| 38 break; | |
| 39 DCHECK(pos == 0 || (*log_list)[pos - 1] == '\n'); | |
| 40 log_list->erase(pos, arraysize(kEmptyLine) - 1); | |
| 41 } while (true); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 // static | |
| 47 void WebRtcLogUtil::DeleteOldWebRtcLogFiles(const base::FilePath& log_dir) { | |
| 48 DeleteOldAndRecentWebRtcLogFiles(log_dir, base::Time::Max()); | |
| 49 } | |
| 50 | |
| 51 // static | |
| 52 void WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles( | |
| 53 const base::FilePath& log_dir, | |
| 54 const base::Time& delete_begin_time) { | |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
| 56 | |
| 57 if (!base::PathExists(log_dir)) { | |
| 58 // This will happen if no logs have been stored or uploaded. | |
| 59 DVLOG(3) << "Could not find directory: " << log_dir.value(); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 const base::Time now = base::Time::Now(); | |
| 64 const base::TimeDelta time_to_keep_logs = | |
| 65 base::TimeDelta::FromDays(kDaysToKeepLogs); | |
| 66 | |
| 67 base::FilePath log_list_path = | |
| 68 WebRtcLogList::GetWebRtcLogListFileForDirectory(log_dir); | |
| 69 std::string log_list; | |
| 70 const bool update_log_list = base::PathExists(log_list_path); | |
| 71 if (update_log_list) { | |
| 72 bool read_ok = base::ReadFileToString(log_list_path, &log_list); | |
| 73 DPCHECK(read_ok); | |
| 74 } | |
| 75 | |
| 76 base::FileEnumerator log_files(log_dir, false, base::FileEnumerator::FILES); | |
| 77 bool delete_ok = true; | |
| 78 for (base::FilePath name = log_files.Next(); !name.empty(); | |
| 79 name = log_files.Next()) { | |
| 80 if (name == log_list_path) | |
| 81 continue; | |
| 82 base::FileEnumerator::FileInfo file_info(log_files.GetInfo()); | |
| 83 base::TimeDelta file_age = now - file_info.GetLastModifiedTime(); | |
| 84 if (file_age > time_to_keep_logs || | |
| 85 (!delete_begin_time.is_max() && | |
| 86 file_info.GetLastModifiedTime() > delete_begin_time)) { | |
| 87 if (!base::DeleteFile(name, false)) | |
| 88 delete_ok = false; | |
| 89 | |
| 90 // Remove the local ID from the log list file. The ID is guaranteed to be | |
| 91 // unique. | |
| 92 std::string id = file_info.GetName().RemoveExtension().MaybeAsASCII(); | |
| 93 size_t id_pos = log_list.find(id); | |
| 94 if (id_pos == std::string::npos) | |
| 95 continue; | |
| 96 log_list.erase(id_pos, id.size()); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 if (!delete_ok) | |
| 101 LOG(WARNING) << "Could not delete all old WebRTC logs."; | |
| 102 | |
| 103 RemoveEmptyEntriesInLogList(&log_list); | |
| 104 | |
| 105 if (update_log_list) { | |
| 106 int written = base::WriteFile(log_list_path, &log_list[0], log_list.size()); | |
| 107 DPCHECK(written == static_cast<int>(log_list.size())); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 // static | |
| 112 void WebRtcLogUtil::DeleteOldWebRtcLogFilesForAllProfiles() { | |
| 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 114 | |
| 115 std::vector<ProfileAttributesEntry*> entries = | |
| 116 g_browser_process->profile_manager()->GetProfileAttributesStorage(). | |
| 117 GetAllProfilesAttributes(); | |
| 118 for (ProfileAttributesEntry* entry : entries) { | |
| 119 content::BrowserThread::PostTask( | |
| 120 content::BrowserThread::FILE, | |
| 121 FROM_HERE, | |
| 122 base::Bind(&DeleteOldWebRtcLogFiles, | |
| 123 WebRtcLogList::GetWebRtcLogDirectoryForProfile( | |
| 124 entry->GetPath()))); | |
| 125 } | |
| 126 } | |
| OLD | NEW |