Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/fetch/FetchStream.h
Line
Count
Source (jump to first uncovered line)
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_FetchStream_h
8
#define mozilla_dom_FetchStream_h
9
10
#include "Fetch.h"
11
#include "jsapi.h"
12
#include "nsIAsyncInputStream.h"
13
#include "nsIObserver.h"
14
#include "nsISupportsImpl.h"
15
#include "nsWeakReference.h"
16
17
class nsIGlobalObject;
18
19
class nsIInputStream;
20
21
namespace mozilla {
22
namespace dom {
23
24
class FetchStreamHolder;
25
class WeakWorkerRef;
26
27
class FetchStream final : public nsIInputStreamCallback
28
                        , public nsIObserver
29
                        , public nsSupportsWeakReference
30
{
31
public:
32
  NS_DECL_THREADSAFE_ISUPPORTS
33
  NS_DECL_NSIINPUTSTREAMCALLBACK
34
  NS_DECL_NSIOBSERVER
35
36
  static void
37
  Create(JSContext* aCx, FetchStreamHolder* aStreamHolder,
38
         nsIGlobalObject* aGlobal, nsIInputStream* aInputStream,
39
         JS::MutableHandle<JSObject*> aStream, ErrorResult& aRv);
40
41
  void
42
  Close();
43
44
  static nsresult
45
  RetrieveInputStream(void* aUnderlyingReadableStreamSource,
46
                      nsIInputStream** aInputStream);
47
48
private:
49
  FetchStream(nsIGlobalObject* aGlobal, FetchStreamHolder* aStreamHolder,
50
              nsIInputStream* aInputStream);
51
  ~FetchStream();
52
53
#ifdef DEBUG
54
  void
55
  AssertIsOnOwningThread();
56
#else
57
  void
58
0
  AssertIsOnOwningThread() {}
59
#endif
60
61
  static void
62
  RequestDataCallback(JSContext* aCx, JS::HandleObject aStream,
63
                      void* aUnderlyingSource, uint8_t aFlags,
64
                      size_t aDesiredSize);
65
66
  static void
67
  WriteIntoReadRequestCallback(JSContext* aCx, JS::HandleObject aStream,
68
                               void* aUnderlyingSource, uint8_t aFlags,
69
                               void* aBuffer, size_t aLength,
70
                               size_t* aByteWritten);
71
72
  static JS::Value
73
  CancelCallback(JSContext* aCx, JS::HandleObject aStream,
74
                 void* aUnderlyingSource, uint8_t aFlags,
75
                 JS::HandleValue aReason);
76
77
  static void
78
  ClosedCallback(JSContext* aCx, JS::HandleObject aStream,
79
                 void* aUnderlyingSource, uint8_t aFlags);
80
81
  static void
82
  ErroredCallback(JSContext* aCx, JS::HandleObject aStream,
83
                  void* aUnderlyingSource, uint8_t aFlags,
84
                  JS::HandleValue reason);
85
86
  static void
87
  FinalizeCallback(void* aUnderlyingSource, uint8_t aFlags);
88
89
  void
90
  ErrorPropagation(JSContext* aCx,
91
                   const MutexAutoLock& aProofOfLock,
92
                   JS::HandleObject aStream, nsresult aRv);
93
94
  void
95
  CloseAndReleaseObjects(JSContext* aCx,
96
                         const MutexAutoLock& aProofOfLock,
97
                         JS::HandleObject aSteam);
98
99
  class WorkerShutdown;
100
101
  void
102
  ReleaseObjects(const MutexAutoLock& aProofOfLock);
103
104
  void
105
  ReleaseObjects();
106
107
  // Common methods
108
109
  enum State {
110
    // This is the beginning state before any reading operation.
111
    eInitializing,
112
113
    // RequestDataCallback has not been called yet. We haven't started to read
114
    // data from the stream yet.
115
    eWaiting,
116
117
    // We are reading data in a separate I/O thread.
118
    eReading,
119
120
    // We are ready to write something in the JS Buffer.
121
    eWriting,
122
123
    // After a writing, we want to check if the stream is closed. After the
124
    // check, we go back to eWaiting. If a reading request happens in the
125
    // meantime, we move to eReading state.
126
    eChecking,
127
128
    // Operation completed.
129
    eClosed,
130
  };
131
132
  // We need a mutex because JS engine can release FetchStream on a non-owning
133
  // thread. We must be sure that the releasing of resources doesn't trigger
134
  // race conditions.
135
  Mutex mMutex;
136
137
  // Protected by mutex.
138
  State mState;
139
140
  nsCOMPtr<nsIGlobalObject> mGlobal;
141
  RefPtr<FetchStreamHolder> mStreamHolder;
142
  nsCOMPtr<nsIEventTarget> mOwningEventTarget;
143
144
  // This is the original inputStream received during the CTOR. It will be
145
  // converted into an nsIAsyncInputStream and stored into mInputStream at the
146
  // first use.
147
  nsCOMPtr<nsIInputStream> mOriginalInputStream;
148
  nsCOMPtr<nsIAsyncInputStream> mInputStream;
149
150
  RefPtr<WeakWorkerRef> mWorkerRef;
151
};
152
153
} // dom namespace
154
} // mozilla namespace
155
156
#endif // mozilla_dom_FetchStream_h