1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2016 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker
17*5a923131SAndroid Build Coastguard Worker #include "update_engine/aosp/daemon_state_android.h"
18*5a923131SAndroid Build Coastguard Worker
19*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
20*5a923131SAndroid Build Coastguard Worker
21*5a923131SAndroid Build Coastguard Worker #include "update_engine/aosp/apex_handler_interface.h"
22*5a923131SAndroid Build Coastguard Worker #include "update_engine/aosp/update_attempter_android.h"
23*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/boot_control.h"
24*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/boot_control_stub.h"
25*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/hardware.h"
26*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/prefs.h"
27*5a923131SAndroid Build Coastguard Worker
28*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
29*5a923131SAndroid Build Coastguard Worker
Initialize()30*5a923131SAndroid Build Coastguard Worker bool DaemonStateAndroid::Initialize() {
31*5a923131SAndroid Build Coastguard Worker boot_control_ = boot_control::CreateBootControl();
32*5a923131SAndroid Build Coastguard Worker if (!boot_control_) {
33*5a923131SAndroid Build Coastguard Worker LOG(WARNING) << "Unable to create BootControl instance, using stub "
34*5a923131SAndroid Build Coastguard Worker << "instead. All update attempts will fail.";
35*5a923131SAndroid Build Coastguard Worker boot_control_.reset(new BootControlStub());
36*5a923131SAndroid Build Coastguard Worker }
37*5a923131SAndroid Build Coastguard Worker
38*5a923131SAndroid Build Coastguard Worker hardware_ = hardware::CreateHardware();
39*5a923131SAndroid Build Coastguard Worker if (!hardware_) {
40*5a923131SAndroid Build Coastguard Worker LOG(ERROR) << "Error initializing the HardwareInterface.";
41*5a923131SAndroid Build Coastguard Worker return false;
42*5a923131SAndroid Build Coastguard Worker }
43*5a923131SAndroid Build Coastguard Worker
44*5a923131SAndroid Build Coastguard Worker LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
45*5a923131SAndroid Build Coastguard Worker LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
46*5a923131SAndroid Build Coastguard Worker
47*5a923131SAndroid Build Coastguard Worker // Initialize prefs.
48*5a923131SAndroid Build Coastguard Worker base::FilePath non_volatile_path;
49*5a923131SAndroid Build Coastguard Worker if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
50*5a923131SAndroid Build Coastguard Worker prefs_.reset(new MemoryPrefs());
51*5a923131SAndroid Build Coastguard Worker LOG(WARNING)
52*5a923131SAndroid Build Coastguard Worker << "Could not get a non-volatile directory, fall back to memory prefs";
53*5a923131SAndroid Build Coastguard Worker } else {
54*5a923131SAndroid Build Coastguard Worker Prefs* prefs = new Prefs();
55*5a923131SAndroid Build Coastguard Worker prefs_.reset(prefs);
56*5a923131SAndroid Build Coastguard Worker if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
57*5a923131SAndroid Build Coastguard Worker LOG(ERROR) << "Failed to initialize preferences.";
58*5a923131SAndroid Build Coastguard Worker return false;
59*5a923131SAndroid Build Coastguard Worker }
60*5a923131SAndroid Build Coastguard Worker }
61*5a923131SAndroid Build Coastguard Worker
62*5a923131SAndroid Build Coastguard Worker // The CertificateChecker singleton is used by the update attempter.
63*5a923131SAndroid Build Coastguard Worker certificate_checker_.reset(
64*5a923131SAndroid Build Coastguard Worker new CertificateChecker(prefs_.get(), &openssl_wrapper_));
65*5a923131SAndroid Build Coastguard Worker certificate_checker_->Init();
66*5a923131SAndroid Build Coastguard Worker
67*5a923131SAndroid Build Coastguard Worker // Initialize the UpdateAttempter before the UpdateManager.
68*5a923131SAndroid Build Coastguard Worker update_attempter_.reset(
69*5a923131SAndroid Build Coastguard Worker new UpdateAttempterAndroid(this,
70*5a923131SAndroid Build Coastguard Worker prefs_.get(),
71*5a923131SAndroid Build Coastguard Worker boot_control_.get(),
72*5a923131SAndroid Build Coastguard Worker hardware_.get(),
73*5a923131SAndroid Build Coastguard Worker ApexHandlerInterface::CreateApexHandler()));
74*5a923131SAndroid Build Coastguard Worker
75*5a923131SAndroid Build Coastguard Worker return true;
76*5a923131SAndroid Build Coastguard Worker }
77*5a923131SAndroid Build Coastguard Worker
StartUpdater()78*5a923131SAndroid Build Coastguard Worker bool DaemonStateAndroid::StartUpdater() {
79*5a923131SAndroid Build Coastguard Worker // The DaemonState in Android is a passive daemon. It will only start applying
80*5a923131SAndroid Build Coastguard Worker // an update when instructed to do so from the exposed binder API.
81*5a923131SAndroid Build Coastguard Worker update_attempter_->Init();
82*5a923131SAndroid Build Coastguard Worker return true;
83*5a923131SAndroid Build Coastguard Worker }
84*5a923131SAndroid Build Coastguard Worker
AddObserver(ServiceObserverInterface * observer)85*5a923131SAndroid Build Coastguard Worker void DaemonStateAndroid::AddObserver(ServiceObserverInterface* observer) {
86*5a923131SAndroid Build Coastguard Worker service_observers_.insert(observer);
87*5a923131SAndroid Build Coastguard Worker }
88*5a923131SAndroid Build Coastguard Worker
RemoveObserver(ServiceObserverInterface * observer)89*5a923131SAndroid Build Coastguard Worker void DaemonStateAndroid::RemoveObserver(ServiceObserverInterface* observer) {
90*5a923131SAndroid Build Coastguard Worker service_observers_.erase(observer);
91*5a923131SAndroid Build Coastguard Worker }
92*5a923131SAndroid Build Coastguard Worker
service_delegate()93*5a923131SAndroid Build Coastguard Worker ServiceDelegateAndroidInterface* DaemonStateAndroid::service_delegate() {
94*5a923131SAndroid Build Coastguard Worker return update_attempter_.get();
95*5a923131SAndroid Build Coastguard Worker }
96*5a923131SAndroid Build Coastguard Worker
97*5a923131SAndroid Build Coastguard Worker } // namespace chromeos_update_engine
98