attachment.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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#include "sync/api/attachments/attachment.h"
6
7#include "base/logging.h"
8#include "base/rand_util.h"
9
10namespace syncer {
11
12Attachment::~Attachment() {}
13
14// Static.
15scoped_ptr<Attachment> Attachment::Create(
16    const scoped_refptr<base::RefCountedMemory>& data) {
17  return CreateWithId(CreateId(), data);
18}
19
20// Static.
21scoped_ptr<Attachment> Attachment::CreateWithId(
22    const sync_pb::AttachmentId& id,
23    const scoped_refptr<base::RefCountedMemory>& data) {
24  return scoped_ptr<Attachment>(new Attachment(id, data)).Pass();
25}
26
27const sync_pb::AttachmentId& Attachment::GetId() const { return id_; }
28
29const scoped_refptr<base::RefCountedMemory>& Attachment::GetData() const {
30  return data_;
31}
32
33Attachment::Attachment(const sync_pb::AttachmentId& id,
34                       const scoped_refptr<base::RefCountedMemory>& data)
35    : id_(id), data_(data) {
36  DCHECK(!id.unique_id().empty());
37  DCHECK(data);
38}
39
40// Static.
41sync_pb::AttachmentId Attachment::CreateId() {
42  sync_pb::AttachmentId result;
43  // Only requirement here is that this id must be globally unique.
44  // TODO(maniscalco): Consider making this base64 encoded.
45  result.set_unique_id(base::RandBytesAsString(16));
46  return result;
47}
48
49}  // namespace syncer
50