OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "Blob.h" | 32 #include "Blob.h" |
33 | 33 |
| 34 #include "BlobItem.h" |
34 #include "FileSystem.h" | 35 #include "FileSystem.h" |
35 | 36 |
36 namespace WebCore { | 37 namespace WebCore { |
37 | 38 |
38 #if ENABLE(BLOB_SLICE) | |
39 const int Blob::toEndOfFile = -1; | |
40 const double Blob::doNotCheckFileChange = 0; | |
41 #endif | |
42 | |
43 Blob::Blob(const String& path) | 39 Blob::Blob(const String& path) |
44 : m_path(path) | |
45 #if ENABLE(BLOB_SLICE) | |
46 , m_start(0) | |
47 , m_length(toEndOfFile) | |
48 , m_snapshotCaptured(false) | |
49 , m_snapshotSize(0) | |
50 , m_snapshotModificationTime(doNotCheckFileChange) | |
51 #endif | |
52 { | 40 { |
| 41 // Note: this doesn't initialize the type unlike File(path). |
| 42 append(FileBlobItem::create(path)); |
53 } | 43 } |
54 | 44 |
55 #if ENABLE(BLOB_SLICE) | |
56 Blob::Blob(const String& path, long long start, long long length, long long snap
shotSize, double snapshotModificationTime) | |
57 : m_path(path) | |
58 , m_start(start) | |
59 , m_length(length) | |
60 , m_snapshotCaptured(true) | |
61 , m_snapshotSize(snapshotSize) | |
62 , m_snapshotModificationTime(snapshotModificationTime) | |
63 { | |
64 ASSERT(start >= 0 && length >= 0 && start + length <= snapshotSize && snapsh
otModificationTime); | |
65 } | |
66 #endif | |
67 | |
68 unsigned long long Blob::size() const | 45 unsigned long long Blob::size() const |
69 { | 46 { |
70 // FIXME: JavaScript cannot represent sizes as large as unsigned long long,
we need to | 47 // FIXME: JavaScript cannot represent sizes as large as unsigned long long,
we need to |
71 // come up with an exception to throw if file size is not represetable. | 48 // come up with an exception to throw if file size is not represetable. |
72 #if ENABLE(BLOB_SLICE) | 49 unsigned long long size = 0; |
73 if (m_snapshotCaptured) | 50 for (size_t i = 0; i < m_items.size(); ++i) |
74 return m_length; | 51 size += m_items[i]->size(); |
75 #endif | 52 return size; |
76 long long size; | 53 } |
77 if (!getFileSize(m_path, size)) | 54 |
78 return 0; | 55 const String& Blob::path() const |
79 return static_cast<unsigned long long>(size); | 56 { |
| 57 ASSERT(m_items.size() == 1 && m_items[0]->toFileBlobItem()); |
| 58 return m_items[0]->toFileBlobItem()->path(); |
| 59 } |
| 60 |
| 61 void Blob::append(PassRefPtr<BlobItem> item) |
| 62 { |
| 63 m_items.append(item); |
80 } | 64 } |
81 | 65 |
82 #if ENABLE(BLOB_SLICE) | 66 #if ENABLE(BLOB_SLICE) |
83 PassRefPtr<Blob> Blob::slice(long long start, long long length) const | 67 PassRefPtr<Blob> Blob::slice(long long start, long long length) const |
84 { | 68 { |
85 // When we slice a file for the first time, we obtain a snapshot of the file
by capturing its current size and modification time. | |
86 // The modification time will be used to verify if the file has been changed
or not, when the underlying data are accessed. | |
87 long long snapshotSize; | |
88 double snapshotModificationTime; | |
89 if (m_snapshotCaptured) { | |
90 snapshotSize = m_snapshotSize; | |
91 snapshotModificationTime = m_snapshotModificationTime; | |
92 } else { | |
93 // If we fail to retrieve the size or modification time, probably due to
that the file has been deleted, an empty blob will be returned. | |
94 time_t modificationTime; | |
95 if (!getFileSize(m_path, snapshotSize) || !getFileModificationTime(m_pat
h, modificationTime)) { | |
96 snapshotSize = 0; | |
97 snapshotModificationTime = 0; | |
98 } else | |
99 snapshotModificationTime = modificationTime; | |
100 } | |
101 | |
102 // Clamp the range if it exceeds the size limit. | |
103 if (start < 0) | 69 if (start < 0) |
104 start = 0; | 70 start = 0; |
105 if (length < 0) | 71 if (length < 0) |
106 length = 0; | 72 length = 0; |
107 | 73 |
108 if (start > snapshotSize) { | 74 // Clamp the range if it exceeds the size limit. |
| 75 unsigned long long totalSize = size(); |
| 76 if (static_cast<unsigned long long>(start) > totalSize) { |
109 start = 0; | 77 start = 0; |
110 length = 0; | 78 length = 0; |
111 } else if (start + length > snapshotSize) | 79 } else if (static_cast<unsigned long long>(start + length) > totalSize) |
112 length = snapshotSize - start; | 80 length = totalSize - start; |
113 | 81 |
114 return adoptRef(new Blob(m_path, m_start + start, length, snapshotSize, snap
shotModificationTime)); | 82 size_t i = 0; |
| 83 RefPtr<Blob> blob = Blob::create(); |
| 84 for (; i < m_items.size() && static_cast<unsigned long long>(start) >= m_ite
ms[i]->size(); ++i) |
| 85 start -= m_items[i]->size(); |
| 86 for (; length > 0 && i < m_items.size(); ++i) { |
| 87 blob->m_items.append(m_items[i]->slice(start, length)); |
| 88 length -= blob->m_items.last()->size(); |
| 89 start = 0; |
| 90 } |
| 91 return blob.release(); |
115 } | 92 } |
116 #endif | 93 #endif // ENABLE(BLOB_SLICE) |
117 | 94 |
118 } // namespace WebCore | 95 } // namespace WebCore |
OLD | NEW |