Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / translate / content / common / cld_data_source.cc
blob9d6e009ce3406b0fd84c2ee3e6f9a84ac9c0881a
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"
9 namespace {
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;
24 } // namespace
26 namespace translate {
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()";
36 return "unknown";
39 // static
40 void CldDataSource::DisableSanityChecksForTest() {
41 g_disable_sanity_checks_for_test = true;
44 // static
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;
65 // static
66 void CldDataSource::SetDefault(CldDataSource* data_source) {
67 if (g_instance == NULL) Set(data_source);
70 // static
71 void CldDataSource::Set(CldDataSource* data_source) {
72 g_instance = data_source;
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();
82 // static
83 CldDataSource* CldDataSource::GetStaticDataSource() {
84 return &g_wrapped_static.Get();
87 // static
88 bool CldDataSource::IsUsingStaticDataSource() {
89 return Get()->GetName() == "static";
92 // static
93 CldDataSource* CldDataSource::GetStandaloneDataSource() {
94 return &g_wrapped_standalone.Get();
97 // static
98 bool CldDataSource::IsUsingStandaloneDataSource() {
99 return Get()->GetName() == "standalone";
102 // static
103 CldDataSource* CldDataSource::GetComponentDataSource() {
104 return &g_wrapped_component.Get();
107 // static
108 bool CldDataSource::IsUsingComponentDataSource() {
109 return Get()->GetName() == "component";
112 } // namespace translate