summaryrefslogtreecommitdiffstats
path: root/dom/media/CloneableWithRangeMediaResource.cpp
blob: 4faac6125fd4e1d0401e4acabfa671552d480115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */

#include "CloneableWithRangeMediaResource.h"

#include "mozilla/AbstractThread.h"
#include "mozilla/Monitor.h"
#include "nsContentUtils.h"
#include "nsIAsyncInputStream.h"
#include "nsITimedChannel.h"
#include "nsNetCID.h"
#include "nsServiceManagerUtils.h"

namespace mozilla {

namespace {

class InputStreamReader final : public nsIInputStreamCallback {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS

  static already_AddRefed<InputStreamReader> Create(
      nsICloneableInputStreamWithRange* aStream, int64_t aStart,
      uint32_t aLength) {
    MOZ_ASSERT(aStream);

    nsCOMPtr<nsIInputStream> stream;
    nsresult rv =
        aStream->CloneWithRange(aStart, aLength, getter_AddRefs(stream));
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return nullptr;
    }

    RefPtr<InputStreamReader> reader = new InputStreamReader(stream);
    return reader.forget();
  }

  nsresult Read(char* aBuffer, uint32_t aSize, uint32_t* aRead) {
    uint32_t done = 0;
    do {
      uint32_t read;
      nsresult rv = SyncRead(aBuffer + done, aSize - done, &read);
      if (NS_SUCCEEDED(rv) && read == 0) {
        break;
      }
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return rv;
      }
      done += read;
    } while (done != aSize);

    *aRead = done;
    return NS_OK;
  }

  NS_IMETHOD
  OnInputStreamReady(nsIAsyncInputStream* aStream) override {
    // Let's continue with SyncRead().
    MonitorAutoLock lock(mMonitor);
    lock.Notify();
    return NS_OK;
  }

 private:
  explicit InputStreamReader(nsIInputStream* aStream)
      : mStream(aStream), mMonitor("InputStreamReader::mMonitor") {
    MOZ_ASSERT(aStream);
  }

  ~InputStreamReader() = default;

  nsresult SyncRead(char* aBuffer, uint32_t aSize, uint32_t* aRead) {
    while (1) {
      nsresult rv = mStream->Read(aBuffer, aSize, aRead);
      // All good.
      if (rv == NS_BASE_STREAM_CLOSED || NS_SUCCEEDED(rv)) {
        return NS_OK;
      }

      // An error.
      if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) {
        return rv;
      }

      // We need to proceed async.
      if (!mAsyncStream) {
        mAsyncStream = do_QueryInterface(mStream);
      }

      if (!mAsyncStream) {
        return NS_ERROR_FAILURE;
      }

      nsCOMPtr<nsIEventTarget> target =
          do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
      MOZ_ASSERT(target);

      {
        // We wait for ::OnInputStreamReady() to be called.
        MonitorAutoLock lock(mMonitor);

        rv = mAsyncStream->AsyncWait(this, 0, aSize, target);
        if (NS_WARN_IF(NS_FAILED(rv))) {
          return rv;
        }

        lock.Wait();
      }
    }
  }

  nsCOMPtr<nsIInputStream> mStream;
  nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
  Monitor mMonitor MOZ_UNANNOTATED;
};

NS_IMPL_ADDREF(InputStreamReader);
NS_IMPL_RELEASE(InputStreamReader);

NS_INTERFACE_MAP_BEGIN(InputStreamReader)
  NS_INTERFACE_MAP_ENTRY(nsIInputStreamCallback)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStreamCallback)
NS_INTERFACE_MAP_END

}  // namespace

void CloneableWithRangeMediaResource::MaybeInitialize() {
  if (!mInitialized) {
    mInitialized = true;
    mCallback->AbstractMainThread()->Dispatch(NewRunnableMethod<nsresult>(
        "MediaResourceCallback::NotifyDataEnded", mCallback.get(),
        &MediaResourceCallback::NotifyDataEnded, NS_OK));
  }
}

nsresult CloneableWithRangeMediaResource::GetCachedRanges(
    MediaByteRangeSet& aRanges) {
  MaybeInitialize();
  aRanges += MediaByteRange(0, (int64_t)mSize);
  return NS_OK;
}

nsresult CloneableWithRangeMediaResource::Open(
    nsIStreamListener** aStreamListener) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aStreamListener);

  *aStreamListener = nullptr;
  return NS_OK;
}

RefPtr<GenericPromise> CloneableWithRangeMediaResource::Close() {
  return GenericPromise::CreateAndResolve(true, __func__);
}

already_AddRefed<nsIPrincipal>
CloneableWithRangeMediaResource::GetCurrentPrincipal() {
  MOZ_ASSERT(NS_IsMainThread());

  nsCOMPtr<nsIPrincipal> principal;
  nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
  if (!secMan || !mChannel) {
    return nullptr;
  }

  secMan->GetChannelResultPrincipal(mChannel, getter_AddRefs(principal));
  return principal.forget();
}

bool CloneableWithRangeMediaResource::HadCrossOriginRedirects() {
  MOZ_ASSERT(NS_IsMainThread());

  nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(mChannel);
  if (!timedChannel) {
    return false;
  }

  bool allRedirectsSameOrigin = false;
  return NS_SUCCEEDED(timedChannel->GetAllRedirectsSameOrigin(
             &allRedirectsSameOrigin)) &&
         !allRedirectsSameOrigin;
}

nsresult CloneableWithRangeMediaResource::ReadFromCache(char* aBuffer,
                                                        int64_t aOffset,
                                                        uint32_t aCount) {
  MaybeInitialize();
  if (!aCount) {
    return NS_OK;
  }

  RefPtr<InputStreamReader> reader =
      InputStreamReader::Create(mStream, aOffset, aCount);
  if (!reader) {
    return NS_ERROR_FAILURE;
  }

  uint32_t bytes = 0;
  nsresult rv = reader->Read(aBuffer, aCount, &bytes);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  return bytes == aCount ? NS_OK : NS_ERROR_FAILURE;
}

nsresult CloneableWithRangeMediaResource::ReadAt(int64_t aOffset, char* aBuffer,
                                                 uint32_t aCount,
                                                 uint32_t* aBytes) {
  MOZ_ASSERT(!NS_IsMainThread());

  RefPtr<InputStreamReader> reader =
      InputStreamReader::Create(mStream, aOffset, aCount);
  if (!reader) {
    return NS_ERROR_FAILURE;
  }

  nsresult rv = reader->Read(aBuffer, aCount, aBytes);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  return NS_OK;
}

}  // namespace mozilla