Skip to content

Commit

Permalink
Implement SyncableFileSystemOperation::CopyInForeignFile
Browse files Browse the repository at this point in the history
The private developer API that writes/reads data to/from SyncFS
via FileSystemOperation layer may directly call
this method, and therefore this operation should also cooperate with
other SyncFS operation scheduling.

BUG=none
TEST=SyncableFileSystemOperationRunner.CopyInForeignFile

Review URL: https://codereview.chromium.org/13910013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194855 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kinuko@chromium.org committed Apr 18, 2013
1 parent 1841da3 commit 24601d6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
6 changes: 3 additions & 3 deletions webkit/fileapi/local_file_system_operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class WEBKIT_STORAGE_EXPORT LocalFileSystemOperation
// - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and
// its parent path is a file.
//
void CopyInForeignFile(const base::FilePath& src_local_disk_path,
const FileSystemURL& dest_url,
const StatusCallback& callback);
virtual void CopyInForeignFile(const base::FilePath& src_local_disk_path,
const FileSystemURL& dest_url,
const StatusCallback& callback);

// Removes a single file.
//
Expand Down
47 changes: 47 additions & 0 deletions webkit/fileapi/syncable/syncable_file_operation_runner_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>

#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
Expand Down Expand Up @@ -55,6 +56,7 @@ class SyncableFileOperationRunnerTest : public testing::Test {
weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}

virtual void SetUp() OVERRIDE {
ASSERT_TRUE(dir_.CreateUniqueTempDir());
file_system_.SetUp();
sync_context_ = new LocalFileSyncContext(base::MessageLoopProxy::current(),
base::MessageLoopProxy::current());
Expand Down Expand Up @@ -119,6 +121,12 @@ class SyncableFileOperationRunnerTest : public testing::Test {
++callback_count_;
}

bool CreateTempFile(base::FilePath* path) {
return file_util::CreateTemporaryFileInDir(dir_.path(), path);
}

base::ScopedTempDir dir_;

MessageLoop message_loop_;
CannedSyncableFileSystem file_system_;
scoped_refptr<LocalFileSyncContext> sync_context_;
Expand Down Expand Up @@ -321,4 +329,43 @@ TEST_F(SyncableFileOperationRunnerTest, QueueAndCancel) {
EXPECT_EQ(2, callback_count_);
}

// Test if CopyInForeignFile runs cooperatively with other Sync operations
// when it is called directly via AsLocalFileSystemOperation.
TEST_F(SyncableFileOperationRunnerTest, CopyInForeignFile) {
const std::string kTestData("test data");

base::FilePath temp_path;
ASSERT_TRUE(CreateTempFile(&temp_path));
ASSERT_EQ(static_cast<int>(kTestData.size()),
file_util::WriteFile(
temp_path, kTestData.data(), kTestData.size()));

sync_status()->StartSyncing(URL(kFile));
ASSERT_FALSE(sync_status()->IsWritable(URL(kFile)));

// The URL is in syncing so CopyIn (which is a write operation) won't run.
ResetCallbackStatus();
file_system_.NewOperation()->AsLocalFileSystemOperation()->CopyInForeignFile(
temp_path, URL(kFile),
ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK));
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, callback_count_);

// End syncing (to enable write).
sync_status()->EndSyncing(URL(kFile));
ASSERT_TRUE(sync_status()->IsWritable(URL(kFile)));

ResetCallbackStatus();
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, callback_count_);

// Now the file must have been created and have the same content as temp_path.
ResetCallbackStatus();
file_system_.DoVerifyFile(
URL(kFile), kTestData,
ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK));
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, callback_count_);
}

} // namespace sync_file_system
21 changes: 21 additions & 0 deletions webkit/fileapi/syncable/syncable_file_system_operation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,27 @@ void SyncableFileSystemOperation::CreateSnapshotFile(
delete this;
}

void SyncableFileSystemOperation::CopyInForeignFile(
const base::FilePath& src_local_disk_path,
const FileSystemURL& dest_url,
const StatusCallback& callback) {
DCHECK(CalledOnValidThread());
if (!operation_runner_) {
AbortOperation(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND);
return;
}
DCHECK(operation_runner_.get());
target_paths_.push_back(dest_url);
completion_callback_ = callback;
scoped_ptr<SyncableFileOperationRunner::Task> task(new QueueableTask(
this,
base::Bind(&LocalFileSystemOperation::CopyInForeignFile,
base::Unretained(NewOperation()),
src_local_disk_path, dest_url,
base::Bind(&self::DidFinish, base::Owned(this)))));
operation_runner_->PostOperationTask(task.Pass());
}

SyncableFileSystemOperation::SyncableFileSystemOperation(
fileapi::FileSystemContext* file_system_context,
scoped_ptr<FileSystemOperationContext> operation_context)
Expand Down
5 changes: 5 additions & 0 deletions webkit/fileapi/syncable/syncable_file_system_operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class WEBKIT_STORAGE_EXPORT SyncableFileSystemOperation
const fileapi::FileSystemURL& path,
const SnapshotFileCallback& callback) OVERRIDE;

// LocalFileSystemOperation overrides.
virtual void CopyInForeignFile(const base::FilePath& src_local_disk_path,
const fileapi::FileSystemURL& dest_url,
const StatusCallback& callback) OVERRIDE;

private:
typedef SyncableFileSystemOperation self;
class QueueableTask;
Expand Down

0 comments on commit 24601d6

Please sign in to comment.