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> |
| 10 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 11 | #include "update_engine/marshal.glibmarshal.h" |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 12 | #include "update_engine/omaha_request_params.h" |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 13 | #include "update_engine/utils.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 14 | |
| 15 | using std::string; |
| 16 | |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 17 | static const char kAUTestURLRequest[] = "autest"; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 18 | // By default autest bypasses scattering. If we want to test scattering, |
| 19 | // we should use autest-scheduled. The Url used is same in both cases, but |
| 20 | // different params are passed to CheckForUpdate method. |
| 21 | static const char kScheduledAUTestURLRequest[] = "autest-scheduled"; |
| 22 | |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 23 | static const char kAUTestURL[] = |
Darin Petkov | 5445e16 | 2011-11-04 10:10:27 +0100 | [diff] [blame] | 24 | "https://omaha.sandbox.google.com/service/update2"; |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 25 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 26 | G_DEFINE_TYPE(UpdateEngineService, update_engine_service, G_TYPE_OBJECT) |
| 27 | |
| 28 | static void update_engine_service_finalize(GObject* object) { |
| 29 | G_OBJECT_CLASS(update_engine_service_parent_class)->finalize(object); |
| 30 | } |
| 31 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 32 | static guint status_update_signal = 0; |
| 33 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 34 | static void update_engine_service_class_init(UpdateEngineServiceClass* klass) { |
| 35 | GObjectClass *object_class; |
| 36 | object_class = G_OBJECT_CLASS(klass); |
| 37 | object_class->finalize = update_engine_service_finalize; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 38 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 39 | status_update_signal = g_signal_new( |
| 40 | "status_update", |
| 41 | G_OBJECT_CLASS_TYPE(klass), |
| 42 | G_SIGNAL_RUN_LAST, |
| 43 | 0, // 0 == no class method associated |
| 44 | NULL, // Accumulator |
| 45 | NULL, // Accumulator data |
| 46 | update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64, |
| 47 | G_TYPE_NONE, // Return type |
| 48 | 5, // param count: |
| 49 | G_TYPE_INT64, |
| 50 | G_TYPE_DOUBLE, |
| 51 | G_TYPE_STRING, |
| 52 | G_TYPE_STRING, |
| 53 | G_TYPE_INT64); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void update_engine_service_init(UpdateEngineService* object) { |
| 57 | } |
| 58 | |
| 59 | UpdateEngineService* update_engine_service_new(void) { |
| 60 | return reinterpret_cast<UpdateEngineService*>( |
| 61 | g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 62 | } |
| 63 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 64 | gboolean update_engine_service_attempt_update(UpdateEngineService* self, |
| 65 | gchar* app_version, |
| 66 | gchar* omaha_url, |
| 67 | GError **error) { |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 68 | string update_app_version; |
| 69 | string update_omaha_url; |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 70 | bool interactive = true; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 71 | |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 72 | // 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] | 73 | // version and update server URL over D-Bus. However, pointing to the |
| 74 | // hardcoded test update server URL is always allowed. |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 75 | if (!chromeos_update_engine::utils::IsOfficialBuild()) { |
| 76 | if (app_version) { |
| 77 | update_app_version = app_version; |
| 78 | } |
| 79 | if (omaha_url) { |
| 80 | update_omaha_url = omaha_url; |
| 81 | } |
| 82 | } |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 83 | if (omaha_url) { |
| 84 | if (strcmp(omaha_url, kScheduledAUTestURLRequest) == 0) { |
| 85 | update_omaha_url = kAUTestURL; |
| 86 | // pretend that it's not user-initiated even though it is, |
| 87 | // so as to test scattering logic, etc. which get kicked off |
| 88 | // only in scheduled update checks. |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 89 | interactive = false; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 90 | } else if (strcmp(omaha_url, kAUTestURLRequest) == 0) { |
| 91 | update_omaha_url = kAUTestURL; |
| 92 | } |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 93 | } |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 94 | LOG(INFO) << "Attempt update: app_version=\"" << update_app_version << "\" " |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 95 | << "omaha_url=\"" << update_omaha_url << "\" " |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 96 | << "interactive=" << (interactive? "yes" : "no"); |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 97 | self->update_attempter_->CheckForUpdate(update_app_version, |
| 98 | update_omaha_url, |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 99 | interactive); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 100 | return TRUE; |
| 101 | } |
| 102 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 103 | gboolean update_engine_service_reset_status(UpdateEngineService* self, |
| 104 | GError **error) { |
| 105 | *error = NULL; |
| 106 | return self->update_attempter_->ResetStatus(); |
| 107 | } |
| 108 | |
| 109 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 110 | gboolean update_engine_service_get_status(UpdateEngineService* self, |
| 111 | int64_t* last_checked_time, |
| 112 | double* progress, |
| 113 | gchar** current_operation, |
| 114 | gchar** new_version, |
| 115 | int64_t* new_size, |
| 116 | GError **error) { |
| 117 | string current_op; |
| 118 | string new_version_str; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 119 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 120 | CHECK(self->update_attempter_->GetStatus(last_checked_time, |
| 121 | progress, |
| 122 | ¤t_op, |
| 123 | &new_version_str, |
| 124 | new_size)); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 125 | |
Satoru Takabayashi | d698231 | 2010-11-29 12:54:12 +0900 | [diff] [blame] | 126 | *current_operation = g_strdup(current_op.c_str()); |
| 127 | *new_version = g_strdup(new_version_str.c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 128 | if (!(*current_operation && *new_version)) { |
| 129 | *error = NULL; |
| 130 | return FALSE; |
| 131 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 132 | return TRUE; |
| 133 | } |
| 134 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 135 | gboolean update_engine_service_get_track(UpdateEngineService* self, |
| 136 | gchar** track, |
| 137 | GError **error) { |
| 138 | string track_str = |
| 139 | chromeos_update_engine::OmahaRequestDeviceParams::GetDeviceTrack(); |
Satoru Takabayashi | d698231 | 2010-11-29 12:54:12 +0900 | [diff] [blame] | 140 | *track = g_strdup(track_str.c_str()); |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 141 | return TRUE; |
| 142 | } |
| 143 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 144 | gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 145 | GError **error) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 146 | if (!self->update_attempter_->RebootIfNeeded()) { |
| 147 | *error = NULL; |
| 148 | return FALSE; |
| 149 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 150 | return TRUE; |
| 151 | } |
| 152 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 153 | gboolean update_engine_service_set_track(UpdateEngineService* self, |
| 154 | gchar* track, |
| 155 | GError **error) { |
| 156 | if (track) { |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 157 | LOG(INFO) << "Setting track to: " << track; |
| 158 | if (!chromeos_update_engine::OmahaRequestDeviceParams::SetDeviceTrack( |
| 159 | track)) { |
| 160 | *error = NULL; |
| 161 | return FALSE; |
| 162 | } |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 163 | } |
| 164 | return TRUE; |
| 165 | } |
| 166 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 167 | gboolean update_engine_service_emit_status_update( |
| 168 | UpdateEngineService* self, |
| 169 | gint64 last_checked_time, |
| 170 | gdouble progress, |
| 171 | const gchar* current_operation, |
| 172 | const gchar* new_version, |
| 173 | gint64 new_size) { |
| 174 | g_signal_emit(self, |
| 175 | status_update_signal, |
| 176 | 0, |
| 177 | last_checked_time, |
| 178 | progress, |
| 179 | current_operation, |
| 180 | new_version, |
| 181 | new_size); |
| 182 | return TRUE; |
| 183 | } |