Index: components/translate/content/common/cld_data_source.cc |
diff --git a/components/translate/content/common/cld_data_source.cc b/components/translate/content/common/cld_data_source.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..40b0a9ef7ee209a5c54f44280f747a0a549cad0c |
--- /dev/null |
+++ b/components/translate/content/common/cld_data_source.cc |
@@ -0,0 +1,120 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/translate/content/common/cld_data_source.h" |
+ |
+#include "base/lazy_instance.h" |
+ |
+namespace { |
+ |
+// This is the global instance managed by Get/Set |
+translate::CldDataSource* g_instance = NULL; |
+ |
picksi1
2014/09/29 15:59:08
Definite Deja Vu now!
Andrew Hayden (chromium.org)
2014/10/30 14:25:15
Done.
|
+// Global instances for the builtin data source types |
+struct WrappedNone { |
+ WrappedNone() { |
+ value = new translate::CldDataSource("none", false, false); |
+ } |
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.
|
+ translate::CldDataSource* value; |
+}; |
+base::LazyInstance<WrappedNone>::Leaky g_wrapped_none = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+struct WrappedStatic { |
+ WrappedStatic() { |
+ value = new translate::CldDataSource("static", false, false); |
+ } |
+ translate::CldDataSource* value; |
+}; |
+base::LazyInstance<WrappedStatic>::Leaky g_wrapped_static = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+struct WrappedStandalone { |
+ WrappedStandalone() { |
+ value = new translate::CldDataSource("standalone", false, true); |
+ } |
+ translate::CldDataSource* value; |
+}; |
+base::LazyInstance<WrappedStandalone>::Leaky g_wrapped_standalone = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+struct WrappedComponent { |
+ WrappedComponent() { |
+ value = new translate::CldDataSource("component", true, false); |
+ } |
+ translate::CldDataSource* value; |
+}; |
+base::LazyInstance<WrappedComponent>::Leaky g_wrapped_component = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+} // namespace |
+ |
+namespace translate { |
+ |
+CldDataSource:: CldDataSource( |
+ std::string name, bool register_for_component_updates, |
+ bool use_standalone_data_file) : |
+ m_name(name), |
+ m_register_for_component_updates(register_for_component_updates), |
+ m_use_standalone_data_file(use_standalone_data_file), |
+ m_cached_filepath(), |
+ m_file_lock() { |
+} |
+ |
+std::string CldDataSource::GetName() { |
+ return m_name; |
+} |
+ |
+bool CldDataSource::ShouldRegisterForComponentUpdates() { |
+ return m_register_for_component_updates; |
+} |
+ |
+bool CldDataSource::ShouldUseStandaloneDataFile() { |
+ return m_use_standalone_data_file; |
+} |
+ |
+void CldDataSource::SetCldDataFilePath(const base::FilePath& path) { |
+ base::AutoLock lock(m_file_lock); |
+ m_cached_filepath = path; |
+} |
+ |
+base::FilePath CldDataSource::GetCldDataFilePath() { |
+ base::AutoLock lock(m_file_lock); |
+ return m_cached_filepath; |
+} |
+ |
+// Static methods below. |
+void CldDataSource::Set(CldDataSource* data_source, bool overwrite) { |
+ if (overwrite || g_instance == NULL) { |
+ g_instance = data_source; |
+ } |
+} |
+ |
+CldDataSource* CldDataSource::Get() { |
+ if (g_instance == NULL) { |
+ // No source set. OK for test code and narrow use cases, but bad if it is |
+ // a real process. Since there's no good way to differentiate these use |
+ // cases at runtime, don't log a warning here. |
+ 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.
|
+ } |
+ return g_instance; |
+} |
+ |
+CldDataSource* CldDataSource::NONE() { |
+ return g_wrapped_none.Get().value; |
+} |
+ |
+CldDataSource* CldDataSource::STATIC() { |
+ return g_wrapped_static.Get().value; |
+} |
+ |
+CldDataSource* CldDataSource::STANDALONE() { |
+ return g_wrapped_standalone.Get().value; |
+} |
+ |
+CldDataSource* CldDataSource::COMPONENT() { |
+ return g_wrapped_component.Get().value; |
+} |
+ |
+} // namespace translate |