OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/cros/cros_library.h" | 5 #include "chrome/browser/chromeos/cros/cros_library.h" |
6 | 6 |
7 #include "chrome/browser/chromeos/cros/burn_library.h" | 7 #include "chrome/browser/chromeos/cros/burn_library.h" |
8 #include "chrome/browser/chromeos/cros/cert_library.h" | 8 #include "chrome/browser/chromeos/cros/cert_library.h" |
9 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | 9 #include "chrome/browser/chromeos/cros/cryptohome_library.h" |
10 #include "chrome/browser/chromeos/cros/library_loader.h" | |
11 #include "chrome/browser/chromeos/cros/network_library.h" | 10 #include "chrome/browser/chromeos/cros/network_library.h" |
12 #include "third_party/cros/chromeos_cros_api.h" | |
13 | 11 |
14 // Pass !libcros_loaded_ to GetDefaultImpl instead of use_stub_impl_ so that | |
15 // we load the stub impl regardless of whether use_stub was specified or the | |
16 // library failed to load. | |
17 #define DEFINE_GET_LIBRARY_METHOD(class_prefix, var_prefix) \ | 12 #define DEFINE_GET_LIBRARY_METHOD(class_prefix, var_prefix) \ |
18 class_prefix##Library* CrosLibrary::Get##class_prefix##Library() { \ | 13 class_prefix##Library* CrosLibrary::Get##class_prefix##Library() { \ |
19 return var_prefix##_lib_.GetDefaultImpl(!libcros_loaded_); \ | 14 return var_prefix##_lib_.GetDefaultImpl(use_stub_impl_); \ |
20 } | 15 } |
21 | 16 |
22 #define DEFINE_SET_LIBRARY_METHOD(class_prefix, var_prefix) \ | 17 #define DEFINE_SET_LIBRARY_METHOD(class_prefix, var_prefix) \ |
23 void CrosLibrary::TestApi::Set##class_prefix##Library( \ | 18 void CrosLibrary::TestApi::Set##class_prefix##Library( \ |
24 class_prefix##Library* library, bool own) { \ | 19 class_prefix##Library* library, bool own) { \ |
25 library_->var_prefix##_lib_.SetImpl(library, own); \ | 20 library_->var_prefix##_lib_.SetImpl(library, own); \ |
26 } | 21 } |
27 | 22 |
28 namespace chromeos { | 23 namespace chromeos { |
29 | 24 |
30 static CrosLibrary* g_cros_library = NULL; | 25 static CrosLibrary* g_cros_library = NULL; |
31 | 26 |
32 CrosLibrary::CrosLibrary(bool use_stub) | 27 CrosLibrary::CrosLibrary(bool use_stub) |
33 : library_loader_(NULL), | 28 : use_stub_impl_(use_stub), |
34 own_library_loader_(false), | |
35 use_stub_impl_(use_stub), | |
36 libcros_loaded_(false), | |
37 load_error_(false), | |
38 test_api_(NULL) { | 29 test_api_(NULL) { |
39 } | 30 } |
40 | 31 |
41 CrosLibrary::~CrosLibrary() { | 32 CrosLibrary::~CrosLibrary() { |
42 if (own_library_loader_) | |
43 delete library_loader_; | |
44 } | 33 } |
45 | 34 |
46 // static | 35 // static |
47 void CrosLibrary::Initialize(bool use_stub) { | 36 void CrosLibrary::Initialize(bool use_stub) { |
48 CHECK(!g_cros_library) << "CrosLibrary: Multiple calls to Initialize()."; | 37 CHECK(!g_cros_library) << "CrosLibrary: Multiple calls to Initialize()."; |
49 g_cros_library = new CrosLibrary(use_stub); | 38 g_cros_library = new CrosLibrary(use_stub); |
50 if (use_stub) { | 39 VLOG_IF(1, use_stub) << "CrosLibrary Initialized with Stub Impl."; |
51 VLOG(1) << "CrosLibrary Initialized with Stub Impl."; | |
52 return; | |
53 } | |
54 // Attempt to load libcros here, so that we can log, show warnings, and | |
55 // set load_error_string_ immediately. | |
56 if (g_cros_library->LoadLibcros()) { | |
57 VLOG(1) << "CrosLibrary Initialized, version = " << kCrosAPIVersion; | |
58 } else { | |
59 LOG(WARNING) << "CrosLibrary failed to Initialize." | |
60 << " Will use stub implementations."; | |
61 } | |
62 } | 40 } |
63 | 41 |
64 // static | 42 // static |
65 void CrosLibrary::Shutdown() { | 43 void CrosLibrary::Shutdown() { |
66 CHECK(g_cros_library) << "CrosLibrary::Shutdown() called with NULL library"; | 44 CHECK(g_cros_library) << "CrosLibrary::Shutdown() called with NULL library"; |
67 VLOG(1) << "CrosLibrary Shutting down..."; | 45 VLOG(1) << "CrosLibrary Shutting down..."; |
68 delete g_cros_library; | 46 delete g_cros_library; |
69 g_cros_library = NULL; | 47 g_cros_library = NULL; |
70 VLOG(1) << " CrosLibrary Shutdown completed."; | 48 VLOG(1) << " CrosLibrary Shutdown completed."; |
71 } | 49 } |
72 | 50 |
73 // static | 51 // static |
74 CrosLibrary* CrosLibrary::Get() { | 52 CrosLibrary* CrosLibrary::Get() { |
75 return g_cros_library; | 53 return g_cros_library; |
76 } | 54 } |
77 | 55 |
78 DEFINE_GET_LIBRARY_METHOD(Burn, burn); | 56 DEFINE_GET_LIBRARY_METHOD(Burn, burn); |
79 DEFINE_GET_LIBRARY_METHOD(Cert, cert); | 57 DEFINE_GET_LIBRARY_METHOD(Cert, cert); |
80 DEFINE_GET_LIBRARY_METHOD(Cryptohome, crypto); | 58 DEFINE_GET_LIBRARY_METHOD(Cryptohome, crypto); |
81 DEFINE_GET_LIBRARY_METHOD(Network, network); | 59 DEFINE_GET_LIBRARY_METHOD(Network, network); |
82 | 60 |
83 bool CrosLibrary::LoadLibcros() { | |
84 if (!libcros_loaded_ && !load_error_) { | |
85 if (!library_loader_) { | |
86 library_loader_ = LibraryLoader::GetImpl(); | |
87 own_library_loader_ = true; | |
88 } | |
89 libcros_loaded_ = library_loader_->Load(&load_error_string_); | |
90 load_error_ = !libcros_loaded_; | |
91 } | |
92 return libcros_loaded_; | |
93 } | |
94 | |
95 CrosLibrary::TestApi* CrosLibrary::GetTestApi() { | 61 CrosLibrary::TestApi* CrosLibrary::GetTestApi() { |
96 if (!test_api_.get()) | 62 if (!test_api_.get()) |
97 test_api_.reset(new TestApi(this)); | 63 test_api_.reset(new TestApi(this)); |
98 return test_api_.get(); | 64 return test_api_.get(); |
99 } | 65 } |
100 | 66 |
101 void CrosLibrary::TestApi::ResetUseStubImpl() { | |
102 library_->use_stub_impl_ = false; | |
103 if (!library_->LoadLibcros()) | |
104 LOG(WARNING) << "ResetUseStubImpl: Unable to load libcros."; | |
105 } | |
106 | |
107 void CrosLibrary::TestApi::SetLibraryLoader(LibraryLoader* loader, bool own) { | |
108 if (library_->library_loader_ == loader) | |
109 return; | |
110 if (library_->own_library_loader_) | |
111 delete library_->library_loader_; | |
112 library_->own_library_loader_ = own; | |
113 library_->library_loader_ = loader; | |
114 // Reset load flags when loader changes. Otherwise some tests are really not | |
115 // going to be happy. | |
116 library_->libcros_loaded_ = false; | |
117 library_->load_error_ = false; | |
118 } | |
119 | |
120 DEFINE_SET_LIBRARY_METHOD(Cert, cert); | 67 DEFINE_SET_LIBRARY_METHOD(Cert, cert); |
121 DEFINE_SET_LIBRARY_METHOD(Burn, burn); | 68 DEFINE_SET_LIBRARY_METHOD(Burn, burn); |
122 DEFINE_SET_LIBRARY_METHOD(Cryptohome, crypto); | 69 DEFINE_SET_LIBRARY_METHOD(Cryptohome, crypto); |
123 DEFINE_SET_LIBRARY_METHOD(Network, network); | 70 DEFINE_SET_LIBRARY_METHOD(Network, network); |
124 | 71 |
125 } // namespace chromeos | 72 } // namespace chromeos |
OLD | NEW |