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.
5 #include "chrome/browser/media/webrtc_log_util.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_util.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"
19 const int kDaysToKeepLogs
= 5;
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";
28 pos
= log_list
->find(kEmptyLine
, pos
);
29 if (pos
== std::string::npos
)
31 DCHECK(pos
== 0 || (*log_list
)[pos
- 1] == '\n');
32 log_list
->erase(pos
, arraysize(kEmptyLine
) - 1);
39 void WebRtcLogUtil::DeleteOldWebRtcLogFiles(const base::FilePath
& log_dir
) {
40 DeleteOldAndRecentWebRtcLogFiles(log_dir
, base::Time::Max());
44 void WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles(
45 const base::FilePath
& log_dir
,
46 const base::Time
& delete_begin_time
) {
47 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
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();
55 const base::Time now
= base::Time::Now();
56 const base::TimeDelta time_to_keep_logs
=
57 base::TimeDelta::FromDays(kDaysToKeepLogs
);
59 base::FilePath log_list_path
=
60 WebRtcLogList::GetWebRtcLogListFileForDirectory(log_dir
);
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
);
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
)
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))
82 // Remove the local ID from the log list file. The ID is guaranteed to be
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
)
88 log_list
.erase(id_pos
, id
.size());
93 LOG(WARNING
) << "Could not delete all old WebRTC logs.";
95 RemoveEmptyEntriesInLogList(&log_list
);
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()));
104 void WebRtcLogUtil::DeleteOldWebRtcLogFilesForAllProfiles() {
105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
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,
114 base::Bind(&DeleteOldWebRtcLogFiles
,
115 WebRtcLogList::GetWebRtcLogDirectoryForProfile(
116 profile_cache
.GetPathOfProfileAtIndex(i
))));