blob: 286ed651f3bd75c838285545185c4e6319cdf0e9 [file] [log] [blame]
Alex Deymo5e3ea272016-01-28 13:42:23 -08001//
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#include "update_engine/update_attempter_android.h"
18
19#include <algorithm>
Alex Deymo218397f2016-02-04 23:55:10 -080020#include <map>
Alex Deymo5e3ea272016-01-28 13:42:23 -080021#include <utility>
22
23#include <base/bind.h>
24#include <base/logging.h>
Alex Deymo218397f2016-02-04 23:55:10 -080025#include <base/strings/string_number_conversions.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080026#include <brillo/bind_lambda.h>
Sen Jiang2703ef42017-03-16 13:36:21 -070027#include <brillo/data_encoding.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080028#include <brillo/message_loops/message_loop.h>
Alex Deymo218397f2016-02-04 23:55:10 -080029#include <brillo/strings/string_utils.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080030
31#include "update_engine/common/constants.h"
Alex Deymo2c131bb2016-05-26 16:43:13 -070032#include "update_engine/common/file_fetcher.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080033#include "update_engine/common/utils.h"
Alex Deymo03a4de72016-07-20 16:08:23 -070034#include "update_engine/daemon_state_interface.h"
Alex Deymo87792ea2016-07-25 15:40:36 -070035#include "update_engine/network_selector.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080036#include "update_engine/payload_consumer/download_action.h"
37#include "update_engine/payload_consumer/filesystem_verifier_action.h"
38#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo3b678db2016-02-09 11:50:06 -080039#include "update_engine/update_status_utils.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080040
Alex Deymo14c0da82016-07-20 16:45:45 -070041#ifndef _UE_SIDELOAD
42// Do not include support for external HTTP(s) urls when building
43// update_engine_sideload.
44#include "update_engine/libcurl_http_fetcher.h"
45#endif
46
Alex Deymo5e3ea272016-01-28 13:42:23 -080047using base::Bind;
48using base::TimeDelta;
49using base::TimeTicks;
50using std::shared_ptr;
51using std::string;
52using std::vector;
53
54namespace chromeos_update_engine {
55
56namespace {
57
Alex Deymo0d298542016-03-30 18:31:49 -070058// Minimum threshold to broadcast an status update in progress and time.
59const double kBroadcastThresholdProgress = 0.01; // 1%
60const int kBroadcastThresholdSeconds = 10;
61
Alex Deymo5e3ea272016-01-28 13:42:23 -080062const char* const kErrorDomain = "update_engine";
63// TODO(deymo): Convert the different errors to a numeric value to report them
64// back on the service error.
65const char* const kGenericError = "generic_error";
66
67// Log and set the error on the passed ErrorPtr.
68bool LogAndSetError(brillo::ErrorPtr* error,
69 const tracked_objects::Location& location,
70 const string& reason) {
71 brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason);
72 LOG(ERROR) << "Replying with failure: " << location.ToString() << ": "
73 << reason;
74 return false;
75}
76
77} // namespace
78
79UpdateAttempterAndroid::UpdateAttempterAndroid(
Alex Deymo03a4de72016-07-20 16:08:23 -070080 DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080081 PrefsInterface* prefs,
82 BootControlInterface* boot_control,
83 HardwareInterface* hardware)
84 : daemon_state_(daemon_state),
85 prefs_(prefs),
86 boot_control_(boot_control),
87 hardware_(hardware),
88 processor_(new ActionProcessor()) {
Alex Deymo87792ea2016-07-25 15:40:36 -070089 network_selector_ = network::CreateNetworkSelector();
Alex Deymo5e3ea272016-01-28 13:42:23 -080090}
91
92UpdateAttempterAndroid::~UpdateAttempterAndroid() {
93 // Release ourselves as the ActionProcessor's delegate to prevent
94 // re-scheduling the updates due to the processing stopped.
95 processor_->set_delegate(nullptr);
96}
97
98void UpdateAttempterAndroid::Init() {
99 // In case of update_engine restart without a reboot we need to restore the
100 // reboot needed state.
101 if (UpdateCompletedOnThisBoot())
Alex Deymo0e061ae2016-02-09 17:49:03 -0800102 SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800103 else
Alex Deymo0e061ae2016-02-09 17:49:03 -0800104 SetStatusAndNotify(UpdateStatus::IDLE);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800105}
106
107bool UpdateAttempterAndroid::ApplyPayload(
108 const string& payload_url,
109 int64_t payload_offset,
110 int64_t payload_size,
111 const vector<string>& key_value_pair_headers,
112 brillo::ErrorPtr* error) {
113 if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
114 return LogAndSetError(
115 error, FROM_HERE, "An update already applied, waiting for reboot");
116 }
117 if (ongoing_update_) {
118 return LogAndSetError(
119 error, FROM_HERE, "Already processing an update, cancel it first.");
120 }
121 DCHECK(status_ == UpdateStatus::IDLE);
122
Alex Deymo218397f2016-02-04 23:55:10 -0800123 std::map<string, string> headers;
124 for (const string& key_value_pair : key_value_pair_headers) {
125 string key;
126 string value;
127 if (!brillo::string_utils::SplitAtFirst(
128 key_value_pair, "=", &key, &value, false)) {
129 return LogAndSetError(
130 error, FROM_HERE, "Passed invalid header: " + key_value_pair);
131 }
132 if (!headers.emplace(key, value).second)
133 return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key);
134 }
135
136 // Unique identifier for the payload. An empty string means that the payload
137 // can't be resumed.
138 string payload_id = (headers[kPayloadPropertyFileHash] +
139 headers[kPayloadPropertyMetadataHash]);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800140
141 // Setup the InstallPlan based on the request.
142 install_plan_ = InstallPlan();
143
144 install_plan_.download_url = payload_url;
145 install_plan_.version = "";
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800146 base_offset_ = payload_offset;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800147 InstallPlan::Payload payload;
148 payload.size = payload_size;
149 if (!payload.size) {
Alex Deymo218397f2016-02-04 23:55:10 -0800150 if (!base::StringToUint64(headers[kPayloadPropertyFileSize],
Sen Jiang0affc2c2017-02-10 15:55:05 -0800151 &payload.size)) {
152 payload.size = 0;
Alex Deymo218397f2016-02-04 23:55:10 -0800153 }
154 }
Sen Jiang2703ef42017-03-16 13:36:21 -0700155 if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash],
Sen Jiang0affc2c2017-02-10 15:55:05 -0800156 &payload.hash)) {
Sen Jiang2703ef42017-03-16 13:36:21 -0700157 LOG(WARNING) << "Unable to decode base64 file hash: "
158 << headers[kPayloadPropertyFileHash];
159 }
Alex Deymo218397f2016-02-04 23:55:10 -0800160 if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize],
Sen Jiang0affc2c2017-02-10 15:55:05 -0800161 &payload.metadata_size)) {
162 payload.metadata_size = 0;
Alex Deymo218397f2016-02-04 23:55:10 -0800163 }
Sen Jiangcdd52062017-05-18 15:33:10 -0700164 // The |payload.type| is not used anymore since minor_version 3.
165 payload.type = InstallPayloadType::kUnknown;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800166 install_plan_.payloads.push_back(payload);
167
Alex Deymo5e3ea272016-01-28 13:42:23 -0800168 // The |public_key_rsa| key would override the public key stored on disk.
169 install_plan_.public_key_rsa = "";
170
171 install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild();
172 install_plan_.is_resume = !payload_id.empty() &&
173 DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
174 if (!install_plan_.is_resume) {
175 if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) {
176 LOG(WARNING) << "Unable to reset the update progress.";
177 }
178 if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) {
179 LOG(WARNING) << "Unable to save the update check response hash.";
180 }
181 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800182 install_plan_.source_slot = boot_control_->GetCurrentSlot();
183 install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
Alex Deymofb905d92016-06-03 19:26:58 -0700184
185 int data_wipe = 0;
186 install_plan_.powerwash_required =
187 base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) &&
188 data_wipe != 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800189
Alex Deymo87792ea2016-07-25 15:40:36 -0700190 NetworkId network_id = kDefaultNetworkId;
191 if (!headers[kPayloadPropertyNetworkId].empty()) {
192 if (!base::StringToUint64(headers[kPayloadPropertyNetworkId],
193 &network_id)) {
194 return LogAndSetError(
195 error,
196 FROM_HERE,
197 "Invalid network_id: " + headers[kPayloadPropertyNetworkId]);
198 }
199 if (!network_selector_->SetProcessNetwork(network_id)) {
200 LOG(WARNING) << "Unable to set network_id, continuing with the update.";
201 }
202 }
203
Alex Deymo5e3ea272016-01-28 13:42:23 -0800204 LOG(INFO) << "Using this install plan:";
205 install_plan_.Dump();
206
Alex Deymo2c131bb2016-05-26 16:43:13 -0700207 BuildUpdateActions(payload_url);
Alex Deymofdd6dec2016-03-03 22:35:43 -0800208 // Setup extra headers.
209 HttpFetcher* fetcher = download_action_->http_fetcher();
210 if (!headers[kPayloadPropertyAuthorization].empty())
211 fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]);
212 if (!headers[kPayloadPropertyUserAgent].empty())
213 fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]);
214
Alex Deymo5e3ea272016-01-28 13:42:23 -0800215 SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
Alex Deymof2858572016-02-25 11:20:13 -0800216 ongoing_update_ = true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800217
218 // Just in case we didn't update boot flags yet, make sure they're updated
219 // before any update processing starts. This will start the update process.
220 UpdateBootFlags();
221 return true;
222}
223
224bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800225 if (!ongoing_update_)
226 return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend.");
227 processor_->SuspendProcessing();
228 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800229}
230
231bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800232 if (!ongoing_update_)
233 return LogAndSetError(error, FROM_HERE, "No ongoing update to resume.");
234 processor_->ResumeProcessing();
235 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800236}
237
238bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800239 if (!ongoing_update_)
Alex Deymo5e3ea272016-01-28 13:42:23 -0800240 return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel.");
Alex Deymof2858572016-02-25 11:20:13 -0800241 processor_->StopProcessing();
242 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800243}
244
Alex Deymo3b678db2016-02-09 11:50:06 -0800245bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) {
246 LOG(INFO) << "Attempting to reset state from "
247 << UpdateStatusToString(status_) << " to UpdateStatus::IDLE";
248
249 switch (status_) {
250 case UpdateStatus::IDLE:
251 return true;
252
253 case UpdateStatus::UPDATED_NEED_REBOOT: {
254 // Remove the reboot marker so that if the machine is rebooted
255 // after resetting to idle state, it doesn't go back to
256 // UpdateStatus::UPDATED_NEED_REBOOT state.
257 bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId);
258
259 // Update the boot flags so the current slot has higher priority.
260 if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot()))
261 ret_value = false;
262
Alex Deymo52590332016-11-29 18:29:13 -0800263 // Mark the current slot as successful again, since marking it as active
264 // may reset the successful bit. We ignore the result of whether marking
265 // the current slot as successful worked.
266 if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){})))
267 ret_value = false;
268
Alex Deymo3b678db2016-02-09 11:50:06 -0800269 if (!ret_value) {
270 return LogAndSetError(
271 error,
272 FROM_HERE,
273 "Failed to reset the status to ");
274 }
275
276 SetStatusAndNotify(UpdateStatus::IDLE);
277 LOG(INFO) << "Reset status successful";
278 return true;
279 }
280
281 default:
282 return LogAndSetError(
283 error,
284 FROM_HERE,
285 "Reset not allowed in this state. Cancel the ongoing update first");
286 }
287}
288
Alex Deymo5e3ea272016-01-28 13:42:23 -0800289void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
290 ErrorCode code) {
291 LOG(INFO) << "Processing Done.";
292
Alex Deymo5990bf32016-07-19 17:01:41 -0700293 switch (code) {
294 case ErrorCode::kSuccess:
295 // Update succeeded.
296 WriteUpdateCompletedMarker();
297 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
298 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800299
Alex Deymo5990bf32016-07-19 17:01:41 -0700300 LOG(INFO) << "Update successfully applied, waiting to reboot.";
301 break;
302
303 case ErrorCode::kFilesystemCopierError:
304 case ErrorCode::kNewRootfsVerificationError:
305 case ErrorCode::kNewKernelVerificationError:
306 case ErrorCode::kFilesystemVerifierError:
307 case ErrorCode::kDownloadStateInitializationError:
308 // Reset the ongoing update for these errors so it starts from the
309 // beginning next time.
310 DeltaPerformer::ResetUpdateProgress(prefs_, false);
311 LOG(INFO) << "Resetting update progress.";
312 break;
313
314 default:
315 // Ignore all other error codes.
316 break;
Alex Deymo03a4de72016-07-20 16:08:23 -0700317 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800318
319 TerminateUpdateAndNotify(code);
320}
321
322void UpdateAttempterAndroid::ProcessingStopped(
323 const ActionProcessor* processor) {
324 TerminateUpdateAndNotify(ErrorCode::kUserCanceled);
325}
326
327void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor,
328 AbstractAction* action,
329 ErrorCode code) {
330 // Reset download progress regardless of whether or not the download
331 // action succeeded.
332 const string type = action->Type();
333 if (type == DownloadAction::StaticType()) {
Alex Deymo0d298542016-03-30 18:31:49 -0700334 download_progress_ = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800335 }
336 if (code != ErrorCode::kSuccess) {
337 // If an action failed, the ActionProcessor will cancel the whole thing.
338 return;
339 }
340 if (type == DownloadAction::StaticType()) {
341 SetStatusAndNotify(UpdateStatus::FINALIZING);
342 }
343}
344
345void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed,
346 uint64_t bytes_received,
347 uint64_t total) {
Alex Deymo0d298542016-03-30 18:31:49 -0700348 double progress = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800349 if (total)
350 progress = static_cast<double>(bytes_received) / static_cast<double>(total);
Alex Deymo0d298542016-03-30 18:31:49 -0700351 if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) {
Alex Deymo5e3ea272016-01-28 13:42:23 -0800352 download_progress_ = progress;
353 SetStatusAndNotify(UpdateStatus::DOWNLOADING);
Alex Deymo0d298542016-03-30 18:31:49 -0700354 } else {
355 ProgressUpdate(progress);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800356 }
357}
358
359bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) {
360 // TODO(deymo): Notify the DownloadAction that it should cancel the update
361 // download.
362 return false;
363}
364
365void UpdateAttempterAndroid::DownloadComplete() {
366 // Nothing needs to be done when the download completes.
367}
368
Alex Deymo0d298542016-03-30 18:31:49 -0700369void UpdateAttempterAndroid::ProgressUpdate(double progress) {
370 // Self throttle based on progress. Also send notifications if progress is
371 // too slow.
372 if (progress == 1.0 ||
373 progress - download_progress_ >= kBroadcastThresholdProgress ||
374 TimeTicks::Now() - last_notify_time_ >=
375 TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) {
376 download_progress_ = progress;
377 SetStatusAndNotify(status_);
378 }
379}
380
Alex Deymo5e3ea272016-01-28 13:42:23 -0800381void UpdateAttempterAndroid::UpdateBootFlags() {
382 if (updated_boot_flags_) {
383 LOG(INFO) << "Already updated boot flags. Skipping.";
384 CompleteUpdateBootFlags(true);
385 return;
386 }
387 // This is purely best effort.
388 LOG(INFO) << "Marking booted slot as good.";
389 if (!boot_control_->MarkBootSuccessfulAsync(
390 Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags,
391 base::Unretained(this)))) {
392 LOG(ERROR) << "Failed to mark current boot as successful.";
393 CompleteUpdateBootFlags(false);
394 }
395}
396
397void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) {
398 updated_boot_flags_ = true;
399 ScheduleProcessingStart();
400}
401
402void UpdateAttempterAndroid::ScheduleProcessingStart() {
403 LOG(INFO) << "Scheduling an action processor start.";
404 brillo::MessageLoop::current()->PostTask(
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700405 FROM_HERE,
406 Bind([](ActionProcessor* processor) { processor->StartProcessing(); },
407 base::Unretained(processor_.get())));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800408}
409
410void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) {
411 if (status_ == UpdateStatus::IDLE) {
412 LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called.";
413 return;
414 }
415
Alex Deymo0d298542016-03-30 18:31:49 -0700416 download_progress_ = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800417 actions_.clear();
418 UpdateStatus new_status =
419 (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT
420 : UpdateStatus::IDLE);
421 SetStatusAndNotify(new_status);
422 ongoing_update_ = false;
423
424 for (auto observer : daemon_state_->service_observers())
425 observer->SendPayloadApplicationComplete(error_code);
426}
427
428void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) {
429 status_ = status;
430 for (auto observer : daemon_state_->service_observers()) {
431 observer->SendStatusUpdate(
Sen Jiang0affc2c2017-02-10 15:55:05 -0800432 0, download_progress_, status_, "", install_plan_.payloads[0].size);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800433 }
434 last_notify_time_ = TimeTicks::Now();
435}
436
Alex Deymo2c131bb2016-05-26 16:43:13 -0700437void UpdateAttempterAndroid::BuildUpdateActions(const string& url) {
Alex Deymo5e3ea272016-01-28 13:42:23 -0800438 CHECK(!processor_->IsRunning());
439 processor_->set_delegate(this);
440
441 // Actions:
442 shared_ptr<InstallPlanAction> install_plan_action(
443 new InstallPlanAction(install_plan_));
444
Alex Deymo2c131bb2016-05-26 16:43:13 -0700445 HttpFetcher* download_fetcher = nullptr;
446 if (FileFetcher::SupportedUrl(url)) {
447 DLOG(INFO) << "Using FileFetcher for file URL.";
448 download_fetcher = new FileFetcher();
449 } else {
Alex Deymo14c0da82016-07-20 16:45:45 -0700450#ifdef _UE_SIDELOAD
451 LOG(FATAL) << "Unsupported sideload URI: " << url;
452#else
Alex Deymo2c131bb2016-05-26 16:43:13 -0700453 LibcurlHttpFetcher* libcurl_fetcher =
454 new LibcurlHttpFetcher(&proxy_resolver_, hardware_);
455 libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload);
456 download_fetcher = libcurl_fetcher;
Alex Deymo14c0da82016-07-20 16:45:45 -0700457#endif // _UE_SIDELOAD
Alex Deymo2c131bb2016-05-26 16:43:13 -0700458 }
Sen Jiang5ae865b2017-04-18 14:24:40 -0700459 shared_ptr<DownloadAction> download_action(
460 new DownloadAction(prefs_,
461 boot_control_,
462 hardware_,
463 nullptr, // system_state, not used.
464 download_fetcher)); // passes ownership
Sen Jiangfef85fd2016-03-25 15:32:49 -0700465 shared_ptr<FilesystemVerifierAction> filesystem_verifier_action(
Sen Jiange6e4bb92016-04-05 14:59:12 -0700466 new FilesystemVerifierAction());
Alex Deymo5e3ea272016-01-28 13:42:23 -0800467
468 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700469 new PostinstallRunnerAction(boot_control_, hardware_));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800470
471 download_action->set_delegate(this);
Sen Jiang5ae865b2017-04-18 14:24:40 -0700472 download_action->set_base_offset(base_offset_);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800473 download_action_ = download_action;
Alex Deymob6eef732016-06-10 12:58:11 -0700474 postinstall_runner_action->set_delegate(this);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800475
476 actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
477 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Sen Jiangfef85fd2016-03-25 15:32:49 -0700478 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800479 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
480
481 // Bond them together. We have to use the leaf-types when calling
482 // BondActions().
483 BondActions(install_plan_action.get(), download_action.get());
Sen Jiangfef85fd2016-03-25 15:32:49 -0700484 BondActions(download_action.get(), filesystem_verifier_action.get());
485 BondActions(filesystem_verifier_action.get(),
Alex Deymo5e3ea272016-01-28 13:42:23 -0800486 postinstall_runner_action.get());
487
488 // Enqueue the actions.
489 for (const shared_ptr<AbstractAction>& action : actions_)
490 processor_->EnqueueAction(action.get());
491}
492
Alex Deymo5e3ea272016-01-28 13:42:23 -0800493bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() {
494 string boot_id;
495 TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
496 prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id);
497 return true;
498}
499
500bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() {
501 // In case of an update_engine restart without a reboot, we stored the boot_id
502 // when the update was completed by setting a pref, so we can check whether
503 // the last update was on this boot or a previous one.
504 string boot_id;
505 TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
506
507 string update_completed_on_boot_id;
508 return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) &&
509 prefs_->GetString(kPrefsUpdateCompletedOnBootId,
510 &update_completed_on_boot_id) &&
511 update_completed_on_boot_id == boot_id);
512}
513
514} // namespace chromeos_update_engine