blob: 5825324fad3bb83cef28f1b0cf415c0d95c0aa2a [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";
33
34} // namespace
35
36namespace chromeos_update_engine {
37
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080038bool WeaveService::Init(DelegateInterface* delegate) {
Alex Deymof7ead812015-10-23 17:37:27 -070039 delegate_ = delegate;
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080040 weave_service_subscription_ = weaved::Service::Connect(
41 brillo::MessageLoop::current(),
42 base::Bind(&WeaveService::OnWeaveServiceConnected,
43 base::Unretained(this)));
Alex Deymof7ead812015-10-23 17:37:27 -070044 return true;
45}
46
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080047void WeaveService::OnWeaveServiceConnected(
48 const std::weak_ptr<weaved::Service>& service) {
49 weave_service_ = service;
50 auto weave_service = weave_service_.lock();
51 if (!weave_service)
52 return;
53
54 weave_service->AddComponent(kWeaveComponent, {"_updater"}, nullptr);
55 weave_service->AddCommandHandler(
56 kWeaveComponent,
57 "_updater.checkForUpdates",
58 base::Bind(&WeaveService::OnCheckForUpdates, base::Unretained(this)));
59 weave_service->AddCommandHandler(
60 kWeaveComponent,
61 "_updater.trackChannel",
62 base::Bind(&WeaveService::OnTrackChannel, base::Unretained(this)));
63 UpdateWeaveState();
64}
65
Alex Deymof7ead812015-10-23 17:37:27 -070066void WeaveService::UpdateWeaveState() {
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080067 auto weave_service = weave_service_.lock();
68 if (!weave_service || !delegate_)
Alex Deymof7ead812015-10-23 17:37:27 -070069 return;
70
71 int64_t last_checked_time;
72 double progress;
73 update_engine::UpdateStatus update_status;
74 string current_channel;
75 string tracking_channel;
76
77 if (!delegate_->GetWeaveState(&last_checked_time,
78 &progress,
79 &update_status,
80 &current_channel,
81 &tracking_channel))
82 return;
83
84 // Round to progress to 1% (0.01) to avoid excessive and meaningless state
85 // changes.
86 progress = std::floor(progress * 100.) / 100.;
87
88 brillo::VariantDictionary state{
89 {"_updater.currentChannel", current_channel},
90 {"_updater.trackingChannel", tracking_channel},
91 {"_updater.status", UpdateStatusToWeaveStatus(update_status)},
92 {"_updater.progress", progress},
93 {"_updater.lastUpdateCheckTimestamp",
94 static_cast<double>(last_checked_time)},
95 };
96
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080097 if (!weave_service->SetStateProperties(kWeaveComponent, state, nullptr)) {
Alex Deymof7ead812015-10-23 17:37:27 -070098 LOG(ERROR) << "Failed to update _updater state.";
99 }
100}
101
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -0800102void WeaveService::OnCheckForUpdates(std::unique_ptr<weaved::Command> command) {
Alex Deymof7ead812015-10-23 17:37:27 -0700103 brillo::ErrorPtr error;
104 if (!delegate_->OnCheckForUpdates(&error)) {
105 command->Abort(error->GetCode(), error->GetMessage(), nullptr);
106 return;
107 }
108 command->Complete({}, nullptr);
109}
110
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -0800111void WeaveService::OnTrackChannel(std::unique_ptr<weaved::Command> command) {
Alex Deymof7ead812015-10-23 17:37:27 -0700112 string channel = command->GetParameter<string>("channel");
113 brillo::ErrorPtr error;
114 if (!delegate_->OnTrackChannel(channel, &error)) {
115 command->Abort(error->GetCode(), error->GetMessage(), nullptr);
116 return;
117 }
118 command->Complete({}, nullptr);
119}
120
121} // namespace chromeos_update_engine