summaryrefslogtreecommitdiffstats
path: root/dom/file/ipc/RemoteLazyInputStreamChild.h
blob: 7e86a90ac85ae6c8f53551001b1534c1f26f596b (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#ifndef mozilla_RemoteLazyInputStreamChild_h
#define mozilla_RemoteLazyInputStreamChild_h

#include "mozilla/PRemoteLazyInputStreamChild.h"
#include "mozilla/RemoteLazyInputStream.h"
#include "mozilla/Mutex.h"
#include "mozilla/UniquePtr.h"
#include "nsTArray.h"

namespace mozilla {

class RemoteLazyInputStream;

namespace dom {
class ThreadSafeWorkerRef;
}

class RemoteLazyInputStreamChild final : public PRemoteLazyInputStreamChild {
 public:
  enum ActorState {
    // The actor is connected via IPDL to the parent.
    eActive,

    // The actor is disconnected.
    eInactive,

    // The actor is waiting to be disconnected. Once it has been disconnected,
    // it will be reactivated on the DOM-File thread.
    eActiveMigrating,

    // The actor has been disconnected and it's waiting to be connected on the
    // DOM-File thread.
    eInactiveMigrating,
  };

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RemoteLazyInputStreamChild, final)

  RemoteLazyInputStreamChild(const nsID& aID, uint64_t aSize);

  void ActorDestroy(IProtocol::ActorDestroyReason aReason) override;

  ActorState State();

  already_AddRefed<RemoteLazyInputStream> CreateStream();

  void ForgetStream(RemoteLazyInputStream* aStream);

  const nsID& ID() const { return mID; }

  uint64_t Size() const { return mSize; }

  void StreamNeeded(RemoteLazyInputStream* aStream,
                    nsIEventTarget* aEventTarget);

  mozilla::ipc::IPCResult RecvStreamReady(const Maybe<IPCStream>& aStream);

  void LengthNeeded(RemoteLazyInputStream* aStream,
                    nsIEventTarget* aEventTarget);

  mozilla::ipc::IPCResult RecvLengthReady(const int64_t& aLength);

  void Shutdown();

  void Migrated();

 private:
  ~RemoteLazyInputStreamChild();

  // Raw pointers because these streams keep this actor alive. When the last
  // stream is unregister, the actor will be deleted. This list is protected by
  // mutex.
  nsTArray<RemoteLazyInputStream*> mStreams;

  // This mutex protects mStreams because that can be touched in any thread.
  Mutex mMutex;

  const nsID mID;
  const uint64_t mSize;

  ActorState mState;

  // This struct and the array are used for creating streams when needed.
  struct PendingOperation {
    RefPtr<RemoteLazyInputStream> mStream;
    nsCOMPtr<nsIEventTarget> mEventTarget;
    enum {
      eStreamNeeded,
      eLengthNeeded,
    } mOp;
  };
  nsTArray<PendingOperation> mPendingOperations;

  nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;

  RefPtr<dom::ThreadSafeWorkerRef> mWorkerRef;
};

}  // namespace mozilla

#endif  // mozilla_RemoteLazyInputStreamChild_h