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