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 #ifndef COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ | |
6 #define COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/files/file_path.h" | |
11 #include "base/macros.h" | |
12 #include "base/synchronization/lock.h" | |
13 | |
14 namespace component_updater { | |
15 // For friend-class declaration, see private section at bottom of class. | |
16 class CldComponentInstallerTest; | |
17 } | |
18 | |
19 namespace translate { | |
20 | |
21 // Provides high-level functionality related to a CLD Data Source. | |
22 class CldDataSource { | |
23 | |
24 public: | |
25 // Generally not used by Chromium code, but available for embedders to | |
26 // configure additional data sources as subclasses. | |
27 // Chromium code should use the getters (GetStaticDataSource(), | |
28 // GetStandaloneDataSource(), and GetComponentDataSource()) and checkers | |
29 // (IsUsingStaticDataSource(), IsUsingStandaloneDataSource() and | |
30 // IsUsingComponentDataSource()) instead as appropriate. | |
31 CldDataSource(); | |
32 virtual ~CldDataSource() {} | |
33 | |
34 // Returns the symbolic name of the data source. In the Chromium | |
35 // open-source tree, the following data sources exist: | |
36 // static uses the static_[browser|renderer]_cld_data_provider | |
37 // implementations. | |
38 // standalone uses the data_file_[browser|renderer]_cld_data_provider | |
39 // implementations. | |
40 // component also uses the data_file_[browser|renderer]_cld_data_provider | |
41 // implementations. | |
42 // | |
43 // Other implementations based upon Chromium may provide CLD differently and | |
44 // may have other names. | |
45 // This method is threadsafe. | |
46 virtual std::string GetName(); | |
47 | |
48 // For data sources that support a separate CLD data file, configures the path | |
49 // of that data file. | |
50 // | |
51 // The 'component' and 'standalone' data sources need this method to be called | |
52 // in order to locate the CLD data on disk. | |
53 // If the data source doesn't need or doesn't support such configuration, this | |
54 // function is a no-op. This is the case for, e.g., the static data source. | |
55 // This method is threadsafe. | |
56 virtual void SetCldDataFilePath(const base::FilePath& path); | |
57 | |
58 // Returns the path most recently set by SetCldDataFilePath. The initial value | |
59 // prior to any such call is the empty path. If the data source doesn't | |
60 // support a data file, returns the empty path. | |
61 // This method is threadsafe. | |
62 virtual base::FilePath GetCldDataFilePath(); | |
63 | |
64 // Sets the default data source for this process, i.e. the data source to be | |
65 // used unless the embedder calls Set(CldDatasource*). This is the method | |
66 // that normal (i.e., non-test) Chromium code should use; embedders can and | |
67 // should use the unconditional Set(CldDataSource*) method instead. If a | |
68 // default data source has already been set, this method does nothing. | |
69 static void SetDefault(CldDataSource* data_source); | |
70 | |
71 // Unconditionally sets the data source for this process, overwriting any | |
72 // previously-configured default. Normal Chromium code should never use this | |
73 // method; it is provided for embedders to inject a data source from outside | |
74 // of the Chromium code base. Test code can also use this method to force the | |
75 // runtime to have a desired behavior. | |
76 static void Set(CldDataSource* data_source); | |
77 | |
78 // Returns the data source for this process. Guaranteed to never be null. | |
79 // If no instance has been set, this returns the same object obtained by | |
80 // calling GetStaticDataSource(), which is always safe but may fail to | |
81 // function if the CLD data is not *actually* statically linked. | |
82 static CldDataSource* Get(); | |
83 | |
84 // Fetch the global instance of the "static" data source. | |
85 // Only use to call SetDefault(CldDataSource*) or Set(CldDataSource*). | |
86 static CldDataSource* GetStaticDataSource(); | |
87 | |
88 // Returns true if and only if the data source returned by Get() is the | |
89 // "static" data source. | |
90 static bool IsUsingStaticDataSource(); | |
91 | |
92 // Fetch the global instance of the "standalone" data source. | |
93 // Only use to call SetDefault(CldDataSource*) or Set(CldDataSource*). | |
94 static CldDataSource* GetStandaloneDataSource(); | |
95 | |
96 // Returns true if and only if the data source returned by Get() is the | |
97 // "static" data source. | |
98 static bool IsUsingStandaloneDataSource(); | |
99 | |
100 // Fetch the global instance of the "component" data source. | |
101 // Only use to call SetDefault(CldDataSource*) or Set(CldDataSource*). | |
102 static CldDataSource* GetComponentDataSource(); | |
103 | |
104 // Returns true if and only if the data source returned by Get() is the | |
105 // "static" data source. | |
106 static bool IsUsingComponentDataSource(); | |
107 | |
108 private: | |
109 friend class component_updater::CldComponentInstallerTest; | |
110 | |
111 // For unit test code ONLY. Under normal circumstances the calls to | |
112 // SetCldDataFilePath() and GetCldDataFilePath() have a DHECK intended to | |
113 // perform a sanity check on the runtime CLD data source configuration; no | |
114 // production code should be calling SetCldDataFilePath() or | |
115 // GetCldDataFilePath() unless the "component" or "standalone" data source is | |
116 // being used. Unit tests will generally be built with the "static" data | |
117 // source, and this method allows tests to bypass the DCHECK for testing | |
118 // purposes. | |
119 // | |
120 // Unit tests that use this function should use it in SetUp(), and then call | |
121 // EnableSanityChecksForTest() in TearDown() for maximum safety. | |
122 static void DisableSanityChecksForTest(); | |
123 | |
124 // This method [re-]enables the sanity check disabled by | |
125 // DisableSanityChecksForTest(). | |
126 static void EnableSanityChecksForTest(); | |
127 | |
128 base::FilePath m_cached_filepath; // Guarded by m_file_lock | |
129 base::Lock m_file_lock; // Guards m_cached_filepath | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(CldDataSource); | |
132 }; | |
133 | |
134 } // namespace translate | |
135 #endif // COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ | |
OLD | NEW |