FIRCallbackWrapper.mm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2024 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRCallbackWrapper.h"
  17. #include <memory>
  18. #include <utility>
  19. #include <vector>
  20. #include "Firestore/core/interfaceForSwift/api/Pipeline.h"
  21. #include "Firestore/core/interfaceForSwift/api/PipelineResult.h"
  22. #include "Firestore/core/src/core/event_listener.h"
  23. #include "Firestore/core/src/util/error_apple.h"
  24. #include "Firestore/core/src/util/statusor.h"
  25. using firebase::firestore::api::PipelineResult;
  26. using firebase::firestore::api::PipelineSnapshotListener;
  27. using firebase::firestore::core::EventListener;
  28. using firebase::firestore::util::MakeNSError;
  29. using firebase::firestore::util::StatusOr;
  30. @implementation FIRCallbackWrapper
  31. // In public Swift documentation for integrating Swift and C++, using raw pointers in C++ is
  32. // generally considered unsafe. However, during an experiment where the result was passed as a value
  33. // instead of a pointer, a double free error occurred. This issue could not be traced effectively
  34. // because the implementation resides within the Swift-C++ transition layer. In this specific use
  35. // case, the C++ OnEvent() scope is destroyed after the Swift callback has been destroyed. Due to
  36. // this ordering, using a raw pointer is a safe workaround for now.
  37. + (PipelineSnapshotListener)wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
  38. completion:(void (^)(CppPipelineResult *_Nullable result,
  39. NSError *_Nullable error))completion {
  40. class Converter : public EventListener<CppPipelineResult> {
  41. public:
  42. explicit Converter(std::shared_ptr<api::Firestore> firestore, PipelineBlock completion)
  43. : firestore_(firestore), completion_(completion) {
  44. }
  45. void OnEvent(StatusOr<CppPipelineResult> maybe_snapshot) override {
  46. if (maybe_snapshot.ok()) {
  47. completion_(&maybe_snapshot.ValueOrDie(), nullptr);
  48. } else {
  49. completion_(nullptr, MakeNSError(maybe_snapshot.status()));
  50. }
  51. }
  52. private:
  53. std::shared_ptr<api::Firestore> firestore_;
  54. PipelineBlock completion_;
  55. };
  56. return std::make_shared<Converter>(firestore, completion);
  57. }
  58. @end