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 file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_simpledb_SDBConnection_h
8 #define mozilla_dom_simpledb_SDBConnection_h
9 
10 #include <cstdint>
11 #include "ErrorList.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/dom/quota/PersistenceType.h"
14 #include "nsCOMPtr.h"
15 #include "nsID.h"
16 #include "nsISDBConnection.h"
17 #include "nsISupports.h"
18 
19 #define NS_SDBCONNECTION_CONTRACTID "@mozilla.org/dom/sdb-connection;1"
20 
21 class nsISDBCloseCallback;
22 
23 namespace mozilla {
24 
25 namespace ipc {
26 
27 class PBackgroundChild;
28 class PrincipalInfo;
29 
30 }  // namespace ipc
31 
32 namespace dom {
33 
34 class SDBConnectionChild;
35 class SDBRequest;
36 class SDBRequestParams;
37 
38 class SDBConnection final : public nsISDBConnection {
39   using PersistenceType = mozilla::dom::quota::PersistenceType;
40   using PBackgroundChild = mozilla::ipc::PBackgroundChild;
41   using PrincipalInfo = mozilla::ipc::PrincipalInfo;
42 
43   nsCOMPtr<nsISDBCloseCallback> mCloseCallback;
44 
45   UniquePtr<PrincipalInfo> mPrincipalInfo;
46 
47   SDBConnectionChild* mBackgroundActor;
48 
49   PersistenceType mPersistenceType;
50   bool mRunningRequest;
51   bool mOpen;
52   bool mAllowedToClose;
53 
54  public:
55   static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
56 
AssertIsOnOwningThread()57   void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(SDBConnection); }
58 
59   void ClearBackgroundActor();
60 
61   void OnNewRequest();
62 
63   void OnRequestFinished();
64 
65   void OnOpen();
66 
67   void OnClose(bool aAbnormal);
68 
69   void AllowToClose();
70 
71  private:
72   SDBConnection();
73 
74   ~SDBConnection();
75 
76   nsresult CheckState();
77 
78   nsresult EnsureBackgroundActor();
79 
80   nsresult InitiateRequest(SDBRequest* aRequest,
81                            const SDBRequestParams& aParams);
82 
83   NS_DECL_ISUPPORTS
84   NS_DECL_NSISDBCONNECTION
85 };
86 
87 }  // namespace dom
88 }  // namespace mozilla
89 
90 #endif /* mozilla_dom_simpledb_SDBConnection_h */
91