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)                    \
25class_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)                    \
30void CrosLibrary::TestApi::Set##class_prefix##Library(                         \
31    class_prefix##Library* library, bool own) {                                \
32  library_->var_prefix##_lib_.SetImpl(library, own);                           \
33}
34
35
36namespace chromeos {
37
38static base::LazyInstance<CrosLibrary> g_cros_library(
39    base::LINKER_INITIALIZED);
40
41CrosLibrary::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
49CrosLibrary::~CrosLibrary() {
50  if (own_library_loader_)
51    delete library_loader_;
52}
53
54// static
55CrosLibrary* CrosLibrary::Get() {
56  return g_cros_library.Pointer();
57}
58
59DEFINE_GET_LIBRARY_METHOD(Brightness, brightness);
60DEFINE_GET_LIBRARY_METHOD(Burn, burn);
61DEFINE_GET_LIBRARY_METHOD(Cryptohome, crypto);
62DEFINE_GET_LIBRARY_METHOD(InputMethod, input_method);
63DEFINE_GET_LIBRARY_METHOD(LibCrosService, libcros_service);
64DEFINE_GET_LIBRARY_METHOD(Login, login);
65DEFINE_GET_LIBRARY_METHOD(Mount, mount);
66DEFINE_GET_LIBRARY_METHOD(Network, network);
67DEFINE_GET_LIBRARY_METHOD(Power, power);
68DEFINE_GET_LIBRARY_METHOD(ScreenLock, screen_lock);
69DEFINE_GET_LIBRARY_METHOD(SpeechSynthesis, speech_synthesis);
70DEFINE_GET_LIBRARY_METHOD(Syslogs, syslogs);
71DEFINE_GET_LIBRARY_METHOD(Touchpad, touchpad);
72DEFINE_GET_LIBRARY_METHOD(Update, update);
73
74bool 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
89CrosLibrary::TestApi* CrosLibrary::GetTestApi() {
90  if (!test_api_.get())
91    test_api_.reset(new TestApi(this));
92  return test_api_.get();
93}
94
95void CrosLibrary::TestApi::SetUseStubImpl() {
96  library_->use_stub_impl_ = true;
97}
98
99void CrosLibrary::TestApi::ResetUseStubImpl() {
100  library_->use_stub_impl_ = false;
101}
102
103void 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
116DEFINE_SET_LIBRARY_METHOD(Brightness, brightness);
117DEFINE_SET_LIBRARY_METHOD(Burn, burn);
118DEFINE_SET_LIBRARY_METHOD(Cryptohome, crypto);
119DEFINE_SET_LIBRARY_METHOD(InputMethod, input_method);
120DEFINE_SET_LIBRARY_METHOD(LibCrosService, libcros_service);
121DEFINE_SET_LIBRARY_METHOD(Login, login);
122DEFINE_SET_LIBRARY_METHOD(Mount, mount);
123DEFINE_SET_LIBRARY_METHOD(Network, network);
124DEFINE_SET_LIBRARY_METHOD(Power, power);
125DEFINE_SET_LIBRARY_METHOD(ScreenLock, screen_lock);
126DEFINE_SET_LIBRARY_METHOD(SpeechSynthesis, speech_synthesis);
127DEFINE_SET_LIBRARY_METHOD(Syslogs, syslogs);
128DEFINE_SET_LIBRARY_METHOD(Touchpad, touchpad);
129DEFINE_SET_LIBRARY_METHOD(Update, update);
130
131} // namespace chromeos
132