Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / translate / cld_data_harness_factory.cc
blob6ceafc53483c6247833d656ff18a15eac70df775
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 "chrome/browser/translate/cld_data_harness_factory.h"
7 #include "base/lazy_instance.h"
8 #include "chrome/browser/translate/component_cld_data_harness.h"
9 #include "chrome/browser/translate/standalone_cld_data_harness.h"
10 #include "components/translate/content/browser/browser_cld_utils.h"
11 #include "components/translate/content/common/cld_data_source.h"
13 namespace {
15 // This is the global instance managed by Get/Set
16 test::CldDataHarnessFactory* g_instance = NULL;
18 // The following three instances are used to "cheat" in Create(). Each of them
19 // is just an instance of this class, but because they are singletons they can
20 // be compared using "this" to pick different behaviors in Create() at runtime.
21 // This avoids the need to explicitly define boilerplate classes with no
22 // significant value.
24 // Embedders can of course specify a different implementation of this class
25 // using Set(CldDataHarnessFactory*), in which case Create() will have whatever
26 // custom behavior is desired.
27 base::LazyInstance<test::CldDataHarnessFactory>::Leaky g_wrapped_component =
28 LAZY_INSTANCE_INITIALIZER;
29 base::LazyInstance<test::CldDataHarnessFactory>::Leaky g_wrapped_standalone =
30 LAZY_INSTANCE_INITIALIZER;
31 base::LazyInstance<test::CldDataHarnessFactory>::Leaky g_wrapped_static =
32 LAZY_INSTANCE_INITIALIZER;
34 } // namespace
36 namespace test {
38 scoped_ptr<CldDataHarness> CldDataHarnessFactory::CreateCldDataHarness() {
39 // Cheat: Since the three "canned" implementations are all well-known, just
40 // check to see if "this" points to one of the singletons and then return
41 // the right answer. Embedder-provided CldDataHarnessFactory implementations
42 // will of course not have this behavior.
43 if (this == GetStaticDataHarnessFactory()) {
44 return CldDataHarness::CreateStaticDataHarness();
46 if (this == GetStandaloneDataHarnessFactory()) {
47 return CldDataHarness::CreateStandaloneDataHarness();
49 if (this == GetComponentDataHarnessFactory()) {
50 return CldDataHarness::CreateComponentDataHarness();
53 // Embedder did something wrong, failed to override this method.
54 NOTREACHED() << "Subclass failed to override CreateCldDataHarness()";
55 return CldDataHarness::CreateStaticDataHarness();
58 CldDataHarnessFactory* CldDataHarnessFactory::Get() {
59 // First honor any factory set by the embedder.
60 if (g_instance != NULL) return g_instance;
62 // Fall back to defaults: try to find a compatible harness for the currently-
63 // configured CldDataSource.
64 translate::BrowserCldUtils::ConfigureDefaultDataProvider();
65 if (translate::CldDataSource::IsUsingStaticDataSource()) {
66 return GetStaticDataHarnessFactory();
68 if (translate::CldDataSource::IsUsingComponentDataSource()) {
69 return GetComponentDataHarnessFactory();
71 if (translate::CldDataSource::IsUsingStandaloneDataSource()) {
72 return GetStandaloneDataHarnessFactory();
75 // The CldDataSource must have been set by an embedder, but the embedder has
76 // not configured a corresponding CldDataHarnessFactory. This is an error; the
77 // embedder must specify a harness that works with the corresponding
78 // CldDataSource. Crash for debug builds and return a no-op harness for
79 // everything else.
80 NOTREACHED() << "No CLD data harness factory was configured!";
81 return GetStaticDataHarnessFactory();
84 // static
85 void CldDataHarnessFactory::Set(CldDataHarnessFactory* instance) {
86 g_instance = instance;
89 // static
90 CldDataHarnessFactory*
91 CldDataHarnessFactory::GetStaticDataHarnessFactory() {
92 return &g_wrapped_static.Get();
95 // static
96 CldDataHarnessFactory*
97 CldDataHarnessFactory::GetStandaloneDataHarnessFactory() {
98 return &g_wrapped_standalone.Get();
101 // static
102 CldDataHarnessFactory*
103 CldDataHarnessFactory::GetComponentDataHarnessFactory() {
104 return &g_wrapped_component.Get();
107 } // namespace test