summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/IDBFactory.h
blob: 08663eaec417f65ab8294b4c129285d45b11e7c7 (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
/* -*- 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_dom_idbfactory_h__
#define mozilla_dom_idbfactory_h__

#include "mozilla/Attributes.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/quota/PersistenceType.h"
#include "mozilla/GlobalTeardownObserver.h"
#include "mozilla/UniquePtr.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsISupports.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsWrapperCache.h"

class nsIGlobalObject;
class nsIPrincipal;
class nsISerialEventTarget;
class nsPIDOMWindowInner;

namespace mozilla {

class ErrorResult;

namespace ipc {

class PBackgroundChild;
class PrincipalInfo;

}  // namespace ipc

namespace dom {

struct IDBOpenDBOptions;
class IDBOpenDBRequest;
template <typename>
class Optional;
class BrowserChild;
enum class CallerType : uint32_t;

namespace indexedDB {
class BackgroundFactoryChild;
class FactoryRequestParams;
class LoggingInfo;
}  // namespace indexedDB

class IDBFactory final : public GlobalTeardownObserver, public nsWrapperCache {
  using PBackgroundChild = mozilla::ipc::PBackgroundChild;
  using PrincipalInfo = mozilla::ipc::PrincipalInfo;

  class BackgroundCreateCallback;
  struct PendingRequestInfo;
  struct IDBFactoryGuard {};

  UniquePtr<PrincipalInfo> mPrincipalInfo;

  // TODO: Unused, remove me!
  nsCOMPtr<nsIGlobalObject> mGlobal;

  // This will only be set if the factory belongs to a window in a child
  // process.
  RefPtr<BrowserChild> mBrowserChild;

  indexedDB::BackgroundFactoryChild* mBackgroundActor;

  // It is either set to a DocGroup-specific EventTarget if created by
  // CreateForWindow() or set to GetCurrentSerialEventTarget() otherwise.
  nsCOMPtr<nsISerialEventTarget> mEventTarget;

  uint64_t mInnerWindowID;
  uint32_t mActiveTransactionCount;
  uint32_t mActiveDatabaseCount;

  // When mAllowed is false we throw security errors on all operations. This is
  // because although we make storage access decisions when we create the
  // IDBFactory, the spec (and content) expects us to only throw if an attempt
  // is made to use the resulting IDBFactory.
  bool mAllowed;
  bool mBackgroundActorFailed;
  bool mPrivateBrowsingMode;

 public:
  IDBFactory(const IDBFactoryGuard&, bool aAllowed);

  static Result<RefPtr<IDBFactory>, nsresult> CreateForWindow(
      nsPIDOMWindowInner* aWindow);

  static Result<RefPtr<IDBFactory>, nsresult> CreateForMainThreadJS(
      nsIGlobalObject* aGlobal);

  // mAllowed shall be false for null aPrincipalInfo.
  static Result<RefPtr<IDBFactory>, nsresult> CreateForWorker(
      nsIGlobalObject* aGlobal, UniquePtr<PrincipalInfo>&& aPrincipalInfo,
      uint64_t aInnerWindowID);

  static bool AllowedForWindow(nsPIDOMWindowInner* aWindow);

  static bool AllowedForPrincipal(nsIPrincipal* aPrincipal,
                                  bool* aIsSystemPrincipal = nullptr);

  static quota::PersistenceType GetPersistenceType(
      const PrincipalInfo& aPrincipalInfo);

  void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(IDBFactory); }

  nsISerialEventTarget* EventTarget() const {
    AssertIsOnOwningThread();
    MOZ_RELEASE_ASSERT(mEventTarget);
    return mEventTarget;
  }

  void ClearBackgroundActor() {
    AssertIsOnOwningThread();

    mBackgroundActor = nullptr;
  }

  // Increase/Decrease the number of active transactions for the decision
  // making of preemption and throttling.
  // Note: If the state of its actor is not committed or aborted, it could block
  // IDB operations in other window.
  void UpdateActiveTransactionCount(int32_t aDelta);

  // Increase/Decrease the number of active databases and IDBOpenRequests for
  // the decision making of preemption and throttling.
  // Note: A non-closed database or a pending IDBOpenRequest could block
  // IDB operations in other window.
  void UpdateActiveDatabaseCount(int32_t aDelta);

  // BindingUtils.h's FindAssociatedGlobalForNative needs this.
  nsIGlobalObject* GetParentObject() const { return GetOwnerGlobal(); }

  BrowserChild* GetBrowserChild() const { return mBrowserChild; }

  PrincipalInfo* GetPrincipalInfo() const {
    AssertIsOnOwningThread();

    return mPrincipalInfo.get();
  }

  uint64_t InnerWindowID() const {
    AssertIsOnOwningThread();

    return mInnerWindowID;
  }

  bool IsChrome() const;

  [[nodiscard]] RefPtr<IDBOpenDBRequest> Open(
      JSContext* aCx, const nsAString& aName,
      const Optional<uint64_t>& aVersion, CallerType aCallerType,
      ErrorResult& aRv);

  [[nodiscard]] RefPtr<IDBOpenDBRequest> DeleteDatabase(JSContext* aCx,
                                                        const nsAString& aName,
                                                        CallerType aCallerType,
                                                        ErrorResult& aRv);

  already_AddRefed<Promise> Databases(JSContext* aCx, ErrorResult& aRv);

  int16_t Cmp(JSContext* aCx, JS::Handle<JS::Value> aFirst,
              JS::Handle<JS::Value> aSecond, ErrorResult& aRv);

  [[nodiscard]] RefPtr<IDBOpenDBRequest> OpenForPrincipal(
      JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
      uint64_t aVersion, SystemCallerGuarantee, ErrorResult& aRv);

  [[nodiscard]] RefPtr<IDBOpenDBRequest> OpenForPrincipal(
      JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
      const IDBOpenDBOptions& aOptions, SystemCallerGuarantee,
      ErrorResult& aRv);

  [[nodiscard]] RefPtr<IDBOpenDBRequest> DeleteForPrincipal(
      JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
      const IDBOpenDBOptions& aOptions, SystemCallerGuarantee,
      ErrorResult& aRv);

  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory)

  // nsWrapperCache
  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 private:
  ~IDBFactory();

  static Result<RefPtr<IDBFactory>, nsresult> CreateForMainThreadJSInternal(
      nsIGlobalObject* aGlobal, UniquePtr<PrincipalInfo> aPrincipalInfo);

  static Result<RefPtr<IDBFactory>, nsresult> CreateInternal(
      nsIGlobalObject* aGlobal, UniquePtr<PrincipalInfo> aPrincipalInfo,
      uint64_t aInnerWindowID);

  static nsresult AllowedForWindowInternal(nsPIDOMWindowInner* aWindow,
                                           nsCOMPtr<nsIPrincipal>* aPrincipal);

  nsresult EnsureBackgroundActor();

  [[nodiscard]] RefPtr<IDBOpenDBRequest> OpenInternal(
      JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
      const Optional<uint64_t>& aVersion, bool aDeleting,
      CallerType aCallerType, ErrorResult& aRv);

  nsresult InitiateRequest(const NotNull<RefPtr<IDBOpenDBRequest>>& aRequest,
                           const indexedDB::FactoryRequestParams& aParams);
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_idbfactory_h__