PolicyManager: UpdateCheckAllowed policy initial implementation.

This patch implements the UpdateCheckAllowed policy for ChromeOS
using the same logic we had on update_check_scheduler.cc. It checks
for updates onces every 45 minutes and does an exponential backoff
up to 4 hours when the update check fails. Some other parts of the
policy are not implemented, such as retry an update check with a
short delay on certain failures.

BUG=chromium:358269
TEST=Unittests added to the policy.

Change-Id: Ief8deff47fd6490bd70a22ba20abed05fcc37ab4
Reviewed-on: https://chromium-review.googlesource.com/197595
Reviewed-by: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/chromeos_policy.h b/policy_manager/chromeos_policy.h
index 3ade8d0..09d937e 100644
--- a/policy_manager/chromeos_policy.h
+++ b/policy_manager/chromeos_policy.h
@@ -5,7 +5,10 @@
 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_CHROMEOS_POLICY_H_
 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_CHROMEOS_POLICY_H_
 
+#include <gtest/gtest_prod.h>  // for FRIEND_TEST
+
 #include "update_engine/policy_manager/policy.h"
+#include "update_engine/policy_manager/prng.h"
 
 namespace chromeos_policy_manager {
 
@@ -16,9 +19,9 @@
   virtual ~ChromeOSPolicy() {}
 
   // Policy overrides.
-  virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
-                                        std::string* error,
-                                        bool* result) const override;
+  virtual EvalStatus UpdateCheckAllowed(
+      EvaluationContext* ec, State* state, std::string* error,
+      UpdateCheckParams* result) const override;
 
   virtual EvalStatus UpdateDownloadAndApplyAllowed(EvaluationContext* ec,
                                                    State* state,
@@ -26,6 +29,31 @@
                                                    bool* result) const override;
 
  private:
+  FRIEND_TEST(PmChromeOSPolicyTest,
+              FirstCheckIsAtMostInitialIntervalAfterStart);
+  FRIEND_TEST(PmChromeOSPolicyTest, ExponentialBackoffIsCapped);
+  FRIEND_TEST(PmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
+
+  // Default update check timeout interval/fuzz values used to compute the
+  // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
+  // indicated value.
+  static const int kTimeoutInitialInterval    =  7 * 60;
+  static const int kTimeoutPeriodicInterval   = 45 * 60;
+  static const int kTimeoutQuickInterval      =  1 * 60;
+  static const int kTimeoutMaxBackoffInterval =  4 * 60 * 60;
+  static const int kTimeoutRegularFuzz        = 10 * 60;
+
+  // A private policy implementation returning the wallclock timestamp when
+  // the next update check should happen.
+  EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
+                                 std::string* error,
+                                 base::Time* next_update_check) const;
+
+  // Returns a TimeDelta based on the provided |interval| seconds +/- half
+  // |fuzz| seconds. The return value is guaranteed to be a non-negative
+  // TimeDelta.
+  static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
+
   DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
 };