blob: 0a145e4e487795f549240b31147dc53a78a3cd0a [file] [log] [blame]
Alex Deymof7ead812015-10-23 17:37:27 -07001//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/weave_service.h"
18
19#include <cmath>
20#include <string>
21
22#include <base/bind.h>
Alex Deymof7ead812015-10-23 17:37:27 -070023#include <brillo/errors/error.h>
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080024#include <brillo/message_loops/message_loop.h>
Alex Deymof7ead812015-10-23 17:37:27 -070025
26#include "update_engine/update_status_utils.h"
27
28using std::string;
29
30namespace {
31
32const char kWeaveComponent[] = "updater";
Alex Vakulenko210591e2016-01-07 10:08:26 -080033const char kWeaveTrait[] = "_updater";
Alex Deymof7ead812015-10-23 17:37:27 -070034
35} // namespace
36
37namespace chromeos_update_engine {
38
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080039bool WeaveService::Init(DelegateInterface* delegate) {
Alex Deymof7ead812015-10-23 17:37:27 -070040 delegate_ = delegate;
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080041 weave_service_subscription_ = weaved::Service::Connect(
42 brillo::MessageLoop::current(),
43 base::Bind(&WeaveService::OnWeaveServiceConnected,
44 base::Unretained(this)));
Alex Deymof7ead812015-10-23 17:37:27 -070045 return true;
46}
47
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080048void WeaveService::OnWeaveServiceConnected(
49 const std::weak_ptr<weaved::Service>& service) {
50 weave_service_ = service;
51 auto weave_service = weave_service_.lock();
52 if (!weave_service)
53 return;
54
Alex Vakulenko210591e2016-01-07 10:08:26 -080055 weave_service->AddComponent(kWeaveComponent, {kWeaveTrait}, nullptr);
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080056 weave_service->AddCommandHandler(
Alex Vakulenko210591e2016-01-07 10:08:26 -080057 kWeaveComponent, kWeaveTrait, "checkForUpdates",
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080058 base::Bind(&WeaveService::OnCheckForUpdates, base::Unretained(this)));
59 weave_service->AddCommandHandler(
Alex Vakulenko210591e2016-01-07 10:08:26 -080060 kWeaveComponent, kWeaveTrait, "trackChannel",
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080061 base::Bind(&WeaveService::OnTrackChannel, base::Unretained(this)));
62 UpdateWeaveState();
63}
64
Alex Deymofa78f142016-01-26 21:36:16 -080065void WeaveService::SendStatusUpdate(int64_t /* last_checked_time */,
66 double /* progress */,
67 update_engine::UpdateStatus /* status */,
68 const string& /* new_version */,
69 int64_t /* new_size */) {
70 // We query the Weave
71 UpdateWeaveState();
72}
73
74void WeaveService::SendChannelChangeUpdate(
75 const string& /* tracking_channel */) {
76 UpdateWeaveState();
77}
78
Alex Deymof7ead812015-10-23 17:37:27 -070079void WeaveService::UpdateWeaveState() {
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080080 auto weave_service = weave_service_.lock();
81 if (!weave_service || !delegate_)
Alex Deymof7ead812015-10-23 17:37:27 -070082 return;
83
84 int64_t last_checked_time;
85 double progress;
86 update_engine::UpdateStatus update_status;
87 string current_channel;
88 string tracking_channel;
89
90 if (!delegate_->GetWeaveState(&last_checked_time,
91 &progress,
92 &update_status,
93 &current_channel,
94 &tracking_channel))
95 return;
96
97 // Round to progress to 1% (0.01) to avoid excessive and meaningless state
98 // changes.
99 progress = std::floor(progress * 100.) / 100.;
100
Alex Vakulenko3629d3e2016-01-28 14:36:46 -0800101 base::DictionaryValue state;
102 state.SetString("_updater.currentChannel", current_channel);
103 state.SetString("_updater.trackingChannel", tracking_channel);
104 state.SetString("_updater.status", UpdateStatusToWeaveStatus(update_status));
105 state.SetDouble("_updater.progress", progress);
106 state.SetDouble("_updater.lastUpdateCheckTimestamp",
107 static_cast<double>(last_checked_time));
Alex Deymof7ead812015-10-23 17:37:27 -0700108
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -0800109 if (!weave_service->SetStateProperties(kWeaveComponent, state, nullptr)) {
Alex Deymof7ead812015-10-23 17:37:27 -0700110 LOG(ERROR) << "Failed to update _updater state.";
111 }
112}
113
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -0800114void WeaveService::OnCheckForUpdates(std::unique_ptr<weaved::Command> command) {
Alex Deymof7ead812015-10-23 17:37:27 -0700115 brillo::ErrorPtr error;
116 if (!delegate_->OnCheckForUpdates(&error)) {
Alex Vakulenkoc7cc45e2016-01-07 10:46:26 -0800117 command->AbortWithCustomError(error.get(), nullptr);
Alex Deymof7ead812015-10-23 17:37:27 -0700118 return;
119 }
120 command->Complete({}, nullptr);
121}
122
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -0800123void WeaveService::OnTrackChannel(std::unique_ptr<weaved::Command> command) {
Alex Deymof7ead812015-10-23 17:37:27 -0700124 string channel = command->GetParameter<string>("channel");
125 brillo::ErrorPtr error;
126 if (!delegate_->OnTrackChannel(channel, &error)) {
Alex Vakulenkoc7cc45e2016-01-07 10:46:26 -0800127 command->AbortWithCustomError(error.get(), nullptr);
Alex Deymof7ead812015-10-23 17:37:27 -0700128 return;
129 }
130 command->Complete({}, nullptr);
131}
132
133} // namespace chromeos_update_engine