1 // Copyright 2014 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.
5 #include "components/translate/content/common/cld_data_source.h"
7 #include "base/lazy_instance.h"
11 // This is the global instance managed by Get/Set/SetDefault
12 translate::CldDataSource
* g_instance
= NULL
;
14 // Global instances for the builtin data source types
15 base::LazyInstance
<translate::CldDataSource
>::Leaky g_wrapped_static
=
16 LAZY_INSTANCE_INITIALIZER
;
17 base::LazyInstance
<translate::CldDataSource
>::Leaky g_wrapped_standalone
=
18 LAZY_INSTANCE_INITIALIZER
;
19 base::LazyInstance
<translate::CldDataSource
>::Leaky g_wrapped_component
=
20 LAZY_INSTANCE_INITIALIZER
;
22 bool g_disable_sanity_checks_for_test
= false;
28 CldDataSource::CldDataSource() : m_cached_filepath(), m_file_lock() {
31 std::string
CldDataSource::GetName() {
32 if (this == GetStaticDataSource()) return "static";
33 if (this == GetComponentDataSource()) return "component";
34 if (this == GetStandaloneDataSource()) return "standalone";
35 NOTREACHED() << "Subclass failed to override GetName()";
40 void CldDataSource::DisableSanityChecksForTest() {
41 g_disable_sanity_checks_for_test
= true;
45 void CldDataSource::EnableSanityChecksForTest() {
46 g_disable_sanity_checks_for_test
= false;
49 void CldDataSource::SetCldDataFilePath(const base::FilePath
& path
) {
50 DCHECK(g_disable_sanity_checks_for_test
||
51 this == GetComponentDataSource() ||
52 this == GetStandaloneDataSource()) << "CLD Data Source misconfigured!";
53 base::AutoLock
lock(m_file_lock
);
54 m_cached_filepath
= path
;
57 base::FilePath
CldDataSource::GetCldDataFilePath() {
58 DCHECK(g_disable_sanity_checks_for_test
||
59 this == GetComponentDataSource() ||
60 this == GetStandaloneDataSource()) << "CLD Data Source misconfigured!";
61 base::AutoLock
lock(m_file_lock
);
62 return m_cached_filepath
;
66 void CldDataSource::SetDefault(CldDataSource
* data_source
) {
67 if (g_instance
== NULL
) Set(data_source
);
71 void CldDataSource::Set(CldDataSource
* data_source
) {
72 g_instance
= data_source
;
76 CldDataSource
* CldDataSource::Get() {
77 DCHECK(g_instance
!= NULL
) << "No CLD data source was configured!";
78 if (g_instance
!= NULL
) return g_instance
;
79 return GetStaticDataSource();
83 CldDataSource
* CldDataSource::GetStaticDataSource() {
84 return &g_wrapped_static
.Get();
88 bool CldDataSource::IsUsingStaticDataSource() {
89 return Get()->GetName() == "static";
93 CldDataSource
* CldDataSource::GetStandaloneDataSource() {
94 return &g_wrapped_standalone
.Get();
98 bool CldDataSource::IsUsingStandaloneDataSource() {
99 return Get()->GetName() == "standalone";
103 CldDataSource
* CldDataSource::GetComponentDataSource() {
104 return &g_wrapped_component
.Get();
108 bool CldDataSource::IsUsingComponentDataSource() {
109 return Get()->GetName() == "component";
112 } // namespace translate