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

Unified Diff: chrome/browser/policy/cloud_policy_refresh_scheduler.cc

Issue 12189011: Split up chrome/browser/policy subdirectory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase, add chrome/browser/chromeos/policy/OWNERS Created 7 years, 9 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_policy_refresh_scheduler.cc
diff --git a/chrome/browser/policy/cloud_policy_refresh_scheduler.cc b/chrome/browser/policy/cloud_policy_refresh_scheduler.cc
deleted file mode 100644
index 2ed065215a2e0d71a2783f872cd5656c957a22de..0000000000000000000000000000000000000000
--- a/chrome/browser/policy/cloud_policy_refresh_scheduler.cc
+++ /dev/null
@@ -1,232 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/policy/cloud_policy_refresh_scheduler.h"
-
-#include <algorithm>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/sequenced_task_runner.h"
-#include "base/time/default_tick_clock.h"
-#include "base/time/tick_clock.h"
-#include "chrome/browser/policy/cloud_policy_constants.h"
-#include "chrome/common/chrome_notification_types.h"
-#include "content/public/browser/notification_details.h"
-
-namespace policy {
-
-namespace {
-
-// The maximum rate at which to refresh policies.
-const size_t kMaxRefreshesPerHour = 5;
-
-} // namespace
-
-const int64 CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs =
- 3 * 60 * 60 * 1000; // 3 hours.
-const int64 CloudPolicyRefreshScheduler::kUnmanagedRefreshDelayMs =
- 24 * 60 * 60 * 1000; // 1 day.
-const int64 CloudPolicyRefreshScheduler::kInitialErrorRetryDelayMs =
- 5 * 60 * 1000; // 5 minutes.
-const int64 CloudPolicyRefreshScheduler::kRefreshDelayMinMs =
- 30 * 60 * 1000; // 30 minutes.
-const int64 CloudPolicyRefreshScheduler::kRefreshDelayMaxMs =
- 24 * 60 * 60 * 1000; // 1 day.
-
-CloudPolicyRefreshScheduler::CloudPolicyRefreshScheduler(
- CloudPolicyClient* client,
- CloudPolicyStore* store,
- const scoped_refptr<base::SequencedTaskRunner>& task_runner)
- : client_(client),
- store_(store),
- task_runner_(task_runner),
- error_retry_delay_ms_(kInitialErrorRetryDelayMs),
- refresh_delay_ms_(kDefaultRefreshDelayMs),
- rate_limiter_(kMaxRefreshesPerHour,
- base::TimeDelta::FromHours(1),
- base::Bind(&CloudPolicyRefreshScheduler::RefreshNow,
- base::Unretained(this)),
- task_runner_,
- scoped_ptr<base::TickClock>(new base::DefaultTickClock())) {
- client_->AddObserver(this);
- store_->AddObserver(this);
- net::NetworkChangeNotifier::AddIPAddressObserver(this);
-
- UpdateLastRefreshFromPolicy();
- ScheduleRefresh();
-}
-
-CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() {
- store_->RemoveObserver(this);
- client_->RemoveObserver(this);
- net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
-}
-
-void CloudPolicyRefreshScheduler::SetRefreshDelay(int64 refresh_delay) {
- refresh_delay_ms_ = std::min(std::max(refresh_delay, kRefreshDelayMinMs),
- kRefreshDelayMaxMs);
- ScheduleRefresh();
-}
-
-void CloudPolicyRefreshScheduler::RefreshSoon() {
- rate_limiter_.PostRequest();
-}
-
-void CloudPolicyRefreshScheduler::OnPolicyFetched(CloudPolicyClient* client) {
- error_retry_delay_ms_ = kInitialErrorRetryDelayMs;
-
- // Schedule the next refresh.
- last_refresh_ = base::Time::NowFromSystemTime();
- ScheduleRefresh();
-}
-
-void CloudPolicyRefreshScheduler::OnRegistrationStateChanged(
- CloudPolicyClient* client) {
- error_retry_delay_ms_ = kInitialErrorRetryDelayMs;
-
- // The client might have registered, so trigger an immediate refresh.
- RefreshNow();
-}
-
-void CloudPolicyRefreshScheduler::OnClientError(CloudPolicyClient* client) {
- // Save the status for below.
- DeviceManagementStatus status = client_->status();
-
- // Schedule an error retry if applicable.
- last_refresh_ = base::Time::NowFromSystemTime();
- ScheduleRefresh();
-
- // Update the retry delay.
- if (client->is_registered() &&
- (status == DM_STATUS_REQUEST_FAILED ||
- status == DM_STATUS_TEMPORARY_UNAVAILABLE)) {
- error_retry_delay_ms_ = std::min(error_retry_delay_ms_ * 2,
- refresh_delay_ms_);
- } else {
- error_retry_delay_ms_ = kInitialErrorRetryDelayMs;
- }
-}
-
-void CloudPolicyRefreshScheduler::OnStoreLoaded(CloudPolicyStore* store) {
- UpdateLastRefreshFromPolicy();
-
- // Re-schedule the next refresh in case the is_managed bit changed.
- ScheduleRefresh();
-}
-
-void CloudPolicyRefreshScheduler::OnStoreError(CloudPolicyStore* store) {
- // If |store_| fails, the is_managed bit that it provides may become stale.
- // The best guess in that situation is to assume is_managed didn't change and
- // continue using the stale information. Thus, no specific response to a store
- // error is required. NB: Changes to is_managed fire OnStoreLoaded().
-}
-
-void CloudPolicyRefreshScheduler::OnIPAddressChanged() {
- if (client_->status() == DM_STATUS_REQUEST_FAILED)
- RefreshAfter(0);
-}
-
-void CloudPolicyRefreshScheduler::UpdateLastRefreshFromPolicy() {
- if (!last_refresh_.is_null())
- return;
-
- // If the client has already fetched policy, assume that happened recently. If
- // that assumption ever breaks, the proper thing to do probably is to move the
- // |last_refresh_| bookkeeping to CloudPolicyClient.
- if (!client_->responses().empty()) {
- last_refresh_ = base::Time::NowFromSystemTime();
- return;
- }
-
- // If there is a cached non-managed response, make sure to only re-query the
- // server after kUnmanagedRefreshDelayMs. NB: For existing policy, an
- // immediate refresh is intentional.
- if (store_->has_policy() && !store_->is_managed()) {
- last_refresh_ =
- base::Time::UnixEpoch() +
- base::TimeDelta::FromMilliseconds(store_->policy()->timestamp());
- }
-}
-
-void CloudPolicyRefreshScheduler::RefreshNow() {
- last_refresh_ = base::Time();
- ScheduleRefresh();
-}
-
-void CloudPolicyRefreshScheduler::ScheduleRefresh() {
- // If the client isn't registered, there is nothing to do.
- if (!client_->is_registered()) {
- refresh_callback_.Cancel();
- return;
- }
-
- // If there is a registration, go by the client's status. That will tell us
- // what the appropriate refresh delay should be.
- switch (client_->status()) {
- case DM_STATUS_SUCCESS:
- if (store_->is_managed())
- RefreshAfter(refresh_delay_ms_);
- else
- RefreshAfter(kUnmanagedRefreshDelayMs);
- return;
- case DM_STATUS_SERVICE_ACTIVATION_PENDING:
- case DM_STATUS_SERVICE_POLICY_NOT_FOUND:
- RefreshAfter(refresh_delay_ms_);
- return;
- case DM_STATUS_REQUEST_FAILED:
- case DM_STATUS_TEMPORARY_UNAVAILABLE:
- RefreshAfter(error_retry_delay_ms_);
- return;
- case DM_STATUS_REQUEST_INVALID:
- case DM_STATUS_HTTP_STATUS_ERROR:
- case DM_STATUS_RESPONSE_DECODING_ERROR:
- case DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED:
- RefreshAfter(kUnmanagedRefreshDelayMs);
- return;
- case DM_STATUS_SERVICE_MANAGEMENT_TOKEN_INVALID:
- case DM_STATUS_SERVICE_DEVICE_NOT_FOUND:
- case DM_STATUS_SERVICE_INVALID_SERIAL_NUMBER:
- case DM_STATUS_SERVICE_DEVICE_ID_CONFLICT:
- case DM_STATUS_SERVICE_MISSING_LICENSES:
- // Need a re-registration, no use in retrying.
- return;
- }
-
- NOTREACHED() << "Invalid client status " << client_->status();
- RefreshAfter(kUnmanagedRefreshDelayMs);
-}
-
-void CloudPolicyRefreshScheduler::PerformRefresh() {
- if (client_->is_registered()) {
- // Update |last_refresh_| so another fetch isn't triggered inadvertently.
- last_refresh_ = base::Time::NowFromSystemTime();
-
- // The result of this operation will be reported through a callback, at
- // which point the next refresh will be scheduled.
- client_->FetchPolicy();
- return;
- }
-
- // This should never happen, as the registration change should have been
- // handled via OnRegistrationStateChanged().
- NOTREACHED();
-}
-
-void CloudPolicyRefreshScheduler::RefreshAfter(int delta_ms) {
- base::TimeDelta delta(base::TimeDelta::FromMilliseconds(delta_ms));
- refresh_callback_.Cancel();
-
- // Schedule the callback.
- base::TimeDelta delay =
- std::max((last_refresh_ + delta) - base::Time::NowFromSystemTime(),
- base::TimeDelta());
- refresh_callback_.Reset(
- base::Bind(&CloudPolicyRefreshScheduler::PerformRefresh,
- base::Unretained(this)));
- task_runner_->PostDelayedTask(FROM_HERE, refresh_callback_.callback(), delay);
-}
-
-} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698