blob: 988ae2e6c6414eeae6fdc1a5d251e8c6d7754f41 [file] [log] [blame]
Darin Petkov820a77b2011-04-27 16:48:58 -07001// Copyright (c) 2011 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";
18static const char kAUTestURL[] =
Darin Petkov5445e162011-11-04 10:10:27 +010019 "https://omaha.sandbox.google.com/service/update2";
Darin Petkov820a77b2011-04-27 16:48:58 -070020
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021G_DEFINE_TYPE(UpdateEngineService, update_engine_service, G_TYPE_OBJECT)
22
23static void update_engine_service_finalize(GObject* object) {
24 G_OBJECT_CLASS(update_engine_service_parent_class)->finalize(object);
25}
26
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070027static guint status_update_signal = 0;
28
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070029static void update_engine_service_class_init(UpdateEngineServiceClass* klass) {
30 GObjectClass *object_class;
31 object_class = G_OBJECT_CLASS(klass);
32 object_class->finalize = update_engine_service_finalize;
Darin Petkov5a7f5652010-07-22 21:40:09 -070033
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070034 status_update_signal = g_signal_new(
35 "status_update",
36 G_OBJECT_CLASS_TYPE(klass),
37 G_SIGNAL_RUN_LAST,
38 0, // 0 == no class method associated
39 NULL, // Accumulator
40 NULL, // Accumulator data
41 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
42 G_TYPE_NONE, // Return type
43 5, // param count:
44 G_TYPE_INT64,
45 G_TYPE_DOUBLE,
46 G_TYPE_STRING,
47 G_TYPE_STRING,
48 G_TYPE_INT64);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070049}
50
51static void update_engine_service_init(UpdateEngineService* object) {
52}
53
54UpdateEngineService* update_engine_service_new(void) {
55 return reinterpret_cast<UpdateEngineService*>(
56 g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
57}
58
Darin Petkov296889c2010-07-23 16:20:54 -070059gboolean update_engine_service_attempt_update(UpdateEngineService* self,
60 gchar* app_version,
61 gchar* omaha_url,
62 GError **error) {
Darin Petkova07586b2010-10-20 13:41:15 -070063 string update_app_version;
64 string update_omaha_url;
65 // Only non-official (e.g., dev and test) builds can override the current
Darin Petkov820a77b2011-04-27 16:48:58 -070066 // version and update server URL over D-Bus. However, pointing to the
67 // hardcoded test update server URL is always allowed.
Darin Petkova07586b2010-10-20 13:41:15 -070068 if (!chromeos_update_engine::utils::IsOfficialBuild()) {
69 if (app_version) {
70 update_app_version = app_version;
71 }
72 if (omaha_url) {
73 update_omaha_url = omaha_url;
74 }
75 }
Darin Petkov820a77b2011-04-27 16:48:58 -070076 if (omaha_url && strcmp(omaha_url, kAUTestURLRequest) == 0) {
77 update_omaha_url = kAUTestURL;
78 }
Darin Petkov296889c2010-07-23 16:20:54 -070079 LOG(INFO) << "Attempt update: app_version=\"" << update_app_version << "\" "
80 << "omaha_url=\"" << update_omaha_url << "\"";
Darin Petkovba3fb172010-10-20 14:35:01 -070081 self->update_attempter_->CheckForUpdate(update_app_version, update_omaha_url);
Darin Petkov296889c2010-07-23 16:20:54 -070082 return TRUE;
83}
84
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070085gboolean update_engine_service_get_status(UpdateEngineService* self,
86 int64_t* last_checked_time,
87 double* progress,
88 gchar** current_operation,
89 gchar** new_version,
90 int64_t* new_size,
91 GError **error) {
92 string current_op;
93 string new_version_str;
Darin Petkov5a7f5652010-07-22 21:40:09 -070094
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070095 CHECK(self->update_attempter_->GetStatus(last_checked_time,
96 progress,
97 &current_op,
98 &new_version_str,
99 new_size));
Darin Petkov5a7f5652010-07-22 21:40:09 -0700100
Satoru Takabayashid6982312010-11-29 12:54:12 +0900101 *current_operation = g_strdup(current_op.c_str());
102 *new_version = g_strdup(new_version_str.c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700103 if (!(*current_operation && *new_version)) {
104 *error = NULL;
105 return FALSE;
106 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700107 return TRUE;
108}
109
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900110gboolean update_engine_service_get_track(UpdateEngineService* self,
111 gchar** track,
112 GError **error) {
113 string track_str =
114 chromeos_update_engine::OmahaRequestDeviceParams::GetDeviceTrack();
Satoru Takabayashid6982312010-11-29 12:54:12 +0900115 *track = g_strdup(track_str.c_str());
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900116 return TRUE;
117}
118
Darin Petkov296889c2010-07-23 16:20:54 -0700119gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700120 GError **error) {
Darin Petkov296889c2010-07-23 16:20:54 -0700121 if (!self->update_attempter_->RebootIfNeeded()) {
122 *error = NULL;
123 return FALSE;
124 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700125 return TRUE;
126}
127
Darin Petkov8daa3242010-10-25 13:28:47 -0700128gboolean update_engine_service_set_track(UpdateEngineService* self,
129 gchar* track,
130 GError **error) {
131 if (track) {
Darin Petkov49d91322010-10-25 16:34:58 -0700132 LOG(INFO) << "Setting track to: " << track;
133 if (!chromeos_update_engine::OmahaRequestDeviceParams::SetDeviceTrack(
134 track)) {
135 *error = NULL;
136 return FALSE;
137 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700138 }
139 return TRUE;
140}
141
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700142gboolean update_engine_service_emit_status_update(
143 UpdateEngineService* self,
144 gint64 last_checked_time,
145 gdouble progress,
146 const gchar* current_operation,
147 const gchar* new_version,
148 gint64 new_size) {
149 g_signal_emit(self,
150 status_update_signal,
151 0,
152 last_checked_time,
153 progress,
154 current_operation,
155 new_version,
156 new_size);
157 return TRUE;
158}