daemon_state_android.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "update_engine/daemon_state_android.h"
  17. #include <base/logging.h>
  18. #include "update_engine/common/boot_control.h"
  19. #include "update_engine/common/boot_control_stub.h"
  20. #include "update_engine/common/hardware.h"
  21. #include "update_engine/common/prefs.h"
  22. #include "update_engine/update_attempter_android.h"
  23. namespace chromeos_update_engine {
  24. bool DaemonStateAndroid::Initialize() {
  25. boot_control_ = boot_control::CreateBootControl();
  26. if (!boot_control_) {
  27. LOG(WARNING) << "Unable to create BootControl instance, using stub "
  28. << "instead. All update attempts will fail.";
  29. boot_control_.reset(new BootControlStub());
  30. }
  31. hardware_ = hardware::CreateHardware();
  32. if (!hardware_) {
  33. LOG(ERROR) << "Error initializing the HardwareInterface.";
  34. return false;
  35. }
  36. LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
  37. LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
  38. // Initialize prefs.
  39. base::FilePath non_volatile_path;
  40. // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
  41. // available.
  42. if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
  43. LOG(ERROR) << "Failed to get a non-volatile directory.";
  44. return false;
  45. }
  46. Prefs* prefs = new Prefs();
  47. prefs_.reset(prefs);
  48. if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
  49. LOG(ERROR) << "Failed to initialize preferences.";
  50. return false;
  51. }
  52. // The CertificateChecker singleton is used by the update attempter.
  53. certificate_checker_.reset(
  54. new CertificateChecker(prefs_.get(), &openssl_wrapper_));
  55. certificate_checker_->Init();
  56. // Initialize the UpdateAttempter before the UpdateManager.
  57. update_attempter_.reset(new UpdateAttempterAndroid(
  58. this, prefs_.get(), boot_control_.get(), hardware_.get()));
  59. return true;
  60. }
  61. bool DaemonStateAndroid::StartUpdater() {
  62. // The DaemonState in Android is a passive daemon. It will only start applying
  63. // an update when instructed to do so from the exposed binder API.
  64. update_attempter_->Init();
  65. return true;
  66. }
  67. void DaemonStateAndroid::AddObserver(ServiceObserverInterface* observer) {
  68. service_observers_.insert(observer);
  69. }
  70. void DaemonStateAndroid::RemoveObserver(ServiceObserverInterface* observer) {
  71. service_observers_.erase(observer);
  72. }
  73. ServiceDelegateAndroidInterface* DaemonStateAndroid::service_delegate() {
  74. return update_attempter_.get();
  75. }
  76. } // namespace chromeos_update_engine