OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/translate/content/common/cld_data_source.h" | |
6 | |
7 #include "base/lazy_instance.h" | |
8 | |
9 namespace { | |
10 | |
11 // This is the global instance managed by Get/Set/SetDefault | |
12 translate::CldDataSource* g_instance = NULL; | |
13 | |
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; | |
21 | |
22 bool g_disable_sanity_checks_for_test = false; | |
23 | |
24 } // namespace | |
25 | |
26 namespace translate { | |
27 | |
28 CldDataSource::CldDataSource() : m_cached_filepath(), m_file_lock() { | |
29 } | |
30 | |
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()"; | |
36 return "unknown"; | |
37 } | |
38 | |
39 // static | |
40 void CldDataSource::DisableSanityChecksForTest() { | |
41 g_disable_sanity_checks_for_test = true; | |
42 } | |
43 | |
44 // static | |
45 void CldDataSource::EnableSanityChecksForTest() { | |
46 g_disable_sanity_checks_for_test = false; | |
47 } | |
48 | |
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; | |
55 } | |
56 | |
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; | |
63 } | |
64 | |
65 // static | |
66 void CldDataSource::SetDefault(CldDataSource* data_source) { | |
67 if (g_instance == NULL) Set(data_source); | |
68 } | |
69 | |
70 // static | |
71 void CldDataSource::Set(CldDataSource* data_source) { | |
72 g_instance = data_source; | |
73 } | |
74 | |
75 // static | |
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(); | |
80 } | |
81 | |
82 // static | |
83 CldDataSource* CldDataSource::GetStaticDataSource() { | |
84 return &g_wrapped_static.Get(); | |
85 } | |
86 | |
87 // static | |
88 bool CldDataSource::IsUsingStaticDataSource() { | |
89 return Get()->GetName() == "static"; | |
90 } | |
91 | |
92 // static | |
93 CldDataSource* CldDataSource::GetStandaloneDataSource() { | |
94 return &g_wrapped_standalone.Get(); | |
95 } | |
96 | |
97 // static | |
98 bool CldDataSource::IsUsingStandaloneDataSource() { | |
99 return Get()->GetName() == "standalone"; | |
100 } | |
101 | |
102 // static | |
103 CldDataSource* CldDataSource::GetComponentDataSource() { | |
104 return &g_wrapped_component.Get(); | |
105 } | |
106 | |
107 // static | |
108 bool CldDataSource::IsUsingComponentDataSource() { | |
109 return Get()->GetName() == "component"; | |
110 } | |
111 | |
112 } // namespace translate | |
OLD | NEW |