Commit 2f3fbbeb authored by Francois Doray's avatar Francois Doray Committed by Commit Bot

Use base::Timer in storage::FileSystemUsageCache.

Previously, storage::FileSystemUsageCache used a custom timer
implementation (TimedTaskHelper). Crash involving this custom timer
implementation have been reported (e.g. http://crash/e2c6ff1ef4863460).
Therefore, we are moving all uses of TimedTaskHelper to the well
maintained base::Timer.

Note: The TaskRunner passed to the TimedTaskHelper in
storage::FileSystemUsageCache was always the one on which the
storage::FileSystemUsageCache was used, so there is no need to
an explicit call to base::Timer::SetTaskRunner in this CL.

Bug: 760326
Change-Id: I4b0727e4f70c2560a2948015dd28236cd760a9f3
Reviewed-on: https://chromium-review.googlesource.com/1012586
Commit-Queue: François Doray <fdoray@chromium.org>
Reviewed-by: default avatarTaiju Tsuiki <tzik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551525}
parent 25109f9b
...@@ -14,24 +14,21 @@ ...@@ -14,24 +14,21 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/pickle.h" #include "base/pickle.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "storage/browser/fileapi/timed_task_helper.h"
namespace storage { namespace storage {
namespace { namespace {
const int64_t kCloseDelaySeconds = 5; constexpr base::TimeDelta kCloseDelay = base::TimeDelta::FromSeconds(5);
const size_t kMaxHandleCacheSize = 2; const size_t kMaxHandleCacheSize = 2;
} // namespace } // namespace
FileSystemUsageCache::FileSystemUsageCache( FileSystemUsageCache::FileSystemUsageCache() : weak_factory_(this) {
base::SequencedTaskRunner* task_runner) DETACH_FROM_SEQUENCE(sequence_checker_);
: task_runner_(task_runner),
weak_factory_(this) {
} }
FileSystemUsageCache::~FileSystemUsageCache() { FileSystemUsageCache::~FileSystemUsageCache() {
task_runner_ = NULL;
CloseCacheFiles(); CloseCacheFiles();
} }
...@@ -48,7 +45,7 @@ const int FileSystemUsageCache::kUsageFileSize = ...@@ -48,7 +45,7 @@ const int FileSystemUsageCache::kUsageFileSize =
bool FileSystemUsageCache::GetUsage(const base::FilePath& usage_file_path, bool FileSystemUsageCache::GetUsage(const base::FilePath& usage_file_path,
int64_t* usage_out) { int64_t* usage_out) {
TRACE_EVENT0("FileSystem", "UsageCache::GetUsage"); TRACE_EVENT0("FileSystem", "UsageCache::GetUsage");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(usage_out); DCHECK(usage_out);
bool is_valid = true; bool is_valid = true;
uint32_t dirty = 0; uint32_t dirty = 0;
...@@ -62,7 +59,7 @@ bool FileSystemUsageCache::GetUsage(const base::FilePath& usage_file_path, ...@@ -62,7 +59,7 @@ bool FileSystemUsageCache::GetUsage(const base::FilePath& usage_file_path,
bool FileSystemUsageCache::GetDirty(const base::FilePath& usage_file_path, bool FileSystemUsageCache::GetDirty(const base::FilePath& usage_file_path,
uint32_t* dirty_out) { uint32_t* dirty_out) {
TRACE_EVENT0("FileSystem", "UsageCache::GetDirty"); TRACE_EVENT0("FileSystem", "UsageCache::GetDirty");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(dirty_out); DCHECK(dirty_out);
bool is_valid = true; bool is_valid = true;
uint32_t dirty = 0; uint32_t dirty = 0;
...@@ -76,7 +73,7 @@ bool FileSystemUsageCache::GetDirty(const base::FilePath& usage_file_path, ...@@ -76,7 +73,7 @@ bool FileSystemUsageCache::GetDirty(const base::FilePath& usage_file_path,
bool FileSystemUsageCache::IncrementDirty( bool FileSystemUsageCache::IncrementDirty(
const base::FilePath& usage_file_path) { const base::FilePath& usage_file_path) {
TRACE_EVENT0("FileSystem", "UsageCache::IncrementDirty"); TRACE_EVENT0("FileSystem", "UsageCache::IncrementDirty");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
bool is_valid = true; bool is_valid = true;
uint32_t dirty = 0; uint32_t dirty = 0;
int64_t usage = 0; int64_t usage = 0;
...@@ -93,7 +90,7 @@ bool FileSystemUsageCache::IncrementDirty( ...@@ -93,7 +90,7 @@ bool FileSystemUsageCache::IncrementDirty(
bool FileSystemUsageCache::DecrementDirty( bool FileSystemUsageCache::DecrementDirty(
const base::FilePath& usage_file_path) { const base::FilePath& usage_file_path) {
TRACE_EVENT0("FileSystem", "UsageCache::DecrementDirty"); TRACE_EVENT0("FileSystem", "UsageCache::DecrementDirty");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
bool is_valid = true; bool is_valid = true;
uint32_t dirty = 0; uint32_t dirty = 0;
int64_t usage = 0; int64_t usage = 0;
...@@ -105,7 +102,7 @@ bool FileSystemUsageCache::DecrementDirty( ...@@ -105,7 +102,7 @@ bool FileSystemUsageCache::DecrementDirty(
bool FileSystemUsageCache::Invalidate(const base::FilePath& usage_file_path) { bool FileSystemUsageCache::Invalidate(const base::FilePath& usage_file_path) {
TRACE_EVENT0("FileSystem", "UsageCache::Invalidate"); TRACE_EVENT0("FileSystem", "UsageCache::Invalidate");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
bool is_valid = true; bool is_valid = true;
uint32_t dirty = 0; uint32_t dirty = 0;
int64_t usage = 0; int64_t usage = 0;
...@@ -117,7 +114,7 @@ bool FileSystemUsageCache::Invalidate(const base::FilePath& usage_file_path) { ...@@ -117,7 +114,7 @@ bool FileSystemUsageCache::Invalidate(const base::FilePath& usage_file_path) {
bool FileSystemUsageCache::IsValid(const base::FilePath& usage_file_path) { bool FileSystemUsageCache::IsValid(const base::FilePath& usage_file_path) {
TRACE_EVENT0("FileSystem", "UsageCache::IsValid"); TRACE_EVENT0("FileSystem", "UsageCache::IsValid");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
bool is_valid = true; bool is_valid = true;
uint32_t dirty = 0; uint32_t dirty = 0;
int64_t usage = 0; int64_t usage = 0;
...@@ -130,7 +127,7 @@ bool FileSystemUsageCache::AtomicUpdateUsageByDelta( ...@@ -130,7 +127,7 @@ bool FileSystemUsageCache::AtomicUpdateUsageByDelta(
const base::FilePath& usage_file_path, const base::FilePath& usage_file_path,
int64_t delta) { int64_t delta) {
TRACE_EVENT0("FileSystem", "UsageCache::AtomicUpdateUsageByDelta"); TRACE_EVENT0("FileSystem", "UsageCache::AtomicUpdateUsageByDelta");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
bool is_valid = true; bool is_valid = true;
uint32_t dirty = 0; uint32_t dirty = 0;
int64_t usage = 0; int64_t usage = 0;
...@@ -142,28 +139,28 @@ bool FileSystemUsageCache::AtomicUpdateUsageByDelta( ...@@ -142,28 +139,28 @@ bool FileSystemUsageCache::AtomicUpdateUsageByDelta(
bool FileSystemUsageCache::UpdateUsage(const base::FilePath& usage_file_path, bool FileSystemUsageCache::UpdateUsage(const base::FilePath& usage_file_path,
int64_t fs_usage) { int64_t fs_usage) {
TRACE_EVENT0("FileSystem", "UsageCache::UpdateUsage"); TRACE_EVENT0("FileSystem", "UsageCache::UpdateUsage");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return Write(usage_file_path, true, 0, fs_usage); return Write(usage_file_path, true, 0, fs_usage);
} }
bool FileSystemUsageCache::Exists(const base::FilePath& usage_file_path) { bool FileSystemUsageCache::Exists(const base::FilePath& usage_file_path) {
TRACE_EVENT0("FileSystem", "UsageCache::Exists"); TRACE_EVENT0("FileSystem", "UsageCache::Exists");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return base::PathExists(usage_file_path); return base::PathExists(usage_file_path);
} }
bool FileSystemUsageCache::Delete(const base::FilePath& usage_file_path) { bool FileSystemUsageCache::Delete(const base::FilePath& usage_file_path) {
TRACE_EVENT0("FileSystem", "UsageCache::Delete"); TRACE_EVENT0("FileSystem", "UsageCache::Delete");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CloseCacheFiles(); CloseCacheFiles();
return base::DeleteFile(usage_file_path, false); return base::DeleteFile(usage_file_path, false);
} }
void FileSystemUsageCache::CloseCacheFiles() { void FileSystemUsageCache::CloseCacheFiles() {
TRACE_EVENT0("FileSystem", "UsageCache::CloseCacheFiles"); TRACE_EVENT0("FileSystem", "UsageCache::CloseCacheFiles");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
cache_files_.clear(); cache_files_.clear();
timer_.reset(); timer_.Stop();
} }
bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path, bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path,
...@@ -171,7 +168,7 @@ bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path, ...@@ -171,7 +168,7 @@ bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path,
uint32_t* dirty_out, uint32_t* dirty_out,
int64_t* usage_out) { int64_t* usage_out) {
TRACE_EVENT0("FileSystem", "UsageCache::Read"); TRACE_EVENT0("FileSystem", "UsageCache::Read");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(is_valid); DCHECK(is_valid);
DCHECK(dirty_out); DCHECK(dirty_out);
DCHECK(usage_out); DCHECK(usage_out);
...@@ -207,7 +204,7 @@ bool FileSystemUsageCache::Write(const base::FilePath& usage_file_path, ...@@ -207,7 +204,7 @@ bool FileSystemUsageCache::Write(const base::FilePath& usage_file_path,
int32_t dirty, int32_t dirty,
int64_t usage) { int64_t usage) {
TRACE_EVENT0("FileSystem", "UsageCache::Write"); TRACE_EVENT0("FileSystem", "UsageCache::Write");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::Pickle write_pickle; base::Pickle write_pickle;
write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize); write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize);
write_pickle.WriteBool(is_valid); write_pickle.WriteBool(is_valid);
...@@ -224,7 +221,7 @@ bool FileSystemUsageCache::Write(const base::FilePath& usage_file_path, ...@@ -224,7 +221,7 @@ bool FileSystemUsageCache::Write(const base::FilePath& usage_file_path,
} }
base::File* FileSystemUsageCache::GetFile(const base::FilePath& file_path) { base::File* FileSystemUsageCache::GetFile(const base::FilePath& file_path) {
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (cache_files_.size() >= kMaxHandleCacheSize) if (cache_files_.size() >= kMaxHandleCacheSize)
CloseCacheFiles(); CloseCacheFiles();
ScheduleCloseTimer(); ScheduleCloseTimer();
...@@ -250,7 +247,7 @@ base::File* FileSystemUsageCache::GetFile(const base::FilePath& file_path) { ...@@ -250,7 +247,7 @@ base::File* FileSystemUsageCache::GetFile(const base::FilePath& file_path) {
bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path, bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path,
char* buffer, char* buffer,
int64_t buffer_size) { int64_t buffer_size) {
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::File* file = GetFile(file_path); base::File* file = GetFile(file_path);
if (!file) if (!file)
return false; return false;
...@@ -260,7 +257,7 @@ bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path, ...@@ -260,7 +257,7 @@ bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path,
bool FileSystemUsageCache::WriteBytes(const base::FilePath& file_path, bool FileSystemUsageCache::WriteBytes(const base::FilePath& file_path,
const char* buffer, const char* buffer,
int64_t buffer_size) { int64_t buffer_size) {
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::File* file = GetFile(file_path); base::File* file = GetFile(file_path);
if (!file) if (!file)
return false; return false;
...@@ -269,7 +266,7 @@ bool FileSystemUsageCache::WriteBytes(const base::FilePath& file_path, ...@@ -269,7 +266,7 @@ bool FileSystemUsageCache::WriteBytes(const base::FilePath& file_path,
bool FileSystemUsageCache::FlushFile(const base::FilePath& file_path) { bool FileSystemUsageCache::FlushFile(const base::FilePath& file_path) {
TRACE_EVENT0("FileSystem", "UsageCache::FlushFile"); TRACE_EVENT0("FileSystem", "UsageCache::FlushFile");
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::File* file = GetFile(file_path); base::File* file = GetFile(file_path);
if (!file) if (!file)
return false; return false;
...@@ -277,27 +274,20 @@ bool FileSystemUsageCache::FlushFile(const base::FilePath& file_path) { ...@@ -277,27 +274,20 @@ bool FileSystemUsageCache::FlushFile(const base::FilePath& file_path) {
} }
void FileSystemUsageCache::ScheduleCloseTimer() { void FileSystemUsageCache::ScheduleCloseTimer() {
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!timer_)
timer_.reset(new TimedTaskHelper(task_runner_.get()));
if (timer_->IsRunning()) { if (timer_.IsRunning()) {
timer_->Reset(); timer_.Reset();
return; return;
} }
timer_->Start(FROM_HERE, timer_.Start(FROM_HERE, kCloseDelay,
base::TimeDelta::FromSeconds(kCloseDelaySeconds),
base::Bind(&FileSystemUsageCache::CloseCacheFiles, base::Bind(&FileSystemUsageCache::CloseCacheFiles,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
bool FileSystemUsageCache::CalledOnValidSequence() {
return !task_runner_.get() || task_runner_->RunsTasksInCurrentSequence();
}
bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) { bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) {
DCHECK(CalledOnValidSequence()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize); DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize);
return base::ContainsKey(cache_files_, file_path); return base::ContainsKey(cache_files_, file_path);
} }
......
...@@ -14,16 +14,15 @@ ...@@ -14,16 +14,15 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h" #include "base/sequence_checker.h"
#include "base/timer/timer.h"
#include "storage/browser/storage_browser_export.h" #include "storage/browser/storage_browser_export.h"
namespace storage { namespace storage {
class TimedTaskHelper;
class STORAGE_EXPORT FileSystemUsageCache { class STORAGE_EXPORT FileSystemUsageCache {
public: public:
explicit FileSystemUsageCache(base::SequencedTaskRunner* task_runner); FileSystemUsageCache();
~FileSystemUsageCache(); ~FileSystemUsageCache();
// Gets the size described in the .usage file even if dirty > 0 or // Gets the size described in the .usage file even if dirty > 0 or
...@@ -88,12 +87,13 @@ class STORAGE_EXPORT FileSystemUsageCache { ...@@ -88,12 +87,13 @@ class STORAGE_EXPORT FileSystemUsageCache {
bool HasCacheFileHandle(const base::FilePath& file_path); bool HasCacheFileHandle(const base::FilePath& file_path);
bool CalledOnValidSequence(); // Used to verify that this is used from a single sequence.
SEQUENCE_CHECKER(sequence_checker_);
std::unique_ptr<TimedTaskHelper> timer_; // Used to scheduled delayed calls to CloseCacheFiles().
std::map<base::FilePath, std::unique_ptr<base::File>> cache_files_; base::OneShotTimer timer_;
scoped_refptr<base::SequencedTaskRunner> task_runner_; std::map<base::FilePath, std::unique_ptr<base::File>> cache_files_;
base::WeakPtrFactory<FileSystemUsageCache> weak_factory_; base::WeakPtrFactory<FileSystemUsageCache> weak_factory_;
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using storage::FileSystemUsageCache; using storage::FileSystemUsageCache;
...@@ -21,8 +20,7 @@ namespace content { ...@@ -21,8 +20,7 @@ namespace content {
class FileSystemUsageCacheTest : public testing::Test { class FileSystemUsageCacheTest : public testing::Test {
public: public:
FileSystemUsageCacheTest() FileSystemUsageCacheTest() = default;
: usage_cache_(base::ThreadTaskRunnerHandle::Get().get()) {}
void SetUp() override { ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); } void SetUp() override { ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); }
......
...@@ -95,9 +95,7 @@ class MockQuotaManagerProxy : public storage::QuotaManagerProxy { ...@@ -95,9 +95,7 @@ class MockQuotaManagerProxy : public storage::QuotaManagerProxy {
class QuotaBackendImplTest : public testing::Test { class QuotaBackendImplTest : public testing::Test {
public: public:
QuotaBackendImplTest() QuotaBackendImplTest() : quota_manager_proxy_(new MockQuotaManagerProxy) {}
: file_system_usage_cache_(file_task_runner()),
quota_manager_proxy_(new MockQuotaManagerProxy) {}
void SetUp() override { void SetUp() override {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
......
...@@ -190,7 +190,7 @@ SandboxFileSystemBackendDelegate::SandboxFileSystemBackendDelegate( ...@@ -190,7 +190,7 @@ SandboxFileSystemBackendDelegate::SandboxFileSystemBackendDelegate(
base::Bind(&GetTypeStringForURL), base::Bind(&GetTypeStringForURL),
GetKnownTypeStrings(), GetKnownTypeStrings(),
this))), this))),
file_system_usage_cache_(new FileSystemUsageCache(file_task_runner)), file_system_usage_cache_(std::make_unique<FileSystemUsageCache>()),
quota_observer_(new SandboxQuotaObserver(quota_manager_proxy, quota_observer_(new SandboxQuotaObserver(quota_manager_proxy,
file_task_runner, file_task_runner,
obfuscated_file_util(), obfuscated_file_util(),
......
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