2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-07-25 04:29:47 +00:00
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
#ifndef mozilla_dom_File_h
|
|
|
|
#define mozilla_dom_File_h
|
2007-07-25 04:29:47 +00:00
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-09-09 11:15:14 +00:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2011-11-16 07:50:19 +00:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2012-10-25 02:06:31 +00:00
|
|
|
#include "mozilla/LinkedList.h"
|
2013-05-24 17:10:48 +00:00
|
|
|
#include "mozilla/StaticMutex.h"
|
2012-10-25 02:06:31 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2015-09-09 11:15:14 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2014-10-08 16:15:22 +00:00
|
|
|
#include "mozilla/dom/Date.h"
|
2014-10-07 17:16:11 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2014-10-08 16:15:22 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2015-05-18 13:52:26 +00:00
|
|
|
#include "nsIDOMBlob.h"
|
2014-10-08 16:15:22 +00:00
|
|
|
#include "nsIFile.h"
|
2016-11-02 09:35:57 +00:00
|
|
|
#include "nsIMemoryReporter.h"
|
2014-10-08 16:15:22 +00:00
|
|
|
#include "nsIMutable.h"
|
|
|
|
#include "nsIXMLHttpRequest.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTemporaryFileInputStream.h"
|
|
|
|
#include "nsWrapperCache.h"
|
2014-09-26 23:21:57 +00:00
|
|
|
#include "nsWeakReference.h"
|
2011-07-12 02:41:29 +00:00
|
|
|
|
2007-07-25 04:29:47 +00:00
|
|
|
class nsIFile;
|
|
|
|
class nsIInputStream;
|
|
|
|
|
2015-07-22 16:03:43 +00:00
|
|
|
#define BLOBIMPL_IID \
|
2015-01-11 21:34:31 +00:00
|
|
|
{ 0xbccb3275, 0x6778, 0x4ac5, \
|
|
|
|
{ 0xaf, 0x03, 0x90, 0xed, 0x37, 0xad, 0xdf, 0x5d } }
|
2014-06-27 03:46:34 +00:00
|
|
|
|
2014-06-26 16:47:44 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2014-04-29 20:02:41 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
struct BlobPropertyBag;
|
2014-10-08 16:15:23 +00:00
|
|
|
struct ChromeFilePropertyBag;
|
2014-10-08 16:15:22 +00:00
|
|
|
struct FilePropertyBag;
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImpl;
|
2015-05-12 12:09:51 +00:00
|
|
|
class File;
|
2016-06-01 10:33:09 +00:00
|
|
|
class OwningArrayBufferOrArrayBufferViewOrBlobOrUSVString;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
class Blob : public nsIDOMBlob
|
|
|
|
, public nsIXHRSendable
|
|
|
|
, public nsIMutable
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
, public nsWrapperCache
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-06-03 16:33:52 +00:00
|
|
|
NS_DECL_NSIDOMBLOB
|
|
|
|
NS_DECL_NSIXHRSENDABLE
|
|
|
|
NS_DECL_NSIMUTABLE
|
|
|
|
|
2014-06-27 03:46:34 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Blob, nsIDOMBlob)
|
2015-05-11 17:50:50 +00:00
|
|
|
|
2016-06-01 10:33:09 +00:00
|
|
|
typedef OwningArrayBufferOrArrayBufferViewOrBlobOrUSVString BlobPart;
|
|
|
|
|
2016-01-20 17:25:03 +00:00
|
|
|
// This creates a Blob or a File based on the type of BlobImpl.
|
2015-05-12 12:09:51 +00:00
|
|
|
static Blob*
|
2015-05-12 12:11:03 +00:00
|
|
|
Create(nsISupports* aParent, BlobImpl* aImpl);
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
static already_AddRefed<Blob>
|
2015-05-11 21:54:02 +00:00
|
|
|
Create(nsISupports* aParent, const nsAString& aContentType,
|
|
|
|
uint64_t aLength);
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
static already_AddRefed<Blob>
|
2015-05-11 21:54:02 +00:00
|
|
|
Create(nsISupports* aParent, const nsAString& aContentType, uint64_t aStart,
|
|
|
|
uint64_t aLength);
|
|
|
|
|
2016-10-25 05:53:54 +00:00
|
|
|
static already_AddRefed<Blob>
|
|
|
|
CreateStringBlob(nsISupports* aParent, const nsACString& aData,
|
|
|
|
const nsAString& aContentType);
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
// The returned Blob takes ownership of aMemoryBuffer. aMemoryBuffer will be
|
2015-05-11 15:43:59 +00:00
|
|
|
// freed by free so it must be allocated by malloc or something
|
|
|
|
// compatible with it.
|
2015-05-12 12:09:51 +00:00
|
|
|
static already_AddRefed<Blob>
|
|
|
|
CreateMemoryBlob(nsISupports* aParent, void* aMemoryBuffer, uint64_t aLength,
|
|
|
|
const nsAString& aContentType);
|
|
|
|
|
|
|
|
static already_AddRefed<Blob>
|
|
|
|
CreateTemporaryBlob(nsISupports* aParent, PRFileDesc* aFD,
|
|
|
|
uint64_t aStartPos, uint64_t aLength,
|
|
|
|
const nsAString& aContentType);
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImpl* Impl() const
|
2015-05-12 12:09:51 +00:00
|
|
|
{
|
|
|
|
return mImpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFile() const;
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
const nsTArray<RefPtr<BlobImpl>>* GetSubBlobImpls() const;
|
2015-05-12 12:09:51 +00:00
|
|
|
|
|
|
|
// This method returns null if this Blob is not a File; it returns
|
|
|
|
// the same object in case this Blob already implements the File interface;
|
2015-05-12 12:11:03 +00:00
|
|
|
// otherwise it returns a new File object with the same BlobImpl.
|
2015-05-12 12:09:51 +00:00
|
|
|
already_AddRefed<File> ToFile();
|
|
|
|
|
|
|
|
// This method creates a new File object with the given name and the same
|
2015-05-12 12:11:03 +00:00
|
|
|
// BlobImpl.
|
2015-12-04 21:15:46 +00:00
|
|
|
already_AddRefed<File> ToFile(const nsAString& aName,
|
|
|
|
ErrorResult& aRv) const;
|
2015-05-12 12:09:51 +00:00
|
|
|
|
|
|
|
already_AddRefed<Blob>
|
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
void
|
|
|
|
GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv);
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
GetFileId();
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
// WebIDL methods
|
|
|
|
nsISupports* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
bool
|
|
|
|
IsMemoryFile() const;
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
// Blob constructor
|
|
|
|
static already_AddRefed<Blob>
|
|
|
|
Constructor(const GlobalObject& aGlobal,
|
2016-06-01 10:33:09 +00:00
|
|
|
const Optional<Sequence<BlobPart>>& aData,
|
2015-05-12 12:09:51 +00:00
|
|
|
const BlobPropertyBag& aBag,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
|
|
|
uint64_t GetSize(ErrorResult& aRv);
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
void GetType(nsAString& aType);
|
2015-05-12 12:09:51 +00:00
|
|
|
|
|
|
|
already_AddRefed<Blob> Slice(const Optional<int64_t>& aStart,
|
|
|
|
const Optional<int64_t>& aEnd,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// File constructor should never be used directly. Use Blob::Create instead.
|
2015-05-12 12:11:03 +00:00
|
|
|
Blob(nsISupports* aParent, BlobImpl* aImpl);
|
2015-05-12 12:09:51 +00:00
|
|
|
virtual ~Blob() {};
|
|
|
|
|
|
|
|
virtual bool HasFileInterface() const { return false; }
|
|
|
|
|
|
|
|
// The member is the real backend implementation of this File/Blob.
|
|
|
|
// It's thread-safe and not CC-able and it's the only element that is moved
|
|
|
|
// between threads.
|
|
|
|
// Note: we should not store any other state in this class!
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> mImpl;
|
2015-05-12 12:09:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsISupports> mParent;
|
|
|
|
};
|
|
|
|
|
|
|
|
class File final : public Blob
|
|
|
|
{
|
|
|
|
friend class Blob;
|
|
|
|
|
|
|
|
public:
|
2015-05-12 12:11:03 +00:00
|
|
|
// Note: BlobImpl must be a File in order to use this method.
|
2015-05-12 12:09:51 +00:00
|
|
|
// Check impl->IsFile().
|
|
|
|
static File*
|
2015-05-12 12:11:03 +00:00
|
|
|
Create(nsISupports* aParent, BlobImpl* aImpl);
|
2015-05-12 12:09:51 +00:00
|
|
|
|
2015-05-11 15:43:59 +00:00
|
|
|
static already_AddRefed<File>
|
2015-05-12 12:09:51 +00:00
|
|
|
Create(nsISupports* aParent, const nsAString& aName,
|
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
2016-03-20 10:56:10 +00:00
|
|
|
int64_t aLastModifiedDate);
|
2015-05-12 12:09:51 +00:00
|
|
|
|
2015-05-11 21:54:02 +00:00
|
|
|
// The returned File takes ownership of aMemoryBuffer. aMemoryBuffer will be
|
|
|
|
// freed by free so it must be allocated by malloc or something
|
|
|
|
// compatible with it.
|
|
|
|
static already_AddRefed<File>
|
|
|
|
CreateMemoryFile(nsISupports* aParent, void* aMemoryBuffer, uint64_t aLength,
|
2015-05-12 12:09:51 +00:00
|
|
|
const nsAString& aName, const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate);
|
2015-05-11 21:54:02 +00:00
|
|
|
|
2016-03-31 07:32:58 +00:00
|
|
|
// This method creates a BlobFileImpl for the new File object. This is
|
|
|
|
// thread-safe, cross-process, cross-thread as any other BlobImpl, but, when
|
|
|
|
// GetType() is called, it must dispatch a runnable to the main-thread in
|
|
|
|
// order to use nsIMIMEService.
|
|
|
|
// Would be nice if we try to avoid to use this method outside the
|
|
|
|
// main-thread to avoid extra runnables.
|
2014-10-08 16:15:23 +00:00
|
|
|
static already_AddRefed<File>
|
2014-10-15 05:55:14 +00:00
|
|
|
CreateFromFile(nsISupports* aParent, nsIFile* aFile, bool aTemporary = false);
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
static already_AddRefed<File>
|
2014-10-08 16:15:22 +00:00
|
|
|
CreateFromFile(nsISupports* aParent, nsIFile* aFile, const nsAString& aName,
|
2014-06-26 16:47:44 +00:00
|
|
|
const nsAString& aContentType);
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
// WebIDL methods
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext *cx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2014-10-08 16:15:22 +00:00
|
|
|
|
|
|
|
// File constructor
|
2014-10-08 16:15:23 +00:00
|
|
|
static already_AddRefed<File>
|
2014-10-08 16:15:22 +00:00
|
|
|
Constructor(const GlobalObject& aGlobal,
|
2016-06-01 10:33:09 +00:00
|
|
|
const Sequence<BlobPart>& aData,
|
2014-10-08 16:15:22 +00:00
|
|
|
const nsAString& aName,
|
|
|
|
const FilePropertyBag& aBag,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2016-11-11 17:56:44 +00:00
|
|
|
// ChromeOnly
|
2014-10-08 16:15:23 +00:00
|
|
|
static already_AddRefed<File>
|
2016-11-11 17:56:44 +00:00
|
|
|
CreateFromFileName(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aData,
|
|
|
|
const ChromeFilePropertyBag& aBag,
|
|
|
|
ErrorResult& aRv);
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2016-11-11 17:56:44 +00:00
|
|
|
// ChromeOnly
|
2014-10-08 16:15:23 +00:00
|
|
|
static already_AddRefed<File>
|
2016-11-11 17:56:44 +00:00
|
|
|
CreateFromNsIFile(const GlobalObject& aGlobal,
|
|
|
|
nsIFile* aData,
|
|
|
|
const ChromeFilePropertyBag& aBag,
|
|
|
|
ErrorResult& aRv);
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2016-04-01 10:50:16 +00:00
|
|
|
void GetName(nsAString& aName) const;
|
2014-10-08 16:15:22 +00:00
|
|
|
|
|
|
|
int64_t GetLastModified(ErrorResult& aRv);
|
|
|
|
|
|
|
|
Date GetLastModifiedDate(ErrorResult& aRv);
|
|
|
|
|
2016-05-13 11:11:38 +00:00
|
|
|
// GetPath and SetPath are currently used only for the webkitRelativePath
|
|
|
|
// attribute and they are only used when this File object is created from a
|
|
|
|
// Directory, generated by a Directory Picker.
|
|
|
|
|
|
|
|
void GetPath(nsAString& aName) const;
|
|
|
|
|
|
|
|
void SetPath(const nsAString& aName);
|
2015-05-12 12:09:51 +00:00
|
|
|
|
2015-06-03 00:11:16 +00:00
|
|
|
void GetMozFullPath(nsAString& aFilename, ErrorResult& aRv) const;
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2015-06-03 00:11:16 +00:00
|
|
|
void GetMozFullPathInternal(nsAString& aName, ErrorResult& aRv) const;
|
2015-05-18 13:51:54 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
protected:
|
|
|
|
virtual bool HasFileInterface() const override { return true; }
|
2014-06-27 03:46:34 +00:00
|
|
|
|
|
|
|
private:
|
2015-05-12 12:09:51 +00:00
|
|
|
// File constructor should never be used directly. Use Blob::Create or
|
|
|
|
// File::Create.
|
2015-05-12 12:11:03 +00:00
|
|
|
File(nsISupports* aParent, BlobImpl* aImpl);
|
2014-10-08 16:15:23 +00:00
|
|
|
~File() {};
|
2014-06-26 16:47:44 +00:00
|
|
|
};
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
// This is the abstract class for any File backend. It must be nsISupports
|
2014-06-26 16:47:44 +00:00
|
|
|
// because this class must be ref-counted and it has to work with IPC.
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImpl : public nsISupports
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-07-22 16:03:43 +00:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(BLOBIMPL_IID)
|
2014-06-27 03:46:34 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImpl() {}
|
2012-10-18 18:29:32 +00:00
|
|
|
|
2016-04-01 10:50:16 +00:00
|
|
|
virtual void GetName(nsAString& aName) const = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2016-05-13 11:11:38 +00:00
|
|
|
virtual void GetPath(nsAString& aName) const = 0;
|
|
|
|
|
|
|
|
virtual void SetPath(const nsAString& aName) = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
virtual int64_t GetLastModified(ErrorResult& aRv) = 0;
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-02-07 18:29:22 +00:00
|
|
|
virtual void SetLastModified(int64_t aLastModified) = 0;
|
|
|
|
|
2015-06-03 00:11:16 +00:00
|
|
|
virtual void GetMozFullPath(nsAString& aName, ErrorResult& aRv) const = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-06-03 00:11:16 +00:00
|
|
|
virtual void GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) const = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
virtual uint64_t GetSize(ErrorResult& aRv) = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
virtual void GetType(nsAString& aType) = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-05-21 01:49:50 +00:00
|
|
|
/**
|
|
|
|
* An effectively-unique serial number identifying this instance of FileImpl.
|
|
|
|
*
|
|
|
|
* Implementations should obtain a serial number from
|
|
|
|
* FileImplBase::NextSerialNumber().
|
|
|
|
*/
|
|
|
|
virtual uint64_t GetSerialNumber() const = 0;
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
already_AddRefed<BlobImpl>
|
2014-10-08 16:15:22 +00:00
|
|
|
Slice(const Optional<int64_t>& aStart, const Optional<int64_t>& aEnd,
|
|
|
|
const nsAString& aContentType, ErrorResult& aRv);
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual already_AddRefed<BlobImpl>
|
2014-06-26 16:47:44 +00:00
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
2014-10-08 16:15:22 +00:00
|
|
|
const nsAString& aContentType, ErrorResult& aRv) = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
virtual const nsTArray<RefPtr<BlobImpl>>*
|
2014-06-27 03:46:34 +00:00
|
|
|
GetSubBlobImpls() const = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
virtual void GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
|
|
|
virtual int64_t GetFileId() = 0;
|
|
|
|
|
|
|
|
virtual nsresult GetSendInfo(nsIInputStream** aBody,
|
|
|
|
uint64_t* aContentLength,
|
|
|
|
nsACString& aContentType,
|
|
|
|
nsACString& aCharset) = 0;
|
|
|
|
|
|
|
|
virtual nsresult GetMutable(bool* aMutable) const = 0;
|
|
|
|
|
|
|
|
virtual nsresult SetMutable(bool aMutable) = 0;
|
|
|
|
|
|
|
|
virtual void SetLazyData(const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aLength,
|
2015-12-07 22:00:51 +00:00
|
|
|
int64_t aLastModifiedDate) = 0;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
|
|
|
virtual bool IsMemoryFile() const = 0;
|
|
|
|
|
|
|
|
virtual bool IsSizeUnknown() const = 0;
|
|
|
|
|
|
|
|
virtual bool IsDateUnknown() const = 0;
|
|
|
|
|
|
|
|
virtual bool IsFile() const = 0;
|
|
|
|
|
2016-07-29 11:41:38 +00:00
|
|
|
// Returns true if the BlobImpl is backed by an nsIFile and the underlying
|
|
|
|
// file is a directory.
|
|
|
|
virtual bool IsDirectory() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-11 21:35:24 +00:00
|
|
|
// True if this implementation can be sent to other threads.
|
|
|
|
virtual bool MayBeClonedToOtherThreads() const
|
2014-06-27 03:46:34 +00:00
|
|
|
{
|
2015-01-11 21:35:24 +00:00
|
|
|
return true;
|
2014-06-27 03:46:34 +00:00
|
|
|
}
|
|
|
|
|
2012-06-03 16:33:52 +00:00
|
|
|
protected:
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual ~BlobImpl() {}
|
2014-06-26 16:47:44 +00:00
|
|
|
};
|
|
|
|
|
2015-07-22 16:03:43 +00:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(BlobImpl, BLOBIMPL_IID)
|
2015-01-11 21:34:31 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImplBase : public BlobImpl
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase(const nsAString& aName, const nsAString& aContentType,
|
2016-03-20 10:56:10 +00:00
|
|
|
uint64_t aLength, int64_t aLastModifiedDate)
|
2014-06-26 16:47:44 +00:00
|
|
|
: mIsFile(true)
|
|
|
|
, mImmutable(false)
|
|
|
|
, mContentType(aContentType)
|
|
|
|
, mName(aName)
|
|
|
|
, mStart(0)
|
|
|
|
, mLength(aLength)
|
|
|
|
, mLastModificationDate(aLastModifiedDate)
|
2015-05-21 01:49:50 +00:00
|
|
|
, mSerialNumber(NextSerialNumber())
|
2012-10-18 18:29:32 +00:00
|
|
|
{
|
|
|
|
// Ensure non-null mContentType by default
|
|
|
|
mContentType.SetIsVoid(false);
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase(const nsAString& aName, const nsAString& aContentType,
|
2014-10-08 16:15:23 +00:00
|
|
|
uint64_t aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
: mIsFile(true)
|
|
|
|
, mImmutable(false)
|
|
|
|
, mContentType(aContentType)
|
|
|
|
, mName(aName)
|
|
|
|
, mStart(0)
|
|
|
|
, mLength(aLength)
|
2015-04-27 11:17:19 +00:00
|
|
|
, mLastModificationDate(INT64_MAX)
|
2015-05-21 01:49:50 +00:00
|
|
|
, mSerialNumber(NextSerialNumber())
|
2011-07-12 02:41:29 +00:00
|
|
|
{
|
|
|
|
// Ensure non-null mContentType by default
|
2011-10-17 14:59:28 +00:00
|
|
|
mContentType.SetIsVoid(false);
|
2011-07-12 02:41:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase(const nsAString& aContentType, uint64_t aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
: mIsFile(false)
|
|
|
|
, mImmutable(false)
|
|
|
|
, mContentType(aContentType)
|
|
|
|
, mStart(0)
|
|
|
|
, mLength(aLength)
|
2015-04-27 11:17:19 +00:00
|
|
|
, mLastModificationDate(INT64_MAX)
|
2015-05-21 01:49:50 +00:00
|
|
|
, mSerialNumber(NextSerialNumber())
|
2011-07-12 02:41:29 +00:00
|
|
|
{
|
|
|
|
// Ensure non-null mContentType by default
|
2011-10-17 14:59:28 +00:00
|
|
|
mContentType.SetIsVoid(false);
|
2011-07-12 02:41:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase(const nsAString& aContentType, uint64_t aStart,
|
2014-10-08 16:15:23 +00:00
|
|
|
uint64_t aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
: mIsFile(false)
|
|
|
|
, mImmutable(false)
|
|
|
|
, mContentType(aContentType)
|
|
|
|
, mStart(aStart)
|
|
|
|
, mLength(aLength)
|
2015-04-27 11:17:19 +00:00
|
|
|
, mLastModificationDate(INT64_MAX)
|
2015-05-21 01:49:50 +00:00
|
|
|
, mSerialNumber(NextSerialNumber())
|
2011-07-12 02:41:29 +00:00
|
|
|
{
|
2012-01-11 08:23:07 +00:00
|
|
|
NS_ASSERTION(aLength != UINT64_MAX,
|
2011-07-12 02:41:29 +00:00
|
|
|
"Must know length when creating slice");
|
|
|
|
// Ensure non-null mContentType by default
|
2011-10-17 14:59:28 +00:00
|
|
|
mContentType.SetIsVoid(false);
|
2011-07-12 02:41:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 10:50:16 +00:00
|
|
|
virtual void GetName(nsAString& aName) const override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2016-05-13 11:11:38 +00:00
|
|
|
virtual void GetPath(nsAString& aName) const override;
|
|
|
|
|
|
|
|
virtual void SetPath(const nsAString& aName) override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual int64_t GetLastModified(ErrorResult& aRv) override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void SetLastModified(int64_t aLastModified) override;
|
2015-02-07 18:29:22 +00:00
|
|
|
|
2015-06-03 00:11:16 +00:00
|
|
|
virtual void GetMozFullPath(nsAString& aName, ErrorResult& aRv) const override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
virtual void GetMozFullPathInternal(nsAString& aFileName,
|
2015-06-03 00:11:16 +00:00
|
|
|
ErrorResult& aRv) const override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual uint64_t GetSize(ErrorResult& aRv) override
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
|
|
|
return mLength;
|
|
|
|
}
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void GetType(nsAString& aType) override;
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-21 01:49:50 +00:00
|
|
|
virtual uint64_t GetSerialNumber() const override { return mSerialNumber; }
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual already_AddRefed<BlobImpl>
|
2014-06-26 16:47:44 +00:00
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
2015-03-21 16:28:04 +00:00
|
|
|
const nsAString& aContentType, ErrorResult& aRv) override
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
virtual const nsTArray<RefPtr<BlobImpl>>*
|
2015-03-21 16:28:04 +00:00
|
|
|
GetSubBlobImpls() const override
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
virtual void GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) override
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
2015-05-19 14:36:37 +00:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
2014-10-08 16:15:22 +00:00
|
|
|
}
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual int64_t GetFileId() override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
|
|
|
virtual nsresult GetSendInfo(nsIInputStream** aBody,
|
|
|
|
uint64_t* aContentLength,
|
|
|
|
nsACString& aContentType,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsACString& aCharset) override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual nsresult GetMutable(bool* aMutable) const override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual nsresult SetMutable(bool aMutable) override;
|
2014-06-26 16:47:44 +00:00
|
|
|
|
|
|
|
virtual void
|
|
|
|
SetLazyData(const nsAString& aName, const nsAString& aContentType,
|
2015-12-07 22:00:51 +00:00
|
|
|
uint64_t aLength, int64_t aLastModifiedDate) override
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
mName = aName;
|
|
|
|
mContentType = aContentType;
|
|
|
|
mLength = aLength;
|
|
|
|
mLastModificationDate = aLastModifiedDate;
|
|
|
|
mIsFile = !aName.IsVoid();
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual bool IsMemoryFile() const override
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual bool IsDateUnknown() const override
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
2015-04-27 11:17:19 +00:00
|
|
|
return mIsFile && mLastModificationDate == INT64_MAX;
|
2014-06-26 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual bool IsFile() const override
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
return mIsFile;
|
|
|
|
}
|
2011-07-12 02:41:29 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual bool IsSizeUnknown() const override
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
return mLength == UINT64_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual ~BlobImplBase() {}
|
2014-06-26 16:47:44 +00:00
|
|
|
|
2015-05-21 01:49:50 +00:00
|
|
|
/**
|
|
|
|
* Returns a new, effectively-unique serial number. This should be used
|
|
|
|
* by implementations to obtain a serial number for GetSerialNumber().
|
|
|
|
* The implementation is thread safe.
|
|
|
|
*/
|
|
|
|
static uint64_t NextSerialNumber();
|
|
|
|
|
2011-07-12 02:41:29 +00:00
|
|
|
bool mIsFile;
|
2011-07-19 16:27:46 +00:00
|
|
|
bool mImmutable;
|
2012-10-18 18:29:32 +00:00
|
|
|
|
2011-07-12 02:41:29 +00:00
|
|
|
nsString mContentType;
|
|
|
|
nsString mName;
|
2013-08-23 07:41:17 +00:00
|
|
|
nsString mPath; // The path relative to a directory chosen by the user
|
2011-07-12 02:41:29 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t mStart;
|
|
|
|
uint64_t mLength;
|
2011-12-16 07:34:24 +00:00
|
|
|
|
2015-04-27 11:17:19 +00:00
|
|
|
int64_t mLastModificationDate;
|
2012-10-18 18:29:32 +00:00
|
|
|
|
2015-05-21 01:49:50 +00:00
|
|
|
const uint64_t mSerialNumber;
|
2011-07-12 02:41:29 +00:00
|
|
|
};
|
|
|
|
|
2016-10-25 05:53:54 +00:00
|
|
|
class BlobImplString final : public BlobImplBase
|
2016-11-02 09:35:57 +00:00
|
|
|
, public nsIMemoryReporter
|
2016-10-25 05:53:54 +00:00
|
|
|
{
|
2016-11-02 09:35:57 +00:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
|
|
|
|
|
2016-10-25 05:53:54 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2016-11-02 09:35:57 +00:00
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
2016-10-25 05:53:54 +00:00
|
|
|
|
2016-11-02 09:35:57 +00:00
|
|
|
static already_AddRefed<BlobImplString>
|
|
|
|
Create(const nsACString& aData, const nsAString& aContentType);
|
2016-10-25 05:53:54 +00:00
|
|
|
|
|
|
|
virtual void GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
virtual already_AddRefed<BlobImpl>
|
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType, ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
private:
|
2016-11-02 09:35:57 +00:00
|
|
|
BlobImplString(const nsACString& aData, const nsAString& aContentType);
|
|
|
|
|
|
|
|
~BlobImplString();
|
2016-10-25 05:53:54 +00:00
|
|
|
|
|
|
|
nsCString mData;
|
|
|
|
};
|
2016-11-02 09:35:57 +00:00
|
|
|
|
2014-06-26 16:47:44 +00:00
|
|
|
/**
|
|
|
|
* This class may be used off the main thread, and in particular, its
|
|
|
|
* constructor and destructor may not run on the same thread. Be careful!
|
|
|
|
*/
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImplMemory final : public BlobImplBase
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-06-27 03:46:34 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplMemory(void* aMemoryBuffer, uint64_t aLength, const nsAString& aName,
|
2015-04-27 11:17:19 +00:00
|
|
|
const nsAString& aContentType, int64_t aLastModifiedDate)
|
2016-03-20 10:56:10 +00:00
|
|
|
: BlobImplBase(aName, aContentType, aLength, aLastModifiedDate)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mDataOwner(new DataOwner(aMemoryBuffer, aLength))
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mDataOwner && mDataOwner->mData, "must have data");
|
|
|
|
}
|
2012-10-18 18:29:32 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplMemory(void* aMemoryBuffer, uint64_t aLength,
|
2014-10-08 16:15:23 +00:00
|
|
|
const nsAString& aContentType)
|
2015-05-12 12:11:03 +00:00
|
|
|
: BlobImplBase(aContentType, aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mDataOwner(new DataOwner(aMemoryBuffer, aLength))
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mDataOwner && mDataOwner->mData, "must have data");
|
|
|
|
}
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
virtual void GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) override;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual already_AddRefed<BlobImpl>
|
2014-06-26 16:47:44 +00:00
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
2015-03-21 16:28:04 +00:00
|
|
|
const nsAString& aContentType, ErrorResult& aRv) override;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual bool IsMemoryFile() const override
|
2014-06-26 16:47:44 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-25 02:09:15 +00:00
|
|
|
|
2015-03-27 18:52:19 +00:00
|
|
|
class DataOwner final : public mozilla::LinkedListElement<DataOwner>
|
|
|
|
{
|
2014-06-26 16:47:44 +00:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataOwner)
|
|
|
|
DataOwner(void* aMemoryBuffer, uint64_t aLength)
|
|
|
|
: mData(aMemoryBuffer)
|
|
|
|
, mLength(aLength)
|
|
|
|
{
|
|
|
|
mozilla::StaticMutexAutoLock lock(sDataOwnerMutex);
|
|
|
|
|
|
|
|
if (!sDataOwners) {
|
|
|
|
sDataOwners = new mozilla::LinkedList<DataOwner>();
|
|
|
|
EnsureMemoryReporterRegistered();
|
|
|
|
}
|
|
|
|
sDataOwners->insertBack(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~DataOwner() {
|
|
|
|
mozilla::StaticMutexAutoLock lock(sDataOwnerMutex);
|
|
|
|
|
|
|
|
remove();
|
|
|
|
if (sDataOwners->isEmpty()) {
|
|
|
|
// Free the linked list if it's empty.
|
|
|
|
sDataOwners = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-02-19 04:51:06 +00:00
|
|
|
free(mData);
|
2014-06-26 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void EnsureMemoryReporterRegistered();
|
|
|
|
|
|
|
|
// sDataOwners and sMemoryReporterRegistered may only be accessed while
|
|
|
|
// holding sDataOwnerMutex! You also must hold the mutex while touching
|
|
|
|
// elements of the linked list that DataOwner inherits from.
|
|
|
|
static mozilla::StaticMutex sDataOwnerMutex;
|
|
|
|
static mozilla::StaticAutoPtr<mozilla::LinkedList<DataOwner> > sDataOwners;
|
|
|
|
static bool sMemoryReporterRegistered;
|
|
|
|
|
|
|
|
void* mData;
|
|
|
|
uint64_t mLength;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Create slice
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplMemory(const BlobImplMemory* aOther, uint64_t aStart,
|
2014-10-08 16:15:23 +00:00
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
2015-05-12 12:11:03 +00:00
|
|
|
: BlobImplBase(aContentType, aOther->mStart + aStart, aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mDataOwner(aOther->mDataOwner)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mDataOwner && mDataOwner->mData, "must have data");
|
|
|
|
mImmutable = aOther->mImmutable;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
~BlobImplMemory() {}
|
2014-06-26 16:47:44 +00:00
|
|
|
|
|
|
|
// Used when backed by a memory store
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataOwner> mDataOwner;
|
2012-06-03 16:33:52 +00:00
|
|
|
};
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImplTemporaryBlob final : public BlobImplBase
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-06-27 03:46:34 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplTemporaryBlob(PRFileDesc* aFD, uint64_t aStartPos,
|
2015-05-12 12:09:51 +00:00
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
2015-05-12 12:11:03 +00:00
|
|
|
: BlobImplBase(aContentType, aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mStartPos(aStartPos)
|
|
|
|
{
|
|
|
|
mFileDescOwner = new nsTemporaryFileInputStream::FileDescOwner(aFD);
|
|
|
|
}
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
virtual void GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) override;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual already_AddRefed<BlobImpl>
|
2014-06-26 16:47:44 +00:00
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
2015-03-21 16:28:04 +00:00
|
|
|
const nsAString& aContentType, ErrorResult& aRv) override;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-06-26 16:47:44 +00:00
|
|
|
private:
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplTemporaryBlob(const BlobImplTemporaryBlob* aOther,
|
2015-05-12 12:09:51 +00:00
|
|
|
uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType)
|
2015-05-12 12:11:03 +00:00
|
|
|
: BlobImplBase(aContentType, aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mStartPos(aStart)
|
|
|
|
, mFileDescOwner(aOther->mFileDescOwner)
|
2015-04-25 03:28:54 +00:00
|
|
|
{}
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
~BlobImplTemporaryBlob() {}
|
2014-06-25 02:09:15 +00:00
|
|
|
|
2014-06-26 16:47:44 +00:00
|
|
|
uint64_t mStartPos;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsTemporaryFileInputStream::FileDescOwner> mFileDescOwner;
|
2012-06-03 16:33:52 +00:00
|
|
|
};
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImplFile : public BlobImplBase
|
2011-07-12 02:41:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-06-27 03:46:34 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2011-07-12 02:41:29 +00:00
|
|
|
// Create as a file
|
2015-05-12 12:11:03 +00:00
|
|
|
explicit BlobImplFile(nsIFile* aFile, bool aTemporary = false)
|
2015-06-22 23:31:40 +00:00
|
|
|
: BlobImplBase(EmptyString(), EmptyString(), UINT64_MAX, INT64_MAX)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mFile(aFile)
|
|
|
|
, mWholeFile(true)
|
2014-10-15 05:55:14 +00:00
|
|
|
, mIsTemporary(aTemporary)
|
2010-10-13 23:25:33 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mFile, "must have file");
|
2011-07-12 02:41:29 +00:00
|
|
|
// Lazily get the content type and size
|
2011-10-17 14:59:28 +00:00
|
|
|
mContentType.SetIsVoid(true);
|
2011-07-12 02:41:29 +00:00
|
|
|
mFile->GetLeafName(mName);
|
2010-10-13 23:25:33 +00:00
|
|
|
}
|
|
|
|
|
2012-06-03 16:33:52 +00:00
|
|
|
// Create as a file
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile(const nsAString& aName, const nsAString& aContentType,
|
2014-10-08 16:15:23 +00:00
|
|
|
uint64_t aLength, nsIFile* aFile)
|
2015-06-22 23:31:40 +00:00
|
|
|
: BlobImplBase(aName, aContentType, aLength, UINT64_MAX)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mFile(aFile)
|
|
|
|
, mWholeFile(true)
|
2014-10-15 05:55:14 +00:00
|
|
|
, mIsTemporary(false)
|
2012-10-18 18:29:32 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mFile, "must have file");
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile(const nsAString& aName, const nsAString& aContentType,
|
2014-10-08 16:15:23 +00:00
|
|
|
uint64_t aLength, nsIFile* aFile,
|
2015-04-27 11:17:19 +00:00
|
|
|
int64_t aLastModificationDate)
|
2015-06-22 23:31:40 +00:00
|
|
|
: BlobImplBase(aName, aContentType, aLength, aLastModificationDate)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mFile(aFile)
|
|
|
|
, mWholeFile(true)
|
2014-10-15 05:55:14 +00:00
|
|
|
, mIsTemporary(false)
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mFile, "must have file");
|
|
|
|
}
|
|
|
|
|
2012-05-21 16:18:30 +00:00
|
|
|
// Create as a file with custom name
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile(nsIFile* aFile, const nsAString& aName,
|
2014-10-08 16:15:23 +00:00
|
|
|
const nsAString& aContentType)
|
2015-06-22 23:31:40 +00:00
|
|
|
: BlobImplBase(aName, aContentType, UINT64_MAX, INT64_MAX)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mFile(aFile)
|
|
|
|
, mWholeFile(true)
|
2014-10-15 05:55:14 +00:00
|
|
|
, mIsTemporary(false)
|
2012-05-21 16:18:30 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mFile, "must have file");
|
2012-11-27 11:48:15 +00:00
|
|
|
if (aContentType.IsEmpty()) {
|
|
|
|
// Lazily get the content type and size
|
|
|
|
mContentType.SetIsVoid(true);
|
|
|
|
}
|
2012-05-21 16:18:30 +00:00
|
|
|
}
|
|
|
|
|
2011-07-12 02:41:29 +00:00
|
|
|
// Overrides
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual uint64_t GetSize(ErrorResult& aRv) override;
|
|
|
|
virtual void GetType(nsAString& aType) override;
|
|
|
|
virtual int64_t GetLastModified(ErrorResult& aRv) override;
|
|
|
|
virtual void SetLastModified(int64_t aLastModified) override;
|
2014-10-08 16:15:22 +00:00
|
|
|
virtual void GetMozFullPathInternal(nsAString& aFullPath,
|
2015-06-03 00:11:16 +00:00
|
|
|
ErrorResult& aRv) const override;
|
2015-05-19 14:36:37 +00:00
|
|
|
virtual void GetInternalStream(nsIInputStream** aInputStream,
|
|
|
|
ErrorResult& aRv) override;
|
2011-07-12 02:41:29 +00:00
|
|
|
|
2016-07-29 11:41:38 +00:00
|
|
|
virtual bool IsDirectory() const override;
|
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
// We always have size and date for this kind of blob.
|
|
|
|
virtual bool IsSizeUnknown() const override { return false; }
|
|
|
|
virtual bool IsDateUnknown() const override { return false; }
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
protected:
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual ~BlobImplFile() {
|
2014-10-15 05:55:14 +00:00
|
|
|
if (mFile && mIsTemporary) {
|
2015-05-12 12:11:03 +00:00
|
|
|
NS_WARNING("In temporary ~BlobImplFile");
|
2014-10-15 05:55:14 +00:00
|
|
|
// Ignore errors if any, not much we can do. Clean-up will be done by
|
|
|
|
// https://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsAnonymousTemporaryFile.cpp?rev=6c1c7e45c902#127
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
mFile->Remove(false);
|
2016-09-01 05:01:16 +00:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to remove temporary DOMFile.");
|
2014-10-15 05:55:14 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-26 23:21:57 +00:00
|
|
|
|
2014-06-26 16:47:44 +00:00
|
|
|
private:
|
2011-07-12 02:41:29 +00:00
|
|
|
// Create slice
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile(const BlobImplFile* aOther, uint64_t aStart,
|
2014-10-08 16:15:23 +00:00
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
2015-05-12 12:11:03 +00:00
|
|
|
: BlobImplBase(aContentType, aOther->mStart + aStart, aLength)
|
2014-06-26 16:47:44 +00:00
|
|
|
, mFile(aOther->mFile)
|
|
|
|
, mWholeFile(false)
|
2014-10-15 05:55:14 +00:00
|
|
|
, mIsTemporary(false)
|
2011-07-12 02:41:29 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mFile, "must have file");
|
2011-07-19 16:27:46 +00:00
|
|
|
mImmutable = aOther->mImmutable;
|
2011-07-12 02:41:29 +00:00
|
|
|
}
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
virtual already_AddRefed<BlobImpl>
|
2012-08-22 15:56:38 +00:00
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
2015-03-21 16:28:04 +00:00
|
|
|
const nsAString& aContentType, ErrorResult& aRv) override;
|
2011-07-12 02:41:29 +00:00
|
|
|
|
2007-07-25 04:29:47 +00:00
|
|
|
nsCOMPtr<nsIFile> mFile;
|
2011-07-12 02:41:29 +00:00
|
|
|
bool mWholeFile;
|
2014-10-15 05:55:14 +00:00
|
|
|
bool mIsTemporary;
|
2007-07-25 04:29:47 +00:00
|
|
|
};
|
|
|
|
|
2016-01-20 17:25:03 +00:00
|
|
|
class EmptyBlobImpl final : public BlobImplBase
|
2016-01-08 08:35:30 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2016-01-20 17:25:03 +00:00
|
|
|
explicit EmptyBlobImpl(const nsAString& aContentType)
|
|
|
|
: BlobImplBase(aContentType, 0 /* aLength */)
|
2016-04-11 18:31:53 +00:00
|
|
|
{
|
|
|
|
mImmutable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EmptyBlobImpl(const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate)
|
|
|
|
: BlobImplBase(aName, aContentType, 0, aLastModifiedDate)
|
|
|
|
{
|
|
|
|
mImmutable = true;
|
|
|
|
}
|
2016-01-08 08:35:30 +00:00
|
|
|
|
|
|
|
virtual void GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
virtual already_AddRefed<BlobImpl>
|
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType, ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
virtual bool IsMemoryFile() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-01-20 17:25:03 +00:00
|
|
|
~EmptyBlobImpl() {}
|
2016-01-08 08:35:30 +00:00
|
|
|
};
|
|
|
|
|
2016-09-21 10:27:26 +00:00
|
|
|
class BlobImplStream final : public BlobImplBase
|
2016-11-04 06:52:04 +00:00
|
|
|
, public nsIMemoryReporter
|
2016-09-21 10:27:26 +00:00
|
|
|
{
|
2016-11-07 07:45:42 +00:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
|
|
|
|
|
2016-09-21 10:27:26 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2016-11-04 06:52:04 +00:00
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
2016-09-21 10:27:26 +00:00
|
|
|
|
2016-11-04 06:52:04 +00:00
|
|
|
static already_AddRefed<BlobImplStream>
|
|
|
|
Create(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aLength);
|
2016-09-21 10:27:26 +00:00
|
|
|
|
2016-11-04 06:52:04 +00:00
|
|
|
static already_AddRefed<BlobImplStream>
|
|
|
|
Create(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate,
|
|
|
|
uint64_t aLength);
|
2016-09-21 10:27:26 +00:00
|
|
|
|
|
|
|
virtual void GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
virtual already_AddRefed<BlobImpl>
|
|
|
|
CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType, ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
virtual bool IsMemoryFile() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-04 06:52:04 +00:00
|
|
|
BlobImplStream(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aLength);
|
|
|
|
|
|
|
|
BlobImplStream(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate,
|
|
|
|
uint64_t aLength);
|
|
|
|
|
2016-09-21 10:27:26 +00:00
|
|
|
BlobImplStream(BlobImplStream* aOther,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aStart,
|
|
|
|
uint64_t aLength);
|
|
|
|
|
|
|
|
~BlobImplStream();
|
|
|
|
|
2016-11-04 06:52:04 +00:00
|
|
|
void MaybeRegisterMemoryReporter();
|
|
|
|
|
2016-09-21 10:27:26 +00:00
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
#endif // mozilla_dom_File_h
|