next_update_check_policy_impl.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Copyright (C) 2017 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #ifndef UPDATE_ENGINE_UPDATE_MANAGER_NEXT_UPDATE_CHECK_POLICY_IMPL_H_
  17. #define UPDATE_ENGINE_UPDATE_MANAGER_NEXT_UPDATE_CHECK_POLICY_IMPL_H_
  18. #include <string>
  19. #include <base/time/time.h>
  20. #include "update_engine/update_manager/policy_utils.h"
  21. #include "update_engine/update_manager/prng.h"
  22. namespace chromeos_update_manager {
  23. // Constants that are provided to the policy implementation.
  24. struct NextUpdateCheckPolicyConstants {
  25. // Default update check timeout interval/fuzz values used to compute the
  26. // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
  27. // indicated value.
  28. int timeout_initial_interval;
  29. int timeout_periodic_interval;
  30. int timeout_max_backoff_interval;
  31. int timeout_regular_fuzz;
  32. // Maximum update attempt backoff interval and fuzz.
  33. int attempt_backoff_max_interval_in_days;
  34. int attempt_backoff_fuzz_in_hours;
  35. };
  36. // Ensure that periodic update checks are timed properly.
  37. class NextUpdateCheckTimePolicyImpl : public PolicyImplBase {
  38. public:
  39. explicit NextUpdateCheckTimePolicyImpl(
  40. const NextUpdateCheckPolicyConstants& constants);
  41. // Policy overrides.
  42. EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
  43. State* state,
  44. std::string* error,
  45. UpdateCheckParams* result) const override;
  46. // A private policy implementation returning the wallclock timestamp when
  47. // the next update check should happen.
  48. // TODO(garnold) We should probably change that to infer a monotonic
  49. // timestamp, which will make the update check intervals more resilient to
  50. // clock skews. Might require switching some of the variables exported by the
  51. // UpdaterProvider to report monotonic time, as well.
  52. //
  53. // NOTE:
  54. // Exposed as a public static so that it's logic can be used to test
  55. // Policy implementations that utilize this fragment for their
  56. // timing, without needing to list them all with FRIEND_TEST (so that
  57. // those Policy implementations can exist without modifying this
  58. // class's definition.
  59. //
  60. // The output value from this method (|next_update_check|), isn't
  61. // available via the UpdateCheckParams |result| of the Policy
  62. // method, and so this timing logic needs to be otherwise exposed.
  63. static EvalStatus NextUpdateCheckTime(
  64. EvaluationContext* ec,
  65. State* state,
  66. std::string* error,
  67. base::Time* next_update_check,
  68. const NextUpdateCheckPolicyConstants& constants);
  69. // Returns a TimeDelta based on the provided |interval| seconds +/- half
  70. // |fuzz| seconds. The return value is guaranteed to be a non-negative
  71. // TimeDelta.
  72. static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
  73. protected:
  74. std::string PolicyName() const override {
  75. return "NextUpdateCheckTimePolicyImpl";
  76. }
  77. private:
  78. const NextUpdateCheckPolicyConstants policy_constants_;
  79. DISALLOW_COPY_AND_ASSIGN(NextUpdateCheckTimePolicyImpl);
  80. };
  81. } // namespace chromeos_update_manager
  82. #endif // UPDATE_ENGINE_UPDATE_MANAGER_NEXT_UPDATE_CHECK_POLICY_IMPL_H_