Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Unified Diff: chrome/browser/policy/cloud/cloud_policy_invalidator.cc

Issue 23592017: Fix policy invalidator lifecycle bugs for Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/cloud/cloud_policy_invalidator.cc
diff --git a/chrome/browser/policy/cloud/cloud_policy_invalidator.cc b/chrome/browser/policy/cloud/cloud_policy_invalidator.cc
index 5012fe7a3998a386029a0b6b5f9782b03f6b487d..e880225811197a249a383bcd723454db84753dc8 100644
--- a/chrome/browser/policy/cloud/cloud_policy_invalidator.cc
+++ b/chrome/browser/policy/cloud/cloud_policy_invalidator.cc
@@ -14,7 +14,8 @@
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/invalidation/invalidation_service.h"
-#include "chrome/browser/invalidation/invalidation_service_factory.h"
+#include "chrome/browser/policy/cloud/cloud_policy_client.h"
+#include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h"
#include "chrome/browser/policy/cloud/enterprise_metrics.h"
#include "chrome/common/chrome_switches.h"
#include "policy/policy_constants.h"
@@ -28,13 +29,11 @@ const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000;
const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000;
CloudPolicyInvalidator::CloudPolicyInvalidator(
- CloudPolicyInvalidationHandler* invalidation_handler,
- CloudPolicyStore* store,
+ CloudPolicyCore* core,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
- : invalidation_handler_(invalidation_handler),
- store_(store),
+ : state_(UNINITIALIZED),
+ core_(core),
task_runner_(task_runner),
- profile_(NULL),
invalidation_service_(NULL),
invalidations_enabled_(false),
invalidation_service_enabled_(false),
@@ -44,40 +43,45 @@ CloudPolicyInvalidator::CloudPolicyInvalidator(
unknown_version_invalidation_count_(0),
ack_handle_(syncer::AckHandle::InvalidAckHandle()),
weak_factory_(this),
- max_fetch_delay_(kMaxFetchDelayDefault) {
- DCHECK(invalidation_handler);
- DCHECK(store);
+ max_fetch_delay_(kMaxFetchDelayDefault),
+ policy_refresh_count_(0) {
+ DCHECK(core);
DCHECK(task_runner.get());
- DCHECK(!IsInitialized());
}
-CloudPolicyInvalidator::~CloudPolicyInvalidator() {}
-
-void CloudPolicyInvalidator::InitializeWithProfile(Profile* profile) {
- DCHECK(!IsInitialized());
- DCHECK(profile);
- profile_ = profile;
- Initialize();
+CloudPolicyInvalidator::~CloudPolicyInvalidator() {
+ DCHECK(state_ == SHUT_DOWN);
}
-void CloudPolicyInvalidator::InitializeWithService(
+void CloudPolicyInvalidator::Initialize(
invalidation::InvalidationService* invalidation_service) {
- DCHECK(!IsInitialized());
+ DCHECK(state_ == UNINITIALIZED);
+ DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(invalidation_service);
invalidation_service_ = invalidation_service;
- Initialize();
+ state_ = STOPPED;
+ core_->AddObserver(this);
+ if (core_->refresh_scheduler())
+ OnRefreshSchedulerStarted(core_);
}
void CloudPolicyInvalidator::Shutdown() {
- if (IsInitialized()) {
+ DCHECK(state_ != SHUT_DOWN);
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (state_ == STARTED) {
if (registered_timestamp_)
invalidation_service_->UnregisterInvalidationHandler(this);
- store_->RemoveObserver(this);
+ core_->store()->RemoveObserver(this);
+ weak_factory_.InvalidateWeakPtrs();
}
+ if (state_ != UNINITIALIZED)
+ core_->RemoveObserver(this);
+ state_ = SHUT_DOWN;
}
void CloudPolicyInvalidator::OnInvalidatorStateChange(
syncer::InvalidatorState state) {
+ DCHECK(state_ == STARTED);
DCHECK(thread_checker_.CalledOnValidThread());
invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED;
UpdateInvalidationsEnabled();
@@ -85,6 +89,7 @@ void CloudPolicyInvalidator::OnInvalidatorStateChange(
void CloudPolicyInvalidator::OnIncomingInvalidation(
const syncer::ObjectIdInvalidationMap& invalidation_map) {
+ DCHECK(state_ == STARTED);
DCHECK(thread_checker_.CalledOnValidThread());
const syncer::ObjectIdInvalidationMap::const_iterator invalidation =
invalidation_map.find(object_id_);
@@ -95,8 +100,28 @@ void CloudPolicyInvalidator::OnIncomingInvalidation(
HandleInvalidation(invalidation->second);
}
+void CloudPolicyInvalidator::OnCoreConnected(CloudPolicyCore* core) {}
+
+void CloudPolicyInvalidator::OnRefreshSchedulerStarted(CloudPolicyCore* core) {
+ DCHECK(state_ == STOPPED);
+ DCHECK(thread_checker_.CalledOnValidThread());
+ state_ = STARTED;
+ OnStoreLoaded(core_->store());
+ core_->store()->AddObserver(this);
+}
+
+void CloudPolicyInvalidator::OnCoreDisconnecting(CloudPolicyCore* core) {
+ DCHECK(state_ == STARTED || state_ == STOPPED);
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (state_ == STARTED) {
+ Unregister();
+ core_->store()->RemoveObserver(this);
+ state_ = STOPPED;
+ }
+}
+
void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) {
- DCHECK(IsInitialized());
+ DCHECK(state_ == STARTED);
DCHECK(thread_checker_.CalledOnValidThread());
if (registered_timestamp_) {
// Update the kMetricPolicyRefresh histogram. In some cases, this object can
@@ -124,21 +149,6 @@ void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) {
void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {}
-base::WeakPtr<CloudPolicyInvalidator> CloudPolicyInvalidator::GetWeakPtr() {
- DCHECK(!IsInitialized());
- return weak_factory_.GetWeakPtr();
-}
-
-void CloudPolicyInvalidator::Initialize() {
- OnStoreLoaded(store_);
- store_->AddObserver(this);
-}
-
-bool CloudPolicyInvalidator::IsInitialized() {
- // Could have been initialized with a profile or invalidation service.
- return profile_ || invalidation_service_;
-}
-
void CloudPolicyInvalidator::HandleInvalidation(
const syncer::Invalidation& invalidation) {
// The invalidation service may send an invalidation more than once if there
@@ -171,20 +181,20 @@ void CloudPolicyInvalidator::HandleInvalidation(
base::TimeDelta delay = base::TimeDelta::FromMilliseconds(
base::RandInt(20, max_fetch_delay_));
- // If there is a payload, the invalidate callback can run at any time, so set
- // the version and payload on the client immediately. Otherwise, the callback
+ // If there is a payload, the policy can be refreshed at any time, so set
+ // the version and payload on the client immediately. Otherwise, the refresh
// must only run after at least kMissingPayloadDelay minutes.
const std::string& payload = invalidation.payload;
if (!invalidation.payload.empty())
- invalidation_handler_->SetInvalidationInfo(invalidation_version_, payload);
+ core_->client()->SetInvalidationInfo(invalidation_version_, payload);
else
delay += base::TimeDelta::FromMinutes(kMissingPayloadDelay);
- // Schedule the invalidate callback to run.
+ // Schedule the policy to be refreshed.
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(
- &CloudPolicyInvalidator::RunInvalidateCallback,
+ &CloudPolicyInvalidator::RefreshPolicy,
weak_factory_.GetWeakPtr(),
payload.empty() /* is_missing_payload */),
delay);
@@ -217,15 +227,6 @@ void CloudPolicyInvalidator::UpdateRegistration(
void CloudPolicyInvalidator::Register(
int64 timestamp,
const invalidation::ObjectId& object_id) {
- // Get the invalidation service from the profile if needed.
- if (!invalidation_service_) {
- DCHECK(profile_);
- invalidation_service_ =
- invalidation::InvalidationServiceFactory::GetForProfile(profile_);
- if (!invalidation_service_)
- return;
- }
-
// Register this handler with the invalidation service if needed.
if (!registered_timestamp_) {
OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState());
@@ -295,33 +296,32 @@ void CloudPolicyInvalidator::UpdateInvalidationsEnabled() {
invalidation_service_enabled_ && registered_timestamp_;
if (invalidations_enabled_ != invalidations_enabled) {
invalidations_enabled_ = invalidations_enabled;
- invalidation_handler_->OnInvalidatorStateChanged(invalidations_enabled);
+ core_->refresh_scheduler()->SetInvalidationServiceAvailability(
+ invalidations_enabled);
}
}
-void CloudPolicyInvalidator::RunInvalidateCallback(bool is_missing_payload) {
+void CloudPolicyInvalidator::RefreshPolicy(bool is_missing_payload) {
DCHECK(thread_checker_.CalledOnValidThread());
// In the missing payload case, the invalidation version has not been set on
// the client yet, so set it now that the required time has elapsed.
- if (is_missing_payload) {
- invalidation_handler_->SetInvalidationInfo(
- invalidation_version_,
- std::string());
- }
- invalidation_handler_->InvalidatePolicy();
+ if (is_missing_payload)
+ core_->client()->SetInvalidationInfo(invalidation_version_, std::string());
+ core_->refresh_scheduler()->RefreshSoon();
+ policy_refresh_count_++;
}
void CloudPolicyInvalidator::AcknowledgeInvalidation() {
DCHECK(invalid_);
invalid_ = false;
- invalidation_handler_->SetInvalidationInfo(0, std::string());
+ core_->client()->SetInvalidationInfo(0, std::string());
invalidation_service_->AcknowledgeInvalidation(object_id_, ack_handle_);
- // Cancel any scheduled invalidate callbacks.
+ // Cancel any scheduled policy refreshes.
weak_factory_.InvalidateWeakPtrs();
}
int CloudPolicyInvalidator::GetPolicyRefreshMetric() {
- if (store_->policy_changed()) {
+ if (core_->store()->policy_changed()) {
if (invalid_)
return METRIC_POLICY_REFRESH_INVALIDATED_CHANGED;
if (invalidations_enabled_)

Powered by Google App Engine
This is Rietveld 408576698