Home | History | Annotate | Download | only in update_manager
      1 //
      2 // Copyright (C) 2014 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 
     17 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_
     18 #define UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_
     19 
     20 #include <string>
     21 
     22 #include <base/time/time.h>
     23 
     24 #include "update_engine/update_manager/next_update_check_policy_impl.h"
     25 #include "update_engine/update_manager/policy_utils.h"
     26 
     27 namespace chromeos_update_manager {
     28 
     29 // Output information from UpdateBackoffAndDownloadUrl.
     30 struct UpdateBackoffAndDownloadUrlResult {
     31   // Whether the failed attempt count (maintained by the caller) needs to be
     32   // incremented.
     33   bool do_increment_failures;
     34   // The current backoff expiry. Null if backoff is not in effect.
     35   base::Time backoff_expiry;
     36   // The new URL index to use and number of download errors associated with it.
     37   // Significant iff |do_increment_failures| is false and |backoff_expiry| is
     38   // null. Negative value means no usable URL was found.
     39   int url_idx;
     40   int url_num_errors;
     41 };
     42 
     43 // Parameters for update scattering, as returned by UpdateScattering.
     44 struct UpdateScatteringResult {
     45   bool is_scattering;
     46   base::TimeDelta wait_period;
     47   int check_threshold;
     48 };
     49 
     50 // ChromeOSPolicy implements the policy-related logic used in ChromeOS.
     51 class ChromeOSPolicy : public Policy {
     52  public:
     53   ChromeOSPolicy() {}
     54   ~ChromeOSPolicy() override {}
     55 
     56   // Policy overrides.
     57   EvalStatus UpdateCheckAllowed(
     58       EvaluationContext* ec, State* state, std::string* error,
     59       UpdateCheckParams* result) const override;
     60 
     61   EvalStatus UpdateCanBeApplied(
     62       EvaluationContext* ec,
     63       State* state,
     64       std::string* error,
     65       chromeos_update_engine::ErrorCode* result,
     66       chromeos_update_engine::InstallPlan* install_plan) const override;
     67 
     68   EvalStatus UpdateCanStart(
     69       EvaluationContext* ec,
     70       State* state,
     71       std::string* error,
     72       UpdateDownloadParams* result,
     73       UpdateState update_state) const override;
     74 
     75   EvalStatus UpdateDownloadAllowed(
     76       EvaluationContext* ec,
     77       State* state,
     78       std::string* error,
     79       bool* result) const override;
     80 
     81   EvalStatus P2PEnabled(
     82       EvaluationContext* ec,
     83       State* state,
     84       std::string* error,
     85       bool* result) const override;
     86 
     87   EvalStatus P2PEnabledChanged(
     88       EvaluationContext* ec,
     89       State* state,
     90       std::string* error,
     91       bool* result,
     92       bool prev_result) const override;
     93 
     94  protected:
     95   // Policy override.
     96   std::string PolicyName() const override { return "ChromeOSPolicy"; }
     97 
     98  private:
     99   friend class UmChromeOSPolicyTest;
    100   FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
    101   FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE);
    102   FRIEND_TEST(UmChromeOSPolicyTest,
    103               UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
    104   FRIEND_TEST(UmChromeOSPolicyTest,
    105               UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
    106   FRIEND_TEST(UmChromeOSPolicyTest,
    107               UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
    108   FRIEND_TEST(UmChromeOSPolicyTest,
    109               UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
    110   FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
    111   FRIEND_TEST(UmChromeOSPolicyTest,
    112               UpdateCanStartAllowedInteractivePreventsScattering);
    113   FRIEND_TEST(UmChromeOSPolicyTest,
    114               UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts);
    115   FRIEND_TEST(UmChromeOSPolicyTest,
    116               UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod);
    117 
    118   // Auxiliary constant (zero by default).
    119   const base::TimeDelta kZeroInterval;
    120 
    121   static const NextUpdateCheckPolicyConstants kNextUpdateCheckPolicyConstants;
    122 
    123   // Maximum number of times we'll allow using P2P for the same update payload.
    124   static const int kMaxP2PAttempts;
    125   // Maximum period of time allowed for download a payload via P2P, in seconds.
    126   static const int kMaxP2PAttemptsPeriodInSeconds;
    127 
    128   // A private policy for determining backoff and the download URL to use.
    129   // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used
    130   // for determining whether backoff is still in effect; if not,
    131   // |download_errors| is scanned past |failures_last_updated|, and a new
    132   // download URL from |download_urls| is found and written to |result->url_idx|
    133   // (-1 means no usable URL exists); |download_errors_max| determines the
    134   // maximum number of attempts per URL, according to the Omaha response. If an
    135   // update failure is identified then |result->do_increment_failures| is set to
    136   // true; if backoff is enabled, a new backoff period is computed (from the
    137   // time of failure) based on |num_failures|. Otherwise, backoff expiry is
    138   // nullified, indicating that no backoff is in effect.
    139   //
    140   // If backing off but the previous backoff expiry is unchanged, returns
    141   // |EvalStatus::kAskMeAgainLater|. Otherwise:
    142   //
    143   // * If backing off with a new expiry time, then |result->backoff_expiry| is
    144   //   set to this time.
    145   //
    146   // * Else, |result->backoff_expiry| is set to null, indicating that no backoff
    147   //   is in effect.
    148   //
    149   // In any of these cases, returns |EvalStatus::kSucceeded|. If an error
    150   // occurred, returns |EvalStatus::kFailed|.
    151   EvalStatus UpdateBackoffAndDownloadUrl(
    152       EvaluationContext* ec, State* state, std::string* error,
    153       UpdateBackoffAndDownloadUrlResult* result,
    154       const UpdateState& update_state) const;
    155 
    156   // A private policy for checking whether scattering is due. Writes in |result|
    157   // the decision as to whether or not to scatter; a wallclock-based scatter
    158   // wait period, which ranges from zero (do not wait) and no greater than the
    159   // current scatter factor provided by the device policy (if available) or the
    160   // maximum wait period determined by Omaha; and an update check-based
    161   // threshold between zero (no threshold) and the maximum number determined by
    162   // the update engine. Within |update_state|, |scatter_wait_period| should
    163   // contain the last scattering period returned by this function, or zero if no
    164   // wait period is known; |scatter_check_threshold| is the last update check
    165   // threshold, or zero if no such threshold is known. If not scattering, or if
    166   // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
    167   // otherwise, |EvalStatus::kAskMeAgainLater|.
    168   EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
    169                               std::string* error,
    170                               UpdateScatteringResult* result,
    171                               const UpdateState& update_state) const;
    172 
    173   DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
    174 };
    175 
    176 }  // namespace chromeos_update_manager
    177 
    178 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_
    179