blob: f90b90ef64a4f4d499880b055f014b9ac5f373d6 [file] [log] [blame]
Jay Srinivasane73acab2012-07-10 14:34:03 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07002// 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/dbus_service.h"
Darin Petkova07586b2010-10-20 13:41:15 -07006
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07007#include <string>
Darin Petkova07586b2010-10-20 13:41:15 -07008
9#include <base/logging.h>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070010#include <policy/device_policy.h>
Darin Petkova07586b2010-10-20 13:41:15 -070011
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070012#include "update_engine/marshal.glibmarshal.h"
Darin Petkov49d91322010-10-25 16:34:58 -070013#include "update_engine/omaha_request_params.h"
Darin Petkova07586b2010-10-20 13:41:15 -070014#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070015
16using std::string;
17
Darin Petkov820a77b2011-04-27 16:48:58 -070018static const char kAUTestURLRequest[] = "autest";
Jay Srinivasane73acab2012-07-10 14:34:03 -070019// By default autest bypasses scattering. If we want to test scattering,
20// we should use autest-scheduled. The Url used is same in both cases, but
21// different params are passed to CheckForUpdate method.
22static const char kScheduledAUTestURLRequest[] = "autest-scheduled";
23
Darin Petkov820a77b2011-04-27 16:48:58 -070024static const char kAUTestURL[] =
Darin Petkov5445e162011-11-04 10:10:27 +010025 "https://omaha.sandbox.google.com/service/update2";
Darin Petkov820a77b2011-04-27 16:48:58 -070026
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070027G_DEFINE_TYPE(UpdateEngineService, update_engine_service, G_TYPE_OBJECT)
28
29static void update_engine_service_finalize(GObject* object) {
30 G_OBJECT_CLASS(update_engine_service_parent_class)->finalize(object);
31}
32
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070033static guint status_update_signal = 0;
34
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070035static void update_engine_service_class_init(UpdateEngineServiceClass* klass) {
36 GObjectClass *object_class;
37 object_class = G_OBJECT_CLASS(klass);
38 object_class->finalize = update_engine_service_finalize;
Darin Petkov5a7f5652010-07-22 21:40:09 -070039
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040 status_update_signal = g_signal_new(
41 "status_update",
42 G_OBJECT_CLASS_TYPE(klass),
43 G_SIGNAL_RUN_LAST,
44 0, // 0 == no class method associated
45 NULL, // Accumulator
46 NULL, // Accumulator data
47 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
48 G_TYPE_NONE, // Return type
49 5, // param count:
50 G_TYPE_INT64,
51 G_TYPE_DOUBLE,
52 G_TYPE_STRING,
53 G_TYPE_STRING,
54 G_TYPE_INT64);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070055}
56
57static void update_engine_service_init(UpdateEngineService* object) {
58}
59
60UpdateEngineService* update_engine_service_new(void) {
61 return reinterpret_cast<UpdateEngineService*>(
62 g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
63}
64
Darin Petkov296889c2010-07-23 16:20:54 -070065gboolean update_engine_service_attempt_update(UpdateEngineService* self,
66 gchar* app_version,
67 gchar* omaha_url,
68 GError **error) {
Darin Petkova07586b2010-10-20 13:41:15 -070069 string update_app_version;
70 string update_omaha_url;
Gilad Arnoldb92f0df2013-01-10 16:32:45 -080071 bool interactive = true;
Jay Srinivasane73acab2012-07-10 14:34:03 -070072
Darin Petkova07586b2010-10-20 13:41:15 -070073 // Only non-official (e.g., dev and test) builds can override the current
Darin Petkov820a77b2011-04-27 16:48:58 -070074 // version and update server URL over D-Bus. However, pointing to the
75 // hardcoded test update server URL is always allowed.
Darin Petkova07586b2010-10-20 13:41:15 -070076 if (!chromeos_update_engine::utils::IsOfficialBuild()) {
77 if (app_version) {
78 update_app_version = app_version;
79 }
80 if (omaha_url) {
81 update_omaha_url = omaha_url;
82 }
83 }
Jay Srinivasane73acab2012-07-10 14:34:03 -070084 if (omaha_url) {
85 if (strcmp(omaha_url, kScheduledAUTestURLRequest) == 0) {
86 update_omaha_url = kAUTestURL;
87 // pretend that it's not user-initiated even though it is,
88 // so as to test scattering logic, etc. which get kicked off
89 // only in scheduled update checks.
Gilad Arnoldb92f0df2013-01-10 16:32:45 -080090 interactive = false;
Jay Srinivasane73acab2012-07-10 14:34:03 -070091 } else if (strcmp(omaha_url, kAUTestURLRequest) == 0) {
92 update_omaha_url = kAUTestURL;
93 }
Darin Petkov820a77b2011-04-27 16:48:58 -070094 }
Darin Petkov296889c2010-07-23 16:20:54 -070095 LOG(INFO) << "Attempt update: app_version=\"" << update_app_version << "\" "
Jay Srinivasane73acab2012-07-10 14:34:03 -070096 << "omaha_url=\"" << update_omaha_url << "\" "
Gilad Arnoldb92f0df2013-01-10 16:32:45 -080097 << "interactive=" << (interactive? "yes" : "no");
Jay Srinivasanae4697c2013-03-18 17:08:08 -070098 self->system_state_->update_attempter()->CheckForUpdate(update_app_version,
99 update_omaha_url,
100 interactive);
Darin Petkov296889c2010-07-23 16:20:54 -0700101 return TRUE;
102}
103
Chris Sosad317e402013-06-12 13:47:09 -0700104gboolean update_engine_service_attempt_rollback(UpdateEngineService* self,
105 bool powerwash,
106 GError **error) {
107 LOG(INFO) << "Attempting rollback to non-active partitions.";
108 self->system_state_->update_attempter()->Rollback(powerwash);
109 return TRUE;
110}
111
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700112gboolean update_engine_service_reset_status(UpdateEngineService* self,
113 GError **error) {
114 *error = NULL;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700115 return self->system_state_->update_attempter()->ResetStatus();
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700116}
117
118
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700119gboolean update_engine_service_get_status(UpdateEngineService* self,
120 int64_t* last_checked_time,
121 double* progress,
122 gchar** current_operation,
123 gchar** new_version,
124 int64_t* new_size,
125 GError **error) {
126 string current_op;
127 string new_version_str;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700128
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700129 CHECK(self->system_state_->update_attempter()->GetStatus(last_checked_time,
130 progress,
131 &current_op,
132 &new_version_str,
133 new_size));
Darin Petkov5a7f5652010-07-22 21:40:09 -0700134
Satoru Takabayashid6982312010-11-29 12:54:12 +0900135 *current_operation = g_strdup(current_op.c_str());
136 *new_version = g_strdup(new_version_str.c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700137 if (!(*current_operation && *new_version)) {
138 *error = NULL;
139 return FALSE;
140 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700141 return TRUE;
142}
143
Darin Petkov296889c2010-07-23 16:20:54 -0700144gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700145 GError **error) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700146 if (!self->system_state_->update_attempter()->RebootIfNeeded()) {
Darin Petkov296889c2010-07-23 16:20:54 -0700147 *error = NULL;
148 return FALSE;
149 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700150 return TRUE;
151}
152
Darin Petkov8daa3242010-10-25 13:28:47 -0700153gboolean update_engine_service_set_track(UpdateEngineService* self,
154 gchar* track,
155 GError **error) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700156 // track == target channel.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700157 // TODO(jaysri): Remove this method once chromium:219292 is fixed.
158 // Since UI won't be ready for now, preserve the existing
159 // behavior for set_track by calling SetTargetChannel directly without the
160 // policy checks instead of calling update_engine_service_set_channel.
161 LOG(INFO) << "Setting destination track to: " << track;
162 if (!self->system_state_->request_params()->SetTargetChannel(track, false)) {
163 *error = NULL;
164 return FALSE;
165 }
166
167 return TRUE;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700168}
169
170gboolean update_engine_service_get_track(UpdateEngineService* self,
171 gchar** track,
172 GError **error) {
173 // track == target channel.
174 return update_engine_service_get_channel(self, false, track, error);
175}
176
177gboolean update_engine_service_set_channel(UpdateEngineService* self,
178 gchar* target_channel,
179 bool is_powerwash_allowed,
180 GError **error) {
181 if (!target_channel)
182 return FALSE;
183
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700184 const policy::DevicePolicy* device_policy =
185 self->system_state_->device_policy();
186 if (!device_policy) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700187 LOG(INFO) << "Cannot set target channel until device policy/settings are "
188 "known";
189 return FALSE;
Darin Petkov8daa3242010-10-25 13:28:47 -0700190 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700191
192 bool delegated = false;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700193 if (!(device_policy->GetReleaseChannelDelegated(&delegated) && delegated)) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700194 LOG(INFO) << "Cannot set target channel explicitly when channel "
195 "policy/settings is not delegated";
196 return FALSE;
197 }
198
199 LOG(INFO) << "Setting destination channel to: " << target_channel;
200 if (!self->system_state_->request_params()->SetTargetChannel(
201 target_channel, is_powerwash_allowed)) {
202 *error = NULL;
203 return FALSE;
204 }
205
206 return TRUE;
207}
208
209gboolean update_engine_service_get_channel(UpdateEngineService* self,
210 bool get_current_channel,
211 gchar** channel,
212 GError **error) {
213 chromeos_update_engine::OmahaRequestParams* rp =
214 self->system_state_->request_params();
215
216 string channel_str = get_current_channel ?
217 rp->current_channel() : rp->target_channel();
218
219 *channel = g_strdup(channel_str.c_str());
Darin Petkov8daa3242010-10-25 13:28:47 -0700220 return TRUE;
221}
222
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700223gboolean update_engine_service_emit_status_update(
224 UpdateEngineService* self,
225 gint64 last_checked_time,
226 gdouble progress,
227 const gchar* current_operation,
228 const gchar* new_version,
229 gint64 new_size) {
230 g_signal_emit(self,
231 status_update_signal,
232 0,
233 last_checked_time,
234 progress,
235 current_operation,
236 new_version,
237 new_size);
238 return TRUE;
239}