blob: ef685961315a76a17b134d3e94bf54ef27e6c394 [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
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700103gboolean 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 Petkov5a7f5652010-07-22 21:40:09 -0700112
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700113 CHECK(self->update_attempter_->GetStatus(last_checked_time,
114 progress,
115 &current_op,
116 &new_version_str,
117 new_size));
Darin Petkov5a7f5652010-07-22 21:40:09 -0700118
Satoru Takabayashid6982312010-11-29 12:54:12 +0900119 *current_operation = g_strdup(current_op.c_str());
120 *new_version = g_strdup(new_version_str.c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700121 if (!(*current_operation && *new_version)) {
122 *error = NULL;
123 return FALSE;
124 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700125 return TRUE;
126}
127
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900128gboolean update_engine_service_get_track(UpdateEngineService* self,
129 gchar** track,
130 GError **error) {
131 string track_str =
132 chromeos_update_engine::OmahaRequestDeviceParams::GetDeviceTrack();
Satoru Takabayashid6982312010-11-29 12:54:12 +0900133 *track = g_strdup(track_str.c_str());
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900134 return TRUE;
135}
136
Darin Petkov296889c2010-07-23 16:20:54 -0700137gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700138 GError **error) {
Darin Petkov296889c2010-07-23 16:20:54 -0700139 if (!self->update_attempter_->RebootIfNeeded()) {
140 *error = NULL;
141 return FALSE;
142 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700143 return TRUE;
144}
145
Darin Petkov8daa3242010-10-25 13:28:47 -0700146gboolean update_engine_service_set_track(UpdateEngineService* self,
147 gchar* track,
148 GError **error) {
149 if (track) {
Darin Petkov49d91322010-10-25 16:34:58 -0700150 LOG(INFO) << "Setting track to: " << track;
151 if (!chromeos_update_engine::OmahaRequestDeviceParams::SetDeviceTrack(
152 track)) {
153 *error = NULL;
154 return FALSE;
155 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700156 }
157 return TRUE;
158}
159
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700160gboolean 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}