OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "storage/common/blob/blob_data.h" | |
6 | |
7 #include "base/logging.h" | 5 #include "base/logging.h" |
8 #include "base/strings/sys_string_conversions.h" | 6 #include "base/strings/sys_string_conversions.h" |
9 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "storage/common/blob/blob_data.h" |
11 | 10 |
12 namespace storage { | 11 namespace storage { |
13 | 12 |
14 BlobData::BlobData() {} | 13 BlobDataItem::BlobDataItem(scoped_ptr<DataElement> item, |
15 BlobData::BlobData(const std::string& uuid) | 14 scoped_refptr<ShareableFileReference> file_handle) |
16 : uuid_(uuid) { | 15 : item_(item.Pass()), file_handle_(file_handle) { |
| 16 } |
| 17 BlobDataItem::BlobDataItem(scoped_ptr<DataElement> item) : item_(item.Pass()) { |
| 18 } |
| 19 BlobDataItem::~BlobDataItem() { |
17 } | 20 } |
18 | 21 |
19 BlobData::~BlobData() {} | 22 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { |
20 | 23 } |
21 void BlobData::AppendData(const char* data, size_t length) { | 24 BlobDataBuilder::~BlobDataBuilder() { |
22 DCHECK(length > 0); | |
23 items_.push_back(Item()); | |
24 items_.back().SetToBytes(data, length); | |
25 } | 25 } |
26 | 26 |
27 void BlobData::AppendFile(const base::FilePath& file_path, | 27 void BlobDataBuilder::AppendData(const char* data, size_t length) { |
28 uint64 offset, uint64 length, | |
29 const base::Time& expected_modification_time) { | |
30 DCHECK(length > 0); | 28 DCHECK(length > 0); |
31 items_.push_back(Item()); | 29 scoped_ptr<DataElement> element(new DataElement()); |
32 items_.back().SetToFilePathRange(file_path, offset, length, | 30 element->SetToBytes(data, length); |
33 expected_modification_time); | 31 items_.push_back(new BlobDataItem(element.Pass())); |
34 } | 32 } |
35 | 33 |
36 void BlobData::AppendBlob(const std::string& uuid, | 34 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
37 uint64 offset, uint64 length) { | 35 uint64 offset, |
38 DCHECK_GT(length, 0ul); | 36 uint64 length, |
39 items_.push_back(Item()); | 37 const base::Time& expected_modification_time) { |
40 items_.back().SetToBlobRange(uuid, offset, length); | 38 DCHECK(length > 0); |
| 39 scoped_ptr<DataElement> element(new DataElement()); |
| 40 element->SetToFilePathRange(file_path, offset, length, |
| 41 expected_modification_time); |
| 42 items_.push_back(new BlobDataItem(element.Pass())); |
41 } | 43 } |
42 | 44 |
43 void BlobData::AppendFileSystemFile( | 45 void BlobDataBuilder::AppendFile( |
44 const GURL& url, uint64 offset, | 46 const base::FilePath& file_path, |
| 47 uint64 offset, |
| 48 uint64 length, |
| 49 const base::Time& expected_modification_time, |
| 50 scoped_refptr<ShareableFileReference> shareable_file) { |
| 51 DCHECK(length > 0); |
| 52 scoped_ptr<DataElement> element(new DataElement()); |
| 53 element->SetToFilePathRange(file_path, offset, length, |
| 54 expected_modification_time); |
| 55 items_.push_back(new BlobDataItem(element.Pass(), shareable_file)); |
| 56 } |
| 57 |
| 58 void BlobDataBuilder::AppendBlob(const std::string& uuid, |
| 59 uint64 offset, |
| 60 uint64 length) { |
| 61 DCHECK_GT(length, 0ul); |
| 62 scoped_ptr<DataElement> element(new DataElement()); |
| 63 element->SetToBlobRange(uuid, offset, length); |
| 64 items_.push_back(new BlobDataItem(element.Pass())); |
| 65 } |
| 66 |
| 67 void BlobDataBuilder::AppendFileSystemFile( |
| 68 const GURL& url, |
| 69 uint64 offset, |
45 uint64 length, | 70 uint64 length, |
46 const base::Time& expected_modification_time) { | 71 const base::Time& expected_modification_time) { |
47 DCHECK(length > 0); | 72 DCHECK(length > 0); |
48 items_.push_back(Item()); | 73 scoped_ptr<DataElement> element(new DataElement()); |
49 items_.back().SetToFileSystemUrlRange(url, offset, length, | 74 element->SetToFileSystemUrlRange(url, offset, length, |
50 expected_modification_time); | 75 expected_modification_time); |
| 76 items_.push_back(new BlobDataItem(element.Pass())); |
51 } | 77 } |
52 | 78 |
53 int64 BlobData::GetMemoryUsage() const { | 79 size_t BlobDataBuilder::GetMemoryUsage() const { |
54 int64 memory = 0; | 80 int64 memory = 0; |
55 for (std::vector<Item>::const_iterator iter = items_.begin(); | 81 for (const auto& data_item : items_) { |
56 iter != items_.end(); ++iter) { | 82 if (data_item->type() == DataElement::TYPE_BYTES) |
57 if (iter->type() == Item::TYPE_BYTES) | 83 memory += data_item->length(); |
58 memory += iter->length(); | |
59 } | 84 } |
60 return memory; | 85 return memory; |
61 } | 86 } |
| 87 |
| 88 scoped_ptr<BlobDataSnapshot> BlobDataBuilder::Build() { |
| 89 return scoped_ptr<BlobDataSnapshot>(new BlobDataSnapshot(uuid_, content_type_, |
| 90 content_disposition_, |
| 91 items_)).Pass(); |
| 92 } |
| 93 |
| 94 BlobDataSnapshot::BlobDataSnapshot( |
| 95 const std::string& uuid, |
| 96 const std::string& content_type, |
| 97 const std::string& content_disposition, |
| 98 const std::vector<scoped_refptr<BlobDataItem>>& items) |
| 99 : uuid_(uuid), |
| 100 content_type_(content_type), |
| 101 content_disposition_(content_disposition), |
| 102 items_(items) { |
| 103 } |
| 104 |
| 105 BlobDataSnapshot::BlobDataSnapshot(const BlobDataSnapshot& other) |
| 106 : uuid_(other.uuid_), |
| 107 content_type_(other.content_type_), |
| 108 content_disposition_(other.content_disposition_), |
| 109 items_(other.items_) { |
| 110 } |
| 111 |
| 112 BlobDataSnapshot::~BlobDataSnapshot() { |
| 113 } |
| 114 |
| 115 size_t BlobDataSnapshot::GetMemoryUsage() const { |
| 116 int64 memory = 0; |
| 117 for (const auto& data_item : items_) { |
| 118 if (data_item->type() == DataElement::TYPE_BYTES) |
| 119 memory += data_item->length(); |
| 120 } |
| 121 return memory; |
| 122 } |
62 | 123 |
63 } // namespace storage | 124 } // namespace storage |
OLD | NEW |