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 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Indexed Database.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * The Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2011
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef mozilla_dom_indexeddb_fileinfo_h__
40 : #define mozilla_dom_indexeddb_fileinfo_h__
41 :
42 : #include "IndexedDatabase.h"
43 : #include "nsAtomicRefcnt.h"
44 : #include "nsThreadUtils.h"
45 : #include "FileManager.h"
46 : #include "IndexedDatabaseManager.h"
47 :
48 : BEGIN_INDEXEDDB_NAMESPACE
49 :
50 : class FileInfo
51 : {
52 : friend class FileManager;
53 :
54 : public:
55 0 : FileInfo(FileManager* aFileManager)
56 0 : : mFileManager(aFileManager)
57 0 : { }
58 :
59 0 : virtual ~FileInfo()
60 0 : {
61 : #ifdef DEBUG
62 0 : NS_ASSERTION(NS_IsMainThread(), "File info destroyed on wrong thread!");
63 : #endif
64 0 : }
65 :
66 : static
67 : FileInfo* Create(FileManager* aFileManager, PRInt64 aId);
68 :
69 0 : void AddRef()
70 : {
71 0 : if (IndexedDatabaseManager::IsClosed()) {
72 0 : NS_AtomicIncrementRefcnt(mRefCnt);
73 : }
74 : else {
75 0 : UpdateReferences(mRefCnt, 1);
76 : }
77 0 : }
78 :
79 0 : void Release()
80 : {
81 0 : if (IndexedDatabaseManager::IsClosed()) {
82 0 : nsrefcnt count = NS_AtomicDecrementRefcnt(mRefCnt);
83 0 : if (count == 0) {
84 0 : mRefCnt = 1;
85 0 : delete this;
86 : }
87 : }
88 : else {
89 0 : UpdateReferences(mRefCnt, -1);
90 : }
91 0 : }
92 :
93 : void UpdateDBRefs(PRInt32 aDelta)
94 : {
95 : UpdateReferences(mDBRefCnt, aDelta);
96 : }
97 :
98 0 : void ClearDBRefs()
99 : {
100 0 : UpdateReferences(mDBRefCnt, 0, true);
101 0 : }
102 :
103 : void UpdateSliceRefs(PRInt32 aDelta)
104 : {
105 : UpdateReferences(mSliceRefCnt, aDelta);
106 : }
107 :
108 : void GetReferences(PRInt32* aRefCnt, PRInt32* aDBRefCnt,
109 : PRInt32* aSliceRefCnt);
110 :
111 : FileManager* Manager() const
112 : {
113 : return mFileManager;
114 : }
115 :
116 : virtual PRInt64 Id() const = 0;
117 :
118 : private:
119 : void UpdateReferences(nsAutoRefCnt& aRefCount, PRInt32 aDelta,
120 : bool aClear = false);
121 : void Cleanup();
122 :
123 : nsAutoRefCnt mRefCnt;
124 : nsAutoRefCnt mDBRefCnt;
125 : nsAutoRefCnt mSliceRefCnt;
126 :
127 : nsRefPtr<FileManager> mFileManager;
128 : };
129 :
130 : #define FILEINFO_SUBCLASS(_bits) \
131 : class FileInfo##_bits : public FileInfo \
132 : { \
133 : public: \
134 : FileInfo##_bits(FileManager* aFileManager, PRInt##_bits aId) \
135 : : FileInfo(aFileManager), mId(aId) \
136 : { } \
137 : \
138 : virtual PRInt64 Id() const \
139 : { \
140 : return mId; \
141 : } \
142 : \
143 : private: \
144 : PRInt##_bits mId; \
145 : };
146 :
147 0 : FILEINFO_SUBCLASS(16)
148 0 : FILEINFO_SUBCLASS(32)
149 0 : FILEINFO_SUBCLASS(64)
150 :
151 : #undef FILEINFO_SUBCLASS
152 :
153 : END_INDEXEDDB_NAMESPACE
154 :
155 : #endif // mozilla_dom_indexeddb_fileinfo_h__
|