Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsDOMFile.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsCExternalHandlerService.h" |
michael@0 | 10 | #include "nsContentCID.h" |
michael@0 | 11 | #include "nsContentUtils.h" |
michael@0 | 12 | #include "nsDOMClassInfoID.h" |
michael@0 | 13 | #include "nsError.h" |
michael@0 | 14 | #include "nsICharsetDetector.h" |
michael@0 | 15 | #include "nsIClassInfo.h" |
michael@0 | 16 | #include "nsIConverterInputStream.h" |
michael@0 | 17 | #include "nsIDocument.h" |
michael@0 | 18 | #include "nsIFileStreams.h" |
michael@0 | 19 | #include "nsIInputStream.h" |
michael@0 | 20 | #include "nsIIPCSerializableInputStream.h" |
michael@0 | 21 | #include "nsIMemoryReporter.h" |
michael@0 | 22 | #include "nsIMIMEService.h" |
michael@0 | 23 | #include "nsISeekableStream.h" |
michael@0 | 24 | #include "nsIUnicharInputStream.h" |
michael@0 | 25 | #include "nsIUnicodeDecoder.h" |
michael@0 | 26 | #include "nsNetCID.h" |
michael@0 | 27 | #include "nsNetUtil.h" |
michael@0 | 28 | #include "nsIUUIDGenerator.h" |
michael@0 | 29 | #include "nsHostObjectProtocolHandler.h" |
michael@0 | 30 | #include "nsStringStream.h" |
michael@0 | 31 | #include "nsJSUtils.h" |
michael@0 | 32 | #include "nsPrintfCString.h" |
michael@0 | 33 | #include "mozilla/SHA1.h" |
michael@0 | 34 | #include "mozilla/CheckedInt.h" |
michael@0 | 35 | #include "mozilla/Preferences.h" |
michael@0 | 36 | #include "mozilla/Attributes.h" |
michael@0 | 37 | #include "nsThreadUtils.h" |
michael@0 | 38 | |
michael@0 | 39 | #include "mozilla/dom/FileListBinding.h" |
michael@0 | 40 | using namespace mozilla; |
michael@0 | 41 | using namespace mozilla::dom; |
michael@0 | 42 | |
michael@0 | 43 | // XXXkhuey the input stream that we pass out of a DOMFile |
michael@0 | 44 | // can outlive the actual DOMFile object. Thus, we must |
michael@0 | 45 | // ensure that the buffer underlying the stream we get |
michael@0 | 46 | // from NS_NewByteInputStream is held alive as long as the |
michael@0 | 47 | // stream is. We do that by passing back this class instead. |
michael@0 | 48 | class DataOwnerAdapter MOZ_FINAL : public nsIInputStream, |
michael@0 | 49 | public nsISeekableStream, |
michael@0 | 50 | public nsIIPCSerializableInputStream |
michael@0 | 51 | { |
michael@0 | 52 | typedef nsDOMMemoryFile::DataOwner DataOwner; |
michael@0 | 53 | public: |
michael@0 | 54 | static nsresult Create(DataOwner* aDataOwner, |
michael@0 | 55 | uint32_t aStart, |
michael@0 | 56 | uint32_t aLength, |
michael@0 | 57 | nsIInputStream** _retval); |
michael@0 | 58 | |
michael@0 | 59 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 60 | |
michael@0 | 61 | // These are mandatory. |
michael@0 | 62 | NS_FORWARD_NSIINPUTSTREAM(mStream->) |
michael@0 | 63 | NS_FORWARD_NSISEEKABLESTREAM(mSeekableStream->) |
michael@0 | 64 | |
michael@0 | 65 | // This is optional. We use a conditional QI to keep it from being called |
michael@0 | 66 | // if the underlying stream doesn't support it. |
michael@0 | 67 | NS_FORWARD_NSIIPCSERIALIZABLEINPUTSTREAM(mSerializableInputStream->) |
michael@0 | 68 | |
michael@0 | 69 | private: |
michael@0 | 70 | DataOwnerAdapter(DataOwner* aDataOwner, |
michael@0 | 71 | nsIInputStream* aStream) |
michael@0 | 72 | : mDataOwner(aDataOwner), mStream(aStream), |
michael@0 | 73 | mSeekableStream(do_QueryInterface(aStream)), |
michael@0 | 74 | mSerializableInputStream(do_QueryInterface(aStream)) |
michael@0 | 75 | { |
michael@0 | 76 | NS_ASSERTION(mSeekableStream, "Somebody gave us the wrong stream!"); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | nsRefPtr<DataOwner> mDataOwner; |
michael@0 | 80 | nsCOMPtr<nsIInputStream> mStream; |
michael@0 | 81 | nsCOMPtr<nsISeekableStream> mSeekableStream; |
michael@0 | 82 | nsCOMPtr<nsIIPCSerializableInputStream> mSerializableInputStream; |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | NS_IMPL_ADDREF(DataOwnerAdapter) |
michael@0 | 86 | NS_IMPL_RELEASE(DataOwnerAdapter) |
michael@0 | 87 | |
michael@0 | 88 | NS_INTERFACE_MAP_BEGIN(DataOwnerAdapter) |
michael@0 | 89 | NS_INTERFACE_MAP_ENTRY(nsIInputStream) |
michael@0 | 90 | NS_INTERFACE_MAP_ENTRY(nsISeekableStream) |
michael@0 | 91 | NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream, |
michael@0 | 92 | mSerializableInputStream) |
michael@0 | 93 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream) |
michael@0 | 94 | NS_INTERFACE_MAP_END |
michael@0 | 95 | |
michael@0 | 96 | nsresult DataOwnerAdapter::Create(DataOwner* aDataOwner, |
michael@0 | 97 | uint32_t aStart, |
michael@0 | 98 | uint32_t aLength, |
michael@0 | 99 | nsIInputStream** _retval) |
michael@0 | 100 | { |
michael@0 | 101 | nsresult rv; |
michael@0 | 102 | NS_ASSERTION(aDataOwner, "Uh ..."); |
michael@0 | 103 | |
michael@0 | 104 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 105 | |
michael@0 | 106 | rv = NS_NewByteInputStream(getter_AddRefs(stream), |
michael@0 | 107 | static_cast<const char*>(aDataOwner->mData) + |
michael@0 | 108 | aStart, |
michael@0 | 109 | (int32_t)aLength, |
michael@0 | 110 | NS_ASSIGNMENT_DEPEND); |
michael@0 | 111 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 112 | |
michael@0 | 113 | NS_ADDREF(*_retval = new DataOwnerAdapter(aDataOwner, stream)); |
michael@0 | 114 | |
michael@0 | 115 | return NS_OK; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 119 | // nsDOMFileBase implementation |
michael@0 | 120 | |
michael@0 | 121 | NS_IMETHODIMP |
michael@0 | 122 | nsDOMFileBase::GetName(nsAString &aFileName) |
michael@0 | 123 | { |
michael@0 | 124 | NS_ASSERTION(mIsFile, "Should only be called on files"); |
michael@0 | 125 | aFileName = mName; |
michael@0 | 126 | return NS_OK; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | NS_IMETHODIMP |
michael@0 | 130 | nsDOMFileBase::GetPath(nsAString &aPath) |
michael@0 | 131 | { |
michael@0 | 132 | NS_ASSERTION(mIsFile, "Should only be called on files"); |
michael@0 | 133 | aPath = mPath; |
michael@0 | 134 | return NS_OK; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | NS_IMETHODIMP |
michael@0 | 138 | nsDOMFileBase::GetLastModifiedDate(JSContext* cx, JS::MutableHandle<JS::Value> aLastModifiedDate) |
michael@0 | 139 | { |
michael@0 | 140 | JS::Rooted<JSObject*> date(cx, JS_NewDateObjectMsec(cx, JS_Now() / PR_USEC_PER_MSEC)); |
michael@0 | 141 | if (!date) { |
michael@0 | 142 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 143 | } |
michael@0 | 144 | aLastModifiedDate.setObject(*date); |
michael@0 | 145 | return NS_OK; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | NS_IMETHODIMP |
michael@0 | 149 | nsDOMFileBase::GetMozFullPath(nsAString &aFileName) |
michael@0 | 150 | { |
michael@0 | 151 | NS_ASSERTION(mIsFile, "Should only be called on files"); |
michael@0 | 152 | |
michael@0 | 153 | // It is unsafe to call IsCallerChrome on a non-main thread. If |
michael@0 | 154 | // you hit the following assertion you need to figure out some other way to |
michael@0 | 155 | // determine privileges and call GetMozFullPathInternal. |
michael@0 | 156 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 157 | |
michael@0 | 158 | if (nsContentUtils::IsCallerChrome()) { |
michael@0 | 159 | return GetMozFullPathInternal(aFileName); |
michael@0 | 160 | } |
michael@0 | 161 | aFileName.Truncate(); |
michael@0 | 162 | return NS_OK; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | NS_IMETHODIMP |
michael@0 | 166 | nsDOMFileBase::GetMozFullPathInternal(nsAString &aFileName) |
michael@0 | 167 | { |
michael@0 | 168 | if (!mIsFile) { |
michael@0 | 169 | return NS_ERROR_FAILURE; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | aFileName.Truncate(); |
michael@0 | 173 | return NS_OK; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | NS_IMETHODIMP |
michael@0 | 177 | nsDOMFileBase::GetSize(uint64_t *aSize) |
michael@0 | 178 | { |
michael@0 | 179 | *aSize = mLength; |
michael@0 | 180 | return NS_OK; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | NS_IMETHODIMP |
michael@0 | 184 | nsDOMFileBase::GetType(nsAString &aType) |
michael@0 | 185 | { |
michael@0 | 186 | aType = mContentType; |
michael@0 | 187 | return NS_OK; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | NS_IMETHODIMP |
michael@0 | 191 | nsDOMFileBase::GetMozLastModifiedDate(uint64_t* aLastModifiedDate) |
michael@0 | 192 | { |
michael@0 | 193 | NS_ASSERTION(mIsFile, "Should only be called on files"); |
michael@0 | 194 | if (IsDateUnknown()) { |
michael@0 | 195 | mLastModificationDate = PR_Now(); |
michael@0 | 196 | } |
michael@0 | 197 | *aLastModifiedDate = mLastModificationDate; |
michael@0 | 198 | return NS_OK; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | // Makes sure that aStart and aEnd is less then or equal to aSize and greater |
michael@0 | 202 | // than 0 |
michael@0 | 203 | static void |
michael@0 | 204 | ParseSize(int64_t aSize, int64_t& aStart, int64_t& aEnd) |
michael@0 | 205 | { |
michael@0 | 206 | CheckedInt64 newStartOffset = aStart; |
michael@0 | 207 | if (aStart < -aSize) { |
michael@0 | 208 | newStartOffset = 0; |
michael@0 | 209 | } |
michael@0 | 210 | else if (aStart < 0) { |
michael@0 | 211 | newStartOffset += aSize; |
michael@0 | 212 | } |
michael@0 | 213 | else if (aStart > aSize) { |
michael@0 | 214 | newStartOffset = aSize; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | CheckedInt64 newEndOffset = aEnd; |
michael@0 | 218 | if (aEnd < -aSize) { |
michael@0 | 219 | newEndOffset = 0; |
michael@0 | 220 | } |
michael@0 | 221 | else if (aEnd < 0) { |
michael@0 | 222 | newEndOffset += aSize; |
michael@0 | 223 | } |
michael@0 | 224 | else if (aEnd > aSize) { |
michael@0 | 225 | newEndOffset = aSize; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | if (!newStartOffset.isValid() || !newEndOffset.isValid() || |
michael@0 | 229 | newStartOffset.value() >= newEndOffset.value()) { |
michael@0 | 230 | aStart = aEnd = 0; |
michael@0 | 231 | } |
michael@0 | 232 | else { |
michael@0 | 233 | aStart = newStartOffset.value(); |
michael@0 | 234 | aEnd = newEndOffset.value(); |
michael@0 | 235 | } |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | NS_IMETHODIMP |
michael@0 | 239 | nsDOMFileBase::Slice(int64_t aStart, int64_t aEnd, |
michael@0 | 240 | const nsAString& aContentType, uint8_t optional_argc, |
michael@0 | 241 | nsIDOMBlob **aBlob) |
michael@0 | 242 | { |
michael@0 | 243 | *aBlob = nullptr; |
michael@0 | 244 | |
michael@0 | 245 | // Truncate aStart and aEnd so that we stay within this file. |
michael@0 | 246 | uint64_t thisLength; |
michael@0 | 247 | nsresult rv = GetSize(&thisLength); |
michael@0 | 248 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 249 | |
michael@0 | 250 | if (optional_argc < 2) { |
michael@0 | 251 | aEnd = (int64_t)thisLength; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | ParseSize((int64_t)thisLength, aStart, aEnd); |
michael@0 | 255 | |
michael@0 | 256 | // Create the new file |
michael@0 | 257 | *aBlob = CreateSlice((uint64_t)aStart, (uint64_t)(aEnd - aStart), |
michael@0 | 258 | aContentType).take(); |
michael@0 | 259 | |
michael@0 | 260 | return *aBlob ? NS_OK : NS_ERROR_UNEXPECTED; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | NS_IMETHODIMP |
michael@0 | 264 | nsDOMFileBase::GetInternalStream(nsIInputStream **aStream) |
michael@0 | 265 | { |
michael@0 | 266 | // Must be overridden |
michael@0 | 267 | NS_NOTREACHED("Must override GetInternalStream"); |
michael@0 | 268 | |
michael@0 | 269 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | NS_IMETHODIMP |
michael@0 | 273 | nsDOMFileBase::GetInternalUrl(nsIPrincipal* aPrincipal, nsAString& aURL) |
michael@0 | 274 | { |
michael@0 | 275 | NS_ENSURE_STATE(aPrincipal); |
michael@0 | 276 | |
michael@0 | 277 | nsCString url; |
michael@0 | 278 | nsresult rv = nsBlobProtocolHandler::AddDataEntry( |
michael@0 | 279 | NS_LITERAL_CSTRING(BLOBURI_SCHEME), |
michael@0 | 280 | static_cast<nsIDOMBlob*>(this), aPrincipal, url); |
michael@0 | 281 | if (NS_FAILED(rv)) { |
michael@0 | 282 | return rv; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | CopyASCIItoUTF16(url, aURL); |
michael@0 | 286 | return NS_OK; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | NS_IMETHODIMP_(int64_t) |
michael@0 | 290 | nsDOMFileBase::GetFileId() |
michael@0 | 291 | { |
michael@0 | 292 | int64_t id = -1; |
michael@0 | 293 | |
michael@0 | 294 | if (IsStoredFile() && IsWholeFile() && !IsSnapshot()) { |
michael@0 | 295 | if (!indexedDB::IndexedDatabaseManager::IsClosed()) { |
michael@0 | 296 | indexedDB::IndexedDatabaseManager::FileMutex().Lock(); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | NS_ASSERTION(!mFileInfos.IsEmpty(), |
michael@0 | 300 | "A stored file must have at least one file info!"); |
michael@0 | 301 | |
michael@0 | 302 | nsRefPtr<indexedDB::FileInfo>& fileInfo = mFileInfos.ElementAt(0); |
michael@0 | 303 | if (fileInfo) { |
michael@0 | 304 | id = fileInfo->Id(); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | if (!indexedDB::IndexedDatabaseManager::IsClosed()) { |
michael@0 | 308 | indexedDB::IndexedDatabaseManager::FileMutex().Unlock(); |
michael@0 | 309 | } |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | return id; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | NS_IMETHODIMP_(void) |
michael@0 | 316 | nsDOMFileBase::AddFileInfo(indexedDB::FileInfo* aFileInfo) |
michael@0 | 317 | { |
michael@0 | 318 | if (indexedDB::IndexedDatabaseManager::IsClosed()) { |
michael@0 | 319 | NS_ERROR("Shouldn't be called after shutdown!"); |
michael@0 | 320 | return; |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | nsRefPtr<indexedDB::FileInfo> fileInfo = aFileInfo; |
michael@0 | 324 | |
michael@0 | 325 | MutexAutoLock lock(indexedDB::IndexedDatabaseManager::FileMutex()); |
michael@0 | 326 | |
michael@0 | 327 | NS_ASSERTION(!mFileInfos.Contains(aFileInfo), |
michael@0 | 328 | "Adding the same file info agan?!"); |
michael@0 | 329 | |
michael@0 | 330 | nsRefPtr<indexedDB::FileInfo>* element = mFileInfos.AppendElement(); |
michael@0 | 331 | element->swap(fileInfo); |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | NS_IMETHODIMP_(indexedDB::FileInfo*) |
michael@0 | 335 | nsDOMFileBase::GetFileInfo(indexedDB::FileManager* aFileManager) |
michael@0 | 336 | { |
michael@0 | 337 | if (indexedDB::IndexedDatabaseManager::IsClosed()) { |
michael@0 | 338 | NS_ERROR("Shouldn't be called after shutdown!"); |
michael@0 | 339 | return nullptr; |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | // A slice created from a stored file must keep the file info alive. |
michael@0 | 343 | // However, we don't support sharing of slices yet, so the slice must be |
michael@0 | 344 | // copied again. That's why we have to ignore the first file info. |
michael@0 | 345 | // Snapshots are handled in a similar way (they have to be copied). |
michael@0 | 346 | uint32_t startIndex; |
michael@0 | 347 | if (IsStoredFile() && (!IsWholeFile() || IsSnapshot())) { |
michael@0 | 348 | startIndex = 1; |
michael@0 | 349 | } |
michael@0 | 350 | else { |
michael@0 | 351 | startIndex = 0; |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | MutexAutoLock lock(indexedDB::IndexedDatabaseManager::FileMutex()); |
michael@0 | 355 | |
michael@0 | 356 | for (uint32_t i = startIndex; i < mFileInfos.Length(); i++) { |
michael@0 | 357 | nsRefPtr<indexedDB::FileInfo>& fileInfo = mFileInfos.ElementAt(i); |
michael@0 | 358 | if (fileInfo->Manager() == aFileManager) { |
michael@0 | 359 | return fileInfo; |
michael@0 | 360 | } |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | return nullptr; |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | NS_IMETHODIMP |
michael@0 | 367 | nsDOMFileBase::GetSendInfo(nsIInputStream** aBody, |
michael@0 | 368 | uint64_t* aContentLength, |
michael@0 | 369 | nsACString& aContentType, |
michael@0 | 370 | nsACString& aCharset) |
michael@0 | 371 | { |
michael@0 | 372 | nsresult rv; |
michael@0 | 373 | |
michael@0 | 374 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 375 | rv = this->GetInternalStream(getter_AddRefs(stream)); |
michael@0 | 376 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 377 | |
michael@0 | 378 | rv = this->GetSize(aContentLength); |
michael@0 | 379 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 380 | |
michael@0 | 381 | nsString contentType; |
michael@0 | 382 | rv = this->GetType(contentType); |
michael@0 | 383 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 384 | |
michael@0 | 385 | CopyUTF16toUTF8(contentType, aContentType); |
michael@0 | 386 | |
michael@0 | 387 | aCharset.Truncate(); |
michael@0 | 388 | |
michael@0 | 389 | stream.forget(aBody); |
michael@0 | 390 | return NS_OK; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | NS_IMETHODIMP |
michael@0 | 394 | nsDOMFileBase::GetMutable(bool* aMutable) |
michael@0 | 395 | { |
michael@0 | 396 | *aMutable = !mImmutable; |
michael@0 | 397 | return NS_OK; |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | NS_IMETHODIMP |
michael@0 | 401 | nsDOMFileBase::SetMutable(bool aMutable) |
michael@0 | 402 | { |
michael@0 | 403 | nsresult rv = NS_OK; |
michael@0 | 404 | |
michael@0 | 405 | NS_ENSURE_ARG(!mImmutable || !aMutable); |
michael@0 | 406 | |
michael@0 | 407 | if (!mImmutable && !aMutable) { |
michael@0 | 408 | // Force the content type and size to be cached |
michael@0 | 409 | nsString dummyString; |
michael@0 | 410 | rv = this->GetType(dummyString); |
michael@0 | 411 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 412 | |
michael@0 | 413 | uint64_t dummyInt; |
michael@0 | 414 | rv = this->GetSize(&dummyInt); |
michael@0 | 415 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | mImmutable = !aMutable; |
michael@0 | 419 | return rv; |
michael@0 | 420 | } |
michael@0 | 421 | |
michael@0 | 422 | NS_IMETHODIMP_(bool) |
michael@0 | 423 | nsDOMFileBase::IsMemoryFile(void) |
michael@0 | 424 | { |
michael@0 | 425 | return false; |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 429 | // nsDOMFile implementation |
michael@0 | 430 | |
michael@0 | 431 | DOMCI_DATA(File, nsDOMFile) |
michael@0 | 432 | DOMCI_DATA(Blob, nsDOMFile) |
michael@0 | 433 | |
michael@0 | 434 | NS_INTERFACE_MAP_BEGIN(nsDOMFile) |
michael@0 | 435 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFile) |
michael@0 | 436 | NS_INTERFACE_MAP_ENTRY(nsIDOMBlob) |
michael@0 | 437 | NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIDOMFile, mIsFile) |
michael@0 | 438 | NS_INTERFACE_MAP_ENTRY(nsIXHRSendable) |
michael@0 | 439 | NS_INTERFACE_MAP_ENTRY(nsIMutable) |
michael@0 | 440 | NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO_CONDITIONAL(File, mIsFile) |
michael@0 | 441 | NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO_CONDITIONAL(Blob, !mIsFile) |
michael@0 | 442 | NS_INTERFACE_MAP_END |
michael@0 | 443 | |
michael@0 | 444 | // Threadsafe when GetMutable() == false |
michael@0 | 445 | NS_IMPL_ADDREF(nsDOMFile) |
michael@0 | 446 | NS_IMPL_RELEASE(nsDOMFile) |
michael@0 | 447 | |
michael@0 | 448 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 449 | // nsDOMFileCC implementation |
michael@0 | 450 | |
michael@0 | 451 | NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileCC) |
michael@0 | 452 | |
michael@0 | 453 | NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsDOMFileCC) |
michael@0 | 454 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMFileCC) |
michael@0 | 455 | // We don't have anything to traverse, but some of our subclasses do. |
michael@0 | 456 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 457 | |
michael@0 | 458 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMFileCC) |
michael@0 | 459 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFile) |
michael@0 | 460 | NS_INTERFACE_MAP_ENTRY(nsIDOMBlob) |
michael@0 | 461 | NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIDOMFile, mIsFile) |
michael@0 | 462 | NS_INTERFACE_MAP_ENTRY(nsIXHRSendable) |
michael@0 | 463 | NS_INTERFACE_MAP_ENTRY(nsIMutable) |
michael@0 | 464 | NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO_CONDITIONAL(File, mIsFile) |
michael@0 | 465 | NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO_CONDITIONAL(Blob, !mIsFile) |
michael@0 | 466 | NS_INTERFACE_MAP_END |
michael@0 | 467 | |
michael@0 | 468 | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMFileCC) |
michael@0 | 469 | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMFileCC) |
michael@0 | 470 | |
michael@0 | 471 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 472 | // nsDOMFileFile implementation |
michael@0 | 473 | |
michael@0 | 474 | already_AddRefed<nsIDOMBlob> |
michael@0 | 475 | nsDOMFileFile::CreateSlice(uint64_t aStart, uint64_t aLength, |
michael@0 | 476 | const nsAString& aContentType) |
michael@0 | 477 | { |
michael@0 | 478 | nsCOMPtr<nsIDOMBlob> t = new nsDOMFileFile(this, aStart, aLength, aContentType); |
michael@0 | 479 | return t.forget(); |
michael@0 | 480 | } |
michael@0 | 481 | |
michael@0 | 482 | NS_IMETHODIMP |
michael@0 | 483 | nsDOMFileFile::GetMozFullPathInternal(nsAString &aFilename) |
michael@0 | 484 | { |
michael@0 | 485 | NS_ASSERTION(mIsFile, "Should only be called on files"); |
michael@0 | 486 | return mFile->GetPath(aFilename); |
michael@0 | 487 | } |
michael@0 | 488 | |
michael@0 | 489 | NS_IMETHODIMP |
michael@0 | 490 | nsDOMFileFile::GetLastModifiedDate(JSContext* cx, JS::MutableHandle<JS::Value> aLastModifiedDate) |
michael@0 | 491 | { |
michael@0 | 492 | NS_ASSERTION(mIsFile, "Should only be called on files"); |
michael@0 | 493 | |
michael@0 | 494 | PRTime msecs; |
michael@0 | 495 | if (IsDateUnknown()) { |
michael@0 | 496 | nsresult rv = mFile->GetLastModifiedTime(&msecs); |
michael@0 | 497 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 498 | mLastModificationDate = msecs; |
michael@0 | 499 | } else { |
michael@0 | 500 | msecs = mLastModificationDate; |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | JSObject* date = JS_NewDateObjectMsec(cx, msecs); |
michael@0 | 504 | if (date) { |
michael@0 | 505 | aLastModifiedDate.setObject(*date); |
michael@0 | 506 | } |
michael@0 | 507 | else { |
michael@0 | 508 | date = JS_NewDateObjectMsec(cx, JS_Now() / PR_USEC_PER_MSEC); |
michael@0 | 509 | aLastModifiedDate.setObject(*date); |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | return NS_OK; |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | NS_IMETHODIMP |
michael@0 | 516 | nsDOMFileFile::GetSize(uint64_t *aFileSize) |
michael@0 | 517 | { |
michael@0 | 518 | if (IsSizeUnknown()) { |
michael@0 | 519 | NS_ASSERTION(mWholeFile, |
michael@0 | 520 | "Should only use lazy size when using the whole file"); |
michael@0 | 521 | int64_t fileSize; |
michael@0 | 522 | nsresult rv = mFile->GetFileSize(&fileSize); |
michael@0 | 523 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 524 | |
michael@0 | 525 | if (fileSize < 0) { |
michael@0 | 526 | return NS_ERROR_FAILURE; |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | mLength = fileSize; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | *aFileSize = mLength; |
michael@0 | 533 | |
michael@0 | 534 | return NS_OK; |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | NS_IMETHODIMP |
michael@0 | 538 | nsDOMFileFile::GetType(nsAString &aType) |
michael@0 | 539 | { |
michael@0 | 540 | if (mContentType.IsVoid()) { |
michael@0 | 541 | NS_ASSERTION(mWholeFile, |
michael@0 | 542 | "Should only use lazy ContentType when using the whole file"); |
michael@0 | 543 | nsresult rv; |
michael@0 | 544 | nsCOMPtr<nsIMIMEService> mimeService = |
michael@0 | 545 | do_GetService(NS_MIMESERVICE_CONTRACTID, &rv); |
michael@0 | 546 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 547 | |
michael@0 | 548 | nsAutoCString mimeType; |
michael@0 | 549 | rv = mimeService->GetTypeFromFile(mFile, mimeType); |
michael@0 | 550 | if (NS_FAILED(rv)) { |
michael@0 | 551 | mimeType.Truncate(); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | AppendUTF8toUTF16(mimeType, mContentType); |
michael@0 | 555 | mContentType.SetIsVoid(false); |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | aType = mContentType; |
michael@0 | 559 | |
michael@0 | 560 | return NS_OK; |
michael@0 | 561 | } |
michael@0 | 562 | |
michael@0 | 563 | NS_IMETHODIMP |
michael@0 | 564 | nsDOMFileFile::GetMozLastModifiedDate(uint64_t* aLastModifiedDate) |
michael@0 | 565 | { |
michael@0 | 566 | NS_ASSERTION(mIsFile, "Should only be called on files"); |
michael@0 | 567 | if (IsDateUnknown()) { |
michael@0 | 568 | PRTime msecs; |
michael@0 | 569 | nsresult rv = mFile->GetLastModifiedTime(&msecs); |
michael@0 | 570 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 571 | mLastModificationDate = msecs; |
michael@0 | 572 | } |
michael@0 | 573 | *aLastModifiedDate = mLastModificationDate; |
michael@0 | 574 | return NS_OK; |
michael@0 | 575 | } |
michael@0 | 576 | |
michael@0 | 577 | const uint32_t sFileStreamFlags = |
michael@0 | 578 | nsIFileInputStream::CLOSE_ON_EOF | |
michael@0 | 579 | nsIFileInputStream::REOPEN_ON_REWIND | |
michael@0 | 580 | nsIFileInputStream::DEFER_OPEN; |
michael@0 | 581 | |
michael@0 | 582 | NS_IMETHODIMP |
michael@0 | 583 | nsDOMFileFile::GetInternalStream(nsIInputStream **aStream) |
michael@0 | 584 | { |
michael@0 | 585 | return mWholeFile ? |
michael@0 | 586 | NS_NewLocalFileInputStream(aStream, mFile, -1, -1, sFileStreamFlags) : |
michael@0 | 587 | NS_NewPartialLocalFileInputStream(aStream, mFile, mStart, mLength, |
michael@0 | 588 | -1, -1, sFileStreamFlags); |
michael@0 | 589 | } |
michael@0 | 590 | |
michael@0 | 591 | void |
michael@0 | 592 | nsDOMFileFile::SetPath(const nsAString& aPath) |
michael@0 | 593 | { |
michael@0 | 594 | MOZ_ASSERT(aPath.IsEmpty() || |
michael@0 | 595 | aPath[aPath.Length() - 1] == char16_t('/'), |
michael@0 | 596 | "Path must end with a path separator"); |
michael@0 | 597 | mPath = aPath; |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 601 | // nsDOMMemoryFile implementation |
michael@0 | 602 | |
michael@0 | 603 | already_AddRefed<nsIDOMBlob> |
michael@0 | 604 | nsDOMMemoryFile::CreateSlice(uint64_t aStart, uint64_t aLength, |
michael@0 | 605 | const nsAString& aContentType) |
michael@0 | 606 | { |
michael@0 | 607 | nsCOMPtr<nsIDOMBlob> t = |
michael@0 | 608 | new nsDOMMemoryFile(this, aStart, aLength, aContentType); |
michael@0 | 609 | return t.forget(); |
michael@0 | 610 | } |
michael@0 | 611 | |
michael@0 | 612 | NS_IMETHODIMP |
michael@0 | 613 | nsDOMMemoryFile::GetInternalStream(nsIInputStream **aStream) |
michael@0 | 614 | { |
michael@0 | 615 | if (mLength > INT32_MAX) |
michael@0 | 616 | return NS_ERROR_FAILURE; |
michael@0 | 617 | |
michael@0 | 618 | return DataOwnerAdapter::Create(mDataOwner, mStart, mLength, aStream); |
michael@0 | 619 | } |
michael@0 | 620 | |
michael@0 | 621 | NS_IMETHODIMP_(bool) |
michael@0 | 622 | nsDOMMemoryFile::IsMemoryFile(void) |
michael@0 | 623 | { |
michael@0 | 624 | return true; |
michael@0 | 625 | } |
michael@0 | 626 | |
michael@0 | 627 | /* static */ StaticMutex |
michael@0 | 628 | nsDOMMemoryFile::DataOwner::sDataOwnerMutex; |
michael@0 | 629 | |
michael@0 | 630 | /* static */ StaticAutoPtr<LinkedList<nsDOMMemoryFile::DataOwner> > |
michael@0 | 631 | nsDOMMemoryFile::DataOwner::sDataOwners; |
michael@0 | 632 | |
michael@0 | 633 | /* static */ bool |
michael@0 | 634 | nsDOMMemoryFile::DataOwner::sMemoryReporterRegistered; |
michael@0 | 635 | |
michael@0 | 636 | MOZ_DEFINE_MALLOC_SIZE_OF(DOMMemoryFileDataOwnerMallocSizeOf) |
michael@0 | 637 | |
michael@0 | 638 | class nsDOMMemoryFileDataOwnerMemoryReporter MOZ_FINAL |
michael@0 | 639 | : public nsIMemoryReporter |
michael@0 | 640 | { |
michael@0 | 641 | public: |
michael@0 | 642 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 643 | |
michael@0 | 644 | NS_IMETHOD CollectReports(nsIMemoryReporterCallback *aCallback, |
michael@0 | 645 | nsISupports *aClosure) |
michael@0 | 646 | { |
michael@0 | 647 | typedef nsDOMMemoryFile::DataOwner DataOwner; |
michael@0 | 648 | |
michael@0 | 649 | StaticMutexAutoLock lock(DataOwner::sDataOwnerMutex); |
michael@0 | 650 | |
michael@0 | 651 | if (!DataOwner::sDataOwners) { |
michael@0 | 652 | return NS_OK; |
michael@0 | 653 | } |
michael@0 | 654 | |
michael@0 | 655 | const size_t LARGE_OBJECT_MIN_SIZE = 8 * 1024; |
michael@0 | 656 | size_t smallObjectsTotal = 0; |
michael@0 | 657 | |
michael@0 | 658 | for (DataOwner *owner = DataOwner::sDataOwners->getFirst(); |
michael@0 | 659 | owner; owner = owner->getNext()) { |
michael@0 | 660 | |
michael@0 | 661 | size_t size = DOMMemoryFileDataOwnerMallocSizeOf(owner->mData); |
michael@0 | 662 | |
michael@0 | 663 | if (size < LARGE_OBJECT_MIN_SIZE) { |
michael@0 | 664 | smallObjectsTotal += size; |
michael@0 | 665 | } |
michael@0 | 666 | else { |
michael@0 | 667 | SHA1Sum sha1; |
michael@0 | 668 | sha1.update(owner->mData, owner->mLength); |
michael@0 | 669 | uint8_t digest[SHA1Sum::HashSize]; // SHA1 digests are 20 bytes long. |
michael@0 | 670 | sha1.finish(digest); |
michael@0 | 671 | |
michael@0 | 672 | nsAutoCString digestString; |
michael@0 | 673 | for (size_t i = 0; i < sizeof(digest); i++) { |
michael@0 | 674 | digestString.AppendPrintf("%02x", digest[i]); |
michael@0 | 675 | } |
michael@0 | 676 | |
michael@0 | 677 | nsresult rv = aCallback->Callback( |
michael@0 | 678 | /* process */ NS_LITERAL_CSTRING(""), |
michael@0 | 679 | nsPrintfCString( |
michael@0 | 680 | "explicit/dom/memory-file-data/large/file(length=%llu, sha1=%s)", |
michael@0 | 681 | owner->mLength, digestString.get()), |
michael@0 | 682 | KIND_HEAP, UNITS_BYTES, size, |
michael@0 | 683 | nsPrintfCString( |
michael@0 | 684 | "Memory used to back a memory file of length %llu bytes. The file " |
michael@0 | 685 | "has a sha1 of %s.\n\n" |
michael@0 | 686 | "Note that the allocator may round up a memory file's length -- " |
michael@0 | 687 | "that is, an N-byte memory file may take up more than N bytes of " |
michael@0 | 688 | "memory.", |
michael@0 | 689 | owner->mLength, digestString.get()), |
michael@0 | 690 | aClosure); |
michael@0 | 691 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 692 | } |
michael@0 | 693 | } |
michael@0 | 694 | |
michael@0 | 695 | if (smallObjectsTotal > 0) { |
michael@0 | 696 | nsresult rv = aCallback->Callback( |
michael@0 | 697 | /* process */ NS_LITERAL_CSTRING(""), |
michael@0 | 698 | NS_LITERAL_CSTRING("explicit/dom/memory-file-data/small"), |
michael@0 | 699 | KIND_HEAP, UNITS_BYTES, smallObjectsTotal, |
michael@0 | 700 | nsPrintfCString( |
michael@0 | 701 | "Memory used to back small memory files (less than %d bytes each).\n\n" |
michael@0 | 702 | "Note that the allocator may round up a memory file's length -- " |
michael@0 | 703 | "that is, an N-byte memory file may take up more than N bytes of " |
michael@0 | 704 | "memory."), |
michael@0 | 705 | aClosure); |
michael@0 | 706 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | return NS_OK; |
michael@0 | 710 | } |
michael@0 | 711 | }; |
michael@0 | 712 | |
michael@0 | 713 | NS_IMPL_ISUPPORTS(nsDOMMemoryFileDataOwnerMemoryReporter, nsIMemoryReporter) |
michael@0 | 714 | |
michael@0 | 715 | /* static */ void |
michael@0 | 716 | nsDOMMemoryFile::DataOwner::EnsureMemoryReporterRegistered() |
michael@0 | 717 | { |
michael@0 | 718 | sDataOwnerMutex.AssertCurrentThreadOwns(); |
michael@0 | 719 | if (sMemoryReporterRegistered) { |
michael@0 | 720 | return; |
michael@0 | 721 | } |
michael@0 | 722 | |
michael@0 | 723 | RegisterStrongMemoryReporter(new nsDOMMemoryFileDataOwnerMemoryReporter()); |
michael@0 | 724 | |
michael@0 | 725 | sMemoryReporterRegistered = true; |
michael@0 | 726 | } |
michael@0 | 727 | |
michael@0 | 728 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 729 | // nsDOMFileList implementation |
michael@0 | 730 | |
michael@0 | 731 | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsDOMFileList) |
michael@0 | 732 | |
michael@0 | 733 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMFileList) |
michael@0 | 734 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
michael@0 | 735 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFileList) |
michael@0 | 736 | NS_INTERFACE_MAP_ENTRY(nsIDOMFileList) |
michael@0 | 737 | NS_INTERFACE_MAP_END |
michael@0 | 738 | |
michael@0 | 739 | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMFileList) |
michael@0 | 740 | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMFileList) |
michael@0 | 741 | |
michael@0 | 742 | JSObject* |
michael@0 | 743 | nsDOMFileList::WrapObject(JSContext *cx) |
michael@0 | 744 | { |
michael@0 | 745 | return FileListBinding::Wrap(cx, this); |
michael@0 | 746 | } |
michael@0 | 747 | |
michael@0 | 748 | NS_IMETHODIMP |
michael@0 | 749 | nsDOMFileList::GetLength(uint32_t* aLength) |
michael@0 | 750 | { |
michael@0 | 751 | *aLength = Length(); |
michael@0 | 752 | |
michael@0 | 753 | return NS_OK; |
michael@0 | 754 | } |
michael@0 | 755 | |
michael@0 | 756 | NS_IMETHODIMP |
michael@0 | 757 | nsDOMFileList::Item(uint32_t aIndex, nsIDOMFile **aFile) |
michael@0 | 758 | { |
michael@0 | 759 | NS_IF_ADDREF(*aFile = Item(aIndex)); |
michael@0 | 760 | |
michael@0 | 761 | return NS_OK; |
michael@0 | 762 | } |
michael@0 | 763 | |
michael@0 | 764 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 765 | // nsDOMFileInternalUrlHolder implementation |
michael@0 | 766 | |
michael@0 | 767 | nsDOMFileInternalUrlHolder::nsDOMFileInternalUrlHolder(nsIDOMBlob* aFile, |
michael@0 | 768 | nsIPrincipal* aPrincipal |
michael@0 | 769 | MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) { |
michael@0 | 770 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
michael@0 | 771 | aFile->GetInternalUrl(aPrincipal, mUrl); |
michael@0 | 772 | } |
michael@0 | 773 | |
michael@0 | 774 | nsDOMFileInternalUrlHolder::~nsDOMFileInternalUrlHolder() { |
michael@0 | 775 | if (!mUrl.IsEmpty()) { |
michael@0 | 776 | nsAutoCString narrowUrl; |
michael@0 | 777 | CopyUTF16toUTF8(mUrl, narrowUrl); |
michael@0 | 778 | nsBlobProtocolHandler::RemoveDataEntry(narrowUrl); |
michael@0 | 779 | } |
michael@0 | 780 | } |
michael@0 | 781 | |
michael@0 | 782 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 783 | // nsDOMTemporaryFileBlob implementation |
michael@0 | 784 | already_AddRefed<nsIDOMBlob> |
michael@0 | 785 | nsDOMTemporaryFileBlob::CreateSlice(uint64_t aStart, uint64_t aLength, |
michael@0 | 786 | const nsAString& aContentType) |
michael@0 | 787 | { |
michael@0 | 788 | if (aStart + aLength > mLength) |
michael@0 | 789 | return nullptr; |
michael@0 | 790 | |
michael@0 | 791 | nsCOMPtr<nsIDOMBlob> t = |
michael@0 | 792 | new nsDOMTemporaryFileBlob(this, aStart + mStartPos, aLength, aContentType); |
michael@0 | 793 | return t.forget(); |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | NS_IMETHODIMP |
michael@0 | 797 | nsDOMTemporaryFileBlob::GetInternalStream(nsIInputStream **aStream) |
michael@0 | 798 | { |
michael@0 | 799 | nsCOMPtr<nsIInputStream> stream = |
michael@0 | 800 | new nsTemporaryFileInputStream(mFileDescOwner, mStartPos, mStartPos + mLength); |
michael@0 | 801 | stream.forget(aStream); |
michael@0 | 802 | return NS_OK; |
michael@0 | 803 | } |