Home | History | Annotate | Download | only in cros
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/cros/cros_library.h"
      6 
      7 #include "base/lazy_instance.h"
      8 #include "chrome/browser/chromeos/cros/brightness_library.h"
      9 #include "chrome/browser/chromeos/cros/burn_library.h"
     10 #include "chrome/browser/chromeos/cros/cros_library_loader.h"
     11 #include "chrome/browser/chromeos/cros/cryptohome_library.h"
     12 #include "chrome/browser/chromeos/cros/input_method_library.h"
     13 #include "chrome/browser/chromeos/cros/libcros_service_library.h"
     14 #include "chrome/browser/chromeos/cros/login_library.h"
     15 #include "chrome/browser/chromeos/cros/mount_library.h"
     16 #include "chrome/browser/chromeos/cros/network_library.h"
     17 #include "chrome/browser/chromeos/cros/power_library.h"
     18 #include "chrome/browser/chromeos/cros/screen_lock_library.h"
     19 #include "chrome/browser/chromeos/cros/speech_synthesis_library.h"
     20 #include "chrome/browser/chromeos/cros/syslogs_library.h"
     21 #include "chrome/browser/chromeos/cros/touchpad_library.h"
     22 #include "chrome/browser/chromeos/cros/update_library.h"
     23 
     24 #define DEFINE_GET_LIBRARY_METHOD(class_prefix, var_prefix)                    \
     25 class_prefix##Library* CrosLibrary::Get##class_prefix##Library() {             \
     26   return var_prefix##_lib_.GetDefaultImpl(use_stub_impl_);                     \
     27 }
     28 
     29 #define DEFINE_SET_LIBRARY_METHOD(class_prefix, var_prefix)                    \
     30 void CrosLibrary::TestApi::Set##class_prefix##Library(                         \
     31     class_prefix##Library* library, bool own) {                                \
     32   library_->var_prefix##_lib_.SetImpl(library, own);                           \
     33 }
     34 
     35 
     36 namespace chromeos {
     37 
     38 static base::LazyInstance<CrosLibrary> g_cros_library(
     39     base::LINKER_INITIALIZED);
     40 
     41 CrosLibrary::CrosLibrary() : library_loader_(NULL),
     42                              own_library_loader_(false),
     43                              use_stub_impl_(false),
     44                              loaded_(false),
     45                              load_error_(false),
     46                              test_api_(NULL) {
     47 }
     48 
     49 CrosLibrary::~CrosLibrary() {
     50   if (own_library_loader_)
     51     delete library_loader_;
     52 }
     53 
     54 // static
     55 CrosLibrary* CrosLibrary::Get() {
     56   return g_cros_library.Pointer();
     57 }
     58 
     59 DEFINE_GET_LIBRARY_METHOD(Brightness, brightness);
     60 DEFINE_GET_LIBRARY_METHOD(Burn, burn);
     61 DEFINE_GET_LIBRARY_METHOD(Cryptohome, crypto);
     62 DEFINE_GET_LIBRARY_METHOD(InputMethod, input_method);
     63 DEFINE_GET_LIBRARY_METHOD(LibCrosService, libcros_service);
     64 DEFINE_GET_LIBRARY_METHOD(Login, login);
     65 DEFINE_GET_LIBRARY_METHOD(Mount, mount);
     66 DEFINE_GET_LIBRARY_METHOD(Network, network);
     67 DEFINE_GET_LIBRARY_METHOD(Power, power);
     68 DEFINE_GET_LIBRARY_METHOD(ScreenLock, screen_lock);
     69 DEFINE_GET_LIBRARY_METHOD(SpeechSynthesis, speech_synthesis);
     70 DEFINE_GET_LIBRARY_METHOD(Syslogs, syslogs);
     71 DEFINE_GET_LIBRARY_METHOD(Touchpad, touchpad);
     72 DEFINE_GET_LIBRARY_METHOD(Update, update);
     73 
     74 bool CrosLibrary::EnsureLoaded() {
     75   if (use_stub_impl_)
     76     return true;
     77 
     78   if (!loaded_ && !load_error_) {
     79     if (!library_loader_) {
     80       library_loader_ = new CrosLibraryLoader();
     81       own_library_loader_ = true;
     82     }
     83     loaded_ = library_loader_->Load(&load_error_string_);
     84     load_error_ = !loaded_;
     85   }
     86   return loaded_;
     87 }
     88 
     89 CrosLibrary::TestApi* CrosLibrary::GetTestApi() {
     90   if (!test_api_.get())
     91     test_api_.reset(new TestApi(this));
     92   return test_api_.get();
     93 }
     94 
     95 void CrosLibrary::TestApi::SetUseStubImpl() {
     96   library_->use_stub_impl_ = true;
     97 }
     98 
     99 void CrosLibrary::TestApi::ResetUseStubImpl() {
    100   library_->use_stub_impl_ = false;
    101 }
    102 
    103 void CrosLibrary::TestApi::SetLibraryLoader(LibraryLoader* loader, bool own) {
    104   if (library_->library_loader_ == loader)
    105     return;
    106   if (library_->own_library_loader_)
    107     delete library_->library_loader_;
    108   library_->own_library_loader_ = own;
    109   library_->library_loader_ = loader;
    110   // Reset load flags when loader changes. Otherwise some tests are really not
    111   // going to be happy.
    112   library_->loaded_ = false;
    113   library_->load_error_ = false;
    114 }
    115 
    116 DEFINE_SET_LIBRARY_METHOD(Brightness, brightness);
    117 DEFINE_SET_LIBRARY_METHOD(Burn, burn);
    118 DEFINE_SET_LIBRARY_METHOD(Cryptohome, crypto);
    119 DEFINE_SET_LIBRARY_METHOD(InputMethod, input_method);
    120 DEFINE_SET_LIBRARY_METHOD(LibCrosService, libcros_service);
    121 DEFINE_SET_LIBRARY_METHOD(Login, login);
    122 DEFINE_SET_LIBRARY_METHOD(Mount, mount);
    123 DEFINE_SET_LIBRARY_METHOD(Network, network);
    124 DEFINE_SET_LIBRARY_METHOD(Power, power);
    125 DEFINE_SET_LIBRARY_METHOD(ScreenLock, screen_lock);
    126 DEFINE_SET_LIBRARY_METHOD(SpeechSynthesis, speech_synthesis);
    127 DEFINE_SET_LIBRARY_METHOD(Syslogs, syslogs);
    128 DEFINE_SET_LIBRARY_METHOD(Touchpad, touchpad);
    129 DEFINE_SET_LIBRARY_METHOD(Update, update);
    130 
    131 } // namespace chromeos
    132