Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / indexedDB / IndexedDatabaseManager.h
blob27bad25a874972651e6acab752a383403c8edfc1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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/. */
7 #ifndef mozilla_dom_indexeddb_indexeddatabasemanager_h__
8 #define mozilla_dom_indexeddb_indexeddatabasemanager_h__
10 #include "mozilla/dom/indexedDB/IndexedDatabase.h"
12 #include "nsIObserver.h"
14 #include "js/TypeDecls.h"
15 #include "mozilla/Atomics.h"
16 #include "mozilla/dom/quota/PersistenceType.h"
17 #include "mozilla/Mutex.h"
18 #include "nsClassHashtable.h"
19 #include "nsHashKeys.h"
21 class nsPIDOMWindow;
23 namespace mozilla {
24 class EventChainPostVisitor;
25 namespace dom {
26 class TabContext;
27 namespace quota {
28 class OriginOrPatternString;
33 BEGIN_INDEXEDDB_NAMESPACE
35 class FileManager;
36 class FileManagerInfo;
38 class IndexedDatabaseManager MOZ_FINAL : public nsIObserver
40 typedef mozilla::dom::quota::OriginOrPatternString OriginOrPatternString;
41 typedef mozilla::dom::quota::PersistenceType PersistenceType;
43 public:
44 NS_DECL_ISUPPORTS
45 NS_DECL_NSIOBSERVER
47 // Returns a non-owning reference.
48 static IndexedDatabaseManager*
49 GetOrCreate();
51 // Returns a non-owning reference.
52 static IndexedDatabaseManager*
53 Get();
55 static bool
56 IsClosed();
58 static bool
59 IsMainProcess()
60 #ifdef DEBUG
62 #else
64 return sIsMainProcess;
66 #endif
68 static bool
69 InLowDiskSpaceMode()
70 #ifdef DEBUG
72 #else
74 return !!sLowDiskSpaceMode;
76 #endif
78 already_AddRefed<FileManager>
79 GetFileManager(PersistenceType aPersistenceType,
80 const nsACString& aOrigin,
81 const nsAString& aDatabaseName);
83 void
84 AddFileManager(FileManager* aFileManager);
86 void
87 InvalidateAllFileManagers();
89 void
90 InvalidateFileManagers(PersistenceType aPersistenceType,
91 const OriginOrPatternString& aOriginOrPattern);
93 void
94 InvalidateFileManager(PersistenceType aPersistenceType,
95 const nsACString& aOrigin,
96 const nsAString& aDatabaseName);
98 nsresult
99 AsyncDeleteFile(FileManager* aFileManager,
100 int64_t aFileId);
102 // Don't call this method in real code, it blocks the main thread!
103 // It is intended to be used by mochitests to test correctness of the special
104 // reference counting of stored blobs/files.
105 nsresult
106 BlockAndGetFileReferences(PersistenceType aPersistenceType,
107 const nsACString& aOrigin,
108 const nsAString& aDatabaseName,
109 int64_t aFileId,
110 int32_t* aRefCnt,
111 int32_t* aDBRefCnt,
112 int32_t* aSliceRefCnt,
113 bool* aResult);
115 static mozilla::Mutex&
116 FileMutex()
118 IndexedDatabaseManager* mgr = Get();
119 NS_ASSERTION(mgr, "Must have a manager here!");
121 return mgr->mFileMutex;
124 static nsresult
125 FireWindowOnError(nsPIDOMWindow* aOwner,
126 EventChainPostVisitor& aVisitor);
128 static bool
129 TabContextMayAccessOrigin(const mozilla::dom::TabContext& aContext,
130 const nsACString& aOrigin);
132 static bool
133 DefineIndexedDB(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
135 private:
136 IndexedDatabaseManager();
137 ~IndexedDatabaseManager();
139 nsresult
140 Init();
142 void
143 Destroy();
145 static PLDHashOperator
146 InvalidateAndRemoveFileManagers(const nsACString& aKey,
147 nsAutoPtr<FileManagerInfo>& aValue,
148 void* aUserArg);
150 // Maintains a list of all file managers per origin. This list isn't
151 // protected by any mutex but it is only ever touched on the IO thread.
152 nsClassHashtable<nsCStringHashKey, FileManagerInfo> mFileManagerInfos;
154 // Lock protecting FileManager.mFileInfos and nsDOMFileBase.mFileInfos
155 // It's s also used to atomically update FileInfo.mRefCnt, FileInfo.mDBRefCnt
156 // and FileInfo.mSliceRefCnt
157 mozilla::Mutex mFileMutex;
159 static bool sIsMainProcess;
160 static mozilla::Atomic<bool> sLowDiskSpaceMode;
163 END_INDEXEDDB_NAMESPACE
165 #endif /* mozilla_dom_indexeddb_indexeddatabasemanager_h__ */