| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 1 | // | 
|  | 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 |  | 
|  | 17 | #include "update_engine/daemon_state_android.h" | 
|  | 18 |  | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 19 | #include <base/logging.h> | 
|  | 20 |  | 
|  | 21 | #include "update_engine/common/boot_control.h" | 
|  | 22 | #include "update_engine/common/boot_control_stub.h" | 
|  | 23 | #include "update_engine/common/hardware.h" | 
|  | 24 | #include "update_engine/common/prefs.h" | 
|  | 25 | #include "update_engine/update_attempter_android.h" | 
|  | 26 |  | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 27 | namespace chromeos_update_engine { | 
|  | 28 |  | 
|  | 29 | bool DaemonStateAndroid::Initialize() { | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 30 | 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_) { | 
|  | 39 | LOG(ERROR) << "Error intializing the HardwareInterface."; | 
|  | 40 | 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; | 
|  | 48 | // TODO(deymo): Fall back to in-memory prefs if there's no physical directory | 
|  | 49 | // available. | 
|  | 50 | if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) { | 
|  | 51 | LOG(ERROR) << "Failed to get a non-volatile directory."; | 
|  | 52 | return false; | 
|  | 53 | } | 
|  | 54 | Prefs* prefs = new Prefs(); | 
|  | 55 | prefs_.reset(prefs); | 
|  | 56 | if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) { | 
|  | 57 | LOG(ERROR) << "Failed to initialize preferences."; | 
|  | 58 | return false; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | // Initialize the UpdateAttempter before the UpdateManager. | 
|  | 62 | update_attempter_.reset(new UpdateAttempterAndroid( | 
|  | 63 | this, prefs_.get(), boot_control_.get(), hardware_.get())); | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 64 |  | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 65 | return true; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | bool DaemonStateAndroid::StartUpdater() { | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 69 | // The DaemonState in Android is a passive daemon. It will only start applying | 
|  | 70 | // an update when instructed to do so from the exposed binder API. | 
| Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 71 | update_attempter_->Init(); | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 72 | return true; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | void DaemonStateAndroid::AddObserver(ServiceObserverInterface* observer) { | 
|  | 76 | service_observers_.insert(observer); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | void DaemonStateAndroid::RemoveObserver(ServiceObserverInterface* observer) { | 
|  | 80 | service_observers_.erase(observer); | 
|  | 81 | } | 
|  | 82 |  | 
| Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 83 | ServiceDelegateAndroidInterface* DaemonStateAndroid::service_delegate() { | 
| Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 84 | return update_attempter_.get(); | 
| Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 87 | }  // namespace chromeos_update_engine |