blob: 9bdd175046cf755ee4c07cecfbc17e526dc15c8b [file] [log] [blame]
Alex Deymofa78f142016-01-26 21:36:16 -08001//
2// Copyright (C) 2016 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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/aosp/daemon_state_android.h"
Alex Deymofa78f142016-01-26 21:36:16 -080018
Alex Deymo5e3ea272016-01-28 13:42:23 -080019#include <base/logging.h>
20
Amin Hassaniec7bc112020-10-29 16:47:58 -070021#include "update_engine/aosp/update_attempter_android.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080022#include "update_engine/common/boot_control.h"
23#include "update_engine/common/boot_control_stub.h"
24#include "update_engine/common/hardware.h"
25#include "update_engine/common/prefs.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080026
Alex Deymofa78f142016-01-26 21:36:16 -080027namespace chromeos_update_engine {
28
29bool DaemonStateAndroid::Initialize() {
Alex Deymo5e3ea272016-01-28 13:42:23 -080030 boot_control_ = boot_control::CreateBootControl();
31 if (!boot_control_) {
32 LOG(WARNING) << "Unable to create BootControl instance, using stub "
33 << "instead. All update attempts will fail.";
34 boot_control_.reset(new BootControlStub());
35 }
36
37 hardware_ = hardware::CreateHardware();
38 if (!hardware_) {
Sen Jiang771f6482018-04-04 17:59:10 -070039 LOG(ERROR) << "Error initializing the HardwareInterface.";
Alex Deymo5e3ea272016-01-28 13:42:23 -080040 return false;
41 }
42
43 LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
44 LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
45
46 // Initialize prefs.
47 base::FilePath non_volatile_path;
Alex Deymo5e3ea272016-01-28 13:42:23 -080048 if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
Kelvin Zhang33d18092020-06-29 16:33:10 -040049 prefs_.reset(new MemoryPrefs());
50 LOG(WARNING)
51 << "Could not get a non-volatile directory, fall back to memory prefs";
52 } else {
53 Prefs* prefs = new Prefs();
54 prefs_.reset(prefs);
55 if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
56 LOG(ERROR) << "Failed to initialize preferences.";
57 return false;
58 }
Alex Deymo5e3ea272016-01-28 13:42:23 -080059 }
60
Alex Deymo765580d2016-06-22 19:23:13 -070061 // The CertificateChecker singleton is used by the update attempter.
62 certificate_checker_.reset(
63 new CertificateChecker(prefs_.get(), &openssl_wrapper_));
64 certificate_checker_->Init();
65
Alex Deymo5e3ea272016-01-28 13:42:23 -080066 // Initialize the UpdateAttempter before the UpdateManager.
67 update_attempter_.reset(new UpdateAttempterAndroid(
68 this, prefs_.get(), boot_control_.get(), hardware_.get()));
Alex Deymo5e3ea272016-01-28 13:42:23 -080069
Alex Deymofa78f142016-01-26 21:36:16 -080070 return true;
71}
72
73bool DaemonStateAndroid::StartUpdater() {
Alex Deymo5e3ea272016-01-28 13:42:23 -080074 // The DaemonState in Android is a passive daemon. It will only start applying
75 // an update when instructed to do so from the exposed binder API.
Alex Deymo0e061ae2016-02-09 17:49:03 -080076 update_attempter_->Init();
Alex Deymofa78f142016-01-26 21:36:16 -080077 return true;
78}
79
80void DaemonStateAndroid::AddObserver(ServiceObserverInterface* observer) {
81 service_observers_.insert(observer);
82}
83
84void DaemonStateAndroid::RemoveObserver(ServiceObserverInterface* observer) {
85 service_observers_.erase(observer);
86}
87
Alex Deymof8bfcff2016-02-02 21:22:11 -080088ServiceDelegateAndroidInterface* DaemonStateAndroid::service_delegate() {
Alex Deymo5e3ea272016-01-28 13:42:23 -080089 return update_attempter_.get();
Alex Deymof8bfcff2016-02-02 21:22:11 -080090}
91
Alex Deymofa78f142016-01-26 21:36:16 -080092} // namespace chromeos_update_engine