Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_dom_MutableBlobStorage_h
8 : #define mozilla_dom_MutableBlobStorage_h
9 :
10 : #include "mozilla/RefPtr.h"
11 : #include "prio.h"
12 :
13 : class nsIEventTarget;
14 :
15 : namespace mozilla {
16 :
17 : class TaskQueue;
18 :
19 : namespace dom {
20 :
21 : class Blob;
22 : class BlobImpl;
23 : class MutableBlobStorage;
24 :
25 4 : class MutableBlobStorageCallback
26 : {
27 : public:
28 : NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
29 :
30 : virtual void BlobStoreCompleted(MutableBlobStorage* aBlobStorage,
31 : Blob* aBlob,
32 : nsresult aRv) = 0;
33 : };
34 :
35 : // This class is main-thread only.
36 : class MutableBlobStorage final
37 : {
38 : public:
39 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MutableBlobStorage);
40 :
41 : enum MutableBlobStorageType
42 : {
43 : eOnlyInMemory,
44 : eCouldBeInTemporaryFile,
45 : };
46 :
47 : explicit MutableBlobStorage(MutableBlobStorageType aType,
48 : nsIEventTarget* aEventTarget = nullptr);
49 :
50 : nsresult Append(const void* aData, uint32_t aLength);
51 :
52 : // This method can be called just once.
53 : // The callback will be called when the Blob is ready.
54 : // The return value is the total size of the blob, when created.
55 : uint64_t GetBlobWhenReady(nsISupports* aParent,
56 : const nsACString& aContentType,
57 : MutableBlobStorageCallback* aCallback);
58 :
59 : void TemporaryFileCreated(PRFileDesc* aFD);
60 :
61 : void CreateBlobAndRespond(already_AddRefed<nsISupports> aParent,
62 : const nsACString& aContentType,
63 : already_AddRefed<MutableBlobStorageCallback> aCallback);
64 :
65 : void ErrorPropagated(nsresult aRv);
66 :
67 0 : nsIEventTarget* EventTarget()
68 : {
69 0 : MOZ_ASSERT(mEventTarget);
70 0 : return mEventTarget;
71 : }
72 :
73 : private:
74 : ~MutableBlobStorage();
75 :
76 : bool ExpandBufferSize(uint64_t aSize);
77 :
78 : bool ShouldBeTemporaryStorage(uint64_t aSize) const;
79 :
80 : nsresult MaybeCreateTemporaryFile();
81 :
82 : void DispatchToIOThread(already_AddRefed<nsIRunnable> aRunnable);
83 :
84 : // All these variables are touched on the main thread only.
85 :
86 : void* mData;
87 : uint64_t mDataLen;
88 : uint64_t mDataBufferLen;
89 :
90 : enum StorageState {
91 : eKeepInMemory,
92 : eInMemory,
93 : eWaitingForTemporaryFile,
94 : eInTemporaryFile,
95 : eClosed
96 : };
97 :
98 : StorageState mStorageState;
99 :
100 : PRFileDesc* mFD;
101 :
102 : nsresult mErrorResult;
103 :
104 : RefPtr<TaskQueue> mTaskQueue;
105 : nsCOMPtr<nsIEventTarget> mEventTarget;
106 :
107 : nsCOMPtr<nsISupports> mPendingParent;
108 : nsCString mPendingContentType;
109 : RefPtr<MutableBlobStorageCallback> mPendingCallback;
110 : };
111 :
112 : } // namespace dom
113 : } // namespace mozilla
114 :
115 : #endif // mozilla_dom_MutableBlobStorage_h
|