15e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
25e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Copyright (C) 2016 The Android Open Source Project
35e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
45e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Licensed under the Apache License, Version 2.0 (the "License");
55e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// you may not use this file except in compliance with the License.
65e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// You may obtain a copy of the License at
75e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
85e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//      http://www.apache.org/licenses/LICENSE-2.0
95e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Unless required by applicable law or agreed to in writing, software
115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// distributed under the License is distributed on an "AS IS" BASIS,
125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// See the License for the specific language governing permissions and
145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// limitations under the License.
155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#ifndef UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#define UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <stdint.h>
215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <memory>
235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <string>
245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <vector>
255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <base/time/time.h>
275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/client_library/include/update_engine/update_status.h"
295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/action_processor.h"
305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/boot_control_interface.h"
315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/cpu_limiter.h"
325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/hardware_interface.h"
335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/prefs_interface.h"
34578b787b0b83ae9a93ff77d4cd73d2f5d3e8994dAlex Deymo#include "update_engine/daemon_state_interface.h"
35b729f5c39691f254beb6fabc185a0db6e47542b9Alex Deymo#include "update_engine/network_selector_interface.h"
365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/download_action.h"
3772ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo#include "update_engine/payload_consumer/postinstall_runner_action.h"
385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/service_delegate_android_interface.h"
395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/service_observer_interface.h"
405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymonamespace chromeos_update_engine {
425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4372ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymoclass UpdateAttempterAndroid
4472ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo    : public ServiceDelegateAndroidInterface,
4572ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo      public ActionProcessorDelegate,
4672ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo      public DownloadActionDelegate,
4772ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo      public PostinstallRunnerAction::DelegateInterface {
485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo public:
495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  using UpdateStatus = update_engine::UpdateStatus;
505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
51578b787b0b83ae9a93ff77d4cd73d2f5d3e8994dAlex Deymo  UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
525e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                         PrefsInterface* prefs,
535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                         BootControlInterface* boot_control_,
545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                         HardwareInterface* hardware_);
555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  ~UpdateAttempterAndroid() override;
565e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
575e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Further initialization to be done post construction.
585e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void Init();
595e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // ServiceDelegateAndroidInterface overrides.
615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool ApplyPayload(const std::string& payload_url,
625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    int64_t payload_offset,
635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    int64_t payload_size,
645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    const std::vector<std::string>& key_value_pair_headers,
655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    brillo::ErrorPtr* error) override;
665e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool SuspendUpdate(brillo::ErrorPtr* error) override;
675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool ResumeUpdate(brillo::ErrorPtr* error) override;
685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool CancelUpdate(brillo::ErrorPtr* error) override;
6928618b1b79163b66a7c4623957b869901e4022d4Alex Deymo  bool ResetStatus(brillo::ErrorPtr* error) override;
705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // ActionProcessorDelegate methods:
725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void ProcessingDone(const ActionProcessor* processor,
735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                      ErrorCode code) override;
745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void ProcessingStopped(const ActionProcessor* processor) override;
755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void ActionCompleted(ActionProcessor* processor,
765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                       AbstractAction* action,
775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                       ErrorCode code) override;
785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
795e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // DownloadActionDelegate overrides.
805e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void BytesReceived(uint64_t bytes_progressed,
815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                     uint64_t bytes_received,
825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                     uint64_t total) override;
835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool ShouldCancel(ErrorCode* cancel_reason) override;
845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void DownloadComplete() override;
855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
8672ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo  // PostinstallRunnerAction::DelegateInterface
8772ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo  void ProgressUpdate(double progress) override;
8872ea95ab7705448b044cafc6b8cf2a2f4d929bd9Alex Deymo
895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo private:
905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Asynchronously marks the current slot as successful if needed. If already
915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // marked as good, CompleteUpdateBootFlags() is called starting the action
925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // processor.
935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void UpdateBootFlags();
945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Called when the boot flags have been updated.
965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void CompleteUpdateBootFlags(bool success);
975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Schedules an event loop callback to start the action processor. This is
995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // scheduled asynchronously to unblock the event loop.
1005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void ScheduleProcessingStart();
1015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Notifies an update request completed with the given error |code| to all
1035e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // observers.
1045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void TerminateUpdateAndNotify(ErrorCode error_code);
1055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Sets the status to the given |status| and notifies a status update to
1075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // all observers.
1085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void SetStatusAndNotify(UpdateStatus status);
1095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Helper method to construct the sequence of actions to be performed for
1116e3ffe73d27de744ef494292c003b5ba907799dfAlex Deymo  // applying an update from the given |url|.
1126e3ffe73d27de744ef494292c003b5ba907799dfAlex Deymo  void BuildUpdateActions(const std::string& url);
1135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Sets up the download parameters based on the update requested on the
1155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // |install_plan_|.
1165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  void SetupDownload();
1175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Writes to the processing completed marker. Does nothing if
1195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // |update_completed_marker_| is empty.
1205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool WriteUpdateCompletedMarker();
1215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Returns whether an update was completed in the current boot.
1235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool UpdateCompletedOnThisBoot();
1245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
125578b787b0b83ae9a93ff77d4cd73d2f5d3e8994dAlex Deymo  DaemonStateInterface* daemon_state_;
1265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // DaemonStateAndroid pointers.
1285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  PrefsInterface* prefs_;
1295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  BootControlInterface* boot_control_;
1305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  HardwareInterface* hardware_;
1315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Last status notification timestamp used for throttling. Use monotonic
1335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // TimeTicks to ensure that notifications are sent even if the system clock is
1345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // set back in the middle of an update.
1355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  base::TimeTicks last_notify_time_;
1365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // The list of actions and action processor that runs them asynchronously.
1385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Only used when |ongoing_update_| is true.
1395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  std::vector<std::shared_ptr<AbstractAction>> actions_;
1405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  std::unique_ptr<ActionProcessor> processor_;
1415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Pointer to the DownloadAction in the actions_ vector.
1435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  std::shared_ptr<DownloadAction> download_action_;
1445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Whether there is an ongoing update. This implies that an update was started
1465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // but not finished yet. This value will be true even if the update was
1475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // suspended.
1485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool ongoing_update_{false};
1495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // The InstallPlan used during the ongoing update.
1515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  InstallPlan install_plan_;
1525e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // For status:
1545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  UpdateStatus status_{UpdateStatus::IDLE};
1555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  double download_progress_{0.0};
1565e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1570fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo  // The offset in the payload file where the CrAU part starts.
1580fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo  int64_t base_offset_{0};
1590fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo
1605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Only direct proxy supported.
1615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  DirectProxyResolver proxy_resolver_;
1625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // CPU limiter during the update.
1645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  CPULimiter cpu_limiter_;
1655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
166b729f5c39691f254beb6fabc185a0db6e47542b9Alex Deymo  // Helper class to select the network to use during the update.
167b729f5c39691f254beb6fabc185a0db6e47542b9Alex Deymo  std::unique_ptr<NetworkSelectorInterface> network_selector_;
168b729f5c39691f254beb6fabc185a0db6e47542b9Alex Deymo
1695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Whether we have marked the current slot as good. This step is required
1705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // before applying an update to the other slot.
1715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  bool updated_boot_flags_ = false;
1725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
1745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo};
1755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}  // namespace chromeos_update_engine
1775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#endif  // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
179