| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WEBKIT_BASE_DATA_ELEMENT_H_ |
| 6 #define WEBKIT_BASE_DATA_ELEMENT_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/time.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "webkit/base/webkit_base_export.h" |
| 16 |
| 17 namespace webkit_base { |
| 18 |
| 19 // Represents a base Web data element. This could be either one of |
| 20 // bytes, file or blob data. |
| 21 class WEBKIT_BASE_EXPORT DataElement { |
| 22 public: |
| 23 enum Type { |
| 24 TYPE_UNKNOWN = -1, |
| 25 TYPE_BYTES, |
| 26 TYPE_FILE, |
| 27 TYPE_BLOB, |
| 28 }; |
| 29 |
| 30 DataElement(); |
| 31 ~DataElement(); |
| 32 |
| 33 Type type() const { return type_; } |
| 34 const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; } |
| 35 const FilePath& path() const { return path_; } |
| 36 const GURL& url() const { return url_; } |
| 37 uint64 offset() const { return offset_; } |
| 38 uint64 length() const { return length_; } |
| 39 const base::Time& expected_modification_time() const { |
| 40 return expected_modification_time_; |
| 41 } |
| 42 |
| 43 // Sets TYPE_BYTES data. This copies the given data into the element. |
| 44 void SetToBytes(const char* bytes, int bytes_len) { |
| 45 type_ = TYPE_BYTES; |
| 46 buf_.assign(bytes, bytes + bytes_len); |
| 47 length_ = buf_.size(); |
| 48 } |
| 49 |
| 50 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller |
| 51 // should make sure the data is alive when this element is accessed. |
| 52 void SetToSharedBytes(const char* bytes, int bytes_len) { |
| 53 type_ = TYPE_BYTES; |
| 54 bytes_ = bytes; |
| 55 length_ = bytes_len; |
| 56 } |
| 57 |
| 58 // Sets TYPE_FILE data. |
| 59 void SetToFilePath(const FilePath& path) { |
| 60 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
| 61 } |
| 62 |
| 63 // Sets TYPE_BLOB data. |
| 64 void SetToBlobUrl(const GURL& blob_url) { |
| 65 SetToBlobUrlRange(blob_url, 0, kuint64max); |
| 66 } |
| 67 |
| 68 // Sets TYPE_FILE data with range. |
| 69 void SetToFilePathRange(const FilePath& path, |
| 70 uint64 offset, uint64 length, |
| 71 const base::Time& expected_modification_time); |
| 72 |
| 73 // Sets TYPE_BLOB data with range. |
| 74 void SetToBlobUrlRange(const GURL& blob_url, |
| 75 uint64 offset, uint64 length); |
| 76 |
| 77 private: |
| 78 Type type_; |
| 79 std::vector<char> buf_; |
| 80 const char* bytes_; |
| 81 FilePath path_; |
| 82 GURL url_; |
| 83 uint64 offset_; |
| 84 uint64 length_; |
| 85 base::Time expected_modification_time_; |
| 86 }; |
| 87 |
| 88 #if defined(UNIT_TEST) |
| 89 inline bool operator==(const DataElement& a, const DataElement& b) { |
| 90 if (a.type() != b.type() || |
| 91 a.offset() != b.offset() || |
| 92 a.length() != b.length()) |
| 93 return false; |
| 94 switch (a.type()) { |
| 95 case DataElement::TYPE_BYTES: |
| 96 return memcmp(a.bytes(), b.bytes(), b.length()) == 0; |
| 97 case DataElement::TYPE_FILE: |
| 98 return a.path() == b.path() && |
| 99 a.expected_modification_time() == b.expected_modification_time(); |
| 100 case DataElement::TYPE_BLOB: |
| 101 return a.url() == b.url(); |
| 102 case DataElement::TYPE_UNKNOWN: |
| 103 NOTREACHED(); |
| 104 return false; |
| 105 } |
| 106 return false; |
| 107 } |
| 108 |
| 109 inline bool operator!=(const DataElement& a, const DataElement& b) { |
| 110 return !(a == b); |
| 111 } |
| 112 #endif // defined(UNIT_TEST) |
| 113 |
| 114 } // namespace webkit_base |
| 115 |
| 116 #endif // WEBKIT_BASE_DATA_ELEMENT_H_ |
| OLD | NEW |