Source code

Revision control

Other Tools

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_indexeddatabasemanager_h__
8
#define mozilla_dom_indexeddatabasemanager_h__
9
10
#include "js/TypeDecls.h"
11
#include "mozilla/Atomics.h"
12
#include "mozilla/dom/quota/PersistenceType.h"
13
#include "mozilla/Mutex.h"
14
#include "nsClassHashtable.h"
15
#include "nsHashKeys.h"
16
17
namespace mozilla {
18
19
class EventChainPostVisitor;
20
21
namespace dom {
22
23
class IDBFactory;
24
25
namespace indexedDB {
26
27
class BackgroundUtilsChild;
28
class FileManager;
29
class FileManagerInfo;
30
31
} // namespace indexedDB
32
33
class IndexedDatabaseManager final {
34
typedef mozilla::dom::quota::PersistenceType PersistenceType;
35
typedef mozilla::dom::indexedDB::FileManager FileManager;
36
typedef mozilla::dom::indexedDB::FileManagerInfo FileManagerInfo;
37
38
public:
39
enum LoggingMode {
40
Logging_Disabled = 0,
41
Logging_Concise,
42
Logging_Detailed,
43
Logging_ConciseProfilerMarks,
44
Logging_DetailedProfilerMarks
45
};
46
47
NS_INLINE_DECL_REFCOUNTING_WITH_DESTROY(IndexedDatabaseManager, Destroy())
48
49
// Returns a non-owning reference.
50
static IndexedDatabaseManager* GetOrCreate();
51
52
// Returns a non-owning reference.
53
static IndexedDatabaseManager* Get();
54
55
static bool IsClosed();
56
57
static bool IsMainProcess()
58
#ifdef DEBUG
59
;
60
#else
61
{
62
return sIsMainProcess;
63
}
64
#endif
65
66
static bool InTestingMode();
67
68
static bool FullSynchronous();
69
70
static LoggingMode GetLoggingMode()
71
#ifdef DEBUG
72
;
73
#else
74
{
75
return sLoggingMode;
76
}
77
#endif
78
79
static mozilla::LogModule* GetLoggingModule()
80
#ifdef DEBUG
81
;
82
#else
83
{
84
return sLoggingModule;
85
}
86
#endif
87
88
static bool ExperimentalFeaturesEnabled();
89
90
static bool ExperimentalFeaturesEnabled(JSContext* aCx, JSObject* aGlobal);
91
92
static bool IsFileHandleEnabled();
93
94
static uint32_t DataThreshold();
95
96
static uint32_t MaxSerializedMsgSize();
97
98
static bool PreprocessingEnabled();
99
100
// The maximum number of extra entries to preload in an Cursor::OpenOp or
101
// Cursor::ContinueOp.
102
static int32_t MaxPreloadExtraRecords();
103
104
void ClearBackgroundActor();
105
106
already_AddRefed<FileManager> GetFileManager(PersistenceType aPersistenceType,
107
const nsACString& aOrigin,
108
const nsAString& aDatabaseName);
109
110
void AddFileManager(FileManager* aFileManager);
111
112
void InvalidateAllFileManagers();
113
114
void InvalidateFileManagers(PersistenceType aPersistenceType,
115
const nsACString& aOrigin);
116
117
void InvalidateFileManager(PersistenceType aPersistenceType,
118
const nsACString& aOrigin,
119
const nsAString& aDatabaseName);
120
121
// Don't call this method in real code, it blocks the main thread!
122
// It is intended to be used by mochitests to test correctness of the special
123
// reference counting of stored blobs/files.
124
nsresult BlockAndGetFileReferences(PersistenceType aPersistenceType,
125
const nsACString& aOrigin,
126
const nsAString& aDatabaseName,
127
int64_t aFileId, int32_t* aRefCnt,
128
int32_t* aDBRefCnt, int32_t* aSliceRefCnt,
129
bool* aResult);
130
131
nsresult FlushPendingFileDeletions();
132
133
static const nsCString& GetLocale();
134
135
static mozilla::Mutex& FileMutex() {
136
IndexedDatabaseManager* mgr = Get();
137
NS_ASSERTION(mgr, "Must have a manager here!");
138
139
return mgr->mFileMutex;
140
}
141
142
static nsresult CommonPostHandleEvent(EventChainPostVisitor& aVisitor,
143
IDBFactory* aFactory);
144
145
static bool ResolveSandboxBinding(JSContext* aCx);
146
147
static bool DefineIndexedDB(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
148
149
private:
150
IndexedDatabaseManager();
151
~IndexedDatabaseManager();
152
153
nsresult Init();
154
155
void Destroy();
156
157
static void LoggingModePrefChangedCallback(const char* aPrefName,
158
void* aClosure);
159
160
// Maintains a list of all file managers per origin. This list isn't
161
// protected by any mutex but it is only ever touched on the IO thread.
162
nsClassHashtable<nsCStringHashKey, FileManagerInfo> mFileManagerInfos;
163
164
nsClassHashtable<nsRefPtrHashKey<FileManager>, nsTArray<int64_t>>
165
mPendingDeleteInfos;
166
167
// Lock protecting FileManager.mFileInfos.
168
// It's s also used to atomically update FileInfo.mRefCnt, FileInfo.mDBRefCnt
169
// and FileInfo.mSliceRefCnt
170
mozilla::Mutex mFileMutex;
171
172
nsCString mLocale;
173
174
indexedDB::BackgroundUtilsChild* mBackgroundActor;
175
176
static bool sIsMainProcess;
177
static bool sFullSynchronousMode;
178
static LazyLogModule sLoggingModule;
179
static Atomic<LoggingMode> sLoggingMode;
180
};
181
182
} // namespace dom
183
} // namespace mozilla
184
185
#endif // mozilla_dom_indexeddatabasemanager_h__