Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 STORAGE_BROWSER_BLOB_BLOB_DATA_BUILDER_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_DATA_BUILDER_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_DATA_BUILDER_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_DATA_BUILDER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <ostream> | |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 15 #include "storage/browser/blob/blob_data_item.h" | 16 #include "storage/browser/blob/blob_data_item.h" |
| 16 #include "storage/browser/blob/blob_data_snapshot.h" | 17 #include "storage/browser/blob/blob_data_snapshot.h" |
| 17 #include "storage/browser/storage_browser_export.h" | 18 #include "storage/browser/storage_browser_export.h" |
| 18 | 19 |
| 19 namespace disk_cache { | 20 namespace disk_cache { |
| 20 class Entry; | 21 class Entry; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace storage { | 24 namespace storage { |
| 24 class BlobStorageContext; | 25 class BlobStorageContext; |
| 25 | 26 |
| 26 class STORAGE_EXPORT BlobDataBuilder { | 27 class STORAGE_EXPORT BlobDataBuilder { |
| 27 public: | 28 public: |
| 28 using DataHandle = BlobDataItem::DataHandle; | 29 using DataHandle = BlobDataItem::DataHandle; |
| 29 | 30 |
| 30 explicit BlobDataBuilder(const std::string& uuid); | 31 explicit BlobDataBuilder(const std::string& uuid); |
| 31 ~BlobDataBuilder(); | 32 ~BlobDataBuilder(); |
| 32 | 33 |
| 33 const std::string& uuid() const { return uuid_; } | 34 const std::string& uuid() const { return uuid_; } |
| 34 | 35 |
| 36 // Validates the data element that was sent over IPC, and copies the data if | |
| 37 // it's a 'bytes' element. Data elements of BYTES_DESCRIPTION or | |
| 38 // DISK_CACHE_ENTRY types are not valid IPC data element types, and cannot be | |
| 39 // given to this method. | |
| 40 void AppendIPCDataElement(const DataElement& element); | |
|
michaeln
2015/10/20 20:50:02
nit: use same param name in .h and .cc
dmurph
2015/10/20 22:49:35
Done.
| |
| 41 | |
| 42 // Copies the given data into the blob. | |
| 35 void AppendData(const std::string& data) { | 43 void AppendData(const std::string& data) { |
| 36 AppendData(data.c_str(), data.size()); | 44 AppendData(data.c_str(), data.size()); |
| 37 } | 45 } |
| 38 | 46 |
| 47 // Copies the given data into the blob. | |
| 39 void AppendData(const char* data, size_t length); | 48 void AppendData(const char* data, size_t length); |
| 40 | 49 |
| 50 // Adds an item that is flagged for future data population. The memory is not | |
| 51 // allocated until the first call to PopulateFutureData. | |
| 52 void AppendFutureData(size_t length); | |
| 53 | |
| 54 // Populates a part of an item previously allocated with AppendFutureData. | |
| 55 // The first call to PopulateFutureData lazily allocates the memory for the | |
| 56 // data element. | |
| 57 // Returns true if: | |
| 58 // * The item was created by using AppendFutureData, | |
| 59 // * The offset and length are valid, and | |
| 60 // * data is a valid pointer. | |
| 61 bool PopulateFutureData(size_t index, | |
| 62 const char* data, | |
| 63 size_t offset, | |
| 64 size_t length); | |
| 65 | |
| 41 // You must know the length of the file, you cannot use kuint64max to specify | 66 // You must know the length of the file, you cannot use kuint64max to specify |
| 42 // the whole file. This method creates a ShareableFileReference to the given | 67 // the whole file. This method creates a ShareableFileReference to the given |
| 43 // file, which is stored in this builder. | 68 // file, which is stored in this builder. |
| 44 void AppendFile(const base::FilePath& file_path, | 69 void AppendFile(const base::FilePath& file_path, |
| 45 uint64_t offset, | 70 uint64_t offset, |
| 46 uint64_t length, | 71 uint64_t length, |
| 47 const base::Time& expected_modification_time); | 72 const base::Time& expected_modification_time); |
| 48 | 73 |
| 49 void AppendBlob(const std::string& uuid, uint64_t offset, uint64_t length); | 74 void AppendBlob(const std::string& uuid, uint64_t offset, uint64_t length); |
| 50 | 75 |
| 51 void AppendBlob(const std::string& uuid); | 76 void AppendBlob(const std::string& uuid); |
| 52 | 77 |
| 53 void AppendFileSystemFile(const GURL& url, | 78 void AppendFileSystemFile(const GURL& url, |
| 54 uint64_t offset, | 79 uint64_t offset, |
| 55 uint64_t length, | 80 uint64_t length, |
| 56 const base::Time& expected_modification_time); | 81 const base::Time& expected_modification_time); |
| 57 | 82 |
| 58 void AppendDiskCacheEntry(const scoped_refptr<DataHandle>& data_handle, | 83 void AppendDiskCacheEntry(const scoped_refptr<DataHandle>& data_handle, |
| 59 disk_cache::Entry* disk_cache_entry, | 84 disk_cache::Entry* disk_cache_entry, |
| 60 int disk_cache_stream_index); | 85 int disk_cache_stream_index); |
| 61 | 86 |
| 62 void set_content_type(const std::string& content_type) { | 87 void set_content_type(const std::string& content_type) { |
| 63 content_type_ = content_type; | 88 content_type_ = content_type; |
| 64 } | 89 } |
| 65 | 90 |
| 66 void set_content_disposition(const std::string& content_disposition) { | 91 void set_content_disposition(const std::string& content_disposition) { |
| 67 content_disposition_ = content_disposition; | 92 content_disposition_ = content_disposition; |
| 68 } | 93 } |
| 69 | 94 |
| 95 void Clear(); | |
| 96 | |
| 70 private: | 97 private: |
| 71 friend class BlobStorageContext; | 98 friend class BlobStorageContext; |
| 99 friend class BlobAsyncBuilderHostTest; | |
| 72 friend bool operator==(const BlobDataBuilder& a, const BlobDataBuilder& b); | 100 friend bool operator==(const BlobDataBuilder& a, const BlobDataBuilder& b); |
| 73 friend bool operator==(const BlobDataSnapshot& a, const BlobDataBuilder& b); | 101 friend bool operator==(const BlobDataSnapshot& a, const BlobDataBuilder& b); |
| 102 friend STORAGE_EXPORT void PrintTo(const BlobDataBuilder& x, | |
| 103 ::std::ostream* os); | |
| 74 | 104 |
| 75 std::string uuid_; | 105 std::string uuid_; |
| 76 std::string content_type_; | 106 std::string content_type_; |
| 77 std::string content_disposition_; | 107 std::string content_disposition_; |
| 78 std::vector<scoped_refptr<BlobDataItem>> items_; | 108 std::vector<scoped_refptr<BlobDataItem>> items_; |
| 79 | 109 |
| 80 DISALLOW_COPY_AND_ASSIGN(BlobDataBuilder); | 110 DISALLOW_COPY_AND_ASSIGN(BlobDataBuilder); |
| 81 }; | 111 }; |
| 82 | 112 |
| 83 #if defined(UNIT_TEST) | 113 #if defined(UNIT_TEST) |
| 84 inline bool operator==(const BlobDataBuilder& a, const BlobDataBuilder& b) { | 114 inline bool operator==(const BlobDataBuilder& a, const BlobDataBuilder& b) { |
| 85 if (a.content_type_ != b.content_type_) | 115 if (a.content_type_ != b.content_type_) |
| 86 return false; | 116 return false; |
| 87 if (a.content_disposition_ != b.content_disposition_) | 117 if (a.content_disposition_ != b.content_disposition_) |
| 88 return false; | 118 return false; |
| 89 if (a.items_.size() != b.items_.size()) | 119 if (a.items_.size() != b.items_.size()) |
| 90 return false; | 120 return false; |
| 91 for (size_t i = 0; i < a.items_.size(); ++i) { | 121 for (size_t i = 0; i < a.items_.size(); ++i) { |
| 92 if (a.items_[i] != b.items_[i]) | 122 if (*(a.items_[i]) != *(b.items_[i])) |
| 93 return false; | 123 return false; |
| 94 } | 124 } |
| 95 return true; | 125 return true; |
| 96 } | 126 } |
| 97 | 127 |
| 98 inline bool operator==(const BlobDataSnapshot& a, const BlobDataBuilder& b) { | 128 inline bool operator==(const BlobDataSnapshot& a, const BlobDataBuilder& b) { |
| 99 if (a.content_type() != b.content_type_) { | 129 if (a.content_type() != b.content_type_) { |
| 100 return false; | 130 return false; |
| 101 } | 131 } |
| 102 if (a.content_disposition() != b.content_disposition_) { | 132 if (a.content_disposition() != b.content_disposition_) { |
| 103 return false; | 133 return false; |
| 104 } | 134 } |
| 105 if (a.items().size() != b.items_.size()) { | 135 if (a.items().size() != b.items_.size()) { |
| 106 return false; | 136 return false; |
| 107 } | 137 } |
| 108 for (size_t i = 0; i < a.items().size(); ++i) { | 138 for (size_t i = 0; i < a.items().size(); ++i) { |
| 109 if (*(a.items()[i]) != *(b.items_[i])) | 139 if (*(a.items()[i]) != *(b.items_[i])) |
| 110 return false; | 140 return false; |
| 111 } | 141 } |
| 112 return true; | 142 return true; |
| 113 } | 143 } |
| 114 | 144 |
| 115 inline bool operator!=(const BlobDataSnapshot& a, const BlobDataBuilder& b) { | 145 inline bool operator!=(const BlobDataSnapshot& a, const BlobDataBuilder& b) { |
| 116 return !(a == b); | 146 return !(a == b); |
| 117 } | 147 } |
| 118 | 148 |
| 119 inline bool operator!=(const BlobDataBuilder& a, const BlobDataBuilder& b) { | 149 inline bool operator!=(const BlobDataBuilder& a, const BlobDataBuilder& b) { |
| 120 return !(a == b); | 150 return !(a == b); |
| 121 } | 151 } |
| 152 | |
| 122 #endif // defined(UNIT_TEST) | 153 #endif // defined(UNIT_TEST) |
| 123 | 154 |
| 124 } // namespace storage | 155 } // namespace storage |
| 125 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_BUILDER_H_ | 156 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_BUILDER_H_ |
| OLD | NEW |