blob: 6b8c3613cfa34a5fc2b3913457a4a85a8f9492b6 [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(
38 new FilesystemCopierAction);
39 shared_ptr<DownloadAction> download_action(
40 new DownloadAction(new LibcurlHttpFetcher));
41 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
42 new PostinstallRunnerAction);
43 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
44 new SetBootableFlagAction);
45
46 response_handler_action_ = response_handler_action;
47
48 actions_.push_back(shared_ptr<AbstractAction>(request_prep_action));
49 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
50 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
51 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
52 actions_.push_back(shared_ptr<AbstractAction>(download_action));
53 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
54 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
55
56 // Enqueue the actions
57 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
58 it != actions_.end(); ++it) {
59 processor_.EnqueueAction(it->get());
60 }
61
62 // Bond them together. We have to use the leaf-types when calling
63 // BondActions().
64 BondActions(request_prep_action.get(), update_check_action.get());
65 BondActions(update_check_action.get(), response_handler_action.get());
66 BondActions(response_handler_action.get(), filesystem_copier_action.get());
67 BondActions(filesystem_copier_action.get(), download_action.get());
68 // TODO(adlr): Bond these actions together properly
69 // BondActions(download_action.get(), install_action.get());
70 // BondActions(install_action.get(), postinstall_runner_action.get());
71 BondActions(postinstall_runner_action.get(), set_bootable_flag_action.get());
72
73 processor_.StartProcessing();
74}
75
76// Delegate method:
77void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
78 bool success) {
79 CHECK(response_handler_action_);
80 if (response_handler_action_->GotNoUpdateResponse()) {
81 // All done.
82 g_main_loop_quit(loop_);
83 return;
84 }
85 if (!success) {
86 if (!full_update_) {
87 LOG(ERROR) << "Update failed. Attempting full update";
88 actions_.clear();
89 response_handler_action_.reset();
90 Update(true);
91 return;
92 } else {
93 LOG(ERROR) << "Full update failed. Aborting";
94 }
95 }
96 g_main_loop_quit(loop_);
97}
98
99// Stop updating. An attempt will be made to record status to the disk
100// so that updates can be resumed later.
101void UpdateAttempter::Terminate() {
102 // TODO(adlr): implement this method.
103 NOTIMPLEMENTED();
104}
105
106// Try to resume from a previously Terminate()d update.
107void UpdateAttempter::ResumeUpdating() {
108 // TODO(adlr): implement this method.
109 NOTIMPLEMENTED();
110}
111
112bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
113 double* progress,
114 std::string* current_operation,
115 std::string* new_version,
116 int64_t* new_size) {
117 // TODO(adlr): Return actual status.
118 *last_checked_time = 123;
119 *progress = 0.2223;
120 *current_operation = "DOWNLOADING";
121 *new_version = "0.2.3.8";
122 *new_size = 10002;
123 return true;
124}
125
126
127} // namespace chromeos_update_engine