Commit 6ae2b341 authored by pavely's avatar pavely Committed by Commit bot

Refactor FakeAttachmentStore

Refactor FakeAttachmentStore into AttachmentStoreProxy and
InMemoryAttachmentStore backend. Break AttachmentStore interface into
AttachmentStoreBase (common interface for backends) and AttachmentStore
(referenced by datatype and AttachmentService).

BUG=
R=maniscalco@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#297339}
parent 16d32a9f
...@@ -65,7 +65,6 @@ ...@@ -65,7 +65,6 @@
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "google_apis/gaia/oauth2_token_service_request.h" #include "google_apis/gaia/oauth2_token_service_request.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "sync/api/attachments/fake_attachment_store.h"
#include "sync/api/syncable_service.h" #include "sync/api/syncable_service.h"
#include "sync/internal_api/public/attachments/attachment_downloader.h" #include "sync/internal_api/public/attachments/attachment_downloader.h"
#include "sync/internal_api/public/attachments/attachment_service.h" #include "sync/internal_api/public/attachments/attachment_service.h"
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "components/sync_driver/change_processor.h" #include "components/sync_driver/change_processor.h"
#include "components/sync_driver/model_associator.h" #include "components/sync_driver/model_associator.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "sync/api/attachments/fake_attachment_store.h" #include "sync/api/attachments/attachment_store.h"
#include "sync/internal_api/public/attachments/attachment_service_impl.h" #include "sync/internal_api/public/attachments/attachment_service_impl.h"
using sync_driver::AssociatorInterface; using sync_driver::AssociatorInterface;
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "components/sync_driver/data_type_error_handler_mock.h" #include "components/sync_driver/data_type_error_handler_mock.h"
#include "components/sync_driver/sync_api_component_factory.h" #include "components/sync_driver/sync_api_component_factory.h"
#include "sync/api/attachments/attachment_id.h" #include "sync/api/attachments/attachment_id.h"
#include "sync/api/attachments/fake_attachment_store.h" #include "sync/api/attachments/attachment_store.h"
#include "sync/api/fake_syncable_service.h" #include "sync/api/fake_syncable_service.h"
#include "sync/api/sync_change.h" #include "sync/api/sync_change.h"
#include "sync/api/sync_merge_result.h" #include "sync/api/sync_merge_result.h"
...@@ -147,8 +147,8 @@ class SyncGenericChangeProcessorTest : public testing::Test { ...@@ -147,8 +147,8 @@ class SyncGenericChangeProcessorTest : public testing::Test {
} }
void ConstructGenericChangeProcessor(syncer::ModelType type) { void ConstructGenericChangeProcessor(syncer::ModelType type) {
scoped_refptr<syncer::AttachmentStore> attachment_store( scoped_refptr<syncer::AttachmentStore> attachment_store =
new syncer::FakeAttachmentStore(base::MessageLoopProxy::current())); syncer::AttachmentStore::CreateInMemoryStore();
scoped_ptr<MockAttachmentService> mock_attachment_service( scoped_ptr<MockAttachmentService> mock_attachment_service(
new MockAttachmentService(attachment_store)); new MockAttachmentService(attachment_store));
// GenericChangeProcessor takes ownership of the AttachmentService, but we // GenericChangeProcessor takes ownership of the AttachmentService, but we
......
...@@ -20,8 +20,6 @@ source_set("sync_core") { ...@@ -20,8 +20,6 @@ source_set("sync_core") {
"api/attachments/attachment_id.h", "api/attachments/attachment_id.h",
"api/attachments/attachment_store.cc", "api/attachments/attachment_store.cc",
"api/attachments/attachment_store.h", "api/attachments/attachment_store.h",
"api/attachments/fake_attachment_store.cc",
"api/attachments/fake_attachment_store.h",
"api/string_ordinal.h", "api/string_ordinal.h",
"api/syncable_service.cc", "api/syncable_service.cc",
"api/syncable_service.h", "api/syncable_service.h",
...@@ -122,16 +120,12 @@ source_set("sync_core") { ...@@ -122,16 +120,12 @@ source_set("sync_core") {
"internal_api/attachments/attachment_service_impl.cc", "internal_api/attachments/attachment_service_impl.cc",
"internal_api/attachments/attachment_service_proxy.cc", "internal_api/attachments/attachment_service_proxy.cc",
"internal_api/attachments/attachment_service_proxy_for_test.cc", "internal_api/attachments/attachment_service_proxy_for_test.cc",
"internal_api/attachments/attachment_store_handle.cc",
"internal_api/attachments/attachment_uploader.cc", "internal_api/attachments/attachment_uploader.cc",
"internal_api/attachments/attachment_uploader_impl.cc", "internal_api/attachments/attachment_uploader_impl.cc",
"internal_api/attachments/fake_attachment_downloader.cc", "internal_api/attachments/fake_attachment_downloader.cc",
"internal_api/attachments/fake_attachment_uploader.cc", "internal_api/attachments/fake_attachment_uploader.cc",
"internal_api/attachments/public/attachment_downloader.h", "internal_api/attachments/in_memory_attachment_store.cc",
"internal_api/attachments/public/attachment_service.h",
"internal_api/attachments/public/attachment_service_impl.h",
"internal_api/attachments/public/attachment_service_proxy_for_test.h",
"internal_api/attachments/public/attachment_service_proxy.h",
"internal_api/attachments/public/attachment_uploader.h",
"internal_api/base_node.cc", "internal_api/base_node.cc",
"internal_api/base_transaction.cc", "internal_api/base_transaction.cc",
"internal_api/change_record.cc", "internal_api/change_record.cc",
...@@ -158,10 +152,18 @@ source_set("sync_core") { ...@@ -158,10 +152,18 @@ source_set("sync_core") {
"internal_api/js_sync_manager_observer.h", "internal_api/js_sync_manager_observer.h",
"internal_api/protocol_event_buffer.cc", "internal_api/protocol_event_buffer.cc",
"internal_api/protocol_event_buffer.h", "internal_api/protocol_event_buffer.h",
"internal_api/public/attachments/attachment_downloader.h",
"internal_api/public/attachments/attachment_downloader_impl.h", "internal_api/public/attachments/attachment_downloader_impl.h",
"internal_api/public/attachments/attachment_service.h",
"internal_api/public/attachments/attachment_service_impl.h",
"internal_api/public/attachments/attachment_service_proxy.h",
"internal_api/public/attachments/attachment_service_proxy_for_test.h",
"internal_api/public/attachments/attachment_store_handle.h",
"internal_api/public/attachments/attachment_uploader.h",
"internal_api/public/attachments/attachment_uploader_impl.h", "internal_api/public/attachments/attachment_uploader_impl.h",
"internal_api/public/attachments/fake_attachment_downloader.h", "internal_api/public/attachments/fake_attachment_downloader.h",
"internal_api/public/attachments/fake_attachment_uploader.h", "internal_api/public/attachments/fake_attachment_uploader.h",
"internal_api/public/attachments/in_memory_attachment_store.h",
"internal_api/public/base/attachment_id_proto.cc", "internal_api/public/base/attachment_id_proto.cc",
"internal_api/public/base/attachment_id_proto.h", "internal_api/public/base/attachment_id_proto.h",
"internal_api/public/base/cancelation_observer.cc", "internal_api/public/base/cancelation_observer.cc",
...@@ -561,6 +563,7 @@ test("sync_unit_tests") { ...@@ -561,6 +563,7 @@ test("sync_unit_tests") {
"internal_api/attachments/attachment_downloader_impl_unittest.cc", "internal_api/attachments/attachment_downloader_impl_unittest.cc",
"internal_api/attachments/attachment_service_impl_unittest.cc", "internal_api/attachments/attachment_service_impl_unittest.cc",
"internal_api/attachments/attachment_service_proxy_unittest.cc", "internal_api/attachments/attachment_service_proxy_unittest.cc",
"internal_api/attachments/attachment_store_handle_unittest.cc",
"internal_api/attachments/attachment_uploader_impl_unittest.cc", "internal_api/attachments/attachment_uploader_impl_unittest.cc",
"internal_api/attachments/fake_attachment_downloader_unittest.cc", "internal_api/attachments/fake_attachment_downloader_unittest.cc",
"internal_api/attachments/fake_attachment_uploader_unittest.cc", "internal_api/attachments/fake_attachment_uploader_unittest.cc",
......
...@@ -4,9 +4,24 @@ ...@@ -4,9 +4,24 @@
#include "sync/api/attachments/attachment_store.h" #include "sync/api/attachments/attachment_store.h"
#include "base/thread_task_runner_handle.h"
#include "sync/internal_api/public/attachments/attachment_store_handle.h"
#include "sync/internal_api/public/attachments/in_memory_attachment_store.h"
namespace syncer { namespace syncer {
AttachmentStoreBase::AttachmentStoreBase() {}
AttachmentStoreBase::~AttachmentStoreBase() {}
AttachmentStore::AttachmentStore() {} AttachmentStore::AttachmentStore() {}
AttachmentStore::~AttachmentStore() {} AttachmentStore::~AttachmentStore() {}
scoped_refptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() {
// Both frontend and backend of attachment store will live on current thread.
scoped_ptr<AttachmentStoreBase> backend(
new InMemoryAttachmentStore(base::ThreadTaskRunnerHandle::Get()));
return scoped_refptr<AttachmentStore>(new AttachmentStoreHandle(
backend.Pass(), base::ThreadTaskRunnerHandle::Get()));
}
} // namespace syncer } // namespace syncer
...@@ -25,10 +25,8 @@ class AttachmentId; ...@@ -25,10 +25,8 @@ class AttachmentId;
// //
// Destroying this object does not necessarily cancel outstanding async // Destroying this object does not necessarily cancel outstanding async
// operations. If you need cancel like semantics, use WeakPtr in the callbacks. // operations. If you need cancel like semantics, use WeakPtr in the callbacks.
class SYNC_EXPORT AttachmentStore : public base::RefCounted<AttachmentStore> { class SYNC_EXPORT AttachmentStoreBase {
public: public:
AttachmentStore();
// TODO(maniscalco): Consider udpating Read and Write methods to support // TODO(maniscalco): Consider udpating Read and Write methods to support
// resumable transfers (bug 353292). // resumable transfers (bug 353292).
...@@ -43,6 +41,9 @@ class SYNC_EXPORT AttachmentStore : public base::RefCounted<AttachmentStore> { ...@@ -43,6 +41,9 @@ class SYNC_EXPORT AttachmentStore : public base::RefCounted<AttachmentStore> {
typedef base::Callback<void(const Result&)> WriteCallback; typedef base::Callback<void(const Result&)> WriteCallback;
typedef base::Callback<void(const Result&)> DropCallback; typedef base::Callback<void(const Result&)> DropCallback;
AttachmentStoreBase();
virtual ~AttachmentStoreBase();
// Asynchronously reads the attachments identified by |ids|. // Asynchronously reads the attachments identified by |ids|.
// //
// |callback| will be invoked when finished. AttachmentStore will attempt to // |callback| will be invoked when finished. AttachmentStore will attempt to
...@@ -81,9 +82,22 @@ class SYNC_EXPORT AttachmentStore : public base::RefCounted<AttachmentStore> { ...@@ -81,9 +82,22 @@ class SYNC_EXPORT AttachmentStore : public base::RefCounted<AttachmentStore> {
// successfully. // successfully.
virtual void Drop(const AttachmentIdList& ids, virtual void Drop(const AttachmentIdList& ids,
const DropCallback& callback) = 0; const DropCallback& callback) = 0;
};
// AttachmentStore is an interface exposed to data type and AttachmentService
// code. Also contains factory methods for default implementations.
class SYNC_EXPORT AttachmentStore
: public AttachmentStoreBase,
public base::RefCountedThreadSafe<AttachmentStore> {
public:
AttachmentStore();
// Creates an AttachmentStoreHandle backed by in-memory implementation of
// attachment store. For now frontend lives on the same thread as backend.
static scoped_refptr<AttachmentStore> CreateInMemoryStore();
protected: protected:
friend class base::RefCounted<AttachmentStore>; friend class base::RefCountedThreadSafe<AttachmentStore>;
virtual ~AttachmentStore(); virtual ~AttachmentStore();
}; };
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "base/thread_task_runner_handle.h" #include "base/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "sync/api/attachments/attachment.h" #include "sync/api/attachments/attachment.h"
#include "sync/api/attachments/fake_attachment_store.h"
#include "sync/internal_api/public/attachments/fake_attachment_downloader.h" #include "sync/internal_api/public/attachments/fake_attachment_downloader.h"
#include "sync/internal_api/public/attachments/fake_attachment_uploader.h" #include "sync/internal_api/public/attachments/fake_attachment_uploader.h"
...@@ -145,8 +144,8 @@ AttachmentServiceImpl::~AttachmentServiceImpl() { ...@@ -145,8 +144,8 @@ AttachmentServiceImpl::~AttachmentServiceImpl() {
// Static. // Static.
scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() {
scoped_refptr<syncer::AttachmentStore> attachment_store( scoped_refptr<syncer::AttachmentStore> attachment_store =
new syncer::FakeAttachmentStore(base::ThreadTaskRunnerHandle::Get())); AttachmentStore::CreateInMemoryStore();
scoped_ptr<AttachmentUploader> attachment_uploader( scoped_ptr<AttachmentUploader> attachment_uploader(
new FakeAttachmentUploader); new FakeAttachmentUploader);
scoped_ptr<AttachmentDownloader> attachment_downloader( scoped_ptr<AttachmentDownloader> attachment_downloader(
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
namespace syncer { namespace syncer {
namespace {
class MockAttachmentStore : public AttachmentStore, class MockAttachmentStore : public AttachmentStore,
public base::SupportsWeakPtr<MockAttachmentStore> { public base::SupportsWeakPtr<MockAttachmentStore> {
public: public:
...@@ -150,6 +152,8 @@ class MockAttachmentUploader ...@@ -150,6 +152,8 @@ class MockAttachmentUploader
DISALLOW_COPY_AND_ASSIGN(MockAttachmentUploader); DISALLOW_COPY_AND_ASSIGN(MockAttachmentUploader);
}; };
} // namespace
class AttachmentServiceImplTest : public testing::Test, class AttachmentServiceImplTest : public testing::Test,
public AttachmentService::Delegate { public AttachmentService::Delegate {
protected: protected:
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sync/internal_api/public/attachments/attachment_store_handle.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/sequenced_task_runner.h"
#include "sync/api/attachments/attachment.h"
namespace syncer {
AttachmentStoreHandle::AttachmentStoreHandle(
scoped_ptr<AttachmentStoreBase> backend,
const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner)
: backend_(backend.Pass()), backend_task_runner_(backend_task_runner) {
DCHECK(backend_);
DCHECK(backend_task_runner_.get());
}
AttachmentStoreHandle::~AttachmentStoreHandle() {
DCHECK(backend_);
backend_task_runner_->DeleteSoon(FROM_HERE, backend_.release());
}
void AttachmentStoreHandle::Read(const AttachmentIdList& ids,
const ReadCallback& callback) {
DCHECK(CalledOnValidThread());
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&AttachmentStoreBase::Read,
base::Unretained(backend_.get()),
ids,
callback));
}
void AttachmentStoreHandle::Write(const AttachmentList& attachments,
const WriteCallback& callback) {
DCHECK(CalledOnValidThread());
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&AttachmentStoreBase::Write,
base::Unretained(backend_.get()),
attachments,
callback));
}
void AttachmentStoreHandle::Drop(const AttachmentIdList& ids,
const DropCallback& callback) {
DCHECK(CalledOnValidThread());
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&AttachmentStoreBase::Drop,
base::Unretained(backend_.get()),
ids,
callback));
}
} // namespace syncer
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sync/internal_api/public/attachments/attachment_store_handle.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
#include "sync/api/attachments/attachment.h"
#include "sync/api/attachments/attachment_id.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
class MockAttachmentStore : public AttachmentStoreBase {
public:
MockAttachmentStore(const base::Closure& read_called,
const base::Closure& write_called,
const base::Closure& drop_called,
const base::Closure& dtor_called)
: read_called_(read_called),
write_called_(write_called),
drop_called_(drop_called),
dtor_called_(dtor_called) {}
virtual ~MockAttachmentStore() {
dtor_called_.Run();
}
virtual void Read(const AttachmentIdList& ids,
const ReadCallback& callback) OVERRIDE {
read_called_.Run();
}
virtual void Write(const AttachmentList& attachments,
const WriteCallback& callback) OVERRIDE {
write_called_.Run();
}
virtual void Drop(const AttachmentIdList& ids,
const DropCallback& callback) OVERRIDE {
drop_called_.Run();
}
base::Closure read_called_;
base::Closure write_called_;
base::Closure drop_called_;
base::Closure dtor_called_;
};
} // namespace
class AttachmentStoreHandleTest : public testing::Test {
protected:
AttachmentStoreHandleTest()
: read_call_count_(0),
write_call_count_(0),
drop_call_count_(0),
dtor_call_count_(0) {}
virtual void SetUp() {
scoped_ptr<AttachmentStoreBase> backend(new MockAttachmentStore(
base::Bind(&AttachmentStoreHandleTest::ReadCalled,
base::Unretained(this)),
base::Bind(&AttachmentStoreHandleTest::WriteCalled,
base::Unretained(this)),
base::Bind(&AttachmentStoreHandleTest::DropCalled,
base::Unretained(this)),
base::Bind(&AttachmentStoreHandleTest::DtorCalled,
base::Unretained(this))));
attachment_store_handle_ = new AttachmentStoreHandle(
backend.Pass(), base::ThreadTaskRunnerHandle::Get());
}
static void ReadDone(const AttachmentStore::Result& result,
scoped_ptr<AttachmentMap> attachments,
scoped_ptr<AttachmentIdList> unavailable_attachments) {
NOTREACHED();
}
static void WriteDone(const AttachmentStore::Result& result) {
NOTREACHED();
}
static void DropDone(const AttachmentStore::Result& result) {
NOTREACHED();
}
void ReadCalled() { ++read_call_count_; }
void WriteCalled() { ++write_call_count_; }
void DropCalled() { ++drop_call_count_; }
void DtorCalled() { ++dtor_call_count_; }
void RunMessageLoop() {
base::RunLoop run_loop;
run_loop.RunUntilIdle();
}
base::MessageLoop message_loop_;
scoped_refptr<AttachmentStoreHandle> attachment_store_handle_;
int read_call_count_;
int write_call_count_;
int drop_call_count_;
int dtor_call_count_;
};
// Test that method calls are forwarded to backend loop
TEST_F(AttachmentStoreHandleTest, MethodsCalled) {
AttachmentIdList ids;
AttachmentList attachments;
attachment_store_handle_->Read(
ids, base::Bind(&AttachmentStoreHandleTest::ReadDone));
EXPECT_EQ(read_call_count_, 0);
RunMessageLoop();
EXPECT_EQ(read_call_count_, 1);
attachment_store_handle_->Write(
attachments, base::Bind(&AttachmentStoreHandleTest::WriteDone));
EXPECT_EQ(write_call_count_, 0);
RunMessageLoop();
EXPECT_EQ(write_call_count_, 1);
attachment_store_handle_->Drop(
ids, base::Bind(&AttachmentStoreHandleTest::DropDone));
EXPECT_EQ(drop_call_count_, 0);
RunMessageLoop();
EXPECT_EQ(drop_call_count_, 1);
// Releasing referehce to AttachmentStoreHandle should result in
// MockAttachmentStore being deleted on backend loop.
attachment_store_handle_ = NULL;
EXPECT_EQ(dtor_call_count_, 0);
RunMessageLoop();
EXPECT_EQ(dtor_call_count_, 1);
}
} // namespace syncer
...@@ -2,47 +2,28 @@ ...@@ -2,47 +2,28 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "sync/api/attachments/fake_attachment_store.h" #include "sync/internal_api/public/attachments/in_memory_attachment_store.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h"
#include "base/location.h" #include "base/location.h"
#include "base/memory/ref_counted_memory.h" #include "base/memory/scoped_ptr.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "sync/api/attachments/attachment.h"
namespace syncer { namespace syncer {
// Backend is where all the work happens. InMemoryAttachmentStore::InMemoryAttachmentStore(
class FakeAttachmentStore::Backend const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner)
: public base::RefCountedThreadSafe<FakeAttachmentStore::Backend> { : callback_task_runner_(callback_task_runner) {
public: // Object is created on one thread but used on another.
// Construct a Backend that posts its results to |frontend_task_runner|. DetachFromThread();
Backend( }
const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner);
void Read(const AttachmentIdList& ids, const ReadCallback& callback);
void Write(const AttachmentList& attachments, const WriteCallback& callback);
void Drop(const AttachmentIdList& ids, const DropCallback& callback);
private:
friend class base::RefCountedThreadSafe<Backend>;
~Backend();
scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_;
AttachmentMap attachments_;
};
FakeAttachmentStore::Backend::Backend(
const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner)
: frontend_task_runner_(frontend_task_runner) {}
FakeAttachmentStore::Backend::~Backend() {} InMemoryAttachmentStore::~InMemoryAttachmentStore() {
}
void FakeAttachmentStore::Backend::Read(const AttachmentIdList& ids, void InMemoryAttachmentStore::Read(const AttachmentIdList& ids,
const ReadCallback& callback) { const ReadCallback& callback) {
DCHECK(CalledOnValidThread());
Result result_code = SUCCESS; Result result_code = SUCCESS;
AttachmentIdList::const_iterator id_iter = ids.begin(); AttachmentIdList::const_iterator id_iter = ids.begin();
AttachmentIdList::const_iterator id_end = ids.end(); AttachmentIdList::const_iterator id_end = ids.end();
...@@ -62,7 +43,7 @@ void FakeAttachmentStore::Backend::Read(const AttachmentIdList& ids, ...@@ -62,7 +43,7 @@ void FakeAttachmentStore::Backend::Read(const AttachmentIdList& ids,
if (!unavailable_attachments->empty()) { if (!unavailable_attachments->empty()) {
result_code = UNSPECIFIED_ERROR; result_code = UNSPECIFIED_ERROR;
} }
frontend_task_runner_->PostTask( callback_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(callback, base::Bind(callback,
result_code, result_code,
...@@ -70,18 +51,20 @@ void FakeAttachmentStore::Backend::Read(const AttachmentIdList& ids, ...@@ -70,18 +51,20 @@ void FakeAttachmentStore::Backend::Read(const AttachmentIdList& ids,
base::Passed(&unavailable_attachments))); base::Passed(&unavailable_attachments)));
} }
void FakeAttachmentStore::Backend::Write(const AttachmentList& attachments, void InMemoryAttachmentStore::Write(const AttachmentList& attachments,
const WriteCallback& callback) { const WriteCallback& callback) {
DCHECK(CalledOnValidThread());
AttachmentList::const_iterator iter = attachments.begin(); AttachmentList::const_iterator iter = attachments.begin();
AttachmentList::const_iterator end = attachments.end(); AttachmentList::const_iterator end = attachments.end();
for (; iter != end; ++iter) { for (; iter != end; ++iter) {
attachments_.insert(std::make_pair(iter->GetId(), *iter)); attachments_.insert(std::make_pair(iter->GetId(), *iter));
} }
frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); callback_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
} }
void FakeAttachmentStore::Backend::Drop(const AttachmentIdList& ids, void InMemoryAttachmentStore::Drop(const AttachmentIdList& ids,
const DropCallback& callback) { const DropCallback& callback) {
DCHECK(CalledOnValidThread());
Result result = SUCCESS; Result result = SUCCESS;
AttachmentIdList::const_iterator ids_iter = ids.begin(); AttachmentIdList::const_iterator ids_iter = ids.begin();
AttachmentIdList::const_iterator ids_end = ids.end(); AttachmentIdList::const_iterator ids_end = ids.end();
...@@ -91,38 +74,7 @@ void FakeAttachmentStore::Backend::Drop(const AttachmentIdList& ids, ...@@ -91,38 +74,7 @@ void FakeAttachmentStore::Backend::Drop(const AttachmentIdList& ids,
attachments_.erase(attachments_iter); attachments_.erase(attachments_iter);
} }
} }
frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); callback_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
}
FakeAttachmentStore::FakeAttachmentStore(
const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner)
: backend_(new Backend(base::ThreadTaskRunnerHandle::Get())),
backend_task_runner_(backend_task_runner) {}
FakeAttachmentStore::~FakeAttachmentStore() {}
void FakeAttachmentStore::Read(const AttachmentIdList& ids,
const ReadCallback& callback) {
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(&FakeAttachmentStore::Backend::Read, backend_, ids, callback));
}
void FakeAttachmentStore::Write(const AttachmentList& attachments,
const WriteCallback& callback) {
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(&FakeAttachmentStore::Backend::Write,
backend_,
attachments,
callback));
}
void FakeAttachmentStore::Drop(const AttachmentIdList& ids,
const DropCallback& callback) {
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, ids, callback));
} }
} // namespace syncer } // namespace syncer
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "sync/api/attachments/fake_attachment_store.h" #include "sync/api/attachments/attachment_store.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ref_counted_memory.h" #include "base/memory/ref_counted_memory.h"
...@@ -18,10 +18,10 @@ namespace syncer { ...@@ -18,10 +18,10 @@ namespace syncer {
const char kTestData1[] = "test data 1"; const char kTestData1[] = "test data 1";
const char kTestData2[] = "test data 2"; const char kTestData2[] = "test data 2";
class FakeAttachmentStoreTest : public testing::Test { class InMemoryAttachmentStoreTest : public testing::Test {
protected: protected:
base::MessageLoop message_loop; base::MessageLoop message_loop;
scoped_refptr<FakeAttachmentStore> store; scoped_refptr<AttachmentStore> store;
AttachmentStore::Result result; AttachmentStore::Result result;
scoped_ptr<AttachmentMap> attachments; scoped_ptr<AttachmentMap> attachments;
scoped_ptr<AttachmentIdList> failed_attachment_ids; scoped_ptr<AttachmentIdList> failed_attachment_ids;
...@@ -33,18 +33,20 @@ class FakeAttachmentStoreTest : public testing::Test { ...@@ -33,18 +33,20 @@ class FakeAttachmentStoreTest : public testing::Test {
scoped_refptr<base::RefCountedString> some_data1; scoped_refptr<base::RefCountedString> some_data1;
scoped_refptr<base::RefCountedString> some_data2; scoped_refptr<base::RefCountedString> some_data2;
FakeAttachmentStoreTest() InMemoryAttachmentStoreTest()
: store(new FakeAttachmentStore(base::ThreadTaskRunnerHandle::Get())) {} : store(AttachmentStore::CreateInMemoryStore()) {}
virtual void SetUp() { virtual void SetUp() {
Clear(); Clear();
read_callback = base::Bind(&FakeAttachmentStoreTest::CopyResultAttachments, read_callback =
base::Unretained(this), base::Bind(&InMemoryAttachmentStoreTest::CopyResultAttachments,
&result, base::Unretained(this),
&attachments, &result,
&failed_attachment_ids); &attachments,
write_callback = base::Bind( &failed_attachment_ids);
&FakeAttachmentStoreTest::CopyResult, base::Unretained(this), &result); write_callback = base::Bind(&InMemoryAttachmentStoreTest::CopyResult,
base::Unretained(this),
&result);
drop_callback = write_callback; drop_callback = write_callback;
some_data1 = new base::RefCountedString; some_data1 = new base::RefCountedString;
...@@ -86,7 +88,7 @@ class FakeAttachmentStoreTest : public testing::Test { ...@@ -86,7 +88,7 @@ class FakeAttachmentStoreTest : public testing::Test {
// Verify that we do not overwrite existing attachments and that we do not treat // Verify that we do not overwrite existing attachments and that we do not treat
// it as an error. // it as an error.
TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) { TEST_F(InMemoryAttachmentStoreTest, Write_NoOverwriteNoError) {
// Create two attachments with the same id but different data. // Create two attachments with the same id but different data.
Attachment attachment1 = Attachment::Create(some_data1); Attachment attachment1 = Attachment::Create(some_data1);
Attachment attachment2 = Attachment attachment2 =
...@@ -120,7 +122,7 @@ TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) { ...@@ -120,7 +122,7 @@ TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) {
} }
// Verify that we can write some attachments and read them back. // Verify that we can write some attachments and read them back.
TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) { TEST_F(InMemoryAttachmentStoreTest, Write_RoundTrip) {
Attachment attachment1 = Attachment::Create(some_data1); Attachment attachment1 = Attachment::Create(some_data1);
Attachment attachment2 = Attachment::Create(some_data2); Attachment attachment2 = Attachment::Create(some_data2);
AttachmentList some_attachments; AttachmentList some_attachments;
...@@ -150,7 +152,7 @@ TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) { ...@@ -150,7 +152,7 @@ TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) {
} }
// Try to read two attachments when only one exists. // Try to read two attachments when only one exists.
TEST_F(FakeAttachmentStoreTest, Read_OneNotFound) { TEST_F(InMemoryAttachmentStoreTest, Read_OneNotFound) {
Attachment attachment1 = Attachment::Create(some_data1); Attachment attachment1 = Attachment::Create(some_data1);
Attachment attachment2 = Attachment::Create(some_data2); Attachment attachment2 = Attachment::Create(some_data2);
...@@ -176,7 +178,7 @@ TEST_F(FakeAttachmentStoreTest, Read_OneNotFound) { ...@@ -176,7 +178,7 @@ TEST_F(FakeAttachmentStoreTest, Read_OneNotFound) {
// Try to drop two attachments when only one exists. Verify that no error occurs // Try to drop two attachments when only one exists. Verify that no error occurs
// and that the existing attachment was dropped. // and that the existing attachment was dropped.
TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { TEST_F(InMemoryAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) {
// First, create two attachments. // First, create two attachments.
Attachment attachment1 = Attachment::Create(some_data1); Attachment attachment1 = Attachment::Create(some_data1);
Attachment attachment2 = Attachment::Create(some_data2); Attachment attachment2 = Attachment::Create(some_data2);
...@@ -223,7 +225,7 @@ TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { ...@@ -223,7 +225,7 @@ TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) {
// Verify that attempting to drop an attachment that does not exist is not an // Verify that attempting to drop an attachment that does not exist is not an
// error. // error.
TEST_F(FakeAttachmentStoreTest, Drop_DoesNotExist) { TEST_F(InMemoryAttachmentStoreTest, Drop_DoesNotExist) {
Attachment attachment1 = Attachment::Create(some_data1); Attachment attachment1 = Attachment::Create(some_data1);
AttachmentList some_attachments; AttachmentList some_attachments;
some_attachments.push_back(attachment1); some_attachments.push_back(attachment1);
......
...@@ -2,20 +2,20 @@ ...@@ -2,20 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef SYNC_API_ATTACHMENTS_FAKE_ATTACHMENT_STORE_H_ #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_STORE_HANDLE_H_
#define SYNC_API_ATTACHMENTS_FAKE_ATTACHMENT_STORE_H_ #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_STORE_HANDLE_H_
#include <map> #include <map>
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/threading/non_thread_safe.h"
#include "sync/api/attachments/attachment_store.h" #include "sync/api/attachments/attachment_store.h"
#include "sync/base/sync_export.h" #include "sync/base/sync_export.h"
namespace base { namespace base {
class SequencedTaskRunner; class SequencedTaskRunner;
class SingleThreadTaskRunner;
} // namespace base } // namespace base
namespace sync_pb { namespace sync_pb {
...@@ -26,14 +26,18 @@ namespace syncer { ...@@ -26,14 +26,18 @@ namespace syncer {
class Attachment; class Attachment;
// A in-memory only implementation of AttachmentStore used for testing. // AttachmentStoreHandle is helper to post AttachmentStore calls to backend on
// // different thread. Backend is expected to know on which thread to post
// Requires that the current thread has a MessageLoop. // callbacks with results.
class SYNC_EXPORT FakeAttachmentStore : public AttachmentStore { // AttachmentStoreHandle takes ownership of backend. Backend is deleted on
// backend thread.
// AttachmentStoreHandle is not thread safe, it should only be accessed from
// model thread.
class SYNC_EXPORT AttachmentStoreHandle : public AttachmentStore,
public base::NonThreadSafe {
public: public:
// Construct a FakeAttachmentStore whose "IO" will be performed in AttachmentStoreHandle(
// |backend_task_runner|. scoped_ptr<AttachmentStoreBase> backend,
explicit FakeAttachmentStore(
const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner); const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner);
// AttachmentStore implementation. // AttachmentStore implementation.
...@@ -45,16 +49,18 @@ class SYNC_EXPORT FakeAttachmentStore : public AttachmentStore { ...@@ -45,16 +49,18 @@ class SYNC_EXPORT FakeAttachmentStore : public AttachmentStore {
const DropCallback& callback) OVERRIDE; const DropCallback& callback) OVERRIDE;
private: private:
class Backend; virtual ~AttachmentStoreHandle();
virtual ~FakeAttachmentStore(); // AttachmentStoreHandle controls backend's lifetime. It is safe for
// AttachmentStoreHandle to bind backend through base::Unretained for posts.
scoped_refptr<Backend> backend_; // Backend is deleted on backend_task_runner, after that backend_ pointer is
// invalid.
scoped_ptr<AttachmentStoreBase> backend_;
scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
DISALLOW_COPY_AND_ASSIGN(FakeAttachmentStore); DISALLOW_COPY_AND_ASSIGN(AttachmentStoreHandle);
}; };
} // namespace syncer } // namespace syncer
#endif // SYNC_API_ATTACHMENTS_FAKE_ATTACHMENT_STORE_H_ #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_STORE_HANDLE_H_
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_IN_MEMORY_ATTACHMENT_STORE_H_
#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_IN_MEMORY_ATTACHMENT_STORE_H_
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/non_thread_safe.h"
#include "sync/api/attachments/attachment.h"
#include "sync/api/attachments/attachment_id.h"
#include "sync/api/attachments/attachment_store.h"
#include "sync/base/sync_export.h"
namespace syncer {
// An in-memory implementation of AttachmentStore used for testing.
// InMemoryAttachmentStore is not threadsafe, it lives on backend thread and
// posts callbacks with results on |callback_task_runner|.
class SYNC_EXPORT InMemoryAttachmentStore : public AttachmentStoreBase,
public base::NonThreadSafe {
public:
InMemoryAttachmentStore(
const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner);
virtual ~InMemoryAttachmentStore();
// AttachmentStoreBase implementation.
virtual void Read(const AttachmentIdList& ids,
const ReadCallback& callback) OVERRIDE;
virtual void Write(const AttachmentList& attachments,
const WriteCallback& callback) OVERRIDE;
virtual void Drop(const AttachmentIdList& ids,
const DropCallback& callback) OVERRIDE;
private:
scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_;
AttachmentMap attachments_;
};
} // namespace syncer
#endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_IN_MEMORY_ATTACHMENT_STORE_H_
...@@ -63,8 +63,6 @@ ...@@ -63,8 +63,6 @@
'api/attachments/attachment_id.h', 'api/attachments/attachment_id.h',
'api/attachments/attachment_store.cc', 'api/attachments/attachment_store.cc',
'api/attachments/attachment_store.h', 'api/attachments/attachment_store.h',
'api/attachments/fake_attachment_store.cc',
'api/attachments/fake_attachment_store.h',
'api/string_ordinal.h', 'api/string_ordinal.h',
'api/sync_change.cc', 'api/sync_change.cc',
'api/sync_change.h', 'api/sync_change.h',
...@@ -165,10 +163,12 @@ ...@@ -165,10 +163,12 @@
'internal_api/attachments/attachment_service_impl.cc', 'internal_api/attachments/attachment_service_impl.cc',
'internal_api/attachments/attachment_service_proxy.cc', 'internal_api/attachments/attachment_service_proxy.cc',
'internal_api/attachments/attachment_service_proxy_for_test.cc', 'internal_api/attachments/attachment_service_proxy_for_test.cc',
'internal_api/attachments/attachment_store_handle.cc',
'internal_api/attachments/attachment_uploader.cc', 'internal_api/attachments/attachment_uploader.cc',
'internal_api/attachments/attachment_uploader_impl.cc', 'internal_api/attachments/attachment_uploader_impl.cc',
'internal_api/attachments/fake_attachment_downloader.cc', 'internal_api/attachments/fake_attachment_downloader.cc',
'internal_api/attachments/fake_attachment_uploader.cc', 'internal_api/attachments/fake_attachment_uploader.cc',
'internal_api/attachments/in_memory_attachment_store.cc',
'internal_api/attachments/task_queue.cc', 'internal_api/attachments/task_queue.cc',
'internal_api/base_node.cc', 'internal_api/base_node.cc',
'internal_api/base_transaction.cc', 'internal_api/base_transaction.cc',
...@@ -196,16 +196,18 @@ ...@@ -196,16 +196,18 @@
'internal_api/js_sync_manager_observer.h', 'internal_api/js_sync_manager_observer.h',
'internal_api/protocol_event_buffer.cc', 'internal_api/protocol_event_buffer.cc',
'internal_api/protocol_event_buffer.h', 'internal_api/protocol_event_buffer.h',
'internal_api/attachments/public/attachment_downloader.h', 'internal_api/public/attachments/attachment_downloader.h',
'internal_api/attachments/public/attachment_service.h',
'internal_api/attachments/public/attachment_service_impl.h',
'internal_api/attachments/public/attachment_service_proxy_for_test.h',
'internal_api/attachments/public/attachment_service_proxy.h',
'internal_api/attachments/public/attachment_uploader.h',
'internal_api/public/attachments/attachment_downloader_impl.h', 'internal_api/public/attachments/attachment_downloader_impl.h',
'internal_api/public/attachments/attachment_service.h',
'internal_api/public/attachments/attachment_service_impl.h',
'internal_api/public/attachments/attachment_service_proxy.h',
'internal_api/public/attachments/attachment_service_proxy_for_test.h',
'internal_api/public/attachments/attachment_store_handle.h',
'internal_api/public/attachments/attachment_uploader.h',
'internal_api/public/attachments/attachment_uploader_impl.h', 'internal_api/public/attachments/attachment_uploader_impl.h',
'internal_api/public/attachments/fake_attachment_downloader.h', 'internal_api/public/attachments/fake_attachment_downloader.h',
'internal_api/public/attachments/fake_attachment_uploader.h', 'internal_api/public/attachments/fake_attachment_uploader.h',
'internal_api/public/attachments/in_memory_attachment_store.h',
'internal_api/public/attachments/task_queue.h', 'internal_api/public/attachments/task_queue.h',
'internal_api/public/base/attachment_id_proto.cc', 'internal_api/public/base/attachment_id_proto.cc',
'internal_api/public/base/attachment_id_proto.h', 'internal_api/public/base/attachment_id_proto.h',
......
...@@ -260,7 +260,6 @@ ...@@ -260,7 +260,6 @@
'sources': [ 'sources': [
'api/attachments/attachment_id_unittest.cc', 'api/attachments/attachment_id_unittest.cc',
'api/attachments/attachment_unittest.cc', 'api/attachments/attachment_unittest.cc',
'api/attachments/fake_attachment_store_unittest.cc',
'api/sync_change_unittest.cc', 'api/sync_change_unittest.cc',
'api/sync_data_unittest.cc', 'api/sync_data_unittest.cc',
'api/sync_error_unittest.cc', 'api/sync_error_unittest.cc',
...@@ -281,9 +280,11 @@ ...@@ -281,9 +280,11 @@
'internal_api/attachments/attachment_downloader_impl_unittest.cc', 'internal_api/attachments/attachment_downloader_impl_unittest.cc',
'internal_api/attachments/attachment_service_impl_unittest.cc', 'internal_api/attachments/attachment_service_impl_unittest.cc',
'internal_api/attachments/attachment_service_proxy_unittest.cc', 'internal_api/attachments/attachment_service_proxy_unittest.cc',
'internal_api/attachments/attachment_store_handle_unittest.cc',
'internal_api/attachments/attachment_uploader_impl_unittest.cc', 'internal_api/attachments/attachment_uploader_impl_unittest.cc',
'internal_api/attachments/fake_attachment_downloader_unittest.cc', 'internal_api/attachments/fake_attachment_downloader_unittest.cc',
'internal_api/attachments/fake_attachment_uploader_unittest.cc', 'internal_api/attachments/fake_attachment_uploader_unittest.cc',
'internal_api/attachments/in_memory_attachment_store_unittest.cc',
'internal_api/attachments/task_queue_unittest.cc', 'internal_api/attachments/task_queue_unittest.cc',
'internal_api/debug_info_event_listener_unittest.cc', 'internal_api/debug_info_event_listener_unittest.cc',
'internal_api/http_bridge_unittest.cc', 'internal_api/http_bridge_unittest.cc',
......
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