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 | |
12 translate::CldDataSource* g_instance = NULL; | |
13 | |
picksi1
2014/09/29 15:59:08
Definite Deja Vu now!
Andrew Hayden (chromium.org)
2014/10/30 14:25:15
Done.
| |
14 // Global instances for the builtin data source types | |
15 struct WrappedNone { | |
16 WrappedNone() { | |
17 value = new translate::CldDataSource("none", false, false); | |
18 } | |
picksi1
2014/09/29 15:59:08
Can we turn <Bool>,<Bool> into either 2 enums or (
Andrew Hayden (chromium.org)
2014/10/30 14:25:15
Done.
| |
19 translate::CldDataSource* value; | |
20 }; | |
21 base::LazyInstance<WrappedNone>::Leaky g_wrapped_none = | |
22 LAZY_INSTANCE_INITIALIZER; | |
23 | |
24 struct WrappedStatic { | |
25 WrappedStatic() { | |
26 value = new translate::CldDataSource("static", false, false); | |
27 } | |
28 translate::CldDataSource* value; | |
29 }; | |
30 base::LazyInstance<WrappedStatic>::Leaky g_wrapped_static = | |
31 LAZY_INSTANCE_INITIALIZER; | |
32 | |
33 struct WrappedStandalone { | |
34 WrappedStandalone() { | |
35 value = new translate::CldDataSource("standalone", false, true); | |
36 } | |
37 translate::CldDataSource* value; | |
38 }; | |
39 base::LazyInstance<WrappedStandalone>::Leaky g_wrapped_standalone = | |
40 LAZY_INSTANCE_INITIALIZER; | |
41 | |
42 struct WrappedComponent { | |
43 WrappedComponent() { | |
44 value = new translate::CldDataSource("component", true, false); | |
45 } | |
46 translate::CldDataSource* value; | |
47 }; | |
48 base::LazyInstance<WrappedComponent>::Leaky g_wrapped_component = | |
49 LAZY_INSTANCE_INITIALIZER; | |
50 | |
51 } // namespace | |
52 | |
53 namespace translate { | |
54 | |
55 CldDataSource:: CldDataSource( | |
56 std::string name, bool register_for_component_updates, | |
57 bool use_standalone_data_file) : | |
58 m_name(name), | |
59 m_register_for_component_updates(register_for_component_updates), | |
60 m_use_standalone_data_file(use_standalone_data_file), | |
61 m_cached_filepath(), | |
62 m_file_lock() { | |
63 } | |
64 | |
65 std::string CldDataSource::GetName() { | |
66 return m_name; | |
67 } | |
68 | |
69 bool CldDataSource::ShouldRegisterForComponentUpdates() { | |
70 return m_register_for_component_updates; | |
71 } | |
72 | |
73 bool CldDataSource::ShouldUseStandaloneDataFile() { | |
74 return m_use_standalone_data_file; | |
75 } | |
76 | |
77 void CldDataSource::SetCldDataFilePath(const base::FilePath& path) { | |
78 base::AutoLock lock(m_file_lock); | |
79 m_cached_filepath = path; | |
80 } | |
81 | |
82 base::FilePath CldDataSource::GetCldDataFilePath() { | |
83 base::AutoLock lock(m_file_lock); | |
84 return m_cached_filepath; | |
85 } | |
86 | |
87 // Static methods below. | |
88 void CldDataSource::Set(CldDataSource* data_source, bool overwrite) { | |
89 if (overwrite || g_instance == NULL) { | |
90 g_instance = data_source; | |
91 } | |
92 } | |
93 | |
94 CldDataSource* CldDataSource::Get() { | |
95 if (g_instance == NULL) { | |
96 // No source set. OK for test code and narrow use cases, but bad if it is | |
97 // a real process. Since there's no good way to differentiate these use | |
98 // cases at runtime, don't log a warning here. | |
99 return NONE(); | |
picksi1
2014/09/29 15:59:08
Can the constructor default this to NONE?
Andrew Hayden (chromium.org)
2014/10/30 14:25:15
Obviated.
| |
100 } | |
101 return g_instance; | |
102 } | |
103 | |
104 CldDataSource* CldDataSource::NONE() { | |
105 return g_wrapped_none.Get().value; | |
106 } | |
107 | |
108 CldDataSource* CldDataSource::STATIC() { | |
109 return g_wrapped_static.Get().value; | |
110 } | |
111 | |
112 CldDataSource* CldDataSource::STANDALONE() { | |
113 return g_wrapped_standalone.Get().value; | |
114 } | |
115 | |
116 CldDataSource* CldDataSource::COMPONENT() { | |
117 return g_wrapped_component.Get().value; | |
118 } | |
119 | |
120 } // namespace translate | |
OLD | NEW |