Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 1 | // |
| 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 Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 23 | #include <brillo/errors/error.h> |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 24 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 25 | |
| 26 | #include "update_engine/update_status_utils.h" |
| 27 | |
| 28 | using std::string; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | const char kWeaveComponent[] = "updater"; |
Alex Vakulenko | 210591e | 2016-01-07 10:08:26 -0800 | [diff] [blame] | 33 | const char kWeaveTrait[] = "_updater"; |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 39 | bool WeaveService::Init(DelegateInterface* delegate) { |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 40 | delegate_ = delegate; |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 41 | weave_service_subscription_ = weaved::Service::Connect( |
| 42 | brillo::MessageLoop::current(), |
| 43 | base::Bind(&WeaveService::OnWeaveServiceConnected, |
| 44 | base::Unretained(this))); |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 45 | return true; |
| 46 | } |
| 47 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 48 | void 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 Vakulenko | 210591e | 2016-01-07 10:08:26 -0800 | [diff] [blame] | 55 | weave_service->AddComponent(kWeaveComponent, {kWeaveTrait}, nullptr); |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 56 | weave_service->AddCommandHandler( |
Alex Vakulenko | 210591e | 2016-01-07 10:08:26 -0800 | [diff] [blame] | 57 | kWeaveComponent, kWeaveTrait, "checkForUpdates", |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 58 | base::Bind(&WeaveService::OnCheckForUpdates, base::Unretained(this))); |
| 59 | weave_service->AddCommandHandler( |
Alex Vakulenko | 210591e | 2016-01-07 10:08:26 -0800 | [diff] [blame] | 60 | kWeaveComponent, kWeaveTrait, "trackChannel", |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 61 | base::Bind(&WeaveService::OnTrackChannel, base::Unretained(this))); |
| 62 | UpdateWeaveState(); |
| 63 | } |
| 64 | |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 65 | void WeaveService::UpdateWeaveState() { |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 66 | auto weave_service = weave_service_.lock(); |
| 67 | if (!weave_service || !delegate_) |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 68 | return; |
| 69 | |
| 70 | int64_t last_checked_time; |
| 71 | double progress; |
| 72 | update_engine::UpdateStatus update_status; |
| 73 | string current_channel; |
| 74 | string tracking_channel; |
| 75 | |
| 76 | if (!delegate_->GetWeaveState(&last_checked_time, |
| 77 | &progress, |
| 78 | &update_status, |
| 79 | ¤t_channel, |
| 80 | &tracking_channel)) |
| 81 | return; |
| 82 | |
| 83 | // Round to progress to 1% (0.01) to avoid excessive and meaningless state |
| 84 | // changes. |
| 85 | progress = std::floor(progress * 100.) / 100.; |
| 86 | |
| 87 | brillo::VariantDictionary state{ |
| 88 | {"_updater.currentChannel", current_channel}, |
| 89 | {"_updater.trackingChannel", tracking_channel}, |
| 90 | {"_updater.status", UpdateStatusToWeaveStatus(update_status)}, |
| 91 | {"_updater.progress", progress}, |
| 92 | {"_updater.lastUpdateCheckTimestamp", |
| 93 | static_cast<double>(last_checked_time)}, |
| 94 | }; |
| 95 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 96 | if (!weave_service->SetStateProperties(kWeaveComponent, state, nullptr)) { |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 97 | LOG(ERROR) << "Failed to update _updater state."; |
| 98 | } |
| 99 | } |
| 100 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 101 | void WeaveService::OnCheckForUpdates(std::unique_ptr<weaved::Command> command) { |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 102 | brillo::ErrorPtr error; |
| 103 | if (!delegate_->OnCheckForUpdates(&error)) { |
Alex Vakulenko | c7cc45e | 2016-01-07 10:46:26 -0800 | [diff] [blame] | 104 | command->AbortWithCustomError(error.get(), nullptr); |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | command->Complete({}, nullptr); |
| 108 | } |
| 109 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 110 | void WeaveService::OnTrackChannel(std::unique_ptr<weaved::Command> command) { |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 111 | string channel = command->GetParameter<string>("channel"); |
| 112 | brillo::ErrorPtr error; |
| 113 | if (!delegate_->OnTrackChannel(channel, &error)) { |
Alex Vakulenko | c7cc45e | 2016-01-07 10:46:26 -0800 | [diff] [blame] | 114 | command->AbortWithCustomError(error.get(), nullptr); |
Alex Deymo | f7ead81 | 2015-10-23 17:37:27 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | command->Complete({}, nullptr); |
| 118 | } |
| 119 | |
| 120 | } // namespace chromeos_update_engine |