blob: 84410addcc0005e7642504db3fed1238903454ea [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/update_attempter.h"
6#include <tr1/memory>
7#include <string>
8#include <vector>
9#include <glib.h>
10#include "update_engine/download_action.h"
11#include "update_engine/filesystem_copier_action.h"
12#include "update_engine/libcurl_http_fetcher.h"
13#include "update_engine/omaha_request_prep_action.h"
14#include "update_engine/omaha_response_handler_action.h"
15#include "update_engine/postinstall_runner_action.h"
16#include "update_engine/set_bootable_flag_action.h"
17#include "update_engine/update_check_action.h"
18
19using std::tr1::shared_ptr;
20using std::string;
21using std::vector;
22
23namespace chromeos_update_engine {
24
25void UpdateAttempter::Update(bool force_full_update) {
26 full_update_ = force_full_update;
27 CHECK(!processor_.IsRunning());
28 processor_.set_delegate(this);
29
30 // Actions:
31 shared_ptr<OmahaRequestPrepAction> request_prep_action(
32 new OmahaRequestPrepAction(force_full_update));
33 shared_ptr<UpdateCheckAction> update_check_action(
34 new UpdateCheckAction(new LibcurlHttpFetcher));
35 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
36 new OmahaResponseHandlerAction);
37 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -070038 new FilesystemCopierAction(false));
39 shared_ptr<FilesystemCopierAction> filesystem_copier_action_kernel(
40 new FilesystemCopierAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070041 shared_ptr<DownloadAction> download_action(
42 new DownloadAction(new LibcurlHttpFetcher));
43 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
44 new PostinstallRunnerAction);
45 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
46 new SetBootableFlagAction);
47
48 response_handler_action_ = response_handler_action;
49
50 actions_.push_back(shared_ptr<AbstractAction>(request_prep_action));
51 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
52 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
53 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -070054 actions_.push_back(shared_ptr<AbstractAction>(
55 filesystem_copier_action_kernel));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070056 actions_.push_back(shared_ptr<AbstractAction>(download_action));
57 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
58 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
59
60 // Enqueue the actions
61 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
62 it != actions_.end(); ++it) {
63 processor_.EnqueueAction(it->get());
64 }
65
66 // Bond them together. We have to use the leaf-types when calling
67 // BondActions().
68 BondActions(request_prep_action.get(), update_check_action.get());
69 BondActions(update_check_action.get(), response_handler_action.get());
70 BondActions(response_handler_action.get(), filesystem_copier_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070071 BondActions(response_handler_action.get(),
72 filesystem_copier_action_kernel.get());
73 BondActions(filesystem_copier_action_kernel.get(),
74 download_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070075 // TODO(adlr): Bond these actions together properly
76 // BondActions(download_action.get(), install_action.get());
77 // BondActions(install_action.get(), postinstall_runner_action.get());
78 BondActions(postinstall_runner_action.get(), set_bootable_flag_action.get());
79
80 processor_.StartProcessing();
81}
82
83// Delegate method:
84void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
85 bool success) {
86 CHECK(response_handler_action_);
87 if (response_handler_action_->GotNoUpdateResponse()) {
88 // All done.
89 g_main_loop_quit(loop_);
90 return;
91 }
92 if (!success) {
93 if (!full_update_) {
94 LOG(ERROR) << "Update failed. Attempting full update";
95 actions_.clear();
96 response_handler_action_.reset();
97 Update(true);
98 return;
99 } else {
100 LOG(ERROR) << "Full update failed. Aborting";
101 }
102 }
103 g_main_loop_quit(loop_);
104}
105
106// Stop updating. An attempt will be made to record status to the disk
107// so that updates can be resumed later.
108void UpdateAttempter::Terminate() {
109 // TODO(adlr): implement this method.
110 NOTIMPLEMENTED();
111}
112
113// Try to resume from a previously Terminate()d update.
114void UpdateAttempter::ResumeUpdating() {
115 // TODO(adlr): implement this method.
116 NOTIMPLEMENTED();
117}
118
119bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
120 double* progress,
121 std::string* current_operation,
122 std::string* new_version,
123 int64_t* new_size) {
124 // TODO(adlr): Return actual status.
125 *last_checked_time = 123;
126 *progress = 0.2223;
127 *current_operation = "DOWNLOADING";
128 *new_version = "0.2.3.8";
129 *new_size = 10002;
130 return true;
131}
132
133
134} // namespace chromeos_update_engine