1a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
2a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// Copyright (C) 2012 The Android Open Source Project
3a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
4a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// Licensed under the Apache License, Version 2.0 (the "License");
5a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// you may not use this file except in compliance with the License.
6a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// You may obtain a copy of the License at
7a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
8a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//      http://www.apache.org/licenses/LICENSE-2.0
9a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
10a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// Unless required by applicable law or agreed to in writing, software
11a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// distributed under the License is distributed on an "AS IS" BASIS,
12a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// See the License for the specific language governing permissions and
14a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// limitations under the License.
15a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin//
16a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
17a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/common_service.h"
18a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
19a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <set>
20a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <string>
21a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
22a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <base/location.h>
23a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <base/logging.h>
24a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <base/strings/stringprintf.h>
25a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <brillo/bind_lambda.h>
26a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <brillo/message_loops/message_loop.h>
27a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <brillo/strings/string_utils.h>
28a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include <policy/device_policy.h>
29a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
30a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/common/clock_interface.h"
31a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/common/hardware_interface.h"
32a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/common/prefs.h"
33a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/common/utils.h"
34a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/connection_manager_interface.h"
35a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/omaha_request_params.h"
36a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/p2p_manager.h"
37a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin#include "update_engine/update_attempter.h"
382997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao#include "update_engine/payload_state_interface.h"
39a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
40a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinusing base::StringPrintf;
41a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinusing brillo::ErrorPtr;
42a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinusing brillo::string_utils::ToString;
43a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinusing std::set;
44a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinusing std::string;
45a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
46a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinnamespace chromeos_update_engine {
47a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
48a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinnamespace {
49a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// Log and set the error on the passed ErrorPtr.
50a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinvoid LogAndSetError(ErrorPtr* error,
51a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                    const tracked_objects::Location& location,
52a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                    const string& reason) {
53a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  brillo::Error::AddTo(error,
54a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                       location,
55a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                       UpdateEngineService::kErrorDomain,
56a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                       UpdateEngineService::kErrorFailed,
57a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                       reason);
58a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
59a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin             << reason;
60a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
61a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}  // namespace
62a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
63a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinconst char* const UpdateEngineService::kErrorDomain = "update_engine";
64a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinconst char* const UpdateEngineService::kErrorFailed =
65a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    "org.chromium.UpdateEngine.Error.Failed";
66a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
67a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey DahlinUpdateEngineService::UpdateEngineService(SystemState* system_state)
68a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    : system_state_(system_state) {
69a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
70a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
71a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin// org::chromium::UpdateEngineInterfaceInterface methods implementation.
72a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
73a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
74a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                        const string& in_app_version,
75a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                        const string& in_omaha_url,
76a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                        int32_t in_flags_as_int) {
77a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  AttemptUpdateFlags flags = static_cast<AttemptUpdateFlags>(in_flags_as_int);
78a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool interactive = !(flags & kAttemptUpdateFlagNonInteractive);
79a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
80a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
81a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin            << "omaha_url=\"" << in_omaha_url << "\" "
82a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin            << "flags=0x" << std::hex << flags << " "
83a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin            << "interactive=" << (interactive ? "yes" : "no");
84a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  system_state_->update_attempter()->CheckForUpdate(
85a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      in_app_version, in_omaha_url, interactive);
86a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
87a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
88a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
89a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
90a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  LOG(INFO) << "Attempting rollback to non-active partitions.";
91a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
92a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
93a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    // TODO(dgarrett): Give a more specific error code/reason.
94a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
95a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
96a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
97a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
98a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
99a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
100a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
101a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                      bool* out_can_rollback) {
102a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool can_rollback = system_state_->update_attempter()->CanRollback();
103a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
104a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  *out_can_rollback = can_rollback;
105a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
106a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
107a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
108a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::ResetStatus(ErrorPtr* error) {
109a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->update_attempter()->ResetStatus()) {
110a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    // TODO(dgarrett): Give a more specific error code/reason.
111a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
112a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
113a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
114a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
115a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
116a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
117a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::GetStatus(ErrorPtr* error,
118a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                    int64_t* out_last_checked_time,
119a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                    double* out_progress,
120a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                    string* out_current_operation,
121a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                    string* out_new_version,
122a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                    int64_t* out_new_size) {
123a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->update_attempter()->GetStatus(out_last_checked_time,
124a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                    out_progress,
125a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                    out_current_operation,
126a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                    out_new_version,
127a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                    out_new_size)) {
128a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error, FROM_HERE, "GetStatus failed.");
129a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
130a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
131a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
132a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
133a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
134a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
135a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->update_attempter()->RebootIfNeeded()) {
136a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    // TODO(dgarrett): Give a more specific error code/reason.
137a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
138a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
139a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
140a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
141a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
142a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
143a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::SetChannel(ErrorPtr* error,
144a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                     const string& in_target_channel,
145a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                     bool in_is_powerwash_allowed) {
146a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  const policy::DevicePolicy* device_policy = system_state_->device_policy();
147a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
148a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // The device_policy is loaded in a lazy way before an update check. Load it
149a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // now from the libbrillo cache if it wasn't already loaded.
150a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!device_policy) {
151a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    UpdateAttempter* update_attempter = system_state_->update_attempter();
152a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    if (update_attempter) {
153a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      update_attempter->RefreshDevicePolicy();
154a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      device_policy = system_state_->device_policy();
155a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    }
156a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
157a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
158a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool delegated = false;
159a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
160a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      !delegated) {
161a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error,
162a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   FROM_HERE,
163a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   "Cannot set target channel explicitly when channel "
164a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   "policy/settings is not delegated");
165a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
166a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
167a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
168a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  LOG(INFO) << "Setting destination channel to: " << in_target_channel;
169a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  string error_message;
170a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->request_params()->SetTargetChannel(
171a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin          in_target_channel, in_is_powerwash_allowed, &error_message)) {
172a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error, FROM_HERE, error_message);
173a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
174a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
175a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Update the weave state because updated the target channel.
176fa78f14d818e8e0b8ed5d05ebc389bf833342e7aAlex Deymo  system_state_->update_attempter()->BroadcastChannel();
177a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
178a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
179a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
180a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
181a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                     bool in_get_current_channel,
182a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                     string* out_channel) {
183a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  OmahaRequestParams* rp = system_state_->request_params();
184a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  *out_channel =
185a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      (in_get_current_channel ? rp->current_channel() : rp->target_channel());
186a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
187a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
188a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
189a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
190a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                 bool in_enabled) {
191a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  PrefsInterface* prefs = system_state_->prefs();
192a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
193a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
194a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(
195a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin        error,
196a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin        FROM_HERE,
197a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin        StringPrintf("Error setting the update via p2p permission to %s.",
198a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                     ToString(in_enabled).c_str()));
199a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
200a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
201a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
202a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
203a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
204a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
205a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                 bool* out_enabled) {
206a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  PrefsInterface* prefs = system_state_->prefs();
207a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
208a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  bool p2p_pref = false;  // Default if no setting is present.
209a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (prefs->Exists(kPrefsP2PEnabled) &&
210a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
211a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
212a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
213a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
214a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
215a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  *out_enabled = p2p_pref;
216a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
217a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
218a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
219a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
220a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                          bool in_allowed) {
221a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  set<string> allowed_types;
222a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  const policy::DevicePolicy* device_policy = system_state_->device_policy();
223a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
224a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // The device_policy is loaded in a lazy way before an update check. Load it
225a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // now from the libbrillo cache if it wasn't already loaded.
226a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!device_policy) {
227a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    UpdateAttempter* update_attempter = system_state_->update_attempter();
228a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    if (update_attempter) {
229a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      update_attempter->RefreshDevicePolicy();
230a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      device_policy = system_state_->device_policy();
231a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    }
232a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
233a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
234a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Check if this setting is allowed by the device policy.
235a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (device_policy &&
236a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
237a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error,
238a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   FROM_HERE,
239a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   "Ignoring the update over cellular setting since there's "
240a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   "a device policy enforcing this setting.");
241a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
242a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
243a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
244a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // If the policy wasn't loaded yet, then it is still OK to change the local
245a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // setting because the policy will be checked again during the update check.
246a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
247a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  PrefsInterface* prefs = system_state_->prefs();
248a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
249a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
250a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error,
251a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   FROM_HERE,
252a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                   string("Error setting the update over cellular to ") +
253a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                       (in_allowed ? "true" : "false"));
254a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
255a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
256a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
257a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
258a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
259a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* /* error */,
260a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                          bool* out_allowed) {
261a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  ConnectionManagerInterface* cm = system_state_->connection_manager();
262a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
263a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // The device_policy is loaded in a lazy way before an update check and is
264a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // used to determine if an update is allowed over cellular. Load the device
265a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // policy now from the libbrillo cache if it wasn't already loaded.
266a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->device_policy()) {
267a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    UpdateAttempter* update_attempter = system_state_->update_attempter();
268a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    if (update_attempter)
269a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      update_attempter->RefreshDevicePolicy();
270a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
271a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
272a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // Return the current setting based on the same logic used while checking for
273a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  // updates. A log message could be printed as the result of this test.
274a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  LOG(INFO) << "Checking if updates over cellular networks are allowed:";
275a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  *out_allowed = cm->IsUpdateAllowedOver(
276a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      chromeos_update_engine::NetworkConnectionType::kCellular,
277a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      chromeos_update_engine::NetworkTethering::kUnknown);
278a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
279a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
280a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
281a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
282a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                                 int64_t* out_usec_wallclock) {
283a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  base::Time time;
284a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
285a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LogAndSetError(error, FROM_HERE, "No pending update.");
286a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
287a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
288a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
289a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  ClockInterface* clock = system_state_->clock();
290a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
291a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
292a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
293a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
294a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
295a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin                                         string* out_prev_version) {
296a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
297a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
298a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
299a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
300a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlinbool UpdateEngineService::GetRollbackPartition(
301a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    ErrorPtr* /* error */, string* out_rollback_partition_name) {
302a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  BootControlInterface::Slot rollback_slot =
303a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin      system_state_->update_attempter()->GetRollbackSlot();
304a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
305a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (rollback_slot == BootControlInterface::kInvalidSlot) {
306a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    out_rollback_partition_name->clear();
307a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return true;
308a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
309a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
310a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  string name;
311a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  if (!system_state_->boot_control()->GetPartitionDevice(
312a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin          "KERNEL", rollback_slot, &name)) {
313a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    LOG(ERROR) << "Invalid rollback device";
314a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin    return false;
315a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  }
316a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
317a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  LOG(INFO) << "Getting rollback partition name. Result: " << name;
318a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  *out_rollback_partition_name = name;
319a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin  return true;
320a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}
321a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin
3222997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhaobool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
3232997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao                                              int32_t* out_last_attempt_error) {
3242997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao  ErrorCode error_code = system_state_->payload_state()->GetAttemptErrorCode();
3252997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao  *out_last_attempt_error = static_cast<int>(error_code);
3262997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao  return true;
3272997173235e88c5e4cb13d2844f74afc7b25d6e2Shuqian Zhao}
328a93cd53c4f8ab8cb926ed9223373567ebbf45cb6Casey Dahlin}  // namespace chromeos_update_engine
329