Home | History | Annotate | Download | only in update_engine
      1 //
      2 // Copyright (C) 2016 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_ATTEMPTER_ANDROID_H_
     18 #define UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
     19 
     20 #include <stdint.h>
     21 
     22 #include <memory>
     23 #include <string>
     24 #include <vector>
     25 
     26 #include <base/time/time.h>
     27 
     28 #include "update_engine/client_library/include/update_engine/update_status.h"
     29 #include "update_engine/common/action_processor.h"
     30 #include "update_engine/common/boot_control_interface.h"
     31 #include "update_engine/common/cpu_limiter.h"
     32 #include "update_engine/common/hardware_interface.h"
     33 #include "update_engine/common/prefs_interface.h"
     34 #include "update_engine/daemon_state_interface.h"
     35 #include "update_engine/network_selector_interface.h"
     36 #include "update_engine/payload_consumer/download_action.h"
     37 #include "update_engine/payload_consumer/postinstall_runner_action.h"
     38 #include "update_engine/service_delegate_android_interface.h"
     39 #include "update_engine/service_observer_interface.h"
     40 
     41 namespace chromeos_update_engine {
     42 
     43 class UpdateAttempterAndroid
     44     : public ServiceDelegateAndroidInterface,
     45       public ActionProcessorDelegate,
     46       public DownloadActionDelegate,
     47       public PostinstallRunnerAction::DelegateInterface {
     48  public:
     49   using UpdateStatus = update_engine::UpdateStatus;
     50 
     51   UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
     52                          PrefsInterface* prefs,
     53                          BootControlInterface* boot_control_,
     54                          HardwareInterface* hardware_);
     55   ~UpdateAttempterAndroid() override;
     56 
     57   // Further initialization to be done post construction.
     58   void Init();
     59 
     60   // ServiceDelegateAndroidInterface overrides.
     61   bool ApplyPayload(const std::string& payload_url,
     62                     int64_t payload_offset,
     63                     int64_t payload_size,
     64                     const std::vector<std::string>& key_value_pair_headers,
     65                     brillo::ErrorPtr* error) override;
     66   bool SuspendUpdate(brillo::ErrorPtr* error) override;
     67   bool ResumeUpdate(brillo::ErrorPtr* error) override;
     68   bool CancelUpdate(brillo::ErrorPtr* error) override;
     69   bool ResetStatus(brillo::ErrorPtr* error) override;
     70 
     71   // ActionProcessorDelegate methods:
     72   void ProcessingDone(const ActionProcessor* processor,
     73                       ErrorCode code) override;
     74   void ProcessingStopped(const ActionProcessor* processor) override;
     75   void ActionCompleted(ActionProcessor* processor,
     76                        AbstractAction* action,
     77                        ErrorCode code) override;
     78 
     79   // DownloadActionDelegate overrides.
     80   void BytesReceived(uint64_t bytes_progressed,
     81                      uint64_t bytes_received,
     82                      uint64_t total) override;
     83   bool ShouldCancel(ErrorCode* cancel_reason) override;
     84   void DownloadComplete() override;
     85 
     86   // PostinstallRunnerAction::DelegateInterface
     87   void ProgressUpdate(double progress) override;
     88 
     89  private:
     90   // Asynchronously marks the current slot as successful if needed. If already
     91   // marked as good, CompleteUpdateBootFlags() is called starting the action
     92   // processor.
     93   void UpdateBootFlags();
     94 
     95   // Called when the boot flags have been updated.
     96   void CompleteUpdateBootFlags(bool success);
     97 
     98   // Schedules an event loop callback to start the action processor. This is
     99   // scheduled asynchronously to unblock the event loop.
    100   void ScheduleProcessingStart();
    101 
    102   // Notifies an update request completed with the given error |code| to all
    103   // observers.
    104   void TerminateUpdateAndNotify(ErrorCode error_code);
    105 
    106   // Sets the status to the given |status| and notifies a status update to
    107   // all observers.
    108   void SetStatusAndNotify(UpdateStatus status);
    109 
    110   // Helper method to construct the sequence of actions to be performed for
    111   // applying an update from the given |url|.
    112   void BuildUpdateActions(const std::string& url);
    113 
    114   // Sets up the download parameters based on the update requested on the
    115   // |install_plan_|.
    116   void SetupDownload();
    117 
    118   // Writes to the processing completed marker. Does nothing if
    119   // |update_completed_marker_| is empty.
    120   bool WriteUpdateCompletedMarker();
    121 
    122   // Returns whether an update was completed in the current boot.
    123   bool UpdateCompletedOnThisBoot();
    124 
    125   DaemonStateInterface* daemon_state_;
    126 
    127   // DaemonStateAndroid pointers.
    128   PrefsInterface* prefs_;
    129   BootControlInterface* boot_control_;
    130   HardwareInterface* hardware_;
    131 
    132   // Last status notification timestamp used for throttling. Use monotonic
    133   // TimeTicks to ensure that notifications are sent even if the system clock is
    134   // set back in the middle of an update.
    135   base::TimeTicks last_notify_time_;
    136 
    137   // The list of actions and action processor that runs them asynchronously.
    138   // Only used when |ongoing_update_| is true.
    139   std::vector<std::shared_ptr<AbstractAction>> actions_;
    140   std::unique_ptr<ActionProcessor> processor_;
    141 
    142   // Pointer to the DownloadAction in the actions_ vector.
    143   std::shared_ptr<DownloadAction> download_action_;
    144 
    145   // Whether there is an ongoing update. This implies that an update was started
    146   // but not finished yet. This value will be true even if the update was
    147   // suspended.
    148   bool ongoing_update_{false};
    149 
    150   // The InstallPlan used during the ongoing update.
    151   InstallPlan install_plan_;
    152 
    153   // For status:
    154   UpdateStatus status_{UpdateStatus::IDLE};
    155   double download_progress_{0.0};
    156 
    157   // The offset in the payload file where the CrAU part starts.
    158   int64_t base_offset_{0};
    159 
    160   // Only direct proxy supported.
    161   DirectProxyResolver proxy_resolver_;
    162 
    163   // CPU limiter during the update.
    164   CPULimiter cpu_limiter_;
    165 
    166   // Helper class to select the network to use during the update.
    167   std::unique_ptr<NetworkSelectorInterface> network_selector_;
    168 
    169   // Whether we have marked the current slot as good. This step is required
    170   // before applying an update to the other slot.
    171   bool updated_boot_flags_ = false;
    172 
    173   DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
    174 };
    175 
    176 }  // namespace chromeos_update_engine
    177 
    178 #endif  // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
    179