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/synchronization/lock.h" | |
12 | |
10 namespace translate { | 13 namespace translate { |
11 | 14 |
12 // Provides high-level functionality related to a CLD Data Source. | 15 // Provides high-level functionality related to a CLD Data Source. |
13 class CldDataSource { | 16 class CldDataSource { |
14 | 17 |
15 public: | 18 public: |
19 CldDataSource(std::string name, | |
jochen (gone - plz use gerrit)
2014/10/23 13:57:39
consider using an enum instead of two bools, as on
Andrew Hayden (chromium.org)
2014/10/30 16:56:32
Totally rebuilt this, now using singletons with id
| |
20 bool register_for_component_updates, | |
21 bool use_standalone_data_file); | |
22 virtual ~CldDataSource() {} | |
16 | 23 |
17 // Returns the symbolic name of the data source. In the Chromium | 24 // Returns the symbolic name of the data source. In the Chromium |
18 // open-source tree, the following data sources exist: | 25 // open-source tree, the following data sources exist: |
19 // static uses the static_[browser|renderer]_cld_data_provider | 26 // static uses the static_[browser|renderer]_cld_data_provider |
20 // implementations. | 27 // implementations. |
21 // standalone uses the data_file_[browser|renderer]_cld_data_provider | 28 // standalone uses the data_file_[browser|renderer]_cld_data_provider |
22 // implementations. | 29 // implementations. |
23 // component also uses the data_file_[browser|renderer]_cld_data_provider | 30 // component also uses the data_file_[browser|renderer]_cld_data_provider |
24 // implementations. | 31 // implementations. |
25 // | 32 // |
26 // Other implementations based upon Chromium may provide CLD differently and | 33 // Other implementations based upon Chromium may provide CLD differently and |
27 // may have other names. This method is primarily provided for those | 34 // may have other names. This method is primarily provided for those |
28 // non-Chromium implementations; Chromium implementations should use the | 35 // non-Chromium implementations; Chromium implementations should use the |
29 // boolean methods in this class instead: | 36 // boolean methods in this class instead: |
30 // ShouldRegisterForComponentUpdates() | 37 // ShouldRegisterForComponentUpdates() |
31 // ShouldUseStandaloneDataFile() | 38 // ShouldUseStandaloneDataFile() |
32 static std::string GetName(); | 39 virtual std::string GetName(); |
33 | 40 |
34 // Returns true if the data source needs to receive updates from the | 41 // Returns true if the data source needs to receive updates from the |
35 // Component Updater. | 42 // Component Updater. |
36 // This is only true if the data source name is "component", but makes caller | 43 // This is only true if the data source name is "component", but makes caller |
37 // logic more generic. | 44 // logic more generic. |
38 static bool ShouldRegisterForComponentUpdates(); | 45 virtual bool ShouldRegisterForComponentUpdates(); |
39 | 46 |
40 // Returns true if the data source needs to have the path to the CLD | 47 // Returns true if the data source needs to have the path to the CLD |
41 // data file configured immediately because it is bundled with Chromium. | 48 // data file configured immediately because it is bundled with Chromium. |
42 // This is only true if the data source name is "standalone", but makes | 49 // This is only true if the data source name is "standalone", but makes |
43 // caller logic more generic. | 50 // caller logic more generic. |
44 static bool ShouldUseStandaloneDataFile(); | 51 virtual bool ShouldUseStandaloneDataFile(); |
52 | |
53 // For data sources that support a separate CLD data file, configures the path | |
54 // of that data file. | |
55 // | |
56 // The 'component' and 'standalone' data sources need this method to be called | |
57 // in order to locate the CLD data on disk. | |
58 // If the data source doesn't need or doesn't support such configuration, this | |
59 // function is a no-op. This is the case for, e.g., the static data source. | |
60 // This method is threadsafe. | |
61 void SetCldDataFilePath(const base::FilePath& path); | |
62 | |
63 // Returns the path most recently set by SetCldDataFilePath. The initial value | |
64 // prior to any such call is the empty path. If the data source doesn't | |
65 // support a data file, returns the empty path. | |
66 // This method is threadsafe. | |
67 base::FilePath GetCldDataFilePath(); | |
68 | |
69 // Sets the data source for this process, optionally overwriting the previous | |
70 // value. Embedders and narrow use-cases (such as shells and test code) should | |
71 // use the overwrite capability, while generic code should not. | |
72 static void Set(CldDataSource* data_source, bool overwrite); | |
73 | |
74 // Returns the data source for this process. Guaranteed to never be null. | |
75 // If no instance has been set, this returns the same object obtained by | |
76 // calling NONE(). | |
77 static CldDataSource* Get(); | |
78 | |
79 // Fetch the global instance of the "NONE" data source, i.e. a data source | |
80 // that gracefully does nothing. Callers should set this data source if it is | |
81 // the intent that there truly be no source of CLD data; this will result in | |
82 // language detection (and subsequently, translation) being unavailable. | |
83 // This is provided for convenience, to avoid a bunch of trivial subclasses | |
84 // whose only differences are in their unique combination of read-only values. | |
85 static CldDataSource* NONE(); | |
86 | |
87 // Fetch the global instance of the "static" data source. | |
88 // This is provided for convenience, to avoid a bunch of trivial subclasses | |
89 // whose only differences are in their unique combination of read-only values. | |
90 static CldDataSource* STATIC(); | |
91 | |
92 // Fetch the global instance of the "standalone" data source. | |
93 // This is provided for convenience, to avoid a bunch of trivial subclasses | |
94 // whose only differences are in their unique combination of read-only values. | |
95 static CldDataSource* STANDALONE(); | |
96 | |
97 // Fetch the global instance of the "component" data source. | |
98 // This is provided for convenience, to avoid a bunch of trivial subclasses | |
99 // whose only differences are in their unique combination of read-only values. | |
100 static CldDataSource* COMPONENT(); | |
101 | |
102 protected: | |
103 const std::string m_name; | |
104 const bool m_register_for_component_updates; | |
105 const bool m_use_standalone_data_file; | |
106 base::FilePath m_cached_filepath; // Guarded by m_file_lock | |
107 base::Lock m_file_lock; // Guards m_cached_filepath | |
108 | |
45 }; | 109 }; |
jochen (gone - plz use gerrit)
2014/10/23 13:57:39
disallow copy/assign
Andrew Hayden (chromium.org)
2014/10/30 16:56:32
Acknowledged.
| |
46 | 110 |
47 } // namespace translate | 111 } // namespace translate |
48 #endif // COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ | 112 #endif // COMPONENTS_TRANSLATE_CONTENT_COMMON_CLD_DATA_SOURCE_H_ |
OLD | NEW |