Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "MutableBlobStreamListener.h"
7 : #include "MutableBlobStorage.h"
8 :
9 : namespace mozilla {
10 : namespace dom {
11 :
12 0 : MutableBlobStreamListener::MutableBlobStreamListener(MutableBlobStorage::MutableBlobStorageType aStorageType,
13 : nsISupports* aParent,
14 : const nsACString& aContentType,
15 : MutableBlobStorageCallback* aCallback,
16 0 : nsIEventTarget* aEventTarget)
17 : : mCallback(aCallback)
18 : , mParent(aParent)
19 : , mStorageType(aStorageType)
20 : , mContentType(aContentType)
21 0 : , mEventTarget(aEventTarget)
22 : {
23 0 : MOZ_ASSERT(NS_IsMainThread());
24 0 : MOZ_ASSERT(aCallback);
25 :
26 0 : if (!mEventTarget) {
27 0 : mEventTarget = GetMainThreadEventTarget();
28 : }
29 :
30 0 : MOZ_ASSERT(mEventTarget);
31 0 : }
32 :
33 0 : MutableBlobStreamListener::~MutableBlobStreamListener()
34 : {
35 0 : MOZ_ASSERT(NS_IsMainThread());
36 0 : }
37 :
38 0 : NS_IMPL_ISUPPORTS(MutableBlobStreamListener,
39 : nsIStreamListener,
40 : nsIRequestObserver)
41 :
42 : NS_IMETHODIMP
43 0 : MutableBlobStreamListener::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
44 : {
45 0 : MOZ_ASSERT(NS_IsMainThread());
46 0 : MOZ_ASSERT(!mStorage);
47 0 : MOZ_ASSERT(mEventTarget);
48 :
49 0 : mStorage = new MutableBlobStorage(mStorageType, mEventTarget);
50 0 : return NS_OK;
51 : }
52 :
53 : NS_IMETHODIMP
54 0 : MutableBlobStreamListener::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
55 : nsresult aStatus)
56 : {
57 0 : MOZ_ASSERT(NS_IsMainThread());
58 0 : MOZ_ASSERT(mStorage);
59 :
60 : // Resetting mStorage to nullptr.
61 0 : RefPtr<MutableBlobStorage> storage;
62 0 : storage.swap(mStorage);
63 :
64 : // Let's propagate the error simulating a failure of the storage.
65 0 : if (NS_FAILED(aStatus)) {
66 0 : mCallback->BlobStoreCompleted(storage, nullptr, aStatus);
67 0 : return NS_OK;
68 : }
69 :
70 0 : storage->GetBlobWhenReady(mParent, mContentType, mCallback);
71 0 : return NS_OK;
72 : }
73 :
74 : NS_IMETHODIMP
75 0 : MutableBlobStreamListener::OnDataAvailable(nsIRequest* aRequest,
76 : nsISupports* aContext,
77 : nsIInputStream* aStream,
78 : uint64_t aSourceOffset,
79 : uint32_t aCount)
80 : {
81 0 : MOZ_ASSERT(NS_IsMainThread());
82 0 : MOZ_ASSERT(mStorage);
83 :
84 : uint32_t countRead;
85 0 : return aStream->ReadSegments(WriteSegmentFun, this, aCount, &countRead);
86 : }
87 :
88 : nsresult
89 0 : MutableBlobStreamListener::WriteSegmentFun(nsIInputStream* aWriterStream,
90 : void* aClosure,
91 : const char* aFromSegment,
92 : uint32_t aToOffset,
93 : uint32_t aCount,
94 : uint32_t* aWriteCount)
95 : {
96 0 : MOZ_ASSERT(NS_IsMainThread());
97 :
98 0 : MutableBlobStreamListener* self = static_cast<MutableBlobStreamListener*>(aClosure);
99 0 : MOZ_ASSERT(self->mStorage);
100 :
101 0 : nsresult rv = self->mStorage->Append(aFromSegment, aCount);
102 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
103 0 : return rv;
104 : }
105 :
106 0 : *aWriteCount = aCount;
107 0 : return NS_OK;
108 : }
109 :
110 : } // namespace net
111 : } // namespace mozilla
|