mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Do not use absl::string_view as keys.
absl::string_view usually is not suitable type for a member variable nor a map key. The string referenced by string_view may run out of scope. PiperOrigin-RevId: 488743930
This commit is contained in:
committed by
Copybara-Service
parent
743e9b4a69
commit
384d144d6e
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "internal/platform/implementation/g3/credential_storage_impl.h"
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -32,7 +33,7 @@ void CredentialStorageImpl::SaveCredentials(
|
||||
absl::string_view manager_app_id, absl::string_view account_name,
|
||||
const std::vector<PrivateCredential>& private_credentials,
|
||||
const std::vector<PublicCredential>& public_credentials,
|
||||
::nearby::presence::PublicCredentialType public_credential_type,
|
||||
PublicCredentialType public_credential_type,
|
||||
::nearby::presence::GenerateCredentialsCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Save Private Credentials for account: "
|
||||
<< account_name << "], manager app ID:[" << manager_app_id
|
||||
@@ -45,14 +46,14 @@ void CredentialStorageImpl::SaveCredentials(
|
||||
}
|
||||
{
|
||||
absl::MutexLock lock(&private_mutex_);
|
||||
auto private_key_value = std::make_pair(
|
||||
std::make_pair(manager_app_id, account_name), private_credentials);
|
||||
auto private_result = private_credentials_map_.insert(private_key_value);
|
||||
PrivateCredentialKey key =
|
||||
CreatePrivateCredentialKey(manager_app_id, account_name);
|
||||
auto private_result = private_credentials_map_.insert(
|
||||
std::make_pair(key, private_credentials));
|
||||
if (!private_result.second) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "Credentials already saved in map. Overwriting previous creds!";
|
||||
private_credentials_map_[std::make_pair(manager_app_id, account_name)] =
|
||||
private_credentials;
|
||||
private_credentials_map_[key] = private_credentials;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,16 +68,14 @@ void CredentialStorageImpl::SaveCredentials(
|
||||
}
|
||||
{
|
||||
absl::MutexLock lock(&public_mutex_);
|
||||
auto public_key_value = std::make_pair(
|
||||
std::make_tuple(manager_app_id, account_name, public_credential_type),
|
||||
public_credentials);
|
||||
auto public_result = public_credentials_map_.insert(public_key_value);
|
||||
PublicCredentialKey key = CreatePublicCredentialKey(
|
||||
manager_app_id, account_name, public_credential_type);
|
||||
auto public_result =
|
||||
public_credentials_map_.insert(std::make_pair(key, public_credentials));
|
||||
if (!public_result.second) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "Credentials already saved in map. Overwriting previous creds!";
|
||||
public_credentials_map_[std::make_tuple(manager_app_id, account_name,
|
||||
public_credential_type)] =
|
||||
public_credentials;
|
||||
public_credentials_map_[key] = public_credentials;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,8 +89,8 @@ void CredentialStorageImpl::GetPrivateCredentials(
|
||||
<< credential_selector.account_name << "], manager app ID:["
|
||||
<< credential_selector.manager_app_id << "]";
|
||||
absl::MutexLock lock(&private_mutex_);
|
||||
auto key = std::make_pair(credential_selector.manager_app_id,
|
||||
credential_selector.account_name);
|
||||
PrivateCredentialKey key = CreatePrivateCredentialKey(
|
||||
credential_selector.manager_app_id, credential_selector.account_name);
|
||||
if (private_credentials_map_.find(key) == private_credentials_map_.end()) {
|
||||
NEARBY_LOGS(WARNING) << "There are no Private Credentials stored for key:"
|
||||
<< std::get<0>(key) << ", " << std::get<1>(key);
|
||||
@@ -112,9 +111,9 @@ void CredentialStorageImpl::GetPublicCredentials(
|
||||
<< credential_selector.account_name << "], manager app ID:["
|
||||
<< credential_selector.manager_app_id << "]";
|
||||
absl::MutexLock lock(&public_mutex_);
|
||||
auto key =
|
||||
std::make_tuple(credential_selector.manager_app_id,
|
||||
credential_selector.account_name, public_credential_type);
|
||||
PublicCredentialKey key = CreatePublicCredentialKey(
|
||||
credential_selector.manager_app_id, credential_selector.account_name,
|
||||
public_credential_type);
|
||||
if (public_credentials_map_.find(key) == public_credentials_map_.end()) {
|
||||
NEARBY_LOGS(WARNING) << "There are no Public Credentials stored for key:"
|
||||
<< std::get<0>(key) << ", " << std::get<1>(key) << ", "
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_G3_CREDENTIAL_STORAGE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -38,17 +39,22 @@ namespace g3 {
|
||||
*/
|
||||
class CredentialStorageImpl : public api::CredentialStorage {
|
||||
public:
|
||||
using PrivateCredential = ::nearby::internal::PrivateCredential;
|
||||
using PublicCredential = ::nearby::internal::PublicCredential;
|
||||
using PublicCredentialType = ::nearby::presence::PublicCredentialType;
|
||||
using PrivateCredentialKey = std::pair<std::string, std::string>;
|
||||
using PublicCredentialKey =
|
||||
std::tuple<std::string, std::string, PublicCredentialType>;
|
||||
|
||||
explicit CredentialStorageImpl() = default;
|
||||
~CredentialStorageImpl() override = default;
|
||||
|
||||
// Used to save private and public credentials.
|
||||
void SaveCredentials(
|
||||
absl::string_view manager_app_id, absl::string_view account_name,
|
||||
const std::vector<::nearby::internal::PrivateCredential>&
|
||||
private_credentials,
|
||||
const std::vector<::nearby::internal::PublicCredential>&
|
||||
public_credentials,
|
||||
::nearby::presence::PublicCredentialType public_credential_type,
|
||||
const std::vector<PrivateCredential>& private_credentials,
|
||||
const std::vector<PublicCredential>& public_credentials,
|
||||
PublicCredentialType public_credential_type,
|
||||
::nearby::presence::GenerateCredentialsCallback callback) override;
|
||||
|
||||
// Used to fetch private creds when broadcasting.
|
||||
@@ -60,16 +66,24 @@ class CredentialStorageImpl : public api::CredentialStorage {
|
||||
// Used to fetch remote public creds when scanning.
|
||||
void GetPublicCredentials(
|
||||
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||
::nearby::presence::PublicCredentialType public_credential_type,
|
||||
PublicCredentialType public_credential_type,
|
||||
::nearby::presence::GetPublicCredentialsResultCallback callback) override;
|
||||
|
||||
private:
|
||||
absl::flat_hash_map<std::pair<absl::string_view, absl::string_view>,
|
||||
std::vector<::nearby::internal::PrivateCredential>>
|
||||
PrivateCredentialKey CreatePrivateCredentialKey(
|
||||
absl::string_view manager_app_id, absl::string_view account_name) {
|
||||
return std::make_tuple(std::string(manager_app_id),
|
||||
std::string(account_name));
|
||||
}
|
||||
PublicCredentialKey CreatePublicCredentialKey(
|
||||
absl::string_view manager_app_id, absl::string_view account_name,
|
||||
PublicCredentialType credential_type) {
|
||||
return std::make_tuple(std::string(manager_app_id),
|
||||
std::string(account_name), credential_type);
|
||||
}
|
||||
absl::flat_hash_map<PrivateCredentialKey, std::vector<PrivateCredential>>
|
||||
private_credentials_map_;
|
||||
absl::flat_hash_map<std::tuple<absl::string_view, absl::string_view,
|
||||
::nearby::presence::PublicCredentialType>,
|
||||
std::vector<::nearby::internal::PublicCredential>>
|
||||
absl::flat_hash_map<PublicCredentialKey, std::vector<PublicCredential>>
|
||||
public_credentials_map_;
|
||||
absl::Mutex private_mutex_;
|
||||
absl::Mutex public_mutex_;
|
||||
|
||||
Reference in New Issue
Block a user