mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Internal timer fix
PiperOrigin-RevId: 655979703
This commit is contained in:
committed by
Copybara-Service
parent
9bf73908e9
commit
7c7b629582
@@ -39,6 +39,7 @@
|
||||
#include "sharing/internal/public/logging.h"
|
||||
#include "sharing/local_device_data/nearby_share_local_device_data_manager.h"
|
||||
#include "sharing/proto/enums.pb.h"
|
||||
#include "sharing/thread_timer.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace sharing {
|
||||
@@ -70,19 +71,17 @@ ShowNotificationStatus GetNotificationStatus(
|
||||
} // namespace
|
||||
|
||||
NearbyShareSettings::NearbyShareSettings(
|
||||
Context* context,
|
||||
nearby::Clock* clock,
|
||||
nearby::DeviceInfo& device_info,
|
||||
Context* context, nearby::Clock* clock, nearby::DeviceInfo& device_info,
|
||||
PreferenceManager& preference_manager,
|
||||
NearbyShareLocalDeviceDataManager* local_device_data_manager,
|
||||
analytics::AnalyticsRecorder* analytics_recorder)
|
||||
: clock_(clock),
|
||||
: context_(context),
|
||||
clock_(clock),
|
||||
device_info_(device_info),
|
||||
preference_manager_(preference_manager),
|
||||
local_device_data_manager_(local_device_data_manager),
|
||||
analytics_recorder_(analytics_recorder) {
|
||||
is_desctructing_ = std::make_shared<bool>(false);
|
||||
visibility_expiration_timer_ = context->CreateTimer();
|
||||
RestoreFallbackVisibility();
|
||||
preference_manager_.AddObserver(
|
||||
kPreferencesObserverName,
|
||||
@@ -104,7 +103,7 @@ NearbyShareSettings::~NearbyShareSettings() {
|
||||
is_desctructing_ = nullptr;
|
||||
preference_manager_.RemoveObserver(kPreferencesObserverName);
|
||||
local_device_data_manager_->RemoveObserver(this);
|
||||
visibility_expiration_timer_->Stop();
|
||||
visibility_expiration_timer_.reset();
|
||||
}
|
||||
|
||||
FastInitiationNotificationState
|
||||
@@ -147,8 +146,9 @@ void NearbyShareSettings::StartVisibilityTimer(
|
||||
absl::Duration expiration) const {
|
||||
NL_LOG(INFO) << __func__
|
||||
<< ": start visibility timer. expiration=" << expiration;
|
||||
visibility_expiration_timer_->Start(
|
||||
absl::ToInt64Milliseconds(expiration), 0, [this]() {
|
||||
visibility_expiration_timer_ = std::make_unique<ThreadTimer>(
|
||||
*context_->GetTaskRunner(), "nearby_share_settings_visibility_timer",
|
||||
expiration, [this]() {
|
||||
NL_LOG(INFO) << __func__ << ": visibility timer expired.";
|
||||
proto::DeviceVisibility visibility;
|
||||
{
|
||||
@@ -157,7 +157,7 @@ void NearbyShareSettings::StartVisibilityTimer(
|
||||
// GetFallbackVisibility() will return the persisted fallback
|
||||
// visibility value instead of UNSPECIFIED.
|
||||
visibility = GetFallbackVisibility().visibility;
|
||||
visibility_expiration_timer_->Stop();
|
||||
visibility_expiration_timer_.reset();
|
||||
}
|
||||
SetVisibility(visibility);
|
||||
});
|
||||
@@ -267,7 +267,7 @@ void NearbyShareSettings::SetDataUsage(DataUsage data_usage) {
|
||||
analytics_recorder_->NewSetDataUsage(GetDataUsage(), data_usage);
|
||||
}
|
||||
preference_manager_.SetInteger(prefs::kNearbySharingDataUsageName,
|
||||
static_cast<int>(data_usage));
|
||||
static_cast<int>(data_usage));
|
||||
}
|
||||
|
||||
void NearbyShareSettings::GetVisibility(
|
||||
@@ -305,10 +305,11 @@ void NearbyShareSettings::SetVisibility(DeviceVisibility visibility,
|
||||
NL_VLOG(1) << __func__
|
||||
<< ": set visibility. visibility=" << static_cast<int>(visibility)
|
||||
<< ", expiration=" << expiration;
|
||||
if (visibility_expiration_timer_->IsRunning()) {
|
||||
if (visibility_expiration_timer_ != nullptr &&
|
||||
visibility_expiration_timer_->IsRunning()) {
|
||||
NL_VLOG(1) << __func__
|
||||
<< ": temporary visibility timer is running. stopped.";
|
||||
visibility_expiration_timer_->Stop();
|
||||
visibility_expiration_timer_.reset();
|
||||
}
|
||||
|
||||
absl::Time now = clock_->Now();
|
||||
@@ -334,9 +335,8 @@ void NearbyShareSettings::SetVisibility(DeviceVisibility visibility,
|
||||
|
||||
last_visibility_timestamp_ = now;
|
||||
last_visibility_ = last_visibility;
|
||||
preference_manager_.SetInteger(
|
||||
prefs::kNearbySharingBackgroundVisibilityName,
|
||||
static_cast<int>(visibility));
|
||||
preference_manager_.SetInteger(prefs::kNearbySharingBackgroundVisibilityName,
|
||||
static_cast<int>(visibility));
|
||||
}
|
||||
|
||||
absl::Time NearbyShareSettings::GetLastVisibilityTimestamp() const {
|
||||
@@ -352,16 +352,15 @@ proto::DeviceVisibility NearbyShareSettings::GetLastVisibility() const {
|
||||
NearbyShareSettings::FallbackVisibilityInfo
|
||||
NearbyShareSettings::GetFallbackVisibility() const {
|
||||
MutexLock lock(&mutex_);
|
||||
FallbackVisibilityInfo result {
|
||||
.visibility = DeviceVisibility::DEVICE_VISIBILITY_UNSPECIFIED,
|
||||
.fallback_time = absl::UnixEpoch(),
|
||||
FallbackVisibilityInfo result{
|
||||
.visibility = DeviceVisibility::DEVICE_VISIBILITY_UNSPECIFIED,
|
||||
.fallback_time = absl::UnixEpoch(),
|
||||
};
|
||||
if (GetIsTemporarilyVisible()) {
|
||||
result.visibility =
|
||||
fallback_visibility_.value_or(prefs::kDefaultFallbackVisibility);
|
||||
result.fallback_time =
|
||||
absl::FromUnixSeconds(preference_manager_.GetInteger(
|
||||
prefs::kNearbySharingBackgroundVisibilityExpirationSeconds, 0));
|
||||
result.fallback_time = absl::FromUnixSeconds(preference_manager_.GetInteger(
|
||||
prefs::kNearbySharingBackgroundVisibilityExpirationSeconds, 0));
|
||||
}
|
||||
NL_VLOG(1) << __func__ << ": get fallback visibility "
|
||||
<< static_cast<int>(result.visibility)
|
||||
@@ -387,7 +386,7 @@ void NearbyShareSettings::SetFallbackVisibility(
|
||||
|
||||
bool NearbyShareSettings::GetIsTemporarilyVisible() const {
|
||||
MutexLock lock(&mutex_);
|
||||
return visibility_expiration_timer_->IsRunning();
|
||||
return visibility_expiration_timer_ != nullptr;
|
||||
}
|
||||
|
||||
void NearbyShareSettings::GetCustomSavePathAsync(
|
||||
@@ -398,8 +397,7 @@ void NearbyShareSettings::GetCustomSavePathAsync(
|
||||
void NearbyShareSettings::SetCustomSavePathAsync(
|
||||
absl::string_view save_path, const std::function<void()>& callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
preference_manager_.SetString(prefs::kNearbySharingCustomSavePath,
|
||||
save_path);
|
||||
preference_manager_.SetString(prefs::kNearbySharingCustomSavePath, save_path);
|
||||
callback();
|
||||
}
|
||||
|
||||
@@ -450,7 +448,7 @@ void NearbyShareSettings::SetIsAnalyticsEnabled(
|
||||
bool is_analytics_enabled) const {
|
||||
MutexLock lock(&mutex_);
|
||||
preference_manager_.SetBoolean(prefs::kNearbySharingIsAnalyticsEnabledName,
|
||||
is_analytics_enabled);
|
||||
is_analytics_enabled);
|
||||
}
|
||||
|
||||
std::string NearbyShareSettings::Dump() const {
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "sharing/internal/public/logging.h"
|
||||
#include "sharing/local_device_data/nearby_share_local_device_data_manager.h"
|
||||
#include "sharing/proto/settings_observer_data.pb.h"
|
||||
#include "sharing/thread_timer.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace sharing {
|
||||
@@ -161,9 +162,7 @@ class NearbyShareSettings
|
||||
};
|
||||
|
||||
NearbyShareSettings(
|
||||
Context* context,
|
||||
nearby::Clock* clock,
|
||||
nearby::DeviceInfo& device_info,
|
||||
Context* context, nearby::Clock* clock, nearby::DeviceInfo& device_info,
|
||||
nearby::sharing::api::PreferenceManager& preference_manager,
|
||||
NearbyShareLocalDeviceDataManager* local_device_data_manager,
|
||||
analytics::AnalyticsRecorder* analytics_recorder = nullptr);
|
||||
@@ -257,6 +256,7 @@ class NearbyShareSettings
|
||||
|
||||
// Make sure thread safe to access Nearby settings
|
||||
mutable RecursiveMutex mutex_;
|
||||
Context* context_;
|
||||
nearby::Clock* const clock_;
|
||||
nearby::DeviceInfo& device_info_;
|
||||
nearby::sharing::api::PreferenceManager& preference_manager_;
|
||||
@@ -267,7 +267,8 @@ class NearbyShareSettings
|
||||
std::shared_ptr<bool> is_desctructing_ = nullptr;
|
||||
bool is_fast_initiation_hardware_supported_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
ObserverList<Observer> observers_set_ ABSL_GUARDED_BY(mutex_);
|
||||
std::unique_ptr<Timer> visibility_expiration_timer_ ABSL_GUARDED_BY(mutex_);
|
||||
mutable std::unique_ptr<ThreadTimer> visibility_expiration_timer_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
mutable std::optional<proto::DeviceVisibility> fallback_visibility_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
|
||||
@@ -309,8 +309,11 @@ TEST_F(NearbyShareSettingsTest,
|
||||
// Fast forward to the expiration time.
|
||||
FastForward(absl::Seconds(prefs::kDefaultMaxVisibilityExpirationSeconds + 1));
|
||||
// Verify that the visibility has expired and we are back to self share.
|
||||
Flush();
|
||||
EXPECT_EQ(settings()->GetVisibility(),
|
||||
DeviceVisibility::DEVICE_VISIBILITY_SELF_SHARE);
|
||||
EXPECT_EQ(settings()->GetFallbackVisibility().visibility,
|
||||
DeviceVisibility::DEVICE_VISIBILITY_UNSPECIFIED);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareSettingsTest,
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -26,6 +25,7 @@
|
||||
#include "sharing/internal/public/context.h"
|
||||
#include "sharing/internal/public/logging.h"
|
||||
#include "sharing/nearby_connections_types.h"
|
||||
#include "sharing/thread_timer.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace sharing {
|
||||
@@ -89,14 +89,14 @@ bool TransferManager::StartTransfer() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (timeout_timer_ != nullptr && timeout_timer_->IsRunning()) {
|
||||
if (timeout_timer_ != nullptr) {
|
||||
NL_LOG(WARNING) << "transfer already started.";
|
||||
return false;
|
||||
}
|
||||
|
||||
timeout_timer_ = context_->CreateTimer();
|
||||
timeout_timer_->Start(
|
||||
kMediumUpgradeTimeout / absl::Milliseconds(1), 0, [&]() {
|
||||
timeout_timer_ = std::make_unique<ThreadTimer>(
|
||||
*context_->GetTaskRunner(), "transfer_manager_timeout_timer",
|
||||
kMediumUpgradeTimeout, [this]() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
NL_LOG(INFO) << "Timed out for endpoint " << endpoint_id_ << " after "
|
||||
@@ -116,12 +116,12 @@ bool TransferManager::StartTransfer() {
|
||||
bool TransferManager::CancelTransfer() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
if (timeout_timer_ == nullptr || !timeout_timer_->IsRunning()) {
|
||||
if (timeout_timer_ == nullptr) {
|
||||
NL_LOG(WARNING) << "No running transfer.";
|
||||
return false;
|
||||
}
|
||||
|
||||
timeout_timer_->Stop();
|
||||
timeout_timer_.reset();
|
||||
NL_LOG(INFO) << __func__ << "Transfer is canceled";
|
||||
return true;
|
||||
}
|
||||
@@ -133,11 +133,9 @@ void TransferManager::StopWaitingForHighQualityMedium() {
|
||||
NL_LOG(INFO) << "Sending delayed payload to endpoint " << endpoint_id_;
|
||||
task();
|
||||
}
|
||||
pending_tasks_.clear();
|
||||
|
||||
if (timeout_timer_ != nullptr) {
|
||||
timeout_timer_->Stop();
|
||||
}
|
||||
pending_tasks_.clear();
|
||||
timeout_timer_.reset();
|
||||
}
|
||||
|
||||
} // namespace sharing
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "absl/time/time.h"
|
||||
#include "sharing/internal/public/context.h"
|
||||
#include "sharing/nearby_connections_types.h"
|
||||
#include "sharing/thread_timer.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace sharing {
|
||||
@@ -47,15 +48,15 @@ class TransferManager {
|
||||
bool CancelTransfer() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
void StopWaitingForHighQualityMedium();
|
||||
void StopWaitingForHighQualityMedium() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
Context* context_;
|
||||
bool is_waiting_for_high_quality_medium_ = true;
|
||||
std::string endpoint_id_;
|
||||
absl::Mutex mutex_;
|
||||
std::vector<std::function<void()>> pending_tasks_;
|
||||
std::vector<std::function<void()>> pending_tasks_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
std::unique_ptr<Timer> timeout_timer_ = nullptr;
|
||||
std::unique_ptr<ThreadTimer> timeout_timer_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace sharing
|
||||
|
||||
Reference in New Issue
Block a user