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