Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 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/dbus_service.h" |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 6 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 7 | #include <string> |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 8 | |
| 9 | #include <base/logging.h> |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 10 | #include <policy/device_policy.h> |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 11 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 12 | #include "update_engine/marshal.glibmarshal.h" |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 13 | #include "update_engine/omaha_request_params.h" |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 14 | #include "update_engine/utils.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 15 | |
| 16 | using std::string; |
| 17 | |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 18 | static const char kAUTestURLRequest[] = "autest"; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 19 | // 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. |
| 22 | static const char kScheduledAUTestURLRequest[] = "autest-scheduled"; |
| 23 | |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 24 | static const char kAUTestURL[] = |
Darin Petkov | 5445e16 | 2011-11-04 10:10:27 +0100 | [diff] [blame] | 25 | "https://omaha.sandbox.google.com/service/update2"; |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 26 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 27 | G_DEFINE_TYPE(UpdateEngineService, update_engine_service, G_TYPE_OBJECT) |
| 28 | |
| 29 | static void update_engine_service_finalize(GObject* object) { |
| 30 | G_OBJECT_CLASS(update_engine_service_parent_class)->finalize(object); |
| 31 | } |
| 32 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 33 | static guint status_update_signal = 0; |
| 34 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 35 | static 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 Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 39 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 40 | 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 Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static void update_engine_service_init(UpdateEngineService* object) { |
| 58 | } |
| 59 | |
| 60 | UpdateEngineService* update_engine_service_new(void) { |
| 61 | return reinterpret_cast<UpdateEngineService*>( |
| 62 | g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 63 | } |
| 64 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 65 | gboolean update_engine_service_attempt_update(UpdateEngineService* self, |
| 66 | gchar* app_version, |
| 67 | gchar* omaha_url, |
| 68 | GError **error) { |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 69 | string update_app_version; |
| 70 | string update_omaha_url; |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 71 | bool interactive = true; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 72 | |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 73 | // Only non-official (e.g., dev and test) builds can override the current |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 74 | // version and update server URL over D-Bus. However, pointing to the |
| 75 | // hardcoded test update server URL is always allowed. |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 76 | 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 Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 84 | 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 Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 90 | interactive = false; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 91 | } else if (strcmp(omaha_url, kAUTestURLRequest) == 0) { |
| 92 | update_omaha_url = kAUTestURL; |
| 93 | } |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 94 | } |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 95 | LOG(INFO) << "Attempt update: app_version=\"" << update_app_version << "\" " |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 96 | << "omaha_url=\"" << update_omaha_url << "\" " |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 97 | << "interactive=" << (interactive? "yes" : "no"); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 98 | self->system_state_->update_attempter()->CheckForUpdate(update_app_version, |
| 99 | update_omaha_url, |
| 100 | interactive); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 101 | return TRUE; |
| 102 | } |
| 103 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 104 | gboolean update_engine_service_reset_status(UpdateEngineService* self, |
| 105 | GError **error) { |
| 106 | *error = NULL; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 107 | return self->system_state_->update_attempter()->ResetStatus(); |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 111 | gboolean update_engine_service_get_status(UpdateEngineService* self, |
| 112 | int64_t* last_checked_time, |
| 113 | double* progress, |
| 114 | gchar** current_operation, |
| 115 | gchar** new_version, |
| 116 | int64_t* new_size, |
| 117 | GError **error) { |
| 118 | string current_op; |
| 119 | string new_version_str; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 120 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 121 | CHECK(self->system_state_->update_attempter()->GetStatus(last_checked_time, |
| 122 | progress, |
| 123 | ¤t_op, |
| 124 | &new_version_str, |
| 125 | new_size)); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 126 | |
Satoru Takabayashi | d698231 | 2010-11-29 12:54:12 +0900 | [diff] [blame] | 127 | *current_operation = g_strdup(current_op.c_str()); |
| 128 | *new_version = g_strdup(new_version_str.c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 129 | if (!(*current_operation && *new_version)) { |
| 130 | *error = NULL; |
| 131 | return FALSE; |
| 132 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 133 | return TRUE; |
| 134 | } |
| 135 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 136 | gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 137 | GError **error) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 138 | if (!self->system_state_->update_attempter()->RebootIfNeeded()) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 139 | *error = NULL; |
| 140 | return FALSE; |
| 141 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 142 | return TRUE; |
| 143 | } |
| 144 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 145 | gboolean update_engine_service_set_track(UpdateEngineService* self, |
| 146 | gchar* track, |
| 147 | GError **error) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 148 | // track == target channel. |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 149 | // TODO(jaysri): Remove this method once chromium:219292 is fixed. |
| 150 | // Since UI won't be ready for now, preserve the existing |
| 151 | // behavior for set_track by calling SetTargetChannel directly without the |
| 152 | // policy checks instead of calling update_engine_service_set_channel. |
| 153 | LOG(INFO) << "Setting destination track to: " << track; |
| 154 | if (!self->system_state_->request_params()->SetTargetChannel(track, false)) { |
| 155 | *error = NULL; |
| 156 | return FALSE; |
| 157 | } |
| 158 | |
| 159 | return TRUE; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | gboolean update_engine_service_get_track(UpdateEngineService* self, |
| 163 | gchar** track, |
| 164 | GError **error) { |
| 165 | // track == target channel. |
| 166 | return update_engine_service_get_channel(self, false, track, error); |
| 167 | } |
| 168 | |
| 169 | gboolean update_engine_service_set_channel(UpdateEngineService* self, |
| 170 | gchar* target_channel, |
| 171 | bool is_powerwash_allowed, |
| 172 | GError **error) { |
| 173 | if (!target_channel) |
| 174 | return FALSE; |
| 175 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 176 | const policy::DevicePolicy* device_policy = |
| 177 | self->system_state_->device_policy(); |
| 178 | if (!device_policy) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 179 | LOG(INFO) << "Cannot set target channel until device policy/settings are " |
| 180 | "known"; |
| 181 | return FALSE; |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 182 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 183 | |
| 184 | bool delegated = false; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 185 | if (!(device_policy->GetReleaseChannelDelegated(&delegated) && delegated)) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 186 | LOG(INFO) << "Cannot set target channel explicitly when channel " |
| 187 | "policy/settings is not delegated"; |
| 188 | return FALSE; |
| 189 | } |
| 190 | |
| 191 | LOG(INFO) << "Setting destination channel to: " << target_channel; |
| 192 | if (!self->system_state_->request_params()->SetTargetChannel( |
| 193 | target_channel, is_powerwash_allowed)) { |
| 194 | *error = NULL; |
| 195 | return FALSE; |
| 196 | } |
| 197 | |
| 198 | return TRUE; |
| 199 | } |
| 200 | |
| 201 | gboolean update_engine_service_get_channel(UpdateEngineService* self, |
| 202 | bool get_current_channel, |
| 203 | gchar** channel, |
| 204 | GError **error) { |
| 205 | chromeos_update_engine::OmahaRequestParams* rp = |
| 206 | self->system_state_->request_params(); |
| 207 | |
| 208 | string channel_str = get_current_channel ? |
| 209 | rp->current_channel() : rp->target_channel(); |
| 210 | |
| 211 | *channel = g_strdup(channel_str.c_str()); |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 212 | return TRUE; |
| 213 | } |
| 214 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 215 | gboolean update_engine_service_emit_status_update( |
| 216 | UpdateEngineService* self, |
| 217 | gint64 last_checked_time, |
| 218 | gdouble progress, |
| 219 | const gchar* current_operation, |
| 220 | const gchar* new_version, |
| 221 | gint64 new_size) { |
| 222 | g_signal_emit(self, |
| 223 | status_update_signal, |
| 224 | 0, |
| 225 | last_checked_time, |
| 226 | progress, |
| 227 | current_operation, |
| 228 | new_version, |
| 229 | new_size); |
| 230 | return TRUE; |
| 231 | } |