aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/src/firebase/firestore
diff options
context:
space:
mode:
authorGravatar zxu <zxu@google.com>2018-02-20 12:25:39 -0500
committerGravatar GitHub <noreply@github.com>2018-02-20 12:25:39 -0500
commit7a4a2ea10844afd6a58dace46854fae74399f55c (patch)
treebb6115c271d6fac490cb5720a79fcc7de21d2de9 /Firestore/core/src/firebase/firestore
parenta9f3f35d483f1031ef2e2860aeda921f56e1bf08 (diff)
replacing FSTGetTokenResult by C++ Token implementation (#805)
* replacing Auth/FSTUser by C++ auth implementation * address changes * replacing FSTGetTokenResult by C++ Token implementation * address changes * address changes * fix another const& v.s. dispatch bug * fix more const& v.s. dispatch bug zxu123 committed * fix * passing by value in callback
Diffstat (limited to 'Firestore/core/src/firebase/firestore')
-rw-r--r--Firestore/core/src/firebase/firestore/auth/credentials_provider.h4
-rw-r--r--Firestore/core/src/firebase/firestore/auth/token.cc10
-rw-r--r--Firestore/core/src/firebase/firestore/auth/token.h19
-rw-r--r--Firestore/core/src/firebase/firestore/util/string_apple.h11
4 files changed, 40 insertions, 4 deletions
diff --git a/Firestore/core/src/firebase/firestore/auth/credentials_provider.h b/Firestore/core/src/firebase/firestore/auth/credentials_provider.h
index 2a52c99..917f8e1 100644
--- a/Firestore/core/src/firebase/firestore/auth/credentials_provider.h
+++ b/Firestore/core/src/firebase/firestore/auth/credentials_provider.h
@@ -31,11 +31,11 @@ namespace auth {
// `TokenErrorListener` is a listener that gets a token or an error.
// token: An auth token as a string, or nullptr if error occurred.
// error: The error if one occurred, or else nullptr.
-typedef std::function<void(const Token& token, const absl::string_view error)>
+typedef std::function<void(Token token, const absl::string_view error)>
TokenListener;
// Listener notified with a User change.
-typedef std::function<void(const User& user)> UserChangeListener;
+typedef std::function<void(User user)> UserChangeListener;
/**
* Provides methods for getting the uid and token for the current user and
diff --git a/Firestore/core/src/firebase/firestore/auth/token.cc b/Firestore/core/src/firebase/firestore/auth/token.cc
index 0618ddb..4ee1b69 100644
--- a/Firestore/core/src/firebase/firestore/auth/token.cc
+++ b/Firestore/core/src/firebase/firestore/auth/token.cc
@@ -21,7 +21,15 @@ namespace firestore {
namespace auth {
Token::Token(const absl::string_view token, const User& user)
- : token_(token), user_(user) {
+ : token_(token), user_(user), is_valid_(true) {
+}
+
+Token::Token() : token_(), user_(User::Unauthenticated()), is_valid_(false) {
+}
+
+const Token& Token::Invalid() {
+ static const Token kInvalidToken;
+ return kInvalidToken;
}
} // namespace auth
diff --git a/Firestore/core/src/firebase/firestore/auth/token.h b/Firestore/core/src/firebase/firestore/auth/token.h
index f3b7363..ff8d2f0 100644
--- a/Firestore/core/src/firebase/firestore/auth/token.h
+++ b/Firestore/core/src/firebase/firestore/auth/token.h
@@ -20,6 +20,7 @@
#include <string>
#include "Firestore/core/src/firebase/firestore/auth/user.h"
+#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h"
#include "absl/strings/string_view.h"
namespace firebase {
@@ -45,6 +46,7 @@ class Token {
/** The actual raw token. */
const std::string& token() const {
+ FIREBASE_ASSERT(is_valid_);
return token_;
}
@@ -56,9 +58,26 @@ class Token {
return user_;
}
+ /**
+ * Whether the token is a valid one.
+ *
+ * ## Portability notes: Invalid token is the equivalent of nil in the iOS
+ * token implementation. We use value instead of pointer for Token instance in
+ * the C++ migration.
+ */
+ bool is_valid() const {
+ return is_valid_;
+ }
+
+ /** Returns an invalid token. */
+ static const Token& Invalid();
+
private:
+ Token();
+
const std::string token_;
const User user_;
+ const bool is_valid_;
};
} // namespace auth
diff --git a/Firestore/core/src/firebase/firestore/util/string_apple.h b/Firestore/core/src/firebase/firestore/util/string_apple.h
index fe2a487..3f6b814 100644
--- a/Firestore/core/src/firebase/firestore/util/string_apple.h
+++ b/Firestore/core/src/firebase/firestore/util/string_apple.h
@@ -42,7 +42,16 @@ inline NSString* WrapNSStringNoCopy(const absl::string_view str) {
return WrapNSStringNoCopy(str.data());
}
-// Creates an absl::string_view wrapper for the contents of the given NSString.
+// Translates a string_view string to the equivalent NSString by making a copy.
+inline NSString* WrapNSString(const absl::string_view str) {
+ return [[NSString alloc]
+ initWithBytes:const_cast<void*>(static_cast<const void*>(str.data()))
+ length:str.length()
+ encoding:NSUTF8StringEncoding];
+}
+
+// Creates an absl::string_view wrapper for the contents of the given
+// NSString.
inline absl::string_view MakeStringView(NSString* str) {
return absl::string_view(
[str UTF8String], [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);