summaryrefslogtreecommitdiffstats
path: root/sync/api/attachments/attachment_store.cc
blob: b945c9f9c556c6f28f0fc9163ae870d06587c7ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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/api/attachments/attachment_store.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/location.h"
#include "base/message_loop/message_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "sync/internal_api/public/attachments/attachment_store_frontend.h"
#include "sync/internal_api/public/attachments/in_memory_attachment_store.h"
#include "sync/internal_api/public/attachments/on_disk_attachment_store.h"

namespace syncer {

namespace {

void NoOpDropCallback(const AttachmentStore::Result& result) {
}
}

AttachmentStore::AttachmentStore(
    const scoped_refptr<AttachmentStoreFrontend>& frontend,
    Component component)
    : frontend_(frontend), component_(component) {
}

AttachmentStore::~AttachmentStore() {
}

void AttachmentStore::Read(const AttachmentIdList& ids,
                           const ReadCallback& callback) {
  frontend_->Read(ids, callback);
}

void AttachmentStore::Write(const AttachmentList& attachments,
                            const WriteCallback& callback) {
  frontend_->Write(component_, attachments, callback);
}

void AttachmentStore::Drop(const AttachmentIdList& ids,
                           const DropCallback& callback) {
  frontend_->DropReference(component_, ids, callback);
}

void AttachmentStore::ReadMetadata(const AttachmentIdList& ids,
                                   const ReadMetadataCallback& callback) {
  frontend_->ReadMetadata(ids, callback);
}

void AttachmentStore::ReadAllMetadata(const ReadMetadataCallback& callback) {
  frontend_->ReadAllMetadata(component_, callback);
}

scoped_ptr<AttachmentStoreForSync>
AttachmentStore::CreateAttachmentStoreForSync() const {
  scoped_ptr<AttachmentStoreForSync> attachment_store_for_sync(
      new AttachmentStoreForSync(frontend_, component_, SYNC));
  return attachment_store_for_sync.Pass();
}

scoped_ptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() {
  // Both frontend and backend of attachment store will live on current thread.
  scoped_refptr<base::SingleThreadTaskRunner> runner;
  if (base::ThreadTaskRunnerHandle::IsSet()) {
    runner = base::ThreadTaskRunnerHandle::Get();
  } else {
    // Dummy runner for tests that don't have MessageLoop.
    base::MessageLoop loop;
    // This works because |runner| takes a ref to the proxy.
    runner = base::ThreadTaskRunnerHandle::Get();
  }
  scoped_ptr<AttachmentStoreBackend> backend(
      new InMemoryAttachmentStore(runner));
  scoped_refptr<AttachmentStoreFrontend> frontend(
      new AttachmentStoreFrontend(backend.Pass(), runner));
  scoped_ptr<AttachmentStore> attachment_store(
      new AttachmentStore(frontend, MODEL_TYPE));
  return attachment_store.Pass();
}

scoped_ptr<AttachmentStore> AttachmentStore::CreateOnDiskStore(
    const base::FilePath& path,
    const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
    const InitCallback& callback) {
  scoped_ptr<OnDiskAttachmentStore> backend(
      new OnDiskAttachmentStore(base::ThreadTaskRunnerHandle::Get(), path));

  scoped_refptr<AttachmentStoreFrontend> frontend =
      new AttachmentStoreFrontend(backend.Pass(), backend_task_runner);
  scoped_ptr<AttachmentStore> attachment_store(
      new AttachmentStore(frontend, MODEL_TYPE));
  frontend->Init(callback);

  return attachment_store.Pass();
}

scoped_ptr<AttachmentStore> AttachmentStore::CreateMockStoreForTest(
    scoped_ptr<AttachmentStoreBackend> backend) {
  scoped_refptr<base::SingleThreadTaskRunner> runner =
      base::ThreadTaskRunnerHandle::Get();
  scoped_refptr<AttachmentStoreFrontend> attachment_store_frontend(
      new AttachmentStoreFrontend(backend.Pass(), runner));
  scoped_ptr<AttachmentStore> attachment_store(
      new AttachmentStore(attachment_store_frontend, MODEL_TYPE));
  return attachment_store.Pass();
}

AttachmentStoreForSync::AttachmentStoreForSync(
    const scoped_refptr<AttachmentStoreFrontend>& frontend,
    Component consumer_component,
    Component sync_component)
    : AttachmentStore(frontend, consumer_component),
      sync_component_(sync_component) {
}

void AttachmentStoreForSync::SetSyncReference(const AttachmentIdList& ids) {
  frontend()->SetReference(sync_component_, ids);
}

void AttachmentStoreForSync::DropSyncReference(const AttachmentIdList& ids) {
  frontend()->DropReference(sync_component_, ids,
                            base::Bind(&NoOpDropCallback));
}

}  // namespace syncer