Skip to content

Commit

Permalink
Implement PrepareForProcessRemoteChange for local file sync service
Browse files Browse the repository at this point in the history
BUG=158026,157867
TEST=LocalFileSyncContextTest.*
TEST=tests for LocalFileSyncService should also come later

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164599 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kinuko@chromium.org committed Oct 29, 2012
1 parent 8bbe31e commit 5d847d4
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 86 deletions.
8 changes: 5 additions & 3 deletions chrome/browser/sync_file_system/local_file_sync_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void LocalFileSyncService::MaybeInitializeFileSystemContext(
AsWeakPtr(), app_origin, file_system_context, callback));
}

void LocalFileSyncService::ProcessChange(
void LocalFileSyncService::ProcessLocalChange(
LocalChangeProcessor* processor,
const SyncCompletionCallback& callback) {
// TODO(kinuko): implement.
Expand All @@ -53,8 +53,10 @@ void LocalFileSyncService::ProcessChange(
void LocalFileSyncService::PrepareForProcessRemoteChange(
const fileapi::FileSystemURL& url,
const PrepareChangeCallback& callback) {
// TODO(kinuko): implement.
NOTIMPLEMENTED();
DCHECK(ContainsKey(origin_to_contexts_, url.origin()));
sync_context_->PrepareForSync(
origin_to_contexts_[url.origin()],
url, callback);
}

void LocalFileSyncService::ApplyRemoteChange(
Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/sync_file_system/local_file_sync_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ class LocalFileSyncService
fileapi::FileSystemContext* file_system_context,
const fileapi::StatusCallback& callback);

// Synchronize one local change (to the remote server) using |processor|.
// Synchronize one (or a set of) local change(s) to the remote server
// using |processor|.
// |processor| must have same or longer lifetime than this service.
void ProcessChange(LocalChangeProcessor* processor,
const fileapi::SyncCompletionCallback& callback);
void ProcessLocalChange(LocalChangeProcessor* processor,
const fileapi::SyncCompletionCallback& callback);

// RemoteChangeProcessor overrides.
virtual void PrepareForProcessRemoteChange(
Expand Down
11 changes: 9 additions & 2 deletions chrome/browser/sync_file_system/remote_change_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "webkit/fileapi/syncable/sync_callbacks.h"
#include "webkit/fileapi/syncable/sync_file_type.h"
#include "webkit/fileapi/syncable/sync_status_code.h"

class FilePath;

namespace fileapi {
class FileChange;
class FileChangeSet;
class FileChangeList;
class FileSystemURL;
}

Expand All @@ -25,9 +26,15 @@ namespace sync_file_system {
// This interface is to be implemented/backed by LocalSyncFileService.
class RemoteChangeProcessor {
public:
// Callback type for PrepareForProcessRemoteChange.
// |file_type| indicates the current file/directory type of the target
// URL in the local filesystem. If the target URL does not exist it is
// set to SYNC_FILE_TYPE_UNKNOWN.
// |changes| indicates a set of pending changes for the target URL.
typedef base::Callback<void(
fileapi::SyncStatusCode status,
fileapi::FileChangeSet& changes)> PrepareChangeCallback;
fileapi::SyncFileType file_type,
const fileapi::FileChangeList& changes)> PrepareChangeCallback;

RemoteChangeProcessor() {}
virtual ~RemoteChangeProcessor() {}
Expand Down
47 changes: 43 additions & 4 deletions webkit/fileapi/syncable/canned_syncable_file_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "webkit/fileapi/syncable/canned_syncable_file_system.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/message_loop_proxy.h"
#include "base/single_thread_task_runner.h"
Expand All @@ -18,6 +19,7 @@
#include "webkit/fileapi/isolated_context.h"
#include "webkit/fileapi/local_file_system_operation.h"
#include "webkit/fileapi/mock_file_system_options.h"
#include "webkit/fileapi/syncable/local_file_change_tracker.h"
#include "webkit/fileapi/syncable/local_file_sync_context.h"
#include "webkit/quota/mock_special_storage_policy.h"
#include "webkit/quota/quota_manager.h"
Expand All @@ -43,7 +45,6 @@ void AssignAndQuit(base::TaskRunner* original_task_runner,
original_task_runner->PostTask(FROM_HERE, base::Bind(&Quit));
}


template <typename R>
R RunOnThread(
base::SingleThreadTaskRunner* task_runner,
Expand All @@ -59,6 +60,18 @@ R RunOnThread(
return result;
}

void RunOnThread(base::SingleThreadTaskRunner* task_runner,
const tracked_objects::Location& location,
const base::Closure& task) {
task_runner->PostTaskAndReply(
location, task,
base::Bind(base::IgnoreResult(
base::Bind(&base::MessageLoopProxy::PostTask,
base::MessageLoopProxy::current(),
FROM_HERE, base::Bind(&Quit)))));
MessageLoop::current()->Run();
}

void EnsureRunningOn(base::SingleThreadTaskRunner* runner) {
EXPECT_TRUE(runner->RunsTasksOnCurrentThread());
}
Expand Down Expand Up @@ -119,12 +132,14 @@ void DidGetUsageAndQuota(const quota::StatusCallback& callback,

CannedSyncableFileSystem::CannedSyncableFileSystem(
const GURL& origin, const std::string& service,
base::SingleThreadTaskRunner* io_task_runner)
base::SingleThreadTaskRunner* io_task_runner,
base::SingleThreadTaskRunner* file_task_runner)
: service_name_(service),
test_helper_(origin, kFileSystemTypeSyncable),
result_(base::PLATFORM_FILE_OK),
sync_status_(SYNC_STATUS_OK),
io_task_runner_(io_task_runner),
file_task_runner_(file_task_runner),
is_filesystem_set_up_(false),
is_filesystem_opened_(false) {
}
Expand All @@ -146,7 +161,10 @@ void CannedSyncableFileSystem::SetUp() {
storage_policy);

file_system_context_ = new FileSystemContext(
FileSystemTaskRunners::CreateMockTaskRunners(),
make_scoped_ptr(new FileSystemTaskRunners(
io_task_runner_,
file_task_runner_,
file_task_runner_)),
storage_policy,
quota_manager_->proxy(),
data_dir_.path(),
Expand Down Expand Up @@ -180,7 +198,7 @@ PlatformFileError CannedSyncableFileSystem::OpenFileSystem() {
true /* create */,
base::Bind(&CannedSyncableFileSystem::DidOpenFileSystem,
base::Unretained(this)));
MessageLoop::current()->RunAllPending();
MessageLoop::current()->Run();
return result_;
}

Expand Down Expand Up @@ -309,6 +327,26 @@ quota::QuotaStatusCode CannedSyncableFileSystem::GetUsageAndQuota(
base::Unretained(this), usage, quota));
}

void CannedSyncableFileSystem::GetChangedURLsInTracker(
std::vector<FileSystemURL>* urls) {
return RunOnThread(
file_task_runner_,
FROM_HERE,
base::Bind(&LocalFileChangeTracker::GetChangedURLs,
base::Unretained(file_system_context_->change_tracker()),
urls));
}

void CannedSyncableFileSystem::FinalizeSyncForURLInTracker(
const FileSystemURL& url) {
return RunOnThread(
file_task_runner_,
FROM_HERE,
base::Bind(&LocalFileChangeTracker::FinalizeSyncForURL,
base::Unretained(file_system_context_->change_tracker()),
url));
}

FileSystemOperation* CannedSyncableFileSystem::NewOperation() {
return file_system_context_->CreateFileSystemOperation(URL(""), NULL);
}
Expand Down Expand Up @@ -409,6 +447,7 @@ void CannedSyncableFileSystem::DidOpenFileSystem(
result_ = result;
root_url_ = root;
is_filesystem_opened_ = true;
MessageLoop::current()->Quit();
}

void CannedSyncableFileSystem::DidInitializeFileSystemContext(
Expand Down
11 changes: 9 additions & 2 deletions webkit/fileapi/syncable/canned_syncable_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class CannedSyncableFileSystem {

CannedSyncableFileSystem(const GURL& origin,
const std::string& service,
base::SingleThreadTaskRunner* io_task_runner);
base::SingleThreadTaskRunner* io_task_runner,
base::SingleThreadTaskRunner* file_task_runner);
~CannedSyncableFileSystem();

// SetUp must be called before using this instance.
Expand Down Expand Up @@ -79,7 +80,8 @@ class CannedSyncableFileSystem {

// Helper routines to perform file system operations.
// OpenFileSystem() must have been called before calling any of them.
// They run on io_task_runner.
// They create an operation and run it on IO task runner, and the operation
// posts a task on file runner.
base::PlatformFileError CreateDirectory(const FileSystemURL& url);
base::PlatformFileError CreateFile(const FileSystemURL& url);
base::PlatformFileError Copy(const FileSystemURL& src_url,
Expand All @@ -102,6 +104,10 @@ class CannedSyncableFileSystem {
// Retrieves the quota and usage.
quota::QuotaStatusCode GetUsageAndQuota(int64* usage, int64* quota);

// ChangeTracker related methods. They run on file task runner.
void GetChangedURLsInTracker(std::vector<FileSystemURL>* urls);
void FinalizeSyncForURLInTracker(const FileSystemURL& url);

// Returns new FileSystemOperation.
FileSystemOperation* NewOperation();

Expand Down Expand Up @@ -155,6 +161,7 @@ class CannedSyncableFileSystem {
SyncStatusCode sync_status_;

scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;

// Boolean flags mainly for helping debug.
bool is_filesystem_set_up_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LocalFileChangeTrackerTest : public testing::Test {
LocalFileChangeTrackerTest()
: message_loop_(MessageLoop::TYPE_IO),
file_system_(GURL("http://example.com"), "test",
base::MessageLoopProxy::current(),
base::MessageLoopProxy::current()) {}

virtual void SetUp() OVERRIDE {
Expand Down
71 changes: 55 additions & 16 deletions webkit/fileapi/syncable/local_file_sync_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

#include "base/bind.h"
#include "base/location.h"
#include "base/platform_file.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/task_runner_util.h"
#include "webkit/fileapi/file_system_context.h"
#include "webkit/fileapi/file_system_file_util.h"
#include "webkit/fileapi/file_system_operation_context.h"
#include "webkit/fileapi/file_system_task_runners.h"
#include "webkit/fileapi/local_file_system_operation.h"
#include "webkit/fileapi/syncable/file_change.h"
Expand Down Expand Up @@ -79,17 +82,16 @@ void LocalFileSyncContext::PrepareForSync(
return;
}
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
if (sync_status()->IsWriting(url)) {
ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(callback, SYNC_STATUS_FILE_BUSY, FileChangeList()));
return;
}
sync_status()->StartSyncing(url);
const bool writing = sync_status()->IsWriting(url);
// Disable writing if it's ready to be synced.
if (!writing)
sync_status()->StartSyncing(url);
ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(&LocalFileSyncContext::DidDisabledWritesForPrepareForSync,
this, make_scoped_refptr(file_system_context), url, callback));
base::Bind(&LocalFileSyncContext::DidGetWritingStatusForPrepareForSync,
this, make_scoped_refptr(file_system_context),
writing ? SYNC_STATUS_FILE_BUSY : SYNC_STATUS_OK,
url, callback));
}

void LocalFileSyncContext::RegisterURLForWaitingSync(
Expand Down Expand Up @@ -285,21 +287,58 @@ void LocalFileSyncContext::DidInitialize(
pending_initialize_callbacks_.erase(file_system_context);
}

void LocalFileSyncContext::DidDisabledWritesForPrepareForSync(
void LocalFileSyncContext::DidGetWritingStatusForPrepareForSync(
FileSystemContext* file_system_context,
SyncStatusCode status,
const FileSystemURL& url,
const ChangeListCallback& callback) {
DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
if (shutdown_on_ui_) {
callback.Run(SYNC_STATUS_ABORT, FileChangeList());
// This gets called on UI thread and relays the task on FILE thread.
DCHECK(file_system_context);
if (!file_system_context->task_runners()->file_task_runner()->
RunsTasksOnCurrentThread()) {
DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
if (shutdown_on_ui_) {
callback.Run(SYNC_STATUS_ABORT, SYNC_FILE_TYPE_UNKNOWN, FileChangeList());
return;
}
file_system_context->task_runners()->file_task_runner()->PostTask(
FROM_HERE,
base::Bind(&LocalFileSyncContext::DidGetWritingStatusForPrepareForSync,
this, make_scoped_refptr(file_system_context),
status, url, callback));
return;
}
DCHECK(file_system_context);
DCHECK(file_system_context->change_tracker());

DCHECK(file_system_context->change_tracker());
FileChangeList changes;
file_system_context->change_tracker()->GetChangesForURL(url, &changes);
callback.Run(SYNC_STATUS_OK, changes);

FilePath platform_path;
base::PlatformFileInfo file_info;
FileSystemFileUtil* file_util = file_system_context->GetFileUtil(url.type());
DCHECK(file_util);
base::PlatformFileError file_error = file_util->GetFileInfo(
make_scoped_ptr(
new FileSystemOperationContext(file_system_context)).get(),
url,
&file_info,
&platform_path);
if (status == SYNC_STATUS_OK &&
file_error != base::PLATFORM_FILE_OK &&
file_error != base::PLATFORM_FILE_ERROR_NOT_FOUND)
status = PlatformFileErrorToSyncStatusCode(file_error);

DCHECK(!file_info.is_symbolic_link);

SyncFileType file_type = SYNC_FILE_TYPE_FILE;
if (file_error == base::PLATFORM_FILE_ERROR_NOT_FOUND)
file_type = SYNC_FILE_TYPE_UNKNOWN;
else if (file_info.is_directory)
file_type = SYNC_FILE_TYPE_DIRECTORY;

ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(callback, status, file_type, changes));
}

void LocalFileSyncContext::DidApplyRemoteChange(
Expand Down
6 changes: 4 additions & 2 deletions webkit/fileapi/syncable/local_file_sync_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class WEBKIT_STORAGE_EXPORT LocalFileSyncContext
public:
typedef base::Callback<void(
SyncStatusCode status,
const FileChangeList& change)> ChangeListCallback;
SyncFileType file_type,
const FileChangeList& changes)> ChangeListCallback;

LocalFileSyncContext(base::SingleThreadTaskRunner* ui_task_runner,
base::SingleThreadTaskRunner* io_task_runner);
Expand Down Expand Up @@ -131,8 +132,9 @@ class WEBKIT_STORAGE_EXPORT LocalFileSyncContext
SyncStatusCode status);

// Helper routines for PrepareForSync.
void DidDisabledWritesForPrepareForSync(
void DidGetWritingStatusForPrepareForSync(
FileSystemContext* file_system_context,
SyncStatusCode status,
const FileSystemURL& url,
const ChangeListCallback& callback);

Expand Down
Loading

0 comments on commit 5d847d4

Please sign in to comment.