blob: 2d0f31bd16100a4068a5828c480f5ed5704b4cdd [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>
10
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070011#include "update_engine/marshal.glibmarshal.h"
Darin Petkov49d91322010-10-25 16:34:58 -070012#include "update_engine/omaha_request_params.h"
Darin Petkova07586b2010-10-20 13:41:15 -070013#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070014
15using std::string;
16
Darin Petkov820a77b2011-04-27 16:48:58 -070017static const char kAUTestURLRequest[] = "autest";
Jay Srinivasane73acab2012-07-10 14:34:03 -070018// 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.
21static const char kScheduledAUTestURLRequest[] = "autest-scheduled";
22
Darin Petkov820a77b2011-04-27 16:48:58 -070023static const char kAUTestURL[] =
Darin Petkov5445e162011-11-04 10:10:27 +010024 "https://omaha.sandbox.google.com/service/update2";
Darin Petkov820a77b2011-04-27 16:48:58 -070025
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026G_DEFINE_TYPE(UpdateEngineService, update_engine_service, G_TYPE_OBJECT)
27
28static void update_engine_service_finalize(GObject* object) {
29 G_OBJECT_CLASS(update_engine_service_parent_class)->finalize(object);
30}
31
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070032static guint status_update_signal = 0;
33
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070034static 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 Petkov5a7f5652010-07-22 21:40:09 -070038
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070039 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 Reyes4e9b9f42010-04-26 15:06:43 -070054}
55
56static void update_engine_service_init(UpdateEngineService* object) {
57}
58
59UpdateEngineService* update_engine_service_new(void) {
60 return reinterpret_cast<UpdateEngineService*>(
61 g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
62}
63
Darin Petkov296889c2010-07-23 16:20:54 -070064gboolean update_engine_service_attempt_update(UpdateEngineService* self,
65 gchar* app_version,
66 gchar* omaha_url,
67 GError **error) {
Darin Petkova07586b2010-10-20 13:41:15 -070068 string update_app_version;
69 string update_omaha_url;
Jay Srinivasane73acab2012-07-10 14:34:03 -070070 bool is_user_initiated = true;
71
Darin Petkova07586b2010-10-20 13:41:15 -070072 // Only non-official (e.g., dev and test) builds can override the current
Darin Petkov820a77b2011-04-27 16:48:58 -070073 // version and update server URL over D-Bus. However, pointing to the
74 // hardcoded test update server URL is always allowed.
Darin Petkova07586b2010-10-20 13:41:15 -070075 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 Srinivasane73acab2012-07-10 14:34:03 -070083 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 Petkov820a77b2011-04-27 16:48:58 -070093 }
Darin Petkov296889c2010-07-23 16:20:54 -070094 LOG(INFO) << "Attempt update: app_version=\"" << update_app_version << "\" "
Jay Srinivasane73acab2012-07-10 14:34:03 -070095 << "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 Petkov296889c2010-07-23 16:20:54 -0700100 return TRUE;
101}
102
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700103gboolean 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 Reyes4e9b9f42010-04-26 15:06:43 -0700110gboolean 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 Petkov5a7f5652010-07-22 21:40:09 -0700119
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700120 CHECK(self->update_attempter_->GetStatus(last_checked_time,
121 progress,
122 &current_op,
123 &new_version_str,
124 new_size));
Darin Petkov5a7f5652010-07-22 21:40:09 -0700125
Satoru Takabayashid6982312010-11-29 12:54:12 +0900126 *current_operation = g_strdup(current_op.c_str());
127 *new_version = g_strdup(new_version_str.c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700128 if (!(*current_operation && *new_version)) {
129 *error = NULL;
130 return FALSE;
131 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700132 return TRUE;
133}
134
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900135gboolean update_engine_service_get_track(UpdateEngineService* self,
136 gchar** track,
137 GError **error) {
138 string track_str =
139 chromeos_update_engine::OmahaRequestDeviceParams::GetDeviceTrack();
Satoru Takabayashid6982312010-11-29 12:54:12 +0900140 *track = g_strdup(track_str.c_str());
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900141 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) {
Darin Petkov296889c2010-07-23 16:20:54 -0700146 if (!self->update_attempter_->RebootIfNeeded()) {
147 *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) {
156 if (track) {
Darin Petkov49d91322010-10-25 16:34:58 -0700157 LOG(INFO) << "Setting track to: " << track;
158 if (!chromeos_update_engine::OmahaRequestDeviceParams::SetDeviceTrack(
159 track)) {
160 *error = NULL;
161 return FALSE;
162 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700163 }
164 return TRUE;
165}
166
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700167gboolean 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}