Commit 012ba293 authored by Elad Alon's avatar Elad Alon Committed by Commit Bot

Clear WebRTC event logs from disk along with cache

* When Chrome's cache is cleared, remove pending WebRTC event logs
  as well.
* Logs which are currently being written to are not cleared; this
  is by design.
* Logs which are currently being uploaded are also not cleared.
  It is debatable whether they should be. Erring on the side of
  caution is probably more user-friendly here; a TODO is left for
  a later CL to clear those logs as well.

Bug: 775415
Change-Id: Icf13c363ff7d04e082ed0ccf52e000b5bdbf75a4
Reviewed-on: https://chromium-review.googlesource.com/966562
Commit-Queue: Elad Alon <eladalon@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarChristos Froussios <cfroussios@chromium.org>
Reviewed-by: default avatarChristian Dullweber <dullweber@chromium.org>
Reviewed-by: default avatarTommi <tommi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548388}
parent a49026ac
......@@ -125,6 +125,7 @@
#endif // defined(OS_CHROMEOS)
#if BUILDFLAG(ENABLE_WEBRTC)
#include "chrome/browser/media/webrtc/webrtc_event_log_manager.h"
#include "components/webrtc_logging/browser/log_cleanup.h"
#include "components/webrtc_logging/browser/log_list.h"
#endif // BUILDFLAG(ENABLE_WEBRTC)
......@@ -983,6 +984,18 @@ void ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData(
CreatePendingTaskCompletionClosure())));
}
#endif
#if BUILDFLAG(ENABLE_WEBRTC)
// TODO(crbug.com/829321): Remove null-check.
auto* webrtc_event_log_manager = WebRtcEventLogManager::GetInstance();
if (webrtc_event_log_manager) {
webrtc_event_log_manager->ClearCacheForBrowserContext(
profile_, delete_begin_, delete_end_,
CreatePendingTaskCompletionClosure());
} else {
LOG(ERROR) << "WebRtcEventLogManager not instantiated.";
}
#endif
}
//////////////////////////////////////////////////////////////////////////////
......
......@@ -311,6 +311,23 @@ void WebRtcEventLogManager::StartRemoteLogging(
std::move(reply)));
}
void WebRtcEventLogManager::ClearCacheForBrowserContext(
const BrowserContext* browser_context,
const base::Time& delete_begin,
const base::Time& delete_end,
base::OnceClosure reply) {
const auto browser_context_id = GetBrowserContextId(browser_context);
DCHECK_NE(browser_context_id, kNullBrowserContextId);
// The object outlives the task queue - base::Unretained(this) is safe.
task_runner_->PostTaskAndReply(
FROM_HERE,
base::BindOnce(
&WebRtcEventLogManager::ClearCacheForBrowserContextInternal,
base::Unretained(this), browser_context_id, delete_begin, delete_end),
std::move(reply));
}
void WebRtcEventLogManager::SetLocalLogsObserver(
WebRtcLocalEventLogsObserver* observer,
base::OnceClosure reply) {
......@@ -581,6 +598,18 @@ void WebRtcEventLogManager::StartRemoteLoggingInternal(
}
}
void WebRtcEventLogManager::ClearCacheForBrowserContextInternal(
BrowserContextId browser_context_id,
const base::Time& delete_begin,
const base::Time& delete_end) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
if (remote_logs_manager_) {
remote_logs_manager_->ClearCacheForBrowserContext(browser_context_id,
delete_begin, delete_end);
}
}
void WebRtcEventLogManager::RenderProcessExitedInternal(int render_process_id) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
local_logs_manager_.RenderProcessHostExitedDestroyed(render_process_id);
......
......@@ -16,6 +16,7 @@
#include "base/memory/scoped_refptr.h"
#include "base/sequenced_task_runner.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_common.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_local.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_remote.h"
......@@ -144,6 +145,16 @@ class WebRtcEventLogManager final : public content::RenderProcessHostObserver,
const std::string& metadata = "",
base::OnceCallback<void(bool)> reply = base::OnceCallback<void(bool)>());
// Clear WebRTC event logs associated with a given browser context, in a given
// time range (|delete_begin| inclusive, |delete_end| exclusive), then
// post |reply| back to the thread from which the method was originally
// invoked (which can be any thread).
void ClearCacheForBrowserContext(
const content::BrowserContext* browser_context,
const base::Time& delete_begin,
const base::Time& delete_end,
base::OnceClosure reply);
// Set (or unset) an observer that will be informed whenever a local log file
// is started/stopped. The observer needs to be able to either run from
// anywhere. If you need the code to run on specific runners or queues, have
......@@ -165,6 +176,7 @@ class WebRtcEventLogManager final : public content::RenderProcessHostObserver,
base::OnceClosure reply = base::OnceClosure());
private:
friend class SigninManagerAndroidTest; // Calls *ForTesting() methods.
friend class WebRtcEventLogManagerTestBase; // Calls *ForTesting() methods.
using PeerConnectionKey = WebRtcEventLogPeerConnectionKey;
......@@ -235,6 +247,10 @@ class WebRtcEventLogManager final : public content::RenderProcessHostObserver,
const std::string& metadata,
base::OnceCallback<void(bool)> reply);
void ClearCacheForBrowserContextInternal(BrowserContextId browser_context_id,
const base::Time& delete_begin,
const base::Time& delete_end);
void RenderProcessExitedInternal(int render_process_id);
void SetLocalLogsObserverInternal(WebRtcLocalEventLogsObserver* observer,
......
......@@ -238,6 +238,14 @@ bool WebRtcRemoteEventLogManager::EventLogWrite(const PeerConnectionKey& key,
return WriteToLogFile(it, message);
}
void WebRtcRemoteEventLogManager::ClearCacheForBrowserContext(
BrowserContextId browser_context_id,
const base::Time& delete_begin,
const base::Time& delete_end) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
RemovePendingLogs(delete_begin, delete_end, browser_context_id);
}
void WebRtcRemoteEventLogManager::RenderProcessHostExitedDestroyed(
int render_process_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
......@@ -472,15 +480,29 @@ void WebRtcRemoteEventLogManager::MaybeStopRemoteLogging(
void WebRtcRemoteEventLogManager::PrunePendingLogs() {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
const base::Time oldest_non_expired_timestamp =
base::Time::Now() - kRemoteBoundWebRtcEventLogsMaxRetention;
RemovePendingLogs(
base::Time::Min(),
base::Time::Now() - kRemoteBoundWebRtcEventLogsMaxRetention);
}
void WebRtcRemoteEventLogManager::RemovePendingLogs(
const base::Time& delete_begin,
const base::Time& delete_end,
base::Optional<BrowserContextId> browser_context_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
for (auto it = pending_logs_.begin(); it != pending_logs_.end();) {
if (it->last_modified < oldest_non_expired_timestamp) {
const bool relevant_browser_content =
!browser_context_id || it->browser_context_id == browser_context_id;
if (relevant_browser_content &&
(delete_begin.is_null() || delete_begin <= it->last_modified) &&
(delete_end.is_null() || it->last_modified < delete_end)) {
DVLOG(1) << "Removing " << it->path << ".";
if (!base::DeleteFile(it->path, /*recursive=*/false)) {
LOG(ERROR) << "Failed to delete " << it->path << ".";
}
it = pending_logs_.erase(it);
} else {
DVLOG(1) << "Keeping " << it->path << " on disk.";
++it;
}
}
......
......@@ -9,13 +9,13 @@
#include <set>
#include <vector>
#include "base/optional.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_common.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_uploader.h"
// TODO(eladalon): Prevent uploading of logs when Chrome shutdown imminent.
// https://crbug.com/775415
// TODO(crbug.com/775415): Avoid uploading logs when Chrome shutdown imminent.
class WebRtcRemoteEventLogManager final
: public LogFileWriter,
......@@ -79,6 +79,16 @@ class WebRtcRemoteEventLogManager final
// an active log.
bool EventLogWrite(const PeerConnectionKey& key, const std::string& message);
// Clear PENDING WebRTC event logs associated with a given browser context,
// in a given time range, then post |reply| back to the thread from which
// the method was originally invoked (which can be any thread).
// Log files currently being written are not interrupted.
// Active uploads are not interrupted.
// TODO(crbug.com/775415): Allow interrupting active uploads.
void ClearCacheForBrowserContext(BrowserContextId browser_context_id,
const base::Time& delete_begin,
const base::Time& delete_end);
// An implicit PeerConnectionRemoved() on all of the peer connections that
// were associated with the renderer process.
void RenderProcessHostExitedDestroyed(int render_process_id);
......@@ -178,6 +188,17 @@ class WebRtcRemoteEventLogManager final
// this check is not too expensive.
void PrunePendingLogs();
// Removes pending logs whose last modification date was between at or later
// than |delete_begin|, and earlier than |delete_end|.
// If a null time-point is given as either |delete_begin| or |delete_begin|,
// it is treated as "beginning-of-time" or "end-of-time", respectively.
// If |browser_context_id| is set, only logs associated with it are considered
// for removal; otherwise, all logs are considered.
void RemovePendingLogs(const base::Time& delete_begin,
const base::Time& delete_end,
base::Optional<BrowserContextId> browser_context_id =
base::Optional<BrowserContextId>());
// Return |true| if and only if we can start another active log (with respect
// to limitations on the numbers active and pending logs).
bool AdditionalActiveLogAllowed(BrowserContextId browser_context_id) const;
......@@ -185,11 +206,11 @@ class WebRtcRemoteEventLogManager final
// Initiating a new upload is only allowed when there are no active peer
// connection which might be adversely affected by the bandwidth consumption
// of the upload.
// This can be overridden by a command line flag - see
// kWebRtcRemoteEventLogUploadNoSuppression.
// TODO(eladalon): Add support for pausing/resuming an upload when peer
// connections are added/removed after an upload was already initiated.
// https://crbug.com/775415
// TODO(crbug.com/775415): Add support for pausing/resuming an upload when
// peer connections are added/removed after an upload was already initiated.
bool UploadingAllowed() const;
// If no upload is in progress, and if uploading is currently permissible,
......
......@@ -6,6 +6,7 @@
#include <algorithm>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
......@@ -64,6 +65,24 @@ using RenderProcessHost = content::RenderProcessHost;
namespace {
// Common default/arbitrary values.
static constexpr int kLid = 478;
auto SaveFilePathTo(base::Optional<base::FilePath>* output) {
return [output](PeerConnectionKey ignored_key, base::FilePath file_path) {
*output = file_path;
};
}
auto SaveKeyAndFilePathTo(base::Optional<PeerConnectionKey>* key_output,
base::Optional<base::FilePath>* file_path_output) {
return [key_output, file_path_output](PeerConnectionKey key,
base::FilePath file_path) {
*key_output = key;
*file_path_output = file_path;
};
}
const int kMaxActiveRemoteLogFiles =
static_cast<int>(kMaxActiveRemoteBoundWebRtcEventLogs);
const int kMaxPendingRemoteLogFiles =
......@@ -76,6 +95,15 @@ PeerConnectionKey GetPeerConnectionKey(const RenderProcessHost* rph, int lid) {
return PeerConnectionKey(rph->GetID(), lid, browser_context_id);
}
base::Time GetLastModificationTime(const base::FilePath& file_path) {
base::File::Info file_info;
if (!base::GetFileInfo(file_path, &file_info)) {
EXPECT_TRUE(false);
return base::Time();
}
return file_info.last_modified;
}
// This implementation does not upload files, nor prtends to have finished an
// upload. Most importantly, it does not get rid of the locally-stored log file
// after finishing a simulated upload; this is useful because it keeps the file
......@@ -270,6 +298,15 @@ class WebRtcEventLogManagerTestBase : public ::testing::TestWithParam<bool> {
kMaxRemoteLogFileSizeBytes, metadata);
}
void ClearCacheForBrowserContext(
const content::BrowserContext* browser_context,
const base::Time& delete_begin,
const base::Time& delete_end) {
event_log_manager_->ClearCacheForBrowserContext(
browser_context, delete_begin, delete_end, VoidReplyClosure());
WaitForReply();
}
void SetLocalLogsObserver(WebRtcLocalEventLogsObserver* observer) {
event_log_manager_->SetLocalLogsObserver(observer, VoidReplyClosure());
WaitForReply();
......@@ -532,6 +569,55 @@ class WebRtcEventLogManagerTest : public WebRtcEventLogManagerTestBase {
base::test::ScopedFeatureList scoped_feature_list_;
};
class WebRtcEventLogManagerTestCacheClearing
: public WebRtcEventLogManagerTest {
public:
~WebRtcEventLogManagerTestCacheClearing() override = default;
void SetUp() override {
WebRtcEventLogManagerTest::SetUp();
SuppressUploading();
}
void CreatePendingLogFilesForBrowserContext(BrowserContext* browser_context) {
ASSERT_TRUE(mapping_.find(browser_context) == mapping_.end());
auto& elements = mapping_[browser_context];
elements = std::make_unique<BrowserContextAssociatedElements>();
for (size_t i = 0; i < kMaxActiveRemoteBoundWebRtcEventLogs; ++i) {
elements->rphs.push_back(
std::make_unique<MockRenderProcessHost>(browser_context));
elements->file_paths.push_back(base::FilePath());
const auto key = GetPeerConnectionKey(elements->rphs[i].get(), kLid);
ON_CALL(remote_observer_, OnRemoteLogStarted(key, _))
.WillByDefault(Invoke(SaveFilePathTo(&elements->file_paths[i])));
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
ASSERT_TRUE(StartRemoteLogging(key.render_process_id, GetUniqueId(key)));
ASSERT_TRUE(PeerConnectionRemoved(key.render_process_id, key.lid));
ASSERT_TRUE(elements->file_paths[i]);
ASSERT_TRUE(base::PathExists(*elements->file_paths[i]));
latest_mod_ = GetLastModificationTime(*elements->file_paths[i]);
if (earliest_mod_.is_null()) { // First file.
earliest_mod_ = latest_mod_;
}
}
}
protected:
struct BrowserContextAssociatedElements {
std::vector<std::unique_ptr<MockRenderProcessHost>> rphs;
std::vector<base::Optional<base::FilePath>> file_paths;
};
std::map<const BrowserContext*,
std::unique_ptr<BrowserContextAssociatedElements>>
mapping_;
base::Time earliest_mod_;
base::Time latest_mod_;
};
class WebRtcEventLogManagerTestWithRemoteLoggingDisabled
: public WebRtcEventLogManagerTestBase {
public:
......@@ -557,24 +643,6 @@ class WebRtcEventLogManagerTestUploadSuppressionDisablingFlag
namespace {
// Common default/arbitrary values.
static constexpr int kLid = 478;
auto SaveFilePathTo(base::Optional<base::FilePath>* output) {
return [output](PeerConnectionKey ignored_key, base::FilePath file_path) {
*output = file_path;
};
}
auto SaveKeyAndFilePathTo(base::Optional<PeerConnectionKey>* key_output,
base::Optional<base::FilePath>* file_path_output) {
return [key_output, file_path_output](PeerConnectionKey key,
base::FilePath file_path) {
*key_output = key;
*file_path_output = file_path;
};
}
class PeerConnectionTrackerProxyForTesting
: public WebRtcEventLogManager::PeerConnectionTrackerProxy {
public:
......@@ -1030,7 +1098,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLoggingRestartCreatesNewFile) {
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
for (size_t i = 0; i < logs.size(); i++) {
for (size_t i = 0; i < logs.size(); ++i) {
ON_CALL(local_observer_, OnLocalLogStarted(_, _))
.WillByDefault(Invoke(SaveKeyAndFilePathTo(&keys[i], &file_paths[i])));
ASSERT_TRUE(EnableLocalLogging());
......@@ -1043,7 +1111,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLoggingRestartCreatesNewFile) {
ASSERT_TRUE(DisableLocalLogging());
}
for (size_t i = 0; i < logs.size(); i++) {
for (size_t i = 0; i < logs.size(); ++i) {
ExpectLocalFileContents(*file_paths[i], logs[i]);
}
}
......@@ -1052,7 +1120,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLogMultipleActiveFiles) {
ASSERT_TRUE(EnableLocalLogging());
std::list<MockRenderProcessHost> rphs;
for (size_t i = 0; i < 3; i++) {
for (size_t i = 0; i < 3; ++i) {
rphs.emplace_back(browser_context_);
}
......@@ -1062,7 +1130,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLogMultipleActiveFiles) {
}
std::vector<base::Optional<base::FilePath>> file_paths(keys.size());
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
ON_CALL(local_observer_, OnLocalLogStarted(keys[i], _))
.WillByDefault(Invoke(SaveFilePathTo(&file_paths[i])));
ASSERT_TRUE(PeerConnectionAdded(keys[i].render_process_id, keys[i].lid));
......@@ -1071,7 +1139,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLogMultipleActiveFiles) {
}
std::vector<std::string> logs;
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
logs.emplace_back(std::to_string(rph_->GetID()) + std::to_string(kLid));
ASSERT_EQ(
OnWebRtcEventLogWrite(keys[i].render_process_id, keys[i].lid, logs[i]),
......@@ -1081,7 +1149,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLogMultipleActiveFiles) {
// Make sure the file woulds be closed, so that we could safely read them.
ASSERT_TRUE(DisableLocalLogging());
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
ExpectLocalFileContents(*file_paths[i], logs[i]);
}
}
......@@ -1091,7 +1159,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLogLimitActiveLocalLogFiles) {
const int kMaxLocalLogFiles =
static_cast<int>(kMaxNumberLocalWebRtcEventLogFiles);
for (int i = 0; i < kMaxLocalLogFiles; i++) {
for (int i = 0; i < kMaxLocalLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rph_.get(), i);
EXPECT_CALL(local_observer_, OnLocalLogStarted(key, _)).Times(1);
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
......@@ -1109,7 +1177,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLogFilledLogNotCountedTowardsLogsLimit) {
const int kMaxLocalLogFiles =
static_cast<int>(kMaxNumberLocalWebRtcEventLogFiles);
for (int i = 0; i < kMaxLocalLogFiles; i++) {
for (int i = 0; i < kMaxLocalLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rph_.get(), i);
EXPECT_CALL(local_observer_, OnLocalLogStarted(key, _)).Times(1);
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
......@@ -1132,7 +1200,7 @@ TEST_F(WebRtcEventLogManagerTest,
const int kMaxLocalLogFiles =
static_cast<int>(kMaxNumberLocalWebRtcEventLogFiles);
for (int i = 0; i < kMaxLocalLogFiles; i++) {
for (int i = 0; i < kMaxLocalLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rph_.get(), i);
EXPECT_CALL(local_observer_, OnLocalLogStarted(key, _)).Times(1);
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
......@@ -1212,7 +1280,7 @@ TEST_F(WebRtcEventLogManagerTest, LocalLogEmptyStringHandledGracefully) {
ASSERT_TRUE(file_path);
ASSERT_FALSE(file_path->empty());
for (size_t i = 0; i < logs.size(); i++) {
for (size_t i = 0; i < logs.size(); ++i) {
ASSERT_EQ(OnWebRtcEventLogWrite(key.render_process_id, key.lid, logs[i]),
std::make_pair(true, false));
}
......@@ -1468,7 +1536,7 @@ TEST_F(WebRtcEventLogManagerTest, RemoteLogFileCreatedInCorrectDirectory) {
constexpr size_t kLogsNum = 3;
TestingProfile* browser_contexts[kLogsNum];
std::vector<std::unique_ptr<MockRenderProcessHost>> rphs;
for (size_t i = 0; i < kLogsNum; i++) {
for (size_t i = 0; i < kLogsNum; ++i) {
auto* const browser_context = CreateBrowserContext();
browser_contexts[i] = browser_context;
rphs.emplace_back(std::make_unique<MockRenderProcessHost>(browser_context));
......@@ -1476,7 +1544,7 @@ TEST_F(WebRtcEventLogManagerTest, RemoteLogFileCreatedInCorrectDirectory) {
// Prepare to store the logs' paths in distinct memory locations.
base::Optional<base::FilePath> file_paths[kLogsNum];
for (size_t i = 0; i < kLogsNum; i++) {
for (size_t i = 0; i < kLogsNum; ++i) {
const auto key = GetPeerConnectionKey(rphs[i].get(), kLid);
EXPECT_CALL(remote_observer_, OnRemoteLogStarted(key, _))
.Times(1)
......@@ -1491,7 +1559,7 @@ TEST_F(WebRtcEventLogManagerTest, RemoteLogFileCreatedInCorrectDirectory) {
}
// All log files must be created in their own context's directory.
for (size_t i = 0; i < arraysize(browser_contexts); i++) {
for (size_t i = 0; i < arraysize(browser_contexts); ++i) {
ASSERT_TRUE(file_paths[i]);
EXPECT_TRUE(browser_contexts[i]->GetPath().IsParent(*file_paths[i]));
}
......@@ -1515,7 +1583,7 @@ TEST_F(WebRtcEventLogManagerTest,
// Make sure the logs get written to separate files.
base::Optional<base::FilePath> file_paths[2];
for (size_t i = 0; i < 2; i++) {
for (size_t i = 0; i < 2; ++i) {
EXPECT_CALL(remote_observer_, OnRemoteLogStarted(keys[i], _))
.Times(1)
.WillOnce(Invoke(SaveFilePathTo(&file_paths[i])));
......@@ -1699,7 +1767,7 @@ TEST_F(WebRtcEventLogManagerTest,
GetPeerConnectionKey(rph_.get(), 2)};
std::vector<base::Optional<base::FilePath>> file_paths(keys.size());
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
ON_CALL(remote_observer_, OnRemoteLogStarted(keys[i], _))
.WillByDefault(Invoke(SaveFilePathTo(&file_paths[i])));
ASSERT_TRUE(PeerConnectionAdded(keys[i].render_process_id, keys[i].lid));
......@@ -1710,7 +1778,7 @@ TEST_F(WebRtcEventLogManagerTest,
}
std::vector<std::string> logs;
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
logs.emplace_back(std::to_string(rph_->GetID()) + std::to_string(i));
ASSERT_EQ(
OnWebRtcEventLogWrite(keys[i].render_process_id, keys[i].lid, logs[i]),
......@@ -1722,7 +1790,7 @@ TEST_F(WebRtcEventLogManagerTest,
ASSERT_TRUE(PeerConnectionRemoved(key.render_process_id, key.lid));
}
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
ExpectRemoteFileContents(*file_paths[i], logs[i]);
}
}
......@@ -1732,7 +1800,7 @@ TEST_F(WebRtcEventLogManagerTest,
constexpr size_t kLogsNum = 3;
TestingProfile* browser_contexts[kLogsNum];
std::vector<std::unique_ptr<MockRenderProcessHost>> rphs;
for (size_t i = 0; i < kLogsNum; i++) {
for (size_t i = 0; i < kLogsNum; ++i) {
TestingProfile* const browser_context = CreateBrowserContext();
browser_contexts[i] = browser_context;
rphs.emplace_back(std::make_unique<MockRenderProcessHost>(browser_context));
......@@ -1744,7 +1812,7 @@ TEST_F(WebRtcEventLogManagerTest,
}
std::vector<base::Optional<base::FilePath>> file_paths(keys.size());
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
ON_CALL(remote_observer_, OnRemoteLogStarted(keys[i], _))
.WillByDefault(Invoke(SaveFilePathTo(&file_paths[i])));
ASSERT_TRUE(PeerConnectionAdded(keys[i].render_process_id, keys[i].lid));
......@@ -1755,7 +1823,7 @@ TEST_F(WebRtcEventLogManagerTest,
}
std::vector<std::string> logs;
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
logs.emplace_back(std::to_string(rph_->GetID()) + std::to_string(i));
ASSERT_EQ(
OnWebRtcEventLogWrite(keys[i].render_process_id, keys[i].lid, logs[i]),
......@@ -1767,7 +1835,7 @@ TEST_F(WebRtcEventLogManagerTest,
ASSERT_TRUE(PeerConnectionRemoved(key.render_process_id, key.lid));
}
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
ExpectRemoteFileContents(*file_paths[i], logs[i]);
}
}
......@@ -1776,20 +1844,20 @@ TEST_F(WebRtcEventLogManagerTest, DifferentRemoteLogsMayHaveDifferentMaximums) {
const std::string logs[2] = {"abra", "cadabra"};
std::vector<base::Optional<base::FilePath>> file_paths(arraysize(logs));
std::vector<PeerConnectionKey> keys;
for (size_t i = 0; i < arraysize(logs); i++) {
for (size_t i = 0; i < arraysize(logs); ++i) {
keys.push_back(GetPeerConnectionKey(rph_.get(), i));
ON_CALL(remote_observer_, OnRemoteLogStarted(keys[i], _))
.WillByDefault(Invoke(SaveFilePathTo(&file_paths[i])));
}
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
ASSERT_TRUE(PeerConnectionAdded(keys[i].render_process_id, keys[i].lid));
ASSERT_TRUE(StartRemoteLogging(
keys[i].render_process_id, GetUniqueId(keys[i]),
kRemoteBoundLogFileHeaderSizeBytes + logs[i].length()));
}
for (size_t i = 0; i < keys.size(); i++) {
for (size_t i = 0; i < keys.size(); ++i) {
// The write is successful, but the file closed, indicating that the maximum
// file size has been reached.
EXPECT_CALL(remote_observer_, OnRemoteLogStopped(keys[i])).Times(1);
......@@ -1884,11 +1952,11 @@ TEST_F(WebRtcEventLogManagerTest, GracefullyHandleFailureToStartRemoteLogFile) {
#endif // defined(OS_POSIX) && !defined(OS_FUCHSIA)
TEST_F(WebRtcEventLogManagerTest, RemoteLogLimitActiveLogFiles) {
for (int i = 0; i < kMaxActiveRemoteLogFiles + 1; i++) {
for (int i = 0; i < kMaxActiveRemoteLogFiles + 1; ++i) {
ASSERT_TRUE(PeerConnectionAdded(rph_->GetID(), i));
}
for (int i = 0; i < kMaxActiveRemoteLogFiles; i++) {
for (int i = 0; i < kMaxActiveRemoteLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rph_.get(), i);
EXPECT_CALL(remote_observer_, OnRemoteLogStarted(key, _)).Times(1);
ASSERT_TRUE(StartRemoteLogging(key.render_process_id, GetUniqueId(key)));
......@@ -1905,7 +1973,7 @@ TEST_F(WebRtcEventLogManagerTest,
RemoteLogFilledLogNotCountedTowardsLogsLimit) {
const std::string log = "very_short_log";
for (int i = 0; i < kMaxActiveRemoteLogFiles; i++) {
for (int i = 0; i < kMaxActiveRemoteLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rph_.get(), i);
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
EXPECT_CALL(remote_observer_, OnRemoteLogStarted(key, _)).Times(1);
......@@ -1930,7 +1998,7 @@ TEST_F(WebRtcEventLogManagerTest,
TEST_F(WebRtcEventLogManagerTest,
RemoteLogForRemovedPeerConnectionNotCountedTowardsLogsLimit) {
for (int i = 0; i < kMaxActiveRemoteLogFiles; i++) {
for (int i = 0; i < kMaxActiveRemoteLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rph_.get(), i);
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
EXPECT_CALL(remote_observer_, OnRemoteLogStarted(key, _)).Times(1);
......@@ -1955,7 +2023,7 @@ TEST_F(WebRtcEventLogManagerTest,
SuppressUploading();
// Produce kMaxPendingRemoteLogFiles pending logs.
for (int i = 0; i < kMaxPendingRemoteLogFiles; i++) {
for (int i = 0; i < kMaxPendingRemoteLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rph_.get(), i);
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
ASSERT_TRUE(StartRemoteLogging(key.render_process_id, GetUniqueId(key)));
......@@ -1985,7 +2053,7 @@ TEST_F(WebRtcEventLogManagerTest,
// Allowed to start kMaxPendingRemoteLogFiles for each BrowserContext.
// Specifically, we can do it for the first BrowserContext.
for (int i = 0; i < kMaxPendingRemoteLogFiles; i++) {
for (int i = 0; i < kMaxPendingRemoteLogFiles; ++i) {
const auto key = GetPeerConnectionKey(rphs[0].get(), i);
// The log could be opened:
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
......@@ -2020,7 +2088,7 @@ TEST_F(WebRtcEventLogManagerTest,
GetLogsDirectoryPath(browser_context_path);
ASSERT_TRUE(CreateDirectory(remote_logs_dir));
for (size_t i = 0; i < kMaxPendingRemoteBoundWebRtcEventLogs; i++) {
for (size_t i = 0; i < kMaxPendingRemoteBoundWebRtcEventLogs; ++i) {
const base::FilePath file_path =
remote_logs_dir.Append(IntToStringType(i))
.AddExtension(kRemoteBoundLogExtension);
......@@ -2115,7 +2183,7 @@ TEST_F(WebRtcEventLogManagerTest, UploadOrderDependsOnLastModificationTime) {
// Create profiles. This creates their directories.
base::FilePath remote_logs_dirs[kProfilesNum];
for (size_t i = 0; i < kProfilesNum; i++) {
for (size_t i = 0; i < kProfilesNum; ++i) {
auto* const browser_context = CreateBrowserContext(profile_names[i]);
remote_logs_dirs[i] = GetLogsDirectoryPath(browser_context->GetPath());
}
......@@ -2126,7 +2194,7 @@ TEST_F(WebRtcEventLogManagerTest, UploadOrderDependsOnLastModificationTime) {
// Seed the directories with log files.
base::FilePath file_paths[kProfilesNum];
for (size_t i = 0; i < kProfilesNum; i++) {
for (size_t i = 0; i < kProfilesNum; ++i) {
ASSERT_TRUE(base::DirectoryExists(remote_logs_dirs[i]));
file_paths[i] = remote_logs_dirs[i].AppendASCII("file").AddExtension(
kRemoteBoundLogExtension);
......@@ -2150,7 +2218,7 @@ TEST_F(WebRtcEventLogManagerTest, UploadOrderDependsOnLastModificationTime) {
std::list<base::FilePath> expected_files;
base::Time mod_time =
base::Time::Now() - base::TimeDelta::FromSeconds(kProfilesNum);
for (size_t i = 0; i < kProfilesNum; i++) {
for (size_t i = 0; i < kProfilesNum; ++i) {
mod_time += base::TimeDelta::FromSeconds(1); // Back to the future.
const base::FilePath& path = file_paths[permutation[i]];
ASSERT_TRUE(base::TouchFile(path, shared_last_accessed, mod_time));
......@@ -2162,7 +2230,7 @@ TEST_F(WebRtcEventLogManagerTest, UploadOrderDependsOnLastModificationTime) {
// loading, rather than in order of date.
LoadProfiles();
SuppressUploading();
for (size_t i = 0; i < kProfilesNum; i++) {
for (size_t i = 0; i < kProfilesNum; ++i) {
CreateBrowserContext(profile_names[i]); // Owned by the profile manager.
}
......@@ -2189,7 +2257,7 @@ TEST_F(WebRtcEventLogManagerTest, ExpiredFilesArePrunedRatherThanUploaded) {
UnloadProfiles();
base::FilePath file_paths[2];
for (size_t i = 0; i < 2; i++) {
for (size_t i = 0; i < 2; ++i) {
file_paths[i] = remote_logs_dir.Append(IntToStringType(i))
.AddExtension(kRemoteBoundLogExtension);
constexpr int file_flags = base::File::FLAG_CREATE |
......@@ -2250,7 +2318,7 @@ TEST_F(WebRtcEventLogManagerTest, RemoteLogEmptyStringHandledGracefully) {
ASSERT_TRUE(file_path);
ASSERT_FALSE(file_path->empty());
for (size_t i = 0; i < logs.size(); i++) {
for (size_t i = 0; i < logs.size(); ++i) {
ASSERT_EQ(OnWebRtcEventLogWrite(key.render_process_id, key.lid, logs[i]),
std::make_pair(false, true));
}
......@@ -2266,7 +2334,7 @@ TEST_F(WebRtcEventLogManagerTest,
UnopenedRemoteLogFilesNotCountedTowardsActiveLogsLimit) {
TestingProfile* browser_contexts[2];
std::unique_ptr<MockRenderProcessHost> rphs[2];
for (size_t i = 0; i < 2; i++) {
for (size_t i = 0; i < 2; ++i) {
browser_contexts[i] = CreateBrowserContext();
rphs[i] = std::make_unique<MockRenderProcessHost>(browser_contexts[i]);
}
......@@ -2289,7 +2357,7 @@ TEST_F(WebRtcEventLogManagerTest,
GetUniqueId(without_permissions_key)));
// Show that this was not counted towards the limit of active files.
for (int i = 0; i < kMaxActiveRemoteLogFiles; i++) {
for (int i = 0; i < kMaxActiveRemoteLogFiles; ++i) {
const auto with_permissions_key =
GetPeerConnectionKey(rphs[with_permissions].get(), i);
ASSERT_TRUE(PeerConnectionAdded(with_permissions_key.render_process_id,
......@@ -2507,7 +2575,7 @@ TEST_F(WebRtcEventLogManagerTest, LogAllPossibleCharacters) {
ASSERT_FALSE(remote_log_file_path->empty());
std::string all_chars;
for (size_t i = 0; i < 256; i++) {
for (size_t i = 0; i < 256; ++i) {
all_chars += static_cast<uint8_t>(i);
}
ASSERT_EQ(OnWebRtcEventLogWrite(key.render_process_id, key.lid, all_chars),
......@@ -2638,6 +2706,100 @@ INSTANTIATE_TEST_CASE_P(UploadCompleteResult,
WebRtcEventLogManagerTest,
::testing::Values(false, true));
TEST_F(WebRtcEventLogManagerTestCacheClearing,
ClearCacheForBrowserContextRemovesFilesInRange) {
auto* const browser_context = CreateBrowserContext("name");
CreatePendingLogFilesForBrowserContext(browser_context);
auto& elements = *(mapping_[browser_context]);
// When closing a file, rather than check its last modification date, which
// is potentially expensive, WebRtcRemoteEventLogManager reads the system
// clock, which should be close enough. For tests, however, the difference
// could be enough to flake the tests, if not for this epsilon. Given the
// test's focus, this epsilon can be arbitrarily large.
constexpr base::TimeDelta kEpsion = base::TimeDelta::FromHours(1);
const base::Time earliest_mod = earliest_mod_ - kEpsion;
const base::Time latest_mod = latest_mod_ + kEpsion;
// Test - ClearCacheForBrowserContext() removed all of the files in the range.
ClearCacheForBrowserContext(browser_context, earliest_mod, latest_mod);
for (size_t i = 0; i < elements.file_paths.size(); ++i) {
EXPECT_FALSE(base::PathExists(*elements.file_paths[i]));
}
}
TEST_F(WebRtcEventLogManagerTestCacheClearing,
ClearCacheForBrowserContextDoesNotRemoveFilesOutOfRange) {
auto* const browser_context = CreateBrowserContext("name");
CreatePendingLogFilesForBrowserContext(browser_context);
auto& elements = *(mapping_[browser_context]);
// Get a range whose intersection with the files' range is empty.
const base::Time earliest_mod = earliest_mod_ - base::TimeDelta::FromHours(2);
const base::Time latest_mod = earliest_mod_ - base::TimeDelta::FromHours(1);
ASSERT_LT(latest_mod, latest_mod_);
// Test - ClearCacheForBrowserContext() does not remove files not in range.
// (Range chosen to be earlier than the oldest file
ClearCacheForBrowserContext(browser_context, earliest_mod, latest_mod);
for (size_t i = 0; i < elements.file_paths.size(); ++i) {
EXPECT_TRUE(base::PathExists(*elements.file_paths[i]));
}
}
TEST_F(WebRtcEventLogManagerTestCacheClearing,
ClearCacheForBrowserContextDoesNotRemoveFilesFromOtherProfiles) {
auto* const cleared_browser_context = CreateBrowserContext("cleared");
CreatePendingLogFilesForBrowserContext(cleared_browser_context);
auto& cleared_elements = *(mapping_[cleared_browser_context]);
auto* const uncleared_browser_context = CreateBrowserContext("pristine");
CreatePendingLogFilesForBrowserContext(uncleared_browser_context);
auto& uncleared_elements = *(mapping_[uncleared_browser_context]);
ASSERT_EQ(cleared_elements.file_paths.size(),
uncleared_elements.file_paths.size());
const size_t kFileCount = cleared_elements.file_paths.size();
// When closing a file, rather than check its last modification date, which
// is potentially expensive, WebRtcRemoteEventLogManager reads the system
// clock, which should be close enough. For tests, however, the difference
// could be enough to flake the tests, if not for this epsilon. Given the
// test's focus, this epsilon can be arbitrarily large.
constexpr base::TimeDelta kEpsion = base::TimeDelta::FromHours(1);
const base::Time earliest_mod = earliest_mod_ - kEpsion;
const base::Time latest_mod = latest_mod_ + kEpsion;
// Test - ClearCacheForBrowserContext() only removes the files which belong
// to the cleared context.
ClearCacheForBrowserContext(cleared_browser_context, earliest_mod,
latest_mod);
for (size_t i = 0; i < kFileCount; ++i) {
EXPECT_FALSE(base::PathExists(*cleared_elements.file_paths[i]));
EXPECT_TRUE(base::PathExists(*uncleared_elements.file_paths[i]));
}
}
// If the file has not yet transitioned from ACTIVE to PENDING, it is not
// considered cached, and is therefore not deleted.
TEST_F(WebRtcEventLogManagerTestCacheClearing, ActiveLogFilesNotRemoved) {
// Setup
const auto key = GetPeerConnectionKey(rph_.get(), kLid);
ASSERT_TRUE(PeerConnectionAdded(key.render_process_id, key.lid));
base::Optional<base::FilePath> file_path;
EXPECT_CALL(remote_observer_, OnRemoteLogStarted(key, _))
.Times(1)
.WillOnce(Invoke(SaveFilePathTo(&file_path)));
ASSERT_TRUE(StartRemoteLogging(key.render_process_id, GetUniqueId(key)));
ASSERT_TRUE(file_path);
ASSERT_TRUE(base::PathExists(*file_path));
// Test
ClearCacheForBrowserContext(browser_context_, base::Time::Min(),
base::Time::Max());
EXPECT_TRUE(base::PathExists(*file_path));
}
TEST_F(WebRtcEventLogManagerTestWithRemoteLoggingDisabled,
SanityPeerConnectionAdded) {
EXPECT_TRUE(PeerConnectionAdded(rph_->GetID(), kLid));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment