attachment.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SYNC_API_ATTACHMENTS_ATTACHMENT_H_
6#define SYNC_API_ATTACHMENTS_ATTACHMENT_H_
7
8#include <map>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/gtest_prod_util.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/ref_counted_memory.h"
15#include "base/memory/scoped_ptr.h"
16#include "sync/api/attachments/attachment_id.h"
17#include "sync/base/sync_export.h"
18
19namespace syncer {
20
21// A blob of in-memory data attached to a sync item.
22//
23// While Attachment objects themselves aren't immutable (they are assignable)
24// the data they wrap is immutable.
25//
26// It is cheap to copy Attachments. Feel free to store and return by value.
27class SYNC_EXPORT Attachment {
28 public:
29  ~Attachment();
30
31  // Default copy and assignment are welcome.
32
33  // Creates an attachment with a unique id and the supplied data.
34  //
35  // Used when creating a brand new attachment.
36  static scoped_ptr<Attachment> Create(
37      const scoped_refptr<base::RefCountedMemory>& data);
38
39  // Creates an attachment with the supplied id and data.
40  //
41  // Used when you want to recreate a specific attachment. E.g. creating a local
42  // copy of an attachment that already exists on the sync server.
43  static scoped_ptr<Attachment> CreateWithId(
44      const AttachmentId& id,
45      const scoped_refptr<base::RefCountedMemory>& data);
46
47  // Returns this attachment's id.
48  const AttachmentId& GetId() const;
49
50  // Returns this attachment's data.
51  const scoped_refptr<base::RefCountedMemory>& GetData() const;
52
53 private:
54  AttachmentId id_;
55  scoped_refptr<base::RefCountedMemory> data_;
56
57  friend class AttachmentTest;
58  FRIEND_TEST_ALL_PREFIXES(AttachmentTest, CreateId_UniqueIdIsUnique);
59
60  Attachment(const AttachmentId& id,
61             const scoped_refptr<base::RefCountedMemory>& data);
62};
63
64typedef std::vector<syncer::Attachment> AttachmentList;
65typedef std::map<AttachmentId, Attachment> AttachmentMap;
66
67}  // namespace syncer
68
69#endif  // SYNC_API_ATTACHMENTS_ATTACHMENT_H_
70