diff --git a/build_scripts/other/package.sh b/build_scripts/other/package.sh index fca579d4b3..9f64183996 100755 --- a/build_scripts/other/package.sh +++ b/build_scripts/other/package.sh @@ -32,9 +32,6 @@ for incdir in */src/include; do done cd "${origpath}" -# Remove unneeded Unity include files. -rm -rf "${destpath}/include/firebase/csharp/" - # Copy release files into packaged SDK. cp -af "${sourcepath}"/release_build_files/* "${destpath}" diff --git a/firestore/src/include/firebase/csharp/api_headers.h b/firestore/src/include/firebase/csharp/api_headers.h deleted file mode 100644 index 5ede195c7b..0000000000 --- a/firestore/src/include/firebase/csharp/api_headers.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_API_HEADERS_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_API_HEADERS_H_ - -#include "firebase/firestore.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -// This class allows limited access to private functions of `Firestore` related -// to sending Cloud headers. -class ApiHeaders { - public: - static void SetClientLanguage(const std::string& language_token) { - Firestore::SetClientLanguage(language_token); - } -}; - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_API_HEADERS_H_ diff --git a/firestore/src/include/firebase/csharp/document_event_listener.cc b/firestore/src/include/firebase/csharp/document_event_listener.cc deleted file mode 100644 index 28fb75407c..0000000000 --- a/firestore/src/include/firebase/csharp/document_event_listener.cc +++ /dev/null @@ -1,69 +0,0 @@ -#include "firebase/csharp/document_event_listener.h" - -#include -#include -#include - -#include "app/src/callback.h" -#include "firebase/firestore/document_reference.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -namespace { - -class ListenerCallback { - public: - ListenerCallback(DocumentEventListenerCallback callback, int32_t callback_id, - std::unique_ptr snapshot, Error error_code, - std::string error_message) - : callback_(callback), - callback_id_(callback_id), - snapshot_(std::move(snapshot)), - error_code_(error_code), - error_message_(std::move(error_message)) {} - - static void Run(ListenerCallback* listener_callback) { - listener_callback->Run(); - } - - private: - void Run() { - // Ownership of the DocumentSnapshot pointer is passed to C#. - callback_(callback_id_, snapshot_.release(), error_code_, - error_message_.c_str()); - } - - DocumentEventListenerCallback callback_ = nullptr; - int32_t callback_id_ = -1; - std::unique_ptr snapshot_; - Error error_code_ = Error::kErrorUnknown; - std::string error_message_; -}; - -} // namespace - -ListenerRegistration AddDocumentSnapshotListener( - DocumentReference* reference, MetadataChanges metadata_changes, - int32_t callback_id, DocumentEventListenerCallback callback) { - auto snapshot_listener = [callback, callback_id]( - const DocumentSnapshot& snapshot, - Error error_code, - const std::string& error_message) { - // NOLINTNEXTLINE(modernize-make-unique) - std::unique_ptr snapshot_ptr( - new DocumentSnapshot(snapshot)); - ListenerCallback listener_callback(callback, callback_id, - std::move(snapshot_ptr), error_code, - error_message); - auto* callback = new callback::CallbackMoveValue1( - std::move(listener_callback), ListenerCallback::Run); - callback::AddCallback(callback); - }; - return reference->AddSnapshotListener(metadata_changes, snapshot_listener); -} - -} // namespace csharp -} // namespace firestore -} // namespace firebase diff --git a/firestore/src/include/firebase/csharp/document_event_listener.h b/firestore/src/include/firebase/csharp/document_event_listener.h deleted file mode 100644 index 301485b479..0000000000 --- a/firestore/src/include/firebase/csharp/document_event_listener.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_DOCUMENT_EVENT_LISTENER_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_DOCUMENT_EVENT_LISTENER_H_ - -#include "firebase/firestore/document_snapshot.h" -#include "firebase/firestore/listener_registration.h" -#include "firebase/firestore/metadata_changes.h" -#include "firebase/firestore/firestore_errors.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -// Add this to make this header compile when SWIG is not involved. -#ifndef SWIGSTDCALL -#if !defined(SWIG) && \ - (defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)) -#define SWIGSTDCALL __stdcall -#else -#define SWIGSTDCALL -#endif -#endif - -// The callbacks that are used by the listener, that need to reach back to C# -// callbacks. The error_message pointer is only valid for the duration of the -// callback. -typedef void(SWIGSTDCALL* DocumentEventListenerCallback)( - int callback_id, DocumentSnapshot* snapshot, Error error_code, - const char* error_message); - -// This method is a proxy to DocumentReference::AddSnapshotListener() -// that can be easily called from C#. It allows our C# wrapper to -// track user callbacks in a dictionary keyed off of a unique int -// for each user callback and then raise the correct one later. -ListenerRegistration AddDocumentSnapshotListener( - DocumentReference* reference, MetadataChanges metadata_changes, - int32_t callback_id, DocumentEventListenerCallback callback); - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_DOCUMENT_EVENT_LISTENER_H_ diff --git a/firestore/src/include/firebase/csharp/firestore_instance_management.cc b/firestore/src/include/firebase/csharp/firestore_instance_management.cc deleted file mode 100644 index 8031b8599f..0000000000 --- a/firestore/src/include/firebase/csharp/firestore_instance_management.cc +++ /dev/null @@ -1,39 +0,0 @@ -#include "firebase/csharp/firestore_instance_management.h" - -#include "firebase/app/client/unity/src/cpp_instance_manager.h" -#include "firebase/firestore.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -namespace { - -CppInstanceManager& GetFirestoreInstanceManager() { - // Allocate the CppInstanceManager on the heap to prevent its destructor - // from executing (go/totw/110#the-fix-safe-initialization-no-destruction). - static CppInstanceManager* firestore_instance_manager = - new CppInstanceManager(); - return *firestore_instance_manager; -} - -} // namespace - -Firestore* GetFirestoreInstance(App* app) { - auto& firestore_instance_manager = GetFirestoreInstanceManager(); - // Acquire the lock used internally by CppInstanceManager::ReleaseReference() - // to avoid racing with deletion of Firestore instances. - MutexLock lock(firestore_instance_manager.mutex()); - Firestore* instance = - Firestore::GetInstance(app, /*init_result_out=*/nullptr); - firestore_instance_manager.AddReference(instance); - return instance; -} - -void ReleaseFirestoreInstance(Firestore* firestore) { - GetFirestoreInstanceManager().ReleaseReference(firestore); -} - -} // namespace csharp -} // namespace firestore -} // namespace firebase diff --git a/firestore/src/include/firebase/csharp/firestore_instance_management.h b/firestore/src/include/firebase/csharp/firestore_instance_management.h deleted file mode 100644 index 61bc89930e..0000000000 --- a/firestore/src/include/firebase/csharp/firestore_instance_management.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_FIRESTORE_INSTANCE_MANAGEMENT_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_FIRESTORE_INSTANCE_MANAGEMENT_H_ - -namespace firebase { - -class App; - -namespace firestore { - -class Firestore; - -namespace csharp { - -/** - * Returns the Firestore instance for the given App, creating it if necessary. - * This method is merely a wrapper around Firestore::GetInstance() that - * increments a reference count each time a given Firestore pointer is returned. - * The caller must call ReleaseFirestoreInstance() with the returned pointer - * once it is no longer referenced to ensure proper garbage collection. - */ -Firestore* GetFirestoreInstance(App* app); - -/** - * Decrements the reference count of the given Firestore, deleting it if the - * reference count becomes zero. The given Firestore pointer must have been - * returned by a previous invocation of GetFirestoreInstance(). - */ -void ReleaseFirestoreInstance(Firestore* firestore); - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_FIRESTORE_INSTANCE_MANAGEMENT_H_ diff --git a/firestore/src/include/firebase/csharp/map.h b/firestore/src/include/firebase/csharp/map.h deleted file mode 100644 index d3c490bbe4..0000000000 --- a/firestore/src/include/firebase/csharp/map.h +++ /dev/null @@ -1,224 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_MAP_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_MAP_H_ - -#include -#include - -#include "app/meta/move.h" -#include "firestore/src/common/futures.h" -#include "firebase/firestore/document_reference.h" -#include "firebase/firestore/document_snapshot.h" -#include "firebase/firestore/field_path.h" -#include "firebase/firestore/field_value.h" -#include "firebase/firestore/query.h" -#include "firebase/firestore/set_options.h" -#include "firebase/firestore/write_batch.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -// Simple wrappers to avoid exposing C++ standard library containers to SWIG. -// -// While it's normally possible to work with standard library containers in SWIG -// (by instantiating them for each template type used via the `%template` -// directive), issues in the build environment make that approach too -// complicated to be worth it. Instead, use simple wrappers and make sure the -// underlying containers are never exposed to C#. -// -// Note: most of the time, these classes should be declared with a `using` -// statement to ensure predictable lifetime of the object when dealing with -// iterators or unsafe views. - -// Wraps `std::unordered_map` for use in C# code. -// Note: `V` has to be default-constructible with a sensible default value. -template -class Map { -#ifdef STLPORT -#define FIRESTORE_STD_NS std::tr1 -#else -#define FIRESTORE_STD_NS std -#endif - using ContainerT = FIRESTORE_STD_NS::unordered_map; - using IterT = typename ContainerT::const_iterator; -#undef FIRESTORE_STD_NS - - public: - Map() = default; - - std::size_t Size() const { return container_.size(); } - - // The returned reference is only valid as long as this `Map` is - // valid. In C#, declare the map with a `using` statement to ensure its - // lifetime exceeds the lifetime of the reference. - const V& GetUnsafeView(const K& key) const { - auto found = container_.find(key); - if (found != container_.end()) { - return found->second; - } - - return InvalidValue(); - } - - V GetCopy(const K& key) const { - // Note: this is safe because the reference returned by `GetUnsafeView` is - // copied into the return value. - return GetUnsafeView(key); - } - - void Insert(const K& key, const V& value) { container_[key] = value; } - - class MapIterator { - public: - bool HasMore() const { return cur_ != end_; } - - void Advance() { ++cur_; } - - const K& UnsafeKeyView() const { return cur_->first; } - const V& UnsafeValueView() const { return cur_->second; } - - K KeyCopy() const { return cur_->first; } - V ValueCopy() const { return cur_->second; } - - private: - friend class Map; - explicit MapIterator(const Map& wrapper) - : cur_{wrapper.Unwrap().begin()}, end_{wrapper.Unwrap().end()} {} - - Map::IterT cur_, end_; - }; - - MapIterator Iterator() const { return MapIterator(*this); } - - // Note: this is a named function and not a constructor to make it easier to - // ignore in SWIG. - static Map Wrap(const std::unordered_map& container) { - return Map(container); - } - - const std::unordered_map& Unwrap() const { return container_; } - - private: - explicit Map(const std::unordered_map& container) - : container_(container) {} - - static const V& InvalidValue() { - static V value; - return value; - } - - ContainerT container_; -}; - -inline Map ConvertFieldValueToMap( - const FieldValue& field_value) { - return Map::Wrap(field_value.map_value()); -} - -inline FieldValue ConvertMapToFieldValue( - const Map& wrapper) { - return FieldValue::Map(wrapper.Unwrap()); -} - -inline FieldValue ConvertSnapshotToFieldValue( - const DocumentSnapshot& snapshot, - DocumentSnapshot::ServerTimestampBehavior stb) { - return FieldValue::Map(snapshot.GetData(stb)); -} - -inline void WriteBatchUpdate(WriteBatch* batch, - const DocumentReference& doc, - const FieldValue& field_value) { - batch->Update(doc, field_value.map_value()); -} - -inline void WriteBatchUpdate( - WriteBatch* batch, const DocumentReference& doc, - const Map& wrapper) { - batch->Update(doc, wrapper.Unwrap()); -} - -inline void WriteBatchUpdate( - WriteBatch* batch, const DocumentReference& doc, - const Map& wrapper) { - batch->Update(doc, wrapper.Unwrap()); -} - -inline Future DocumentReferenceSet(DocumentReference& doc, - const FieldValue& field_value, - const SetOptions& options) { - return doc.Set(field_value.map_value(), options); -} - -inline Future DocumentReferenceUpdate( - DocumentReference& doc, const FieldValue& field_value) { - return doc.Update(field_value.map_value()); -} - -inline Future DocumentReferenceUpdate( - DocumentReference& doc, const Map& wrapper) { - return doc.Update(wrapper.Unwrap()); -} - -inline Query QueryWhereArrayContainsAny(Query& query, - const std::string& field, - const FieldValue& values) { - // TODO(b/158342776): prevent an avoidable copy. SWIG would allocate a new - // `Query` on the heap and initialize it with a copy of this return value. - return query.WhereArrayContainsAny(field, values.array_value()); -} - -inline Query QueryWhereArrayContainsAny(Query& query, - const FieldPath& field, - const FieldValue& values) { - return query.WhereArrayContainsAny(field, values.array_value()); -} - -inline Query QueryWhereIn(Query& query, const std::string& field, - const FieldValue& values) { - return query.WhereIn(field, values.array_value()); -} - -inline Query QueryWhereIn(Query& query, const FieldPath& field, - const FieldValue& values) { - return query.WhereIn(field, values.array_value()); -} - -inline Query QueryWhereNotIn(Query& query, const std::string& field, - const FieldValue& values) { - return query.WhereNotIn(field, values.array_value()); -} - -inline Query QueryWhereNotIn(Query& query, const FieldPath& field, - const FieldValue& values) { - return query.WhereNotIn(field, values.array_value()); -} - -inline Query QueryStartAt(Query& query, const FieldValue& values) { - return query.StartAt(values.array_value()); -} - -inline Query QueryStartAfter(Query& query, const FieldValue& values) { - return query.StartAfter(values.array_value()); -} - -inline Query QueryEndBefore(Query& query, const FieldValue& values) { - return query.EndBefore(values.array_value()); -} - -inline Query QueryEndAt(Query& query, const FieldValue& values) { - return query.EndAt(values.array_value()); -} - -inline void WriteBatchSet(WriteBatch& write_batch, - const DocumentReference& document, - const FieldValue& data, - const SetOptions& options) { - write_batch.Set(document, data.map_value(), options); -} - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_MAP_H_ diff --git a/firestore/src/include/firebase/csharp/query_event_listener.cc b/firestore/src/include/firebase/csharp/query_event_listener.cc deleted file mode 100644 index c43ac8d1b6..0000000000 --- a/firestore/src/include/firebase/csharp/query_event_listener.cc +++ /dev/null @@ -1,67 +0,0 @@ -#include "firebase/csharp/query_event_listener.h" - -#include -#include -#include - -#include "app/src/callback.h" -#include "firebase/firestore/query.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -namespace { - -class ListenerCallback { - public: - ListenerCallback(QueryEventListenerCallback callback, int32_t callback_id, - std::unique_ptr snapshot, Error error_code, - std::string error_message) - : callback_(callback), - callback_id_(callback_id), - snapshot_(std::move(snapshot)), - error_code_(error_code), - error_message_(std::move(error_message)) {} - - static void Run(ListenerCallback* listener_callback) { - listener_callback->Run(); - } - - private: - void Run() { - // Ownership of the QuerySnapshot pointer is passed to C#. - callback_(callback_id_, snapshot_.release(), error_code_, - error_message_.c_str()); - } - - QueryEventListenerCallback callback_ = nullptr; - int32_t callback_id_ = -1; - std::unique_ptr snapshot_; - Error error_code_ = Error::kErrorUnknown; - std::string error_message_; -}; - -} // namespace - -ListenerRegistration AddQuerySnapshotListener( - Query* query, MetadataChanges metadata_changes, int32_t callback_id, - QueryEventListenerCallback callback) { - auto snapshot_listener = [callback, callback_id]( - const QuerySnapshot& snapshot, Error error_code, - const std::string& error_message) { - // NOLINTNEXTLINE(modernize-make-unique) - std::unique_ptr snapshot_ptr(new QuerySnapshot(snapshot)); - ListenerCallback listener_callback(callback, callback_id, - std::move(snapshot_ptr), error_code, - error_message); - auto* callback = new callback::CallbackMoveValue1( - std::move(listener_callback), ListenerCallback::Run); - callback::AddCallback(callback); - }; - return query->AddSnapshotListener(metadata_changes, snapshot_listener); -} - -} // namespace csharp -} // namespace firestore -} // namespace firebase diff --git a/firestore/src/include/firebase/csharp/query_event_listener.h b/firestore/src/include/firebase/csharp/query_event_listener.h deleted file mode 100644 index 0b2f1c0322..0000000000 --- a/firestore/src/include/firebase/csharp/query_event_listener.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_QUERY_EVENT_LISTENER_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_QUERY_EVENT_LISTENER_H_ - -#include "firebase/firestore/listener_registration.h" -#include "firebase/firestore/query_snapshot.h" -#include "firebase/firestore/firestore_errors.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -// Add this to make this header compile when SWIG is not involved. -#ifndef SWIGSTDCALL -#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) -#define SWIGSTDCALL __stdcall -#else -#define SWIGSTDCALL -#endif -#endif - -// The callbacks that are used by the listener, that need to reach back to C# -// callbacks. The error_message pointer is only valid for the duration of the -// callback. -typedef void(SWIGSTDCALL* QueryEventListenerCallback)( - int32_t callback_id, QuerySnapshot* snapshot, Error error_code, - const char* error_message); - -// This method is a proxy to Query::AddSnapshotsListener() -// that can be easily called from C#. It allows our C# wrapper to -// track user callbacks in a dictionary keyed off of a unique int -// for each user callback and then raise the correct one later. -ListenerRegistration AddQuerySnapshotListener( - Query* query, MetadataChanges metadata_changes, int32_t callback_id, - QueryEventListenerCallback callback); - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_QUERY_EVENT_LISTENER_H_ diff --git a/firestore/src/include/firebase/csharp/snapshots_in_sync_listener.cc b/firestore/src/include/firebase/csharp/snapshots_in_sync_listener.cc deleted file mode 100644 index b494212425..0000000000 --- a/firestore/src/include/firebase/csharp/snapshots_in_sync_listener.cc +++ /dev/null @@ -1,43 +0,0 @@ -#include "firebase/csharp/snapshots_in_sync_listener.h" - -#include "app/src/callback.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -namespace { - -class ListenerCallback { - public: - ListenerCallback(SnapshotsInSyncCallback callback, int32_t callback_id) - : callback_(callback), callback_id_(callback_id) {} - - static void Run(ListenerCallback* listener_callback) { - listener_callback->Run(); - } - - private: - void Run() { callback_(callback_id_); } - - SnapshotsInSyncCallback callback_ = nullptr; - int32_t callback_id_ = -1; -}; - -} // namespace - -ListenerRegistration AddSnapshotsInSyncListener( - Firestore* firestore, int32_t callback_id, - SnapshotsInSyncCallback callback) { - auto snapshots_in_sync_listener = [callback, callback_id] { - ListenerCallback listener_callback(callback, callback_id); - auto* callback = new callback::CallbackMoveValue1( - std::move(listener_callback), ListenerCallback::Run); - callback::AddCallback(callback); - }; - return firestore->AddSnapshotsInSyncListener(snapshots_in_sync_listener); -} - -} // namespace csharp -} // namespace firestore -} // namespace firebase diff --git a/firestore/src/include/firebase/csharp/snapshots_in_sync_listener.h b/firestore/src/include/firebase/csharp/snapshots_in_sync_listener.h deleted file mode 100644 index 374031abd1..0000000000 --- a/firestore/src/include/firebase/csharp/snapshots_in_sync_listener.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_ - -#include "firebase/firestore.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -// Add this to make this header compile when SWIG is not involved. -#ifndef SWIGSTDCALL -#if !defined(SWIG) && \ - (defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)) -#define SWIGSTDCALL __stdcall -#else -#define SWIGSTDCALL -#endif -#endif - -// The callbacks that are used by the listener, that need to reach back to C# -// callbacks. -typedef void(SWIGSTDCALL* SnapshotsInSyncCallback)(int callback_id); - -// This method is a proxy to Firestore::AddSnapshotsInSyncListener() -// that can be easily called from C#. It allows our C# wrapper to -// track user callbacks in a dictionary keyed off of a unique int -// for each user callback and then raise the correct one later. -ListenerRegistration AddSnapshotsInSyncListener( - Firestore* firestore, int32_t callback_id, - SnapshotsInSyncCallback callback); - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_ diff --git a/firestore/src/include/firebase/csharp/transaction_manager.cc b/firestore/src/include/firebase/csharp/transaction_manager.cc deleted file mode 100644 index 26a641aa2f..0000000000 --- a/firestore/src/include/firebase/csharp/transaction_manager.cc +++ /dev/null @@ -1,303 +0,0 @@ -#include "firebase/csharp/transaction_manager.h" - -#include // NOLINT(build/c++11) -#include - -#include "app/src/assert.h" -#include "app/src/callback.h" -#include "firebase/firestore/document_reference.h" -#include "firebase/firestore/field_path.h" -#include "firebase/firestore/field_value.h" -#include "firebase/firestore/set_options.h" -#include "firebase/firestore/transaction.h" - -#if defined(__ANDROID__) -#include "firestore/src/android/firestore_android.h" -#elif defined(FIRESTORE_STUB_BUILD) -#include "firestore/src/stub/firestore_stub.h" -#else -#include "firestore/src/ios/firestore_ios.h" -#endif // defined(__ANDROID__) - -namespace firebase { -namespace firestore { -namespace csharp { - -class TransactionCallbackInternal { - public: - explicit TransactionCallbackInternal(Transaction& transaction) - : transaction_(transaction) {} - - void OnCompletion(bool callback_successful) { - std::lock_guard lock(completion_mutex_); - if (is_completed_) { - return; - } - is_completed_ = true; - result_ = callback_successful; - completion_condition_.notify_all(); - } - - // Waits for `OnCompletion()` to be invoked. - // - // If `OnCompletion()` has already been invoked then this method returns - // immediately; otherwise, it blocks waiting for it to be invoked. - // - // Returns the value that was specified to `OnCompletion()` for its argument. - bool AwaitCompletion() { - std::unique_lock lock(completion_mutex_); - completion_condition_.wait(lock, [&] { return is_completed_; }); - return result_; - } - - // Marks the `Transaction` reference that was given to the constructor as - // "invalid". - // - // After this method is invoked, all transaction-related methods in this class - // (i.e. `Get()`, `Set()`, `Update()`, and `Delete()`) will simply return a - // "failed" value and will not actually invoke methods on the encapsulated - // `Transaction` reference. - // - // This method will block until all in-flight transaction-related methods - // at the time of invocation have completed. - void InvalidateTransaction() { - std::lock_guard transaction_lock(transaction_mutex_); - is_transaction_valid_ = false; - } - - TransactionGetResult Get(const DocumentReference& doc) { - std::lock_guard transaction_lock(transaction_mutex_); - if (!is_transaction_valid_) { - return {}; - } - Error error_code = Error::kErrorUnknown; - std::string error_message; - DocumentSnapshot snapshot = - transaction_.Get(doc, &error_code, &error_message); - return {std::move(snapshot), error_code, std::move(error_message)}; - } - - bool Update(const DocumentReference& doc, const FieldValue& field_value) { - std::lock_guard transaction_lock(transaction_mutex_); - if (!is_transaction_valid_) { - return false; - } - transaction_.Update(doc, field_value.map_value()); - return true; - } - - bool Update(const DocumentReference& doc, - const Map& wrapper) { - std::lock_guard transaction_lock(transaction_mutex_); - if (!is_transaction_valid_) { - return false; - } - transaction_.Update(doc, wrapper.Unwrap()); - return true; - } - - bool Update(const DocumentReference& doc, - const Map& wrapper) { - std::lock_guard transaction_lock(transaction_mutex_); - if (!is_transaction_valid_) { - return false; - } - transaction_.Update(doc, wrapper.Unwrap()); - return true; - } - - bool Set(const DocumentReference& doc, const FieldValue& data, - const SetOptions& options) { - std::lock_guard transaction_lock(transaction_mutex_); - if (!is_transaction_valid_) { - return false; - } - transaction_.Set(doc, data.map_value(), options); - return true; - } - - bool Delete(const DocumentReference& doc) { - std::lock_guard transaction_lock(transaction_mutex_); - if (!is_transaction_valid_) { - return false; - } - transaction_.Delete(doc); - return true; - } - - private: - // TODO(b/172565676) Change `transaction_mutex_` from an exclusive mutex to a - // shared mutex to enable concurrent method invocations on `transaction_`. - std::mutex transaction_mutex_; - Transaction& transaction_; - bool is_transaction_valid_ = true; - - std::mutex completion_mutex_; - std::condition_variable completion_condition_; - bool is_completed_ = false; - bool result_ = false; -}; - -class TransactionManagerInternal - : public std::enable_shared_from_this { - public: - explicit TransactionManagerInternal(Firestore* firestore) - : firestore_(firestore) { - FIREBASE_ASSERT(firestore); - } - - ~TransactionManagerInternal() { - std::lock_guard lock(mutex_); - FIREBASE_ASSERT(is_disposed_); - FIREBASE_ASSERT(running_callbacks_.empty()); - } - - void Dispose() { - std::lock_guard lock(mutex_); - is_disposed_ = true; - for (TransactionCallbackInternal* running_callback : running_callbacks_) { - running_callback->OnCompletion(false); - } - } - - Future RunTransaction(int32_t callback_id, - TransactionCallbackFn callback_fn) { - std::lock_guard lock(mutex_); - if (is_disposed_) { - return {}; - } - - auto shared_this = shared_from_this(); - return firestore_->RunTransaction([shared_this, callback_id, callback_fn]( - Transaction& transaction, - std::string& error_message) { - if (shared_this->ExecuteCallback(callback_id, callback_fn, transaction)) { - return Error::kErrorOk; - } else { - // Return a non-retryable error code. - return Error::kErrorInvalidArgument; - } - }); - } - - private: - bool ExecuteCallback(int32_t callback_id, TransactionCallbackFn callback_fn, - Transaction& transaction) { - auto transaction_callback_internal = - std::make_shared(transaction); - - { - std::lock_guard lock(mutex_); - if (is_disposed_) { - return false; - } - running_callbacks_.insert(transaction_callback_internal.get()); - } - - auto transaction_callback = - // NOLINTNEXTLINE(modernize-make-unique) - std::unique_ptr(new TransactionCallback( - transaction_callback_internal, callback_id, callback_fn)); - callback::AddCallback( - new callback::CallbackMoveValue1>( - std::move(transaction_callback), ExecuteCallbackFromMainThread)); - bool result = transaction_callback_internal->AwaitCompletion(); - transaction_callback_internal->InvalidateTransaction(); - - { - std::lock_guard lock(mutex_); - running_callbacks_.erase(transaction_callback_internal.get()); - } - - return result; - } - - static void ExecuteCallbackFromMainThread( - std::unique_ptr* transaction_callback) { - TransactionCallbackFn callback_fn = (*transaction_callback)->callback(); - std::shared_ptr transaction_callback_internal = - (*transaction_callback)->internal(); - bool successful = callback_fn(transaction_callback->release()); - if (!successful) { - transaction_callback_internal->OnCompletion(false); - } - } - - std::mutex mutex_; - Firestore* firestore_ = nullptr; - bool is_disposed_ = false; - // NOLINTNEXTLINE(google3-runtime-unneeded-pointer-stability-check) - std::unordered_set running_callbacks_; -}; - -TransactionManager::TransactionManager(Firestore* firestore) - : internal_(std::make_shared(firestore)), - cleanup_notifier_(firestore->internal_->cleanup()) { - cleanup_notifier_.RegisterObject(this, CleanUp); -} - -TransactionManager::~TransactionManager() { Dispose(); } - -void TransactionManager::CleanUp(void* object) { - static_cast(object)->Dispose(); -} - -void TransactionManager::Dispose() { - std::lock_guard lock(dispose_mutex_); - if (!internal_) { - return; - } - - internal_->Dispose(); - internal_.reset(); - - cleanup_notifier_.UnregisterObject(this); -} - -Future TransactionManager::RunTransaction( - int32_t callback_id, TransactionCallbackFn callback_fn) { - // Make a local copy of `internal_` since it could be reset asynchronously - // by a call to `Dispose()`. - std::shared_ptr internal_local = internal_; - if (!internal_local) { - return {}; - } - return internal_local->RunTransaction(callback_id, callback_fn); -} - -void TransactionCallback::OnCompletion(bool callback_successful) { - internal_->OnCompletion(callback_successful); -} - -bool TransactionCallback::Update(const DocumentReference& doc, - const FieldValue& field_value) { - return internal_->Update(doc, field_value); -} - -bool TransactionCallback::Update(const DocumentReference& doc, - const Map& wrapper) { - return internal_->Update(doc, wrapper); -} - -bool TransactionCallback::Update(const DocumentReference& doc, - const Map& wrapper) { - return internal_->Update(doc, wrapper); -} - -bool TransactionCallback::Set(const DocumentReference& doc, - const FieldValue& data, - const SetOptions& options) { - return internal_->Set(doc, data, options); -} - -bool TransactionCallback::Delete(const DocumentReference& doc) { - return internal_->Delete(doc); -} - -TransactionGetResult TransactionCallback::Get(const DocumentReference& doc) { - return internal_->Get(doc); -} - -} // namespace csharp -} // namespace firestore -} // namespace firebase diff --git a/firestore/src/include/firebase/csharp/transaction_manager.h b/firestore/src/include/firebase/csharp/transaction_manager.h deleted file mode 100644 index 2f19c97d35..0000000000 --- a/firestore/src/include/firebase/csharp/transaction_manager.h +++ /dev/null @@ -1,193 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_TRANSACTION_MANAGER_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_TRANSACTION_MANAGER_H_ - -#include -#include -#include // NOLINT(build/c++11) -#include -#include - -#include "app/src/cleanup_notifier.h" -#include "firebase/future.h" -#include "firebase/csharp/map.h" -#include "firebase/firestore.h" -#include "firebase/firestore/document_snapshot.h" -#include "firebase/firestore/firestore_errors.h" - -// Add this to make this header compile when SWIG is not involved. -#ifndef SWIGSTDCALL -#if !defined(SWIG) && \ - (defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)) -#define SWIGSTDCALL __stdcall -#else -#define SWIGSTDCALL -#endif -#endif - -namespace firebase { -namespace firestore { -namespace csharp { - -class TransactionCallback; -class TransactionCallbackInternal; -class TransactionManagerInternal; - -// The type of the callback function to be specified to -// `TransactionManager::RunTransaction()`. The `TransactionCallback` argument -// contains all information required for C# to execute the callback. Ownership -// of the `TransactionCallback` is transferred to the function. -// -// Returns `true` if the callback was successful or `false` otherwise. If -// successful, then the `OnCompletion()` must have been invoked or must be -// invoked at some point in the future when the callback completes. If -// unsuccessful, then it is not required that `OnCompletion()` be invoked, and -// the transaction will complete in a failed state. -// NOLINTNEXTLINE(readability/casting) -typedef bool(SWIGSTDCALL* TransactionCallbackFn)(TransactionCallback*); - -// Stores the result of calling `Transaction::Get()` in a manner that is -// convenient for exposure to C# via SWIG. -// -// This class is not thread safe. -class TransactionGetResult { - public: - // Creates a new "invalid" instance of this class. - TransactionGetResult() = default; - - // Creates a new "valid" instance of this class with the given information. - TransactionGetResult(DocumentSnapshot&& snapshot, Error error_code, - std::string&& error_message) - : valid_(true), - snapshot_(std::move(snapshot)), - error_code_(error_code), - error_message_(std::move(error_message)) {} - - // Returns whether or not this object is "valid". - // - // If this object is "valid" then the other methods in this class have well- - // defined return values and behaviors; otherwise, the other methods in this - // class have undefined return values and behaviors and should not be invoked. - bool is_valid() const { return valid_; } - - // Returns the `DocumentSnapshot` result of the `Transaction::Get()` call. - // - // This method consumes the `DocumentSnapshot` upon its first invocation; - // subsequent invocations will return an invalid snapshot. - DocumentSnapshot TakeSnapshot() { return std::move(snapshot_); } - - // Returns the error code result of the `Transaction::Get()` call. - Error error_code() const { return error_code_; } - - // Returns the error message result of the `Transaction::Get()` call. - const std::string& error_message() const { return error_message_; } - - private: - // TODO(b/172570071) Replace `valid_` with `DocumentSnapshot::is_valid()`. - bool valid_ = false; - DocumentSnapshot snapshot_; - Error error_code_ = Error::kErrorUnknown; - std::string error_message_; -}; - -// Provides all information and machinery required to perform a transaction -// callback. This includes the callback ID that was specified to -// `RunTransaction()`, an `OnCompletion()` method to signal the success or -// failure of the callback, and indirect access to the `Transaction` object -// associated with the callback. -// -// This class is thread safe. -class TransactionCallback { - public: - TransactionCallback(std::shared_ptr internal, - int32_t callback_id, TransactionCallbackFn callback) - : internal_(internal), callback_id_(callback_id), callback_(callback) {} - - // Returns the internal `shared_ptr` that was specified to - // the constructor. - std::shared_ptr internal() const { - return internal_; - } - - // Returns the callback ID that was specified to - // `TransactionManager::RunTransaction()`. - int32_t callback_id() const { return callback_id_; } - - // Returns the callback function that was specified to - // `TransactionManager::RunTransaction()`. - TransactionCallbackFn callback() const { return callback_; } - - // Calls `Get()` on the encapsulated `Transaction` object with the given - // arguments. Returns a "valid" result object if the call succeeded or an - // "invalid" result object if it failed because the callback has completed. - TransactionGetResult Get(const DocumentReference& doc); - - // Calls `Update()`, `Set()` or `Delete()`, respectively, on the encapsulated - // `Transaction` object with the given arguments. Returns `true` if the call - // succeeded or `false` if it failed because the callback has completed. - bool Update(const DocumentReference& doc, const FieldValue& field_value); - bool Update(const DocumentReference& doc, - const Map& wrapper); - bool Update(const DocumentReference& doc, - const Map& wrapper); - bool Set(const DocumentReference& doc, const FieldValue& data, - const SetOptions& options); - bool Delete(const DocumentReference& doc); - - // Notifies the `TransactionManager` that the callback has completed. - // - // The `callback_successful` argument is `true` if the callback succeeded or - // `false` otherwise. - // - // This method must be invoked at least once to wake up the blocked - // transaction thread and complete the callback. Subsequent invocations, - // although allowed, have no effect. - void OnCompletion(bool callback_successful); - - private: - std::shared_ptr internal_; - int32_t callback_id_ = -1; - TransactionCallbackFn callback_ = nullptr; -}; - -// Bridges the C++ transaction API to the C# transaction API. -// -// This class is thread safe. -class TransactionManager { - public: - explicit TransactionManager(Firestore* firestore); - - ~TransactionManager(); - - // Shuts down this object. - // - // This method will cause any in-flight transactions to immediately fail and - // unblock any C++ transaction callback threads that are awaiting the - // completion of the transactions. - // - // This method is idempotent and may be safely called multiple times. - void Dispose(); - - // Runs a transaction. - // - // The `callback` function will be invoked to perform the transaction, and the - // `callback_id()` method of the `TransactionCallback` object specified to it - // will contain the given `callback_id`. - // - // This method uses the `RunTransaction()` method of the `Firestore` instance - // that was specified to the constructor to actually run the transaction. - Future RunTransaction(int32_t callback_id, - TransactionCallbackFn callback); - - private: - static void CleanUp(void* object); - - std::shared_ptr internal_; - CleanupNotifier& cleanup_notifier_; - std::mutex dispose_mutex_; -}; - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_TRANSACTION_MANAGER_H_ diff --git a/firestore/src/include/firebase/csharp/vector.h b/firestore/src/include/firebase/csharp/vector.h deleted file mode 100644 index ffef65aa73..0000000000 --- a/firestore/src/include/firebase/csharp/vector.h +++ /dev/null @@ -1,94 +0,0 @@ -#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_VECTOR_H_ -#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_VECTOR_H_ - -#include "firebase/firestore/document_change.h" -#include "firebase/firestore/document_snapshot.h" -#include "firebase/firestore/field_path.h" -#include "firebase/firestore/field_value.h" -#include "firebase/firestore/metadata_changes.h" -#include "firebase/firestore/query_snapshot.h" -#include "firebase/firestore/set_options.h" - -namespace firebase { -namespace firestore { -namespace csharp { - -// Simple wrappers to avoid exposing C++ standard library containers to SWIG. -// -// While it's normally possible to work with standard library containers in SWIG -// (by instantiating them for each template type used via the `%template` -// directive), issues in the build environment make that approach too -// complicated to be worth it. Instead, use simple wrappers and make sure the -// underlying containers are never exposed to C#. -// -// Note: most of the time, these classes should be declared with a `using` -// statement to ensure predictable lifetime of the object when dealing with -// iterators or unsafe views. - -// Wraps `std::vector` for use in C# code. -template -class Vector { - public: - Vector() = default; - - std::size_t Size() const { return container_.size(); } - - // The returned reference is only valid as long as this `Vector` is - // valid. In C#, declare the vector with a `using` statement to ensure its - // lifetime exceeds the lifetime of the reference. - const T& GetUnsafeView(std::size_t i) const { return container_[i]; } - - T GetCopy(std::size_t i) const { return container_[i]; } - - void PushBack(const T& value) { container_.push_back(value); } - - // Note: this is a named function and not a constructor to make it easier to - // ignore in SWIG. - static Vector Wrap(const std::vector& container) { - return Vector(container); - } - - const std::vector& Unwrap() const { return container_; } - - private: - explicit Vector(const std::vector& container) : container_(container) {} - - std::vector container_; -}; - -Vector ConvertFieldValueToVector(const FieldValue& value) { - return Vector::Wrap(value.array_value()); -} - -inline FieldValue ConvertVectorToFieldValue(const Vector& wrapper) { - return FieldValue::Array(wrapper.Unwrap()); -} - -inline FieldValue FieldValueArrayUnion(const Vector& wrapper) { - return FieldValue::ArrayUnion(wrapper.Unwrap()); -} - -inline FieldValue FieldValueArrayRemove(const Vector& wrapper) { - return FieldValue::ArrayRemove(wrapper.Unwrap()); -} - -inline Vector QuerySnapshotDocuments( - const QuerySnapshot& snapshot) { - return Vector::Wrap(snapshot.documents()); -} - -inline Vector QuerySnapshotDocumentChanges( - const QuerySnapshot& snapshot, MetadataChanges metadata_changes) { - return Vector::Wrap( - snapshot.DocumentChanges(metadata_changes)); -} - -inline SetOptions SetOptionsMergeFieldPaths(const Vector& fields) { - return SetOptions::MergeFieldPaths(fields.Unwrap()); -} - -} // namespace csharp -} // namespace firestore -} // namespace firebase - -#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_VECTOR_H_