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 19 matching lines...) Expand all Loading... |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "core/fileapi/Blob.h" | 32 #include "core/fileapi/Blob.h" |
33 | 33 |
34 #include "core/fileapi/BlobRegistry.h" | 34 #include "core/fileapi/BlobRegistry.h" |
35 #include "core/fileapi/BlobURL.h" | 35 #include "core/fileapi/BlobURL.h" |
36 #include "core/fileapi/File.h" | 36 #include "core/fileapi/File.h" |
37 | 37 |
38 namespace WebCore { | 38 namespace WebCore { |
39 | 39 |
| 40 namespace { |
| 41 |
40 class BlobURLRegistry : public URLRegistry { | 42 class BlobURLRegistry : public URLRegistry { |
41 public: | 43 public: |
42 virtual void registerURL(SecurityOrigin*, const KURL&, URLRegistrable*) OVER
RIDE; | 44 virtual void registerURL(SecurityOrigin*, const KURL&, URLRegistrable*) OVER
RIDE; |
43 virtual void unregisterURL(const KURL&) OVERRIDE; | 45 virtual void unregisterURL(const KURL&) OVERRIDE; |
44 | 46 |
45 static URLRegistry& registry(); | 47 static URLRegistry& registry(); |
46 }; | 48 }; |
47 | 49 |
48 | |
49 void BlobURLRegistry::registerURL(SecurityOrigin* origin, const KURL& publicURL,
URLRegistrable* blob) | 50 void BlobURLRegistry::registerURL(SecurityOrigin* origin, const KURL& publicURL,
URLRegistrable* blob) |
50 { | 51 { |
51 ASSERT(&blob->registry() == this); | 52 ASSERT(&blob->registry() == this); |
52 BlobRegistry::registerBlobURL(origin, publicURL, static_cast<Blob*>(blob)->u
rl()); | 53 BlobRegistry::registerPublicBlobURL(origin, publicURL, static_cast<Blob*>(bl
ob)->blobDataHandle()); |
53 } | 54 } |
54 | 55 |
55 void BlobURLRegistry::unregisterURL(const KURL& url) | 56 void BlobURLRegistry::unregisterURL(const KURL& publicURL) |
56 { | 57 { |
57 BlobRegistry::unregisterBlobURL(url); | 58 BlobRegistry::revokePublicBlobURL(publicURL); |
58 } | 59 } |
59 | 60 |
60 URLRegistry& BlobURLRegistry::registry() | 61 URLRegistry& BlobURLRegistry::registry() |
61 { | 62 { |
62 DEFINE_STATIC_LOCAL(BlobURLRegistry, instance, ()); | 63 DEFINE_STATIC_LOCAL(BlobURLRegistry, instance, ()); |
63 return instance; | 64 return instance; |
64 } | 65 } |
65 | 66 |
| 67 } // namespace |
66 | 68 |
67 Blob::Blob() | 69 Blob::Blob(PassRefPtr<BlobDataHandle> dataHandle) |
68 : m_size(0) | 70 : m_blobDataHandle(dataHandle) |
69 { | 71 { |
70 ScriptWrappable::init(this); | 72 ScriptWrappable::init(this); |
71 OwnPtr<BlobData> blobData = BlobData::create(); | |
72 | |
73 // Create a new internal URL and register it with the provided blob data. | |
74 m_internalURL = BlobURL::createInternalURL(); | |
75 BlobRegistry::registerBlobURL(m_internalURL, blobData.release()); | |
76 } | |
77 | |
78 Blob::Blob(PassOwnPtr<BlobData> blobData, long long size) | |
79 : m_type(blobData->contentType()) | |
80 , m_size(size) | |
81 { | |
82 ASSERT(blobData); | |
83 ScriptWrappable::init(this); | |
84 | |
85 // Create a new internal URL and register it with the provided blob data. | |
86 m_internalURL = BlobURL::createInternalURL(); | |
87 BlobRegistry::registerBlobURL(m_internalURL, blobData); | |
88 } | |
89 | |
90 Blob::Blob(const KURL& srcURL, const String& type, long long size) | |
91 : m_type(type) | |
92 , m_size(size) | |
93 { | |
94 ScriptWrappable::init(this); | |
95 | |
96 // Create a new internal URL and register it with the same blob data as the
source URL. | |
97 m_internalURL = BlobURL::createInternalURL(); | |
98 BlobRegistry::registerBlobURL(0, m_internalURL, srcURL); | |
99 } | 73 } |
100 | 74 |
101 Blob::~Blob() | 75 Blob::~Blob() |
102 { | 76 { |
103 BlobRegistry::unregisterBlobURL(m_internalURL); | |
104 } | 77 } |
105 | 78 |
106 PassRefPtr<Blob> Blob::slice(long long start, long long end, const String& conte
ntType) const | 79 PassRefPtr<Blob> Blob::slice(long long start, long long end, const String& conte
ntType) const |
107 { | 80 { |
108 // When we slice a file for the first time, we obtain a snapshot of the file
by capturing its current size and modification time. | 81 // When we slice a file for the first time, we obtain a snapshot of the file
by capturing its current size and modification time. |
109 // The modification time will be used to verify if the file has been changed
or not, when the underlying data are accessed. | 82 // The modification time will be used to verify if the file has been changed
or not, when the underlying data are accessed. |
110 long long size; | 83 long long size; |
111 double modificationTime; | 84 double modificationTime; |
112 if (isFile()) { | 85 if (isFile()) { |
113 // FIXME: This involves synchronous file operation. We need to figure ou
t how to make it asynchronous. | 86 // FIXME: This involves synchronous file operation. We need to figure ou
t how to make it asynchronous. |
114 toFile(this)->captureSnapshot(size, modificationTime); | 87 toFile(this)->captureSnapshot(size, modificationTime); |
115 } else { | 88 } else { |
116 ASSERT(m_size != -1); | 89 size = this->size(); |
117 size = m_size; | 90 ASSERT(size != -1); |
118 } | 91 } |
119 | 92 |
120 // Convert the negative value that is used to select from the end. | 93 // Convert the negative value that is used to select from the end. |
121 if (start < 0) | 94 if (start < 0) |
122 start = start + size; | 95 start = start + size; |
123 if (end < 0) | 96 if (end < 0) |
124 end = end + size; | 97 end = end + size; |
125 | 98 |
126 // Clamp the range if it exceeds the size limit. | 99 // Clamp the range if it exceeds the size limit. |
127 if (start < 0) | 100 if (start < 0) |
128 start = 0; | 101 start = 0; |
129 if (end < 0) | 102 if (end < 0) |
130 end = 0; | 103 end = 0; |
131 if (start >= size) { | 104 if (start >= size) { |
132 start = 0; | 105 start = 0; |
133 end = 0; | 106 end = 0; |
134 } else if (end < start) | 107 } else if (end < start) |
135 end = start; | 108 end = start; |
136 else if (end > size) | 109 else if (end > size) |
137 end = size; | 110 end = size; |
138 | 111 |
139 long long length = end - start; | 112 long long length = end - start; |
140 OwnPtr<BlobData> blobData = BlobData::create(); | 113 OwnPtr<BlobData> blobData = BlobData::create(); |
141 blobData->setContentType(contentType); | 114 blobData->setContentType(contentType); |
142 if (isFile()) { | 115 if (isFile()) { |
143 if (!toFile(this)->fileSystemURL().isEmpty()) | 116 if (!toFile(this)->fileSystemURL().isEmpty()) |
144 blobData->appendURL(toFile(this)->fileSystemURL(), start, length, mo
dificationTime); | 117 blobData->appendFileSystemURL(toFile(this)->fileSystemURL(), start,
length, modificationTime); |
145 else | 118 else |
146 blobData->appendFile(toFile(this)->path(), start, length, modificati
onTime); | 119 blobData->appendFile(toFile(this)->path(), start, length, modificati
onTime); |
147 } else | 120 } else { |
148 blobData->appendBlob(m_internalURL, start, length); | 121 blobData->appendBlob(m_blobDataHandle, start, length); |
149 | 122 } |
150 return Blob::create(blobData.release(), length); | 123 return Blob::create(BlobDataHandle::create(blobData.release(), length)); |
151 } | 124 } |
152 | 125 |
153 URLRegistry& Blob::registry() const | 126 URLRegistry& Blob::registry() const |
154 { | 127 { |
155 return BlobURLRegistry::registry(); | 128 return BlobURLRegistry::registry(); |
156 } | 129 } |
157 | 130 |
158 | 131 |
159 } // namespace WebCore | 132 } // namespace WebCore |
OLD | NEW |