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 } // namespace |
| 23 |
| 24 namespace translate { |
| 25 |
| 26 CldDataSource::CldDataSource() : m_cached_filepath(), m_file_lock() { |
| 27 } |
| 28 |
| 29 std::string CldDataSource::GetName() { |
| 30 if (this == GetStaticDataSource()) return "static"; |
| 31 if (this == GetComponentDataSource()) return "component"; |
| 32 if (this == GetStandaloneDataSource()) return "standalone"; |
| 33 NOTREACHED() << "Subclass failed to override GetName()"; |
| 34 return "unknown"; |
| 35 } |
| 36 |
| 37 void CldDataSource::SetCldDataFilePath(const base::FilePath& path) { |
| 38 DCHECK(this == GetComponentDataSource() || |
| 39 this == GetStandaloneDataSource()) << "CLD Data source misconfigured!"; |
| 40 base::AutoLock lock(m_file_lock); |
| 41 m_cached_filepath = path; |
| 42 } |
| 43 |
| 44 base::FilePath CldDataSource::GetCldDataFilePath() { |
| 45 DCHECK(this == GetComponentDataSource() || |
| 46 this == GetStandaloneDataSource()) << "CLD Data source misconfigured!"; |
| 47 base::AutoLock lock(m_file_lock); |
| 48 return m_cached_filepath; |
| 49 } |
| 50 |
| 51 // static |
| 52 void CldDataSource::SetDefault(CldDataSource* data_source) { |
| 53 if (g_instance == NULL) Set(data_source); |
| 54 } |
| 55 |
| 56 // static |
| 57 void CldDataSource::Set(CldDataSource* data_source) { |
| 58 g_instance = data_source; |
| 59 } |
| 60 |
| 61 // static |
| 62 CldDataSource* CldDataSource::Get() { |
| 63 DCHECK(g_instance != NULL) << "No CLD data source was configured!"; |
| 64 if (g_instance != NULL) return g_instance; |
| 65 return GetStaticDataSource(); |
| 66 } |
| 67 |
| 68 // static |
| 69 CldDataSource* CldDataSource::GetStaticDataSource() { |
| 70 return &g_wrapped_static.Get(); |
| 71 } |
| 72 |
| 73 // static |
| 74 CldDataSource* CldDataSource::GetStandaloneDataSource() { |
| 75 return &g_wrapped_standalone.Get(); |
| 76 } |
| 77 |
| 78 // static |
| 79 CldDataSource* CldDataSource::GetComponentDataSource() { |
| 80 return &g_wrapped_component.Get(); |
| 81 } |
| 82 |
| 83 } // namespace translate |
OLD | NEW |