OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "chrome/browser/web_resource/resource_request_allowed_notifier.h" |
14 | 15 |
15 class TranslateURLFetcher; | 16 class TranslateURLFetcher; |
16 | 17 |
17 // The TranslateLanguageList class is responsible for maintaining the latest | 18 // The TranslateLanguageList class is responsible for maintaining the latest |
18 // supporting language list. | 19 // supporting language list. |
19 // This class is defined to be owned only by TranslateManager. | 20 // This class is defined to be owned only by TranslateManager. |
20 class TranslateLanguageList { | 21 class TranslateLanguageList : public ResourceRequestAllowedNotifier::Observer { |
21 public: | 22 public: |
22 TranslateLanguageList(); | 23 TranslateLanguageList(); |
23 virtual ~TranslateLanguageList(); | 24 virtual ~TranslateLanguageList(); |
24 | 25 |
25 // Returns the last-updated time when the language list is fetched from the | 26 // Returns the last-updated time when the language list is fetched from the |
26 // Translate server. Returns null time if the list is yet to be fetched. | 27 // Translate server. Returns null time if the list is yet to be fetched. |
27 base::Time last_updated() { return last_updated_; } | 28 base::Time last_updated() { return last_updated_; } |
28 | 29 |
29 // Fills |languages| with the list of languages that the translate server can | 30 // Fills |languages| with the list of languages that the translate server can |
30 // translate to and from. |languages| will include alpha languages. | 31 // translate to and from. |languages| will include alpha languages. |
31 void GetSupportedLanguages(std::vector<std::string>* languages); | 32 void GetSupportedLanguages(std::vector<std::string>* languages); |
32 | 33 |
33 // Returns the language code that can be used with the Translate method for a | 34 // Returns the language code that can be used with the Translate method for a |
34 // specified |chrome_locale|. | 35 // specified |chrome_locale|. |
35 std::string GetLanguageCode(const std::string& chrome_locale); | 36 std::string GetLanguageCode(const std::string& chrome_locale); |
36 | 37 |
37 // Returns true if |language| is supported by the translation server. It also | 38 // Returns true if |language| is supported by the translation server. It also |
38 // returns true against alpha languages. | 39 // returns true against alpha languages. |
39 bool IsSupportedLanguage(const std::string& language); | 40 bool IsSupportedLanguage(const std::string& language); |
40 | 41 |
41 // Returns true if |language| is supported by the translation server as a | 42 // Returns true if |language| is supported by the translation server as a |
42 // alpha language. | 43 // alpha language. |
43 bool IsAlphaLanguage(const std::string& language); | 44 bool IsAlphaLanguage(const std::string& language); |
44 | 45 |
45 // Fetches the language list from the translate server. It will retry | 46 // Fetches the language list from the translate server. It will retry |
46 // automatically when a server return 5xx errors and retry count doesn't | 47 // automatically when a server return 5xx errors and retry count doesn't |
47 // reach to limits. | 48 // reach to limits. |
48 void RequestLanguageList(); | 49 void RequestLanguageList(); |
49 | 50 |
| 51 // ResourceRequestAllowedNotifier::Observer implementation: |
| 52 virtual void OnResourceRequestsAllowed() OVERRIDE; |
| 53 |
| 54 // Disables the language list updater. This is used only for testing now. |
| 55 static void DisableUpdate(); |
| 56 |
50 // static const values shared with our browser tests. | 57 // static const values shared with our browser tests. |
51 static const char kLanguageListCallbackName[]; | 58 static const char kLanguageListCallbackName[]; |
52 static const char kTargetLanguagesKey[]; | 59 static const char kTargetLanguagesKey[]; |
53 | 60 |
54 private: | 61 private: |
55 // Callback function called when TranslateURLFetcher::Request() is finished. | 62 // Callback function called when TranslateURLFetcher::Request() is finished. |
56 void OnLanguageListFetchComplete(int id, | 63 void OnLanguageListFetchComplete(int id, |
57 bool success, | 64 bool success, |
58 const std::string& data); | 65 const std::string& data); |
59 | 66 |
(...skipping 15 matching lines...) Expand all Loading... |
75 // language list. | 82 // language list. |
76 scoped_ptr<TranslateURLFetcher> language_list_fetcher_; | 83 scoped_ptr<TranslateURLFetcher> language_list_fetcher_; |
77 | 84 |
78 // A LanguageListFetcher instance to fetch a server providing supported alpha | 85 // A LanguageListFetcher instance to fetch a server providing supported alpha |
79 // language list. | 86 // language list. |
80 scoped_ptr<TranslateURLFetcher> alpha_language_list_fetcher_; | 87 scoped_ptr<TranslateURLFetcher> alpha_language_list_fetcher_; |
81 | 88 |
82 // The last-updated time when the language list is sent. | 89 // The last-updated time when the language list is sent. |
83 base::Time last_updated_; | 90 base::Time last_updated_; |
84 | 91 |
| 92 // Helper class to know if it's allowed to make network resource requests. |
| 93 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; |
| 94 |
85 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList); | 95 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList); |
86 }; | 96 }; |
87 | 97 |
88 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ | 98 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ |
OLD | NEW |