OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ |
6 #define COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ | 6 #define COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 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 |
10 namespace translate { | 19 namespace translate { |
11 | 20 |
12 // Provides high-level functionality related to a CLD Data Source. | 21 // Provides high-level functionality related to a CLD Data Source. |
13 class CldDataSource { | 22 class CldDataSource { |
14 | 23 |
15 public: | 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() {} |
16 | 33 |
17 // Returns the symbolic name of the data source. In the Chromium | 34 // Returns the symbolic name of the data source. In the Chromium |
18 // open-source tree, the following data sources exist: | 35 // open-source tree, the following data sources exist: |
19 // static uses the static_[browser|renderer]_cld_data_provider | 36 // static uses the static_[browser|renderer]_cld_data_provider |
20 // implementations. | 37 // implementations. |
21 // standalone uses the data_file_[browser|renderer]_cld_data_provider | 38 // standalone uses the data_file_[browser|renderer]_cld_data_provider |
22 // implementations. | 39 // implementations. |
23 // component also uses the data_file_[browser|renderer]_cld_data_provider | 40 // component also uses the data_file_[browser|renderer]_cld_data_provider |
24 // implementations. | 41 // implementations. |
25 // | 42 // |
26 // Other implementations based upon Chromium may provide CLD differently and | 43 // Other implementations based upon Chromium may provide CLD differently and |
27 // may have other names. This method is primarily provided for those | 44 // may have other names. |
28 // non-Chromium implementations; Chromium implementations should use the | 45 // This method is threadsafe. |
29 // boolean methods in this class instead: | 46 virtual std::string GetName(); |
30 // ShouldRegisterForComponentUpdates() | |
31 // ShouldUseStandaloneDataFile() | |
32 static std::string GetName(); | |
33 | 47 |
34 // Returns true if the data source needs to receive updates from the | 48 // For data sources that support a separate CLD data file, configures the path |
35 // Component Updater. | 49 // of that data file. |
36 // This is only true if the data source name is "component", but makes caller | 50 // |
37 // logic more generic. | 51 // The 'component' and 'standalone' data sources need this method to be called |
38 static bool ShouldRegisterForComponentUpdates(); | 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 void SetCldDataFilePath(const base::FilePath& path); |
39 | 57 |
40 // Returns true if the data source needs to have the path to the CLD | 58 // Returns the path most recently set by SetCldDataFilePath. The initial value |
41 // data file configured immediately because it is bundled with Chromium. | 59 // prior to any such call is the empty path. If the data source doesn't |
42 // This is only true if the data source name is "standalone", but makes | 60 // support a data file, returns the empty path. |
43 // caller logic more generic. | 61 // This method is threadsafe. |
44 static bool ShouldUseStandaloneDataFile(); | 62 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); |
45 }; | 132 }; |
46 | 133 |
47 } // namespace translate | 134 } // namespace translate |
48 #endif // COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ | 135 #endif // COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ |
OLD | NEW |