- Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathdatabase_ios.h
203 lines (153 loc) · 6.67 KB
/
database_ios.h
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
// Copyright 2016 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef FIREBASE_DATABASE_SRC_IOS_DATABASE_IOS_H_
#defineFIREBASE_DATABASE_SRC_IOS_DATABASE_IOS_H_
#include<map>
#include<set>
#include<memory>
#include"app/src/cleanup_notifier.h"
#include"app/src/future_manager.h"
#include"app/src/include/firebase/app.h"
#include"app/src/include/firebase/internal/mutex.h"
#include"app/src/logger.h"
#include"app/src/util_ios.h"
#include"database/src/common/listener.h"
#include"database/src/include/firebase/database/database_reference.h"
#include"database/src/ios/query_ios.h"
#ifdef __OBJC__
#import"FIRDatabase.h"
#endif// __OBJC__
namespace firebase {
class ReferenceCountedFutureImpl;
namespace database {
namespace internal {
// For constructing, copying or moving DatabaseReferences atomically.
extern Mutex g_database_reference_constructor_mutex;
// This defines the class FIRDatabasePointer, which is a C++-compatible wrapper
// around the FIRDatabase Obj-C class.
OBJ_C_PTR_WRAPPER(FIRDatabase);
// This defines the class firebase::database::internal::NSRecursiveLockPointer,
// which is a C++-compatible wrapper around the NSRecursiveLock Obj-C class,
// used by observer callbacks of FIRDatabaseQuery.
OBJ_C_PTR_WRAPPER(NSRecursiveLock);
#pragma clang assume_nonnull begin
// This is the iOS implementation of Database.
class DatabaseInternal {
public:
explicit DatabaseInternal(App* app);
DatabaseInternal(App* app, constchar* url);
~DatabaseInternal();
// Get the firebase::App that this Database was created with.
App* GetApp();
// Get a DatabaseReference to the root of the database.
DatabaseReference GetReference() const;
// Get a DatabaseReference for the specified path.
DatabaseReference GetReference(constchar* path) const;
// Get a DatabaseReference for the provided URL.
DatabaseReference GetReferenceFromUrl(constchar* url) const;
// Shuts down the connection to the Firebase Realtime Database backend until
// GoOnline() is called.
voidGoOffline();
// Resumes the connection to the Firebase Realtime Database backend after a
// previous GoOffline() call.
voidGoOnline();
// Purge all pending writes to the Firebase Realtime Database server.
voidPurgeOutstandingWrites();
// Gets the SDK version for the running library.
constchar* GetSdkVersion();
// Sets whether pending write data will persist between application exits.
voidSetPersistenceEnabled(bool enabled);
// Set the logging verbosity.
// The iOS implementation only enables logging for kLogLevelVerbose &
// kLogLevelDebug, logging is disabled in for all other levels.
voidset_log_level(LogLevel log_level);
// Get the logging verbosity.
LogLevel log_level() const;
#ifdef __OBJC__
boolRegisterValueListener(const internal::QuerySpec& spec,
ValueListener* listener,
FIRCPPDatabaseQueryCallbackState* callback_state);
boolUnregisterValueListener(const internal::QuerySpec& spec,
ValueListener* listener,
FIRDatabaseQuery* query_impl);
voidUnregisterAllValueListeners(const internal::QuerySpec& spec,
FIRDatabaseQuery* query_impl);
boolRegisterChildListener(const internal::QuerySpec& spec,
ChildListener* listener,
FIRCPPDatabaseQueryCallbackState* callback_state);
boolUnregisterChildListener(const internal::QuerySpec& spec,
ChildListener* listener,
FIRDatabaseQuery* query_impl);
voidUnregisterAllChildListeners(const internal::QuerySpec& spec,
FIRDatabaseQuery* query_impl);
#endif// __OBJC__
// Track a transient listener.
voidAddSingleValueListener(ValueListener* listener) {
MutexLock lock(listener_mutex_);
single_value_listeners_.insert(listener);
}
// Finish tracking a transient listener.
voidRemoveSingleValueListener(ValueListener* listener) {
MutexLock lock(listener_mutex_);
auto it = single_value_listeners_.find(listener);
if (it == single_value_listeners_.end()) return;
single_value_listeners_.erase(it);
}
FutureManager& future_manager() { return future_manager_; }
// Whether this object was successfully initialized by the constructor.
boolinitialized() const;
// When this is deleted, it will clean up all DatabaseReferences,
// DataSnapshots, and other such objects.
CleanupNotifier& cleanup() { return cleanup_; }
// The url that was passed to the constructor.
const std::string& constructor_url() const { return constructor_url_; }
#ifdef __OBJC__
// Guard access to C++ objects referenced by
// FIRCPPDatabaseQueryCallbackStatePointer.
NSRecursiveLock* query_lock() const { return query_lock_->get(); }
#endif// __OBJC__
Logger* logger() { return &logger_; }
private:
#ifdef __OBJC__
FIRDatabase* impl() const { return impl_->get(); }
#endif// __OBJC__
// The firebase::App that this Database was created with.
App* app_;
// Object lifetime managed by Objective C ARC.
std::unique_ptr<FIRDatabasePointer> impl_;
// Lock used to guard access to C++ objects referenced by FIRDatabaseQuery
// callbacks.
std::unique_ptr<NSRecursiveLockPointer> query_lock_;
// For registering listeners.
Mutex listener_mutex_;
// Listeners indexed by QuerySpec.
ListenerCollection<ValueListener> value_listeners_by_query_;
ListenerCollection<ChildListener> child_listeners_by_query_;
std::map<ValueListener*, FIRCPPDatabaseQueryCallbackStatePointer>
cleanup_value_listener_lookup_;
std::map<ChildListener*, FIRCPPDatabaseQueryCallbackStatePointer>
cleanup_child_listener_lookup_;
std::set<ValueListener*> single_value_listeners_;
FutureManager future_manager_;
CleanupNotifier cleanup_;
// The url passed to the constructor (or "" if none was passed).
// We keep it so that we can find the database in our cache.
std::string constructor_url_;
Logger logger_;
};
#pragma clang assume_nonnull end
} // namespace internal
} // namespace database
} // namespace firebase
#endif// FIREBASE_DATABASE_SRC_IOS_DATABASE_IOS_H_