Commit 2275446a authored by fgorski's avatar fgorski Committed by Commit bot

[GCM] Wiring heartbeat interval calls to GCMDriver

Wires the call to Add/RemoveHeartbeatInterval through:
* GCMDriver/Desktop/Android/Fake
* GCMDriverDesktop::IOWorker
* GCMClient/Impl to MCSClient
* Adding loading of persisted interval in MCSClient

BUG=481174

Review URL: https://codereview.chromium.org/1124783002

Cr-Commit-Position: refs/heads/master@{#329750}
parent 6bbfc661
...@@ -148,6 +148,13 @@ std::string FakeGCMClient::GetInstanceIDData(const std::string& app_id) { ...@@ -148,6 +148,13 @@ std::string FakeGCMClient::GetInstanceIDData(const std::string& app_id) {
return std::string(); return std::string();
} }
void FakeGCMClient::AddHeartbeatInterval(const std::string& scope,
int interval_ms) {
}
void FakeGCMClient::RemoveHeartbeatInterval(const std::string& scope) {
}
void FakeGCMClient::PerformDelayedStart() { void FakeGCMClient::PerformDelayedStart() {
DCHECK(ui_thread_->RunsTasksOnCurrentThread()); DCHECK(ui_thread_->RunsTasksOnCurrentThread());
......
...@@ -60,6 +60,8 @@ class FakeGCMClient : public GCMClient { ...@@ -60,6 +60,8 @@ class FakeGCMClient : public GCMClient {
const std::string& instance_id_data) override; const std::string& instance_id_data) override;
void RemoveInstanceIDData(const std::string& app_id) override; void RemoveInstanceIDData(const std::string& app_id) override;
std::string GetInstanceIDData(const std::string& app_id) override; std::string GetInstanceIDData(const std::string& app_id) override;
void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
void RemoveHeartbeatInterval(const std::string& scope) override;
// Initiate the start that has been delayed. // Initiate the start that has been delayed.
// Called on UI thread. // Called on UI thread.
......
...@@ -102,4 +102,11 @@ InstanceIDStore* FakeGCMDriver::GetInstanceIDStore() { ...@@ -102,4 +102,11 @@ InstanceIDStore* FakeGCMDriver::GetInstanceIDStore() {
return NULL; return NULL;
} }
void FakeGCMDriver::AddHeartbeatInterval(const std::string& scope,
int interval_ms) {
}
void FakeGCMDriver::RemoveHeartbeatInterval(const std::string& scope) {
}
} // namespace gcm } // namespace gcm
...@@ -42,6 +42,8 @@ class FakeGCMDriver : public GCMDriver { ...@@ -42,6 +42,8 @@ class FakeGCMDriver : public GCMDriver {
void SetLastTokenFetchTime(const base::Time& time) override; void SetLastTokenFetchTime(const base::Time& time) override;
void WakeFromSuspendForHeartbeat(bool wake) override; void WakeFromSuspendForHeartbeat(bool wake) override;
InstanceIDStore* GetInstanceIDStore() override; InstanceIDStore* GetInstanceIDStore() override;
void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
void RemoveHeartbeatInterval(const std::string& scope) override;
protected: protected:
// GCMDriver implementation: // GCMDriver implementation:
......
...@@ -320,6 +320,14 @@ class GCMClient { ...@@ -320,6 +320,14 @@ class GCMClient {
// Retrieves the Instance ID data for a specific app from the persistent // Retrieves the Instance ID data for a specific app from the persistent
// store. // store.
virtual std::string GetInstanceIDData(const std::string& app_id) = 0; virtual std::string GetInstanceIDData(const std::string& app_id) = 0;
// Gets and sets custom heartbeat interval for the MCS connection.
// |scope| is used to identify the component that requests a custom interval
// to be set, and allows that component to later revoke the setting. It should
// be unique.
virtual void AddHeartbeatInterval(const std::string& scope,
int interval_ms) = 0;
virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
}; };
} // namespace gcm } // namespace gcm
......
...@@ -556,6 +556,17 @@ std::string GCMClientImpl::GetInstanceIDData(const std::string& app_id) { ...@@ -556,6 +556,17 @@ std::string GCMClientImpl::GetInstanceIDData(const std::string& app_id) {
return iter->second; return iter->second;
} }
void GCMClientImpl::AddHeartbeatInterval(const std::string& scope,
int interval_ms) {
DCHECK(mcs_client_);
mcs_client_->AddHeartbeatInterval(scope, interval_ms);
}
void GCMClientImpl::RemoveHeartbeatInterval(const std::string& scope) {
DCHECK(mcs_client_);
mcs_client_->RemoveHeartbeatInterval(scope);
}
void GCMClientImpl::StartCheckin() { void GCMClientImpl::StartCheckin() {
// Make sure no checkin is in progress. // Make sure no checkin is in progress.
if (checkin_request_.get()) if (checkin_request_.get())
......
...@@ -130,6 +130,8 @@ class GCMClientImpl ...@@ -130,6 +130,8 @@ class GCMClientImpl
const std::string& instance_id_data) override; const std::string& instance_id_data) override;
void RemoveInstanceIDData(const std::string& app_id) override; void RemoveInstanceIDData(const std::string& app_id) override;
std::string GetInstanceIDData(const std::string& app_id) override; std::string GetInstanceIDData(const std::string& app_id) override;
void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
void RemoveHeartbeatInterval(const std::string& scope) override;
// GCMStatsRecorder::Delegate implemenation. // GCMStatsRecorder::Delegate implemenation.
void OnActivityRecorded() override; void OnActivityRecorded() override;
......
...@@ -169,6 +169,28 @@ class GCMDriver { ...@@ -169,6 +169,28 @@ class GCMDriver {
// Supports saving the Instance ID data in the GCM store. // Supports saving the Instance ID data in the GCM store.
virtual InstanceIDStore* GetInstanceIDStore() = 0; virtual InstanceIDStore* GetInstanceIDStore() = 0;
// Adds or removes a custom client requested heartbeat interval. If multiple
// components set that setting, the lowest setting will be used. If the
// setting is outside of GetMax/MinClientHeartbeatIntervalMs() it will be
// ignored. If a new setting is less than the currently used, the connection
// will be reset with the new heartbeat. Client that no longer require
// aggressive heartbeats, should remove their requested interval. Heartbeats
// set this way survive connection/Chrome restart.
//
// GCM Driver can decide to postpone the action until Client is properly
// initialized, hence this setting can be called at any time.
//
// Server can overwrite the setting to a different value.
//
// |scope| is used to identify the component that requests a custom interval
// to be set, and allows that component to later revoke the setting.
// |interval_ms| should be between 2 minues and 15 minues (28 minues on
// cellular networks). For details check
// GetMin/MaxClientHeartbeatItnervalMs() in HeartbeatManager.
virtual void AddHeartbeatInterval(const std::string& scope,
int interval_ms) = 0;
virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
protected: protected:
// Ensures that the GCM service starts (if necessary conditions are met). // Ensures that the GCM service starts (if necessary conditions are met).
virtual GCMClient::Result EnsureStarted(GCMClient::StartMode start_mode) = 0; virtual GCMClient::Result EnsureStarted(GCMClient::StartMode start_mode) = 0;
......
...@@ -167,6 +167,13 @@ InstanceIDStore* GCMDriverAndroid::GetInstanceIDStore() { ...@@ -167,6 +167,13 @@ InstanceIDStore* GCMDriverAndroid::GetInstanceIDStore() {
return NULL; return NULL;
} }
void GCMDriverAndroid::AddHeartbeatInterval(const std::string& scope,
int interval_ms) {
}
void GCMDriverAndroid::RemoveHeartbeatInterval(const std::string& scope) {
}
GCMClient::Result GCMDriverAndroid::EnsureStarted( GCMClient::Result GCMDriverAndroid::EnsureStarted(
GCMClient::StartMode start_mode) { GCMClient::StartMode start_mode) {
// TODO(johnme): Maybe we should check if GMS is available? // TODO(johnme): Maybe we should check if GMS is available?
......
...@@ -65,6 +65,8 @@ class GCMDriverAndroid : public GCMDriver { ...@@ -65,6 +65,8 @@ class GCMDriverAndroid : public GCMDriver {
void SetLastTokenFetchTime(const base::Time& time) override; void SetLastTokenFetchTime(const base::Time& time) override;
void WakeFromSuspendForHeartbeat(bool wake) override; void WakeFromSuspendForHeartbeat(bool wake) override;
InstanceIDStore* GetInstanceIDStore() override; InstanceIDStore* GetInstanceIDStore() override;
void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
void RemoveHeartbeatInterval(const std::string& scope) override;
protected: protected:
// GCMDriver implementation: // GCMDriver implementation:
......
...@@ -91,6 +91,8 @@ class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { ...@@ -91,6 +91,8 @@ class GCMDriverDesktop::IOWorker : public GCMClient::Delegate {
const std::string& instance_id_data); const std::string& instance_id_data);
void RemoveInstanceIDData(const std::string& app_id); void RemoveInstanceIDData(const std::string& app_id);
void GetInstanceIDData(const std::string& app_id); void GetInstanceIDData(const std::string& app_id);
void AddHeartbeatInterval(const std::string& scope, int interval_ms);
void RemoveHeartbeatInterval(const std::string& scope);
// For testing purpose. Can be called from UI thread. Use with care. // For testing purpose. Can be called from UI thread. Use with care.
GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); }
...@@ -392,6 +394,18 @@ void GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat(bool wake) { ...@@ -392,6 +394,18 @@ void GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat(bool wake) {
#endif #endif
} }
void GCMDriverDesktop::IOWorker::AddHeartbeatInterval(const std::string& scope,
int interval_ms) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());
gcm_client_->AddHeartbeatInterval(scope, interval_ms);
}
void GCMDriverDesktop::IOWorker::RemoveHeartbeatInterval(
const std::string& scope) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());
gcm_client_->RemoveHeartbeatInterval(scope);
}
GCMDriverDesktop::GCMDriverDesktop( GCMDriverDesktop::GCMDriverDesktop(
scoped_ptr<GCMClientFactory> gcm_client_factory, scoped_ptr<GCMClientFactory> gcm_client_factory,
const GCMClient::ChromeBuildInfo& chrome_build_info, const GCMClient::ChromeBuildInfo& chrome_build_info,
...@@ -768,6 +782,49 @@ void GCMDriverDesktop::WakeFromSuspendForHeartbeat(bool wake) { ...@@ -768,6 +782,49 @@ void GCMDriverDesktop::WakeFromSuspendForHeartbeat(bool wake) {
wake_from_suspend_enabled_)); wake_from_suspend_enabled_));
} }
void GCMDriverDesktop::AddHeartbeatInterval(const std::string& scope,
int interval_ms) {
DCHECK(ui_thread_->RunsTasksOnCurrentThread());
// The GCM service has not been initialized.
if (!delayed_task_controller_)
return;
if (!delayed_task_controller_->CanRunTaskWithoutDelay()) {
// The GCM service was initialized but has not started yet.
delayed_task_controller_->AddTask(
base::Bind(&GCMDriverDesktop::AddHeartbeatInterval,
weak_ptr_factory_.GetWeakPtr(), scope, interval_ms));
return;
}
io_thread_->PostTask(
FROM_HERE,
base::Bind(&GCMDriverDesktop::IOWorker::AddHeartbeatInterval,
base::Unretained(io_worker_.get()), scope, interval_ms));
}
void GCMDriverDesktop::RemoveHeartbeatInterval(const std::string& scope) {
DCHECK(ui_thread_->RunsTasksOnCurrentThread());
// The GCM service has not been initialized.
if (!delayed_task_controller_)
return;
if (!delayed_task_controller_->CanRunTaskWithoutDelay()) {
// The GCM service was initialized but has not started yet.
delayed_task_controller_->AddTask(
base::Bind(&GCMDriverDesktop::RemoveHeartbeatInterval,
weak_ptr_factory_.GetWeakPtr(), scope));
return;
}
io_thread_->PostTask(
FROM_HERE,
base::Bind(&GCMDriverDesktop::IOWorker::RemoveHeartbeatInterval,
base::Unretained(io_worker_.get()), scope));
}
void GCMDriverDesktop::SetAccountTokens( void GCMDriverDesktop::SetAccountTokens(
const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
DCHECK(ui_thread_->RunsTasksOnCurrentThread()); DCHECK(ui_thread_->RunsTasksOnCurrentThread());
......
...@@ -85,6 +85,8 @@ class GCMDriverDesktop : public GCMDriver, ...@@ -85,6 +85,8 @@ class GCMDriverDesktop : public GCMDriver,
void SetLastTokenFetchTime(const base::Time& time) override; void SetLastTokenFetchTime(const base::Time& time) override;
void WakeFromSuspendForHeartbeat(bool wake) override; void WakeFromSuspendForHeartbeat(bool wake) override;
InstanceIDStore* GetInstanceIDStore() override; InstanceIDStore* GetInstanceIDStore() override;
void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
void RemoveHeartbeatInterval(const std::string& scope) override;
// InstanceIDStore overrides: // InstanceIDStore overrides:
void AddInstanceIDData(const std::string& app_id, void AddInstanceIDData(const std::string& app_id,
......
...@@ -26,6 +26,7 @@ void GCMStore::LoadResult::Reset() { ...@@ -26,6 +26,7 @@ void GCMStore::LoadResult::Reset() {
last_token_fetch_time = base::Time::FromInternalValue(0LL); last_token_fetch_time = base::Time::FromInternalValue(0LL);
last_checkin_accounts.clear(); last_checkin_accounts.clear();
account_mappings.clear(); account_mappings.clear();
heartbeat_intervals.clear();
success = false; success = false;
instance_id_data.clear(); instance_id_data.clear();
} }
......
...@@ -276,6 +276,12 @@ void MCSClient::Initialize( ...@@ -276,6 +276,12 @@ void MCSClient::Initialize(
collapse_key_map_[collapse_key] = packet_info; collapse_key_map_[collapse_key] = packet_info;
} }
} }
// Establish if there is any custom client interval persisted from the last
// run and set it on the heartbeat manager.
custom_heartbeat_intervals_.swap(load_result->heartbeat_intervals);
int min_interval_ms = GetMinHeartbeatIntervalMs();
heartbeat_manager_.SetClientHeartbeatIntervalMs(min_interval_ms);
} }
void MCSClient::Login(uint64 android_id, uint64 security_token) { void MCSClient::Login(uint64 android_id, uint64 security_token) {
...@@ -380,19 +386,25 @@ void MCSClient::AddHeartbeatInterval(const std::string& scope, ...@@ -380,19 +386,25 @@ void MCSClient::AddHeartbeatInterval(const std::string& scope,
return; return;
custom_heartbeat_intervals_[scope] = interval_ms; custom_heartbeat_intervals_[scope] = interval_ms;
// TODO(fgorski): Save in the gcm store as well. gcm_store_->AddHeartbeatInterval(scope, interval_ms,
base::Bind(&MCSClient::OnGCMUpdateFinished,
weak_ptr_factory_.GetWeakPtr()));
int min_interval_ms = GetMinCustomHeartbeatInterval(); int min_interval_ms = GetMinHeartbeatIntervalMs();
heartbeat_manager_.SetClientHeartbeatIntervalMs(min_interval_ms); heartbeat_manager_.SetClientHeartbeatIntervalMs(min_interval_ms);
} }
void MCSClient::RemoveHeartbeatInterval(const std::string& scope) { void MCSClient::RemoveHeartbeatInterval(const std::string& scope) {
custom_heartbeat_intervals_.erase(scope); custom_heartbeat_intervals_.erase(scope);
int min_interval = GetMinCustomHeartbeatInterval(); gcm_store_->RemoveHeartbeatInterval(
scope, base::Bind(&MCSClient::OnGCMUpdateFinished,
weak_ptr_factory_.GetWeakPtr()));
int min_interval = GetMinHeartbeatIntervalMs();
heartbeat_manager_.SetClientHeartbeatIntervalMs(min_interval); heartbeat_manager_.SetClientHeartbeatIntervalMs(min_interval);
} }
int MCSClient::GetMinCustomHeartbeatInterval() { int MCSClient::GetMinHeartbeatIntervalMs() {
if (custom_heartbeat_intervals_.empty()) if (custom_heartbeat_intervals_.empty())
return kNoCustomHeartbeat; return kNoCustomHeartbeat;
......
...@@ -160,8 +160,8 @@ class GCM_EXPORT MCSClient { ...@@ -160,8 +160,8 @@ class GCM_EXPORT MCSClient {
void AddHeartbeatInterval(const std::string& scope, int interval_ms); void AddHeartbeatInterval(const std::string& scope, int interval_ms);
void RemoveHeartbeatInterval(const std::string& scope); void RemoveHeartbeatInterval(const std::string& scope);
HeartbeatManager& GetHeartbeatManagerForTesting() { HeartbeatManager* GetHeartbeatManagerForTesting() {
return heartbeat_manager_; return &heartbeat_manager_;
} }
private: private:
...@@ -225,8 +225,9 @@ class GCM_EXPORT MCSClient { ...@@ -225,8 +225,9 @@ class GCM_EXPORT MCSClient {
// any associated state). // any associated state).
MCSPacketInternal PopMessageForSend(); MCSPacketInternal PopMessageForSend();
// Gets the minimum interval from the map of scopes to intervals. // Gets the minimum interval from the map of scopes to intervals in
int GetMinCustomHeartbeatInterval(); // milliseconds.
int GetMinHeartbeatIntervalMs();
// Local version string. Sent on login. // Local version string. Sent on login.
const std::string version_string_; const std::string version_string_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment