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; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 70 | bool is_user_initiated = true; |
| 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. |
| 89 | is_user_initiated = false; |
| 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 << "\" " |
| 96 | << "is_user_initiated=" << (is_user_initiated? "yes" : "no"); |
| 97 | self->update_attempter_->CheckForUpdate(update_app_version, |
| 98 | update_omaha_url, |
| 99 | is_user_initiated); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 100 | return TRUE; |
| 101 | } |
| 102 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 103 | gboolean update_engine_service_get_status(UpdateEngineService* self, |
| 104 | int64_t* last_checked_time, |
| 105 | double* progress, |
| 106 | gchar** current_operation, |
| 107 | gchar** new_version, |
| 108 | int64_t* new_size, |
| 109 | GError **error) { |
| 110 | string current_op; |
| 111 | string new_version_str; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 112 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 113 | CHECK(self->update_attempter_->GetStatus(last_checked_time, |
| 114 | progress, |
| 115 | ¤t_op, |
| 116 | &new_version_str, |
| 117 | new_size)); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 118 | |
Satoru Takabayashi | d698231 | 2010-11-29 12:54:12 +0900 | [diff] [blame] | 119 | *current_operation = g_strdup(current_op.c_str()); |
| 120 | *new_version = g_strdup(new_version_str.c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 121 | if (!(*current_operation && *new_version)) { |
| 122 | *error = NULL; |
| 123 | return FALSE; |
| 124 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 125 | return TRUE; |
| 126 | } |
| 127 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 128 | gboolean update_engine_service_get_track(UpdateEngineService* self, |
| 129 | gchar** track, |
| 130 | GError **error) { |
| 131 | string track_str = |
| 132 | chromeos_update_engine::OmahaRequestDeviceParams::GetDeviceTrack(); |
Satoru Takabayashi | d698231 | 2010-11-29 12:54:12 +0900 | [diff] [blame] | 133 | *track = g_strdup(track_str.c_str()); |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 134 | return TRUE; |
| 135 | } |
| 136 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 137 | gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 138 | GError **error) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 139 | if (!self->update_attempter_->RebootIfNeeded()) { |
| 140 | *error = NULL; |
| 141 | return FALSE; |
| 142 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 143 | return TRUE; |
| 144 | } |
| 145 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 146 | gboolean update_engine_service_set_track(UpdateEngineService* self, |
| 147 | gchar* track, |
| 148 | GError **error) { |
| 149 | if (track) { |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 150 | LOG(INFO) << "Setting track to: " << track; |
| 151 | if (!chromeos_update_engine::OmahaRequestDeviceParams::SetDeviceTrack( |
| 152 | track)) { |
| 153 | *error = NULL; |
| 154 | return FALSE; |
| 155 | } |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 156 | } |
| 157 | return TRUE; |
| 158 | } |
| 159 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 160 | gboolean update_engine_service_emit_status_update( |
| 161 | UpdateEngineService* self, |
| 162 | gint64 last_checked_time, |
| 163 | gdouble progress, |
| 164 | const gchar* current_operation, |
| 165 | const gchar* new_version, |
| 166 | gint64 new_size) { |
| 167 | g_signal_emit(self, |
| 168 | status_update_signal, |
| 169 | 0, |
| 170 | last_checked_time, |
| 171 | progress, |
| 172 | current_operation, |
| 173 | new_version, |
| 174 | new_size); |
| 175 | return TRUE; |
| 176 | } |