diff options
author | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-27 07:26:15 +0000 |
---|---|---|
committer | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-27 07:26:15 +0000 |
commit | 8bf65dfa49224e68a67e887365dd37ea615e2cc0 (patch) | |
tree | 092c40ce2305ec9d9205777754ee7d9eaaa90d6b | |
parent | f46724728002380f033e0c7832eac9eb40f59712 (diff) | |
download | chromium_src-8bf65dfa49224e68a67e887365dd37ea615e2cc0.zip chromium_src-8bf65dfa49224e68a67e887365dd37ea615e2cc0.tar.gz chromium_src-8bf65dfa49224e68a67e887365dd37ea615e2cc0.tar.bz2 |
Resubmitting 86978 - Fix FileWriterDelegate's usage tracking behavior
Resolved merge conflicts that had caused breakage in the previous attempt.
Review URL: http://codereview.chromium.org/7067021
BUG=74841
TEST=FileWriterDelegateTest.*, QuotaFileUtilTest.*
TBR=ericu, msw
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86988 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | webkit/fileapi/file_system_test_helper.cc | 25 | ||||
-rw-r--r-- | webkit/fileapi/file_system_test_helper.h | 4 | ||||
-rw-r--r-- | webkit/fileapi/file_writer_delegate.cc | 62 | ||||
-rw-r--r-- | webkit/fileapi/file_writer_delegate.h | 5 | ||||
-rw-r--r-- | webkit/fileapi/file_writer_delegate_unittest.cc | 266 | ||||
-rw-r--r-- | webkit/fileapi/quota_file_util_unittest.cc | 105 |
6 files changed, 343 insertions, 124 deletions
diff --git a/webkit/fileapi/file_system_test_helper.cc b/webkit/fileapi/file_system_test_helper.cc index dd2334b..bef48ba 100644 --- a/webkit/fileapi/file_system_test_helper.cc +++ b/webkit/fileapi/file_system_test_helper.cc @@ -23,7 +23,7 @@ namespace { class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { public: - TestSpecialStoragePolicy(bool unlimited_quota) + explicit TestSpecialStoragePolicy(bool unlimited_quota) : unlimited_quota_(unlimited_quota) {} virtual bool IsStorageProtected(const GURL& origin) { @@ -92,7 +92,15 @@ void FileSystemTestOriginHelper::SetUp( // Initialize the usage cache file. FilePath usage_cache_path = file_system_context_->path_manager() ->sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_); - FileSystemUsageCache::UpdateUsage(usage_cache_path, 0); + FileSystemUsageCache::UpdateUsage( + usage_cache_path, FileSystemUsageCache::kUsageFileSize); + + // We expect the origin directory to be always empty, except for possibly + // the usage cache file. We record the initial usage file size here + // (it will be either 0 or kUsageFileSize) so that later we can compute + // how much the size of the origin directory has grown. + initial_usage_size_ = file_util::ComputeDirectorySize( + GetOriginRootPath()); } void FileSystemTestOriginHelper::TearDown() { @@ -129,6 +137,19 @@ FilePath FileSystemTestOriginHelper::GetUsageCachePath() const { ->sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_); } +int64 FileSystemTestOriginHelper::GetCachedOriginUsage() const { + return FileSystemUsageCache::GetUsage(GetUsageCachePath()) - + FileSystemUsageCache::kUsageFileSize; +} + +int64 FileSystemTestOriginHelper::ComputeCurrentOriginUsage() const { + // Depending on the file_util GetOriginRootPath() may include usage + // cache file size or may not. Here we subtract the initial size to + // make it work for multiple file_utils. + return file_util::ComputeDirectorySize(GetOriginRootPath()) - + initial_usage_size_; +} + FileSystemOperation* FileSystemTestOriginHelper::NewOperation( FileSystemCallbackDispatcher* callback_dispatcher) { DCHECK(file_system_context_.get()); diff --git a/webkit/fileapi/file_system_test_helper.h b/webkit/fileapi/file_system_test_helper.h index 2cf8ebb..c813cf7 100644 --- a/webkit/fileapi/file_system_test_helper.h +++ b/webkit/fileapi/file_system_test_helper.h @@ -53,6 +53,9 @@ class FileSystemTestOriginHelper { GURL GetURLForPath(const FilePath& path) const; FilePath GetUsageCachePath() const; + int64 GetCachedOriginUsage() const; + int64 ComputeCurrentOriginUsage() const; + FileSystemOperation* NewOperation( FileSystemCallbackDispatcher* callback_dispatcher); FileSystemOperationContext* NewOperationContext(); @@ -75,6 +78,7 @@ class FileSystemTestOriginHelper { const GURL origin_; const FileSystemType type_; FileSystemFileUtil* file_util_; + int64 initial_usage_size_; }; } // namespace fileapi diff --git a/webkit/fileapi/file_writer_delegate.cc b/webkit/fileapi/file_writer_delegate.cc index 38e7260..83287da 100644 --- a/webkit/fileapi/file_writer_delegate.cc +++ b/webkit/fileapi/file_writer_delegate.cc @@ -35,7 +35,7 @@ class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { base::MessageLoopProxy::CreateForCurrentThread()), error_code_(base::PLATFORM_FILE_OK), file_(file), - context_(context), + context_(*context), callback_(callback) { DCHECK(callback); } @@ -55,13 +55,13 @@ class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { } void ProcessOnTargetThread() { - DCHECK(context_->file_system_context()); - FileSystemQuotaUtil* quota_util = context_->file_system_context()-> - GetQuotaUtil(context_->src_type()); + DCHECK(context_.file_system_context()); + FileSystemQuotaUtil* quota_util = context_.file_system_context()-> + GetQuotaUtil(context_.src_type()); if (quota_util) { DCHECK(quota_util->proxy()); quota_util->proxy()->StartUpdateOrigin( - context_->src_origin_url(), context_->src_type()); + context_.src_origin_url(), context_.src_type()); } if (!base::GetPlatformFileInfo(file_, &file_info_)) error_code_ = base::PLATFORM_FILE_ERROR_FAILED; @@ -73,7 +73,7 @@ class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { base::PlatformFileError error_code_; base::PlatformFile file_; - FileSystemOperationContext* context_; + FileSystemOperationContext context_; InitializeTaskCallback* callback_; base::PlatformFileInfo file_info_; @@ -88,7 +88,7 @@ FileWriterDelegate::FileWriterDelegate( file_(base::kInvalidPlatformFileValue), offset_(offset), proxy_(proxy), - bytes_read_backlog_(0), + bytes_written_backlog_(0), bytes_written_(0), bytes_read_(0), total_bytes_written_(0), @@ -110,10 +110,22 @@ void FileWriterDelegate::OnGetFileInfoAndCallStartUpdate( OnError(error); return; } - if (allowed_bytes_growth_ != QuotaFileUtil::kNoLimit) - allowed_bytes_to_write_ = file_info.size - offset_ + allowed_bytes_growth_; - else + int64 allowed_bytes_growth = + file_system_operation_context()->allowed_bytes_growth(); + if (allowed_bytes_growth == QuotaFileUtil::kNoLimit || + file_system_operation_->file_system_context()->IsStorageUnlimited( + file_system_operation_context()->src_origin_url())) { + // TODO(kinuko): kNoLimit is kint64max therefore all the calculation/ + // comparison with the value should just work, but we should drop + // such implicit assumption and should use an explicit boolean flag + // or something. allowed_bytes_to_write_ = QuotaFileUtil::kNoLimit; + } else { + if (allowed_bytes_growth < 0) + allowed_bytes_growth = 0; + allowed_bytes_to_write_ = file_info.size - offset_ + allowed_bytes_growth; + } + size_ = file_info.size; file_stream_.reset(new net::FileStream(file_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC)); @@ -124,8 +136,6 @@ void FileWriterDelegate::Start(base::PlatformFile file, net::URLRequest* request) { file_ = file; request_ = request; - allowed_bytes_growth_ = - file_system_operation_context()->allowed_bytes_growth(); scoped_refptr<InitializeTask> relay = new InitializeTask( file_, file_system_operation_context(), @@ -211,7 +221,12 @@ void FileWriterDelegate::OnDataReceived(int bytes_read) { } void FileWriterDelegate::Write() { - DCHECK(total_bytes_written_ <= allowed_bytes_to_write_); + // allowed_bytes_to_write could be negative if the file size is + // greater than the current (possibly new) quota. + // (The UI should clear the entire origin data if the smaller quota size + // is set in general, though the UI/deletion code is not there yet.) + DCHECK(total_bytes_written_ <= allowed_bytes_to_write_ || + allowed_bytes_to_write_ < 0); if (total_bytes_written_ >= allowed_bytes_to_write_) { OnError(base::PLATFORM_FILE_ERROR_NO_SPACE); return; @@ -260,23 +275,27 @@ void FileWriterDelegate::OnError(base::PlatformFileError error) { file_system_operation_->DidWrite(error, 0, true); } -void FileWriterDelegate::OnProgress(int bytes_read, bool done) { - DCHECK(bytes_read + bytes_read_backlog_ >= bytes_read_backlog_); - if (bytes_read > 0 && quota_util()) { +void FileWriterDelegate::OnProgress(int bytes_written, bool done) { + DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); + if (bytes_written > 0 && + total_bytes_written_ + bytes_written + offset_ > size_) { + int overlapped = 0; + if (total_bytes_written_ + offset_ < size_) + overlapped = size_ - total_bytes_written_ - offset_; quota_util()->proxy()->UpdateOriginUsage( file_system_operation_->file_system_context()->quota_manager_proxy(), file_system_operation_context()->src_origin_url(), file_system_operation_context()->src_type(), - bytes_read); + bytes_written - overlapped); } static const int kMinProgressDelayMS = 200; base::Time currentTime = base::Time::Now(); if (done || last_progress_event_time_.is_null() || (currentTime - last_progress_event_time_).InMilliseconds() > kMinProgressDelayMS) { - bytes_read += bytes_read_backlog_; + bytes_written += bytes_written_backlog_; last_progress_event_time_ = currentTime; - bytes_read_backlog_ = 0; + bytes_written_backlog_ = 0; if (done && quota_util()) { if (quota_util()) { quota_util()->proxy()->EndUpdateOrigin( @@ -284,10 +303,11 @@ void FileWriterDelegate::OnProgress(int bytes_read, bool done) { file_system_operation_context()->src_type()); } } - file_system_operation_->DidWrite(base::PLATFORM_FILE_OK, bytes_read, done); + file_system_operation_->DidWrite( + base::PLATFORM_FILE_OK, bytes_written, done); return; } - bytes_read_backlog_ += bytes_read; + bytes_written_backlog_ += bytes_written; } FileSystemOperationContext* diff --git a/webkit/fileapi/file_writer_delegate.h b/webkit/fileapi/file_writer_delegate.h index cf59e5a..6d94e6e 100644 --- a/webkit/fileapi/file_writer_delegate.h +++ b/webkit/fileapi/file_writer_delegate.h @@ -65,15 +65,14 @@ class FileWriterDelegate : public net::URLRequest::Delegate { FileSystemOperation* file_system_operation_; base::PlatformFile file_; - base::PlatformFileInfo file_info_; + int64 size_; int64 offset_; scoped_refptr<base::MessageLoopProxy> proxy_; base::Time last_progress_event_time_; - int bytes_read_backlog_; + int bytes_written_backlog_; int bytes_written_; int bytes_read_; int64 total_bytes_written_; - int64 allowed_bytes_growth_; int64 allowed_bytes_to_write_; scoped_refptr<net::IOBufferWithSize> io_buffer_; scoped_ptr<net::FileStream> file_stream_; diff --git a/webkit/fileapi/file_writer_delegate_unittest.cc b/webkit/fileapi/file_writer_delegate_unittest.cc index 1d07fd8..27bf0f1 100644 --- a/webkit/fileapi/file_writer_delegate_unittest.cc +++ b/webkit/fileapi/file_writer_delegate_unittest.cc @@ -11,6 +11,7 @@ #include <string> #include <vector> +#include "base/basictypes.h" #include "base/file_util_proxy.h" #include "base/message_loop.h" #include "base/scoped_temp_dir.h" @@ -65,19 +66,49 @@ class Result { bool complete_; }; +const char kData[] = "The quick brown fox jumps over the lazy dog.\n"; +const int kDataSize = ARRAYSIZE_UNSAFE(kData) - 1; + } // namespace (anonymous) class FileWriterDelegateTest : public PlatformTest { public: FileWriterDelegateTest() - : loop_(MessageLoop::TYPE_IO) {} + : loop_(MessageLoop::TYPE_IO), + file_(base::kInvalidPlatformFileValue) {} protected: virtual void SetUp(); virtual void TearDown(); - int64 GetCachedUsage() { - return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath()); + virtual void SetUpTestHelper(const FilePath& base_dir) { + test_helper_.SetUp(base_dir, QuotaFileUtil::GetInstance()); + } + + int64 ComputeCurrentOriginUsage() { + base::FlushPlatformFile(file_); + return test_helper_.ComputeCurrentOriginUsage(); + } + + // Creates and sets up a FileWriterDelegate for writing the given |blob_url| + // to a file (file_) from |offset| with |allowed_growth| quota setting. + void PrepareForWrite(const GURL& blob_url, + int64 offset, + int64 allowed_growth) { + bool created; + base::PlatformFileError error_code; + file_ = base::CreatePlatformFile( + file_path_, + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | + base::PLATFORM_FILE_ASYNC, + &created, &error_code); + ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); + + result_.reset(new Result()); + file_writer_delegate_.reset(new FileWriterDelegate( + CreateNewOperation(result_.get(), allowed_growth), + offset, base::MessageLoopProxy::CreateForCurrentThread())); + request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); } FileSystemOperation* CreateNewOperation(Result* result, int64 quota); @@ -90,7 +121,6 @@ class FileWriterDelegateTest : public PlatformTest { FileSystemTestOriginHelper test_helper_; MessageLoop loop_; - ScopedTempDir dir_; FilePath file_path_; PlatformFile file_; @@ -189,29 +219,14 @@ net::URLRequestJob* FileWriterDelegateTest::Factory( void FileWriterDelegateTest::SetUp() { ASSERT_TRUE(dir_.CreateUniqueTempDir()); FilePath base_dir = dir_.path().AppendASCII("filesystem"); - test_helper_.SetUp(base_dir, QuotaFileUtil::GetInstance()); - - FilePath filesystem_dir = test_helper_.GetOriginRootPath(); - + SetUpTestHelper(base_dir); ASSERT_TRUE(file_util::CreateTemporaryFileInDir( - filesystem_dir, &file_path_)); - bool created; - base::PlatformFileError error_code; - file_ = base::CreatePlatformFile( - file_path_, - base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | - base::PLATFORM_FILE_ASYNC, - &created, &error_code); - ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); - - result_.reset(new Result()); - + test_helper_.GetOriginRootPath(), &file_path_)); net::URLRequest::RegisterProtocolFactory("blob", &Factory); } void FileWriterDelegateTest::TearDown() { net::URLRequest::RegisterProtocolFactory("blob", NULL); - result_.reset(); base::ClosePlatformFile(file_); test_helper_.TearDown(); } @@ -227,19 +242,18 @@ FileSystemOperation* FileWriterDelegateTest::CreateNewOperation( } TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { - GURL blob_url("blob:nolimit"); - content_ = "The quick brown fox jumps over the lazy dog.\n"; - file_writer_delegate_.reset(new FileWriterDelegate( - CreateNewOperation(result_.get(), QuotaFileUtil::kNoLimit), - 0, base::MessageLoopProxy::CreateForCurrentThread())); - request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); + const GURL kBlobURL("blob:nolimit"); + content_ = kData; - ASSERT_EQ(0, GetCachedUsage()); + PrepareForWrite(kBlobURL, 0, QuotaFileUtil::kNoLimit); + + ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); file_writer_delegate_->Start(file_, request_.get()); MessageLoop::current()->Run(); - ASSERT_EQ(45, GetCachedUsage()); + ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); - EXPECT_EQ(45, result_->bytes_written()); + EXPECT_EQ(kDataSize, result_->bytes_written()); EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); EXPECT_TRUE(result_->complete()); @@ -247,61 +261,58 @@ TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { } TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) { - GURL blob_url("blob:just"); - content_ = "The quick brown fox jumps over the lazy dog.\n"; - file_writer_delegate_.reset(new FileWriterDelegate( - CreateNewOperation(result_.get(), 45), - 0, base::MessageLoopProxy::CreateForCurrentThread())); - request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); + const GURL kBlobURL("blob:just"); + content_ = kData; + const int64 kAllowedGrowth = kDataSize; + PrepareForWrite(kBlobURL, 0, kAllowedGrowth); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); file_writer_delegate_->Start(file_, request_.get()); MessageLoop::current()->Run(); - ASSERT_EQ(45, GetCachedUsage()); + ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); file_writer_delegate_.reset(); - EXPECT_EQ(45, result_->bytes_written()); + EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); EXPECT_TRUE(result_->complete()); } TEST_F(FileWriterDelegateTest, WriteFailureByQuota) { - GURL blob_url("blob:failure"); - content_ = "The quick brown fox jumps over the lazy dog.\n"; - file_writer_delegate_.reset(new FileWriterDelegate( - CreateNewOperation(result_.get(), 44), - 0, base::MessageLoopProxy::CreateForCurrentThread())); - request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); + const GURL kBlobURL("blob:failure"); + content_ = kData; + const int64 kAllowedGrowth = kDataSize - 1; + PrepareForWrite(kBlobURL, 0, kAllowedGrowth); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); file_writer_delegate_->Start(file_, request_.get()); MessageLoop::current()->Run(); - ASSERT_EQ(44, GetCachedUsage()); + ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); file_writer_delegate_.reset(); - EXPECT_EQ(44, result_->bytes_written()); + EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); EXPECT_TRUE(result_->complete()); } TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) { - GURL blob_url("blob:zero"); + const GURL kBlobURL("blob:zero"); content_ = ""; - file_writer_delegate_.reset(new FileWriterDelegate( - CreateNewOperation(result_.get(), 0), - 0, base::MessageLoopProxy::CreateForCurrentThread())); - request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); + int64 kAllowedGrowth = 0; + PrepareForWrite(kBlobURL, 0, kAllowedGrowth); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); file_writer_delegate_->Start(file_, request_.get()); MessageLoop::current()->Run(); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); file_writer_delegate_.reset(); - EXPECT_EQ(0, result_->bytes_written()); + EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); EXPECT_TRUE(result_->complete()); } @@ -311,46 +322,161 @@ TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimitConcurrent) { scoped_ptr<net::URLRequest> request2; scoped_ptr<Result> result2; + FilePath file_path2; PlatformFile file2; bool created; base::PlatformFileError error_code; + ASSERT_TRUE(file_util::CreateTemporaryFileInDir( + test_helper_.GetOriginRootPath(), &file_path2)); file2 = base::CreatePlatformFile( - file_path_, + file_path2, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC, &created, &error_code); ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); - result2.reset(new Result()); + const GURL kBlobURL("blob:nolimitconcurrent"); + const GURL kBlobURL2("blob:nolimitconcurrent2"); + content_ = kData; - GURL blob_url("blob:nolimitconcurrent"); - GURL blob_url2("blob:nolimitconcurrent2"); - content_ = "The quick brown fox jumps over the lazy dog.\n"; - file_writer_delegate_.reset(new FileWriterDelegate( - CreateNewOperation(result_.get(), QuotaFileUtil::kNoLimit), - 0, base::MessageLoopProxy::CreateForCurrentThread())); + PrepareForWrite(kBlobURL, 0, QuotaFileUtil::kNoLimit); + + // Credate another FileWriterDelegate for concurrent write. + result2.reset(new Result()); file_writer_delegate2.reset(new FileWriterDelegate( CreateNewOperation(result2.get(), QuotaFileUtil::kNoLimit), 0, base::MessageLoopProxy::CreateForCurrentThread())); - request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); - request2.reset(new net::URLRequest(blob_url2, file_writer_delegate2.get())); + request2.reset(new net::URLRequest(kBlobURL2, file_writer_delegate2.get())); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); file_writer_delegate_->Start(file_, request_.get()); file_writer_delegate2->Start(file2, request2.get()); MessageLoop::current()->Run(); if (!result_->complete() || !result2->complete()) MessageLoop::current()->Run(); - ASSERT_EQ(90, GetCachedUsage()); + + ASSERT_EQ(kDataSize * 2, test_helper_.GetCachedOriginUsage()); + base::FlushPlatformFile(file2); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); file_writer_delegate_.reset(); - EXPECT_EQ(45, result_->bytes_written()); + EXPECT_EQ(kDataSize, result_->bytes_written()); EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); EXPECT_TRUE(result_->complete()); - EXPECT_EQ(45, result2->bytes_written()); + EXPECT_EQ(kDataSize, result2->bytes_written()); EXPECT_EQ(base::PLATFORM_FILE_OK, result2->status()); EXPECT_TRUE(result2->complete()); + + base::ClosePlatformFile(file2); +} + +TEST_F(FileWriterDelegateTest, WritesWithQuotaAndOffset) { + const GURL kBlobURL("blob:failure-with-updated-quota"); + content_ = kData; + + // Writing kDataSize (=45) bytes data while allowed_growth is 100. + int64 offset = 0; + int64 allowed_growth = 100; + ASSERT_LT(kDataSize, allowed_growth); + PrepareForWrite(kBlobURL, offset, allowed_growth); + + ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); + file_writer_delegate_->Start(file_, request_.get()); + MessageLoop::current()->Run(); + ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(kDataSize, result_->bytes_written()); + EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); + EXPECT_TRUE(result_->complete()); + + // Trying to overwrite kDataSize bytes data while allowed_growth is 20. + offset = 0; + allowed_growth = 20; + PrepareForWrite(kBlobURL, offset, allowed_growth); + + file_writer_delegate_->Start(file_, request_.get()); + MessageLoop::current()->Run(); + EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(kDataSize, result_->bytes_written()); + EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); + EXPECT_TRUE(result_->complete()); + + // Trying to write kDataSize bytes data from offset 25 while + // allowed_growth is 55. + offset = 25; + allowed_growth = 55; + PrepareForWrite(kBlobURL, offset, allowed_growth); + + file_writer_delegate_->Start(file_, request_.get()); + MessageLoop::current()->Run(); + EXPECT_EQ(offset + kDataSize, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(kDataSize, result_->bytes_written()); + EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); + EXPECT_TRUE(result_->complete()); + + // Trying to overwrite 45 bytes data while allowed_growth is -20. + offset = 0; + allowed_growth = -20; + PrepareForWrite(kBlobURL, offset, allowed_growth); + + int64 pre_write_usage = ComputeCurrentOriginUsage(); + file_writer_delegate_->Start(file_, request_.get()); + MessageLoop::current()->Run(); + EXPECT_EQ(pre_write_usage, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(kDataSize, result_->bytes_written()); + EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); + EXPECT_TRUE(result_->complete()); + + // Trying to overwrite 45 bytes data with offset pre_write_usage - 20, + // while allowed_growth is 10. + const int kOverlap = 20; + offset = pre_write_usage - kOverlap; + allowed_growth = 10; + PrepareForWrite(kBlobURL, offset, allowed_growth); + + file_writer_delegate_->Start(file_, request_.get()); + MessageLoop::current()->Run(); + EXPECT_EQ(pre_write_usage + allowed_growth, + test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(kOverlap + allowed_growth, result_->bytes_written()); + EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); + EXPECT_TRUE(result_->complete()); +} + +class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest { + protected: + virtual void SetUpTestHelper(const FilePath& path) OVERRIDE; +}; + +void FileWriterDelegateUnlimitedTest::SetUpTestHelper(const FilePath& path) { + test_helper_.SetUp( + path, + false /* incognito */, + true /* unlimited */, + NULL /* quota manager proxy */, + QuotaFileUtil::GetInstance()); +} + +TEST_F(FileWriterDelegateUnlimitedTest, WriteWithQuota) { + const GURL kBlobURL("blob:with-unlimited"); + content_ = kData; + + // Set small allowed_growth bytes + PrepareForWrite(kBlobURL, 0, 10); + + // We shouldn't fail as the context is configured as 'unlimited'. + file_writer_delegate_->Start(file_, request_.get()); + MessageLoop::current()->Run(); + EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); + EXPECT_EQ(kDataSize, result_->bytes_written()); + EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); + EXPECT_TRUE(result_->complete()); } } // namespace fileapi diff --git a/webkit/fileapi/quota_file_util_unittest.cc b/webkit/fileapi/quota_file_util_unittest.cc index 3322bd6..df59c70 100644 --- a/webkit/fileapi/quota_file_util_unittest.cc +++ b/webkit/fileapi/quota_file_util_unittest.cc @@ -67,9 +67,8 @@ class QuotaFileUtilTest : public testing::Test { context.get(), Path(file_name), created); } - int64 GetCachedUsage() { - return FileSystemUsageCache::GetUsage( - quota_test_helper_.GetUsageCachePath()); + const FileSystemTestOriginHelper& quota_test_helper() const { + return quota_test_helper_; } private: @@ -119,7 +118,9 @@ TEST_F(QuotaFileUtilTest, Truncate) { QuotaFileUtil::GetInstance()->Truncate(truncate_context.get(), Path(file_name), 1020)); - ASSERT_EQ(1020, GetCachedUsage()); + ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); truncate_context.reset(NewContext()); truncate_context->set_allowed_bytes_growth(0); @@ -127,7 +128,9 @@ TEST_F(QuotaFileUtilTest, Truncate) { QuotaFileUtil::GetInstance()->Truncate(truncate_context.get(), Path(file_name), 0)); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(0, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); truncate_context.reset(NewContext()); truncate_context->set_allowed_bytes_growth(1020); @@ -135,7 +138,9 @@ TEST_F(QuotaFileUtilTest, Truncate) { QuotaFileUtil::GetInstance()->Truncate(truncate_context.get(), Path(file_name), 1021)); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(0, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); } TEST_F(QuotaFileUtilTest, CopyFile) { @@ -156,7 +161,9 @@ TEST_F(QuotaFileUtilTest, CopyFile) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(from_file), 1020)); - ASSERT_EQ(1020, GetCachedUsage()); + ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); @@ -164,7 +171,9 @@ TEST_F(QuotaFileUtilTest, CopyFile) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(obstacle_file), 1)); - ASSERT_EQ(1021, GetCachedUsage()); + ASSERT_EQ(1021, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(1020); @@ -172,7 +181,9 @@ TEST_F(QuotaFileUtilTest, CopyFile) { QuotaFileUtil::GetInstance()->Copy(context.get(), Path(from_file), Path(to_file1))); - ASSERT_EQ(2041, GetCachedUsage()); + ASSERT_EQ(2041, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(1019); @@ -180,7 +191,9 @@ TEST_F(QuotaFileUtilTest, CopyFile) { QuotaFileUtil::GetInstance()->Copy(context.get(), Path(from_file), Path(to_file2))); - ASSERT_EQ(2041, GetCachedUsage()); + ASSERT_EQ(2041, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(1019); @@ -188,7 +201,9 @@ TEST_F(QuotaFileUtilTest, CopyFile) { QuotaFileUtil::GetInstance()->Copy(context.get(), Path(from_file), Path(obstacle_file))); - ASSERT_EQ(3060, GetCachedUsage()); + ASSERT_EQ(3060, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); } TEST_F(QuotaFileUtilTest, CopyDirectory) { @@ -213,7 +228,9 @@ TEST_F(QuotaFileUtilTest, CopyDirectory) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(from_file), 1020)); - ASSERT_EQ(1020, GetCachedUsage()); + ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(1020); @@ -221,7 +238,9 @@ TEST_F(QuotaFileUtilTest, CopyDirectory) { QuotaFileUtil::GetInstance()->Copy(context.get(), Path(from_dir), Path(to_dir1))); - ASSERT_EQ(2040, GetCachedUsage()); + ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(1019); @@ -229,7 +248,9 @@ TEST_F(QuotaFileUtilTest, CopyDirectory) { QuotaFileUtil::GetInstance()->Copy(context.get(), Path(from_dir), Path(to_dir2))); - ASSERT_EQ(2040, GetCachedUsage()); + ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); } TEST_F(QuotaFileUtilTest, MoveFile) { @@ -247,7 +268,9 @@ TEST_F(QuotaFileUtilTest, MoveFile) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(from_file), 1020)); - ASSERT_EQ(1020, GetCachedUsage()); + ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(0); @@ -255,7 +278,9 @@ TEST_F(QuotaFileUtilTest, MoveFile) { QuotaFileUtil::GetInstance()->Move(context.get(), Path(from_file), Path(to_file))); - ASSERT_EQ(1020, GetCachedUsage()); + ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); ASSERT_TRUE(created); @@ -268,7 +293,9 @@ TEST_F(QuotaFileUtilTest, MoveFile) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(from_file), 1020)); - ASSERT_EQ(2040, GetCachedUsage()); + ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); @@ -276,7 +303,9 @@ TEST_F(QuotaFileUtilTest, MoveFile) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(obstacle_file), 1)); - ASSERT_EQ(2041, GetCachedUsage()); + ASSERT_EQ(2041, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(0); @@ -284,7 +313,9 @@ TEST_F(QuotaFileUtilTest, MoveFile) { QuotaFileUtil::GetInstance()->Move(context.get(), Path(from_file), Path(obstacle_file))); - ASSERT_EQ(2040, GetCachedUsage()); + ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); } TEST_F(QuotaFileUtilTest, MoveDirectory) { @@ -309,7 +340,9 @@ TEST_F(QuotaFileUtilTest, MoveDirectory) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(from_file), 1020)); - ASSERT_EQ(1020, GetCachedUsage()); + ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(1020); @@ -317,7 +350,9 @@ TEST_F(QuotaFileUtilTest, MoveDirectory) { QuotaFileUtil::GetInstance()->Move(context.get(), Path(from_dir), Path(to_dir1))); - ASSERT_EQ(1020, GetCachedUsage()); + ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); ASSERT_EQ(base::PLATFORM_FILE_OK, @@ -333,7 +368,9 @@ TEST_F(QuotaFileUtilTest, MoveDirectory) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(from_file), 1020)); - ASSERT_EQ(2040, GetCachedUsage()); + ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(1019); @@ -341,7 +378,9 @@ TEST_F(QuotaFileUtilTest, MoveDirectory) { QuotaFileUtil::GetInstance()->Move(context.get(), Path(from_dir), Path(to_dir2))); - ASSERT_EQ(2040, GetCachedUsage()); + ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); } TEST_F(QuotaFileUtilTest, Remove) { @@ -370,7 +409,9 @@ TEST_F(QuotaFileUtilTest, Remove) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(file), 340)); - ASSERT_EQ(340, GetCachedUsage()); + ASSERT_EQ(340, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); @@ -378,7 +419,9 @@ TEST_F(QuotaFileUtilTest, Remove) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(dfile1), 1020)); - ASSERT_EQ(1360, GetCachedUsage()); + ASSERT_EQ(1360, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); @@ -386,7 +429,9 @@ TEST_F(QuotaFileUtilTest, Remove) { QuotaFileUtil::GetInstance()->Truncate(context.get(), Path(dfile2), 120)); - ASSERT_EQ(1480, GetCachedUsage()); + ASSERT_EQ(1480, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); @@ -394,7 +439,9 @@ TEST_F(QuotaFileUtilTest, Remove) { QuotaFileUtil::GetInstance()->Delete(context.get(), Path(file), false)); - ASSERT_EQ(1140, GetCachedUsage()); + ASSERT_EQ(1140, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); context.reset(NewContext()); context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); @@ -402,7 +449,9 @@ TEST_F(QuotaFileUtilTest, Remove) { QuotaFileUtil::GetInstance()->Delete(context.get(), Path(dir), true)); - ASSERT_EQ(0, GetCachedUsage()); + ASSERT_EQ(0, quota_test_helper().GetCachedOriginUsage()); + ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(), + quota_test_helper().GetCachedOriginUsage()); } } // namespace fileapi |