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/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "chrome/browser/web_resource/resource_request_allowed_notifier.h" |
15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
16 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 class URLFetcher; | 20 class URLFetcher; |
20 } | 21 } |
21 | 22 |
22 // The TranslateLanguageList class is responsible for maintaining the latest | 23 // The TranslateLanguageList class is responsible for maintaining the latest |
23 // supporting language list. | 24 // supporting language list. |
24 // This class is defined to be owned only by TranslateManager. | 25 // This class is defined to be owned only by TranslateManager. |
25 class TranslateLanguageList { | 26 class TranslateLanguageList : public ResourceRequestAllowedNotifier::Observer { |
26 public: | 27 public: |
27 TranslateLanguageList(); | 28 TranslateLanguageList(); |
28 virtual ~TranslateLanguageList(); | 29 virtual ~TranslateLanguageList(); |
29 | 30 |
30 // Returns the last-updated time when the language list is fetched from the | 31 // Returns the last-updated time when the language list is fetched from the |
31 // Translate server. Returns null time if the list is yet to be fetched. | 32 // Translate server. Returns null time if the list is yet to be fetched. |
32 base::Time last_updated() { return last_updated_; } | 33 base::Time last_updated() { return last_updated_; } |
33 | 34 |
34 // Fills |languages| with the list of languages that the translate server can | 35 // Fills |languages| with the list of languages that the translate server can |
35 // translate to and from. |languages| will include alpha languages. | 36 // translate to and from. |languages| will include alpha languages. |
36 void GetSupportedLanguages(std::vector<std::string>* languages); | 37 void GetSupportedLanguages(std::vector<std::string>* languages); |
37 | 38 |
38 // Returns the language code that can be used with the Translate method for a | 39 // Returns the language code that can be used with the Translate method for a |
39 // specified |chrome_locale|. | 40 // specified |chrome_locale|. |
40 std::string GetLanguageCode(const std::string& chrome_locale); | 41 std::string GetLanguageCode(const std::string& chrome_locale); |
41 | 42 |
42 // Returns true if |language| is supported by the translation server. It also | 43 // Returns true if |language| is supported by the translation server. It also |
43 // returns true against alpha languages. | 44 // returns true against alpha languages. |
44 bool IsSupportedLanguage(const std::string& language); | 45 bool IsSupportedLanguage(const std::string& language); |
45 | 46 |
46 // Returns true if |language| is supported by the translation server as a | 47 // Returns true if |language| is supported by the translation server as a |
47 // alpha language. | 48 // alpha language. |
48 bool IsAlphaLanguage(const std::string& language); | 49 bool IsAlphaLanguage(const std::string& language); |
49 | 50 |
50 // Fetches the language list from the translate server. It will not retry | 51 // Fetches the language list from the translate server. It will not retry |
51 // more than kMaxRetryLanguageListFetch times. Do nothing if the list is | 52 // more than kMaxRetryLanguageListFetch times. Do nothing if the list is |
52 // already updated. | 53 // already updated. |
53 void RequestLanguageList(); | 54 void RequestLanguageList(); |
54 | 55 |
| 56 // ResourceRequestAllowedNotifier::Observer implementation: |
| 57 virtual void OnResourceRequestsAllowed() OVERRIDE; |
| 58 |
55 // static const values shared with our browser tests. | 59 // static const values shared with our browser tests. |
56 static const char kLanguageListCallbackName[]; | 60 static const char kLanguageListCallbackName[]; |
57 static const char kTargetLanguagesKey[]; | 61 static const char kTargetLanguagesKey[]; |
58 | 62 |
59 private: | 63 private: |
60 // The LanguageListFetcher class implements a client to fetch a server | 64 // The LanguageListFetcher class implements a client to fetch a server |
61 // supported language list. It also maintains the state to represent if the | 65 // supported language list. It also maintains the state to represent if the |
62 // fetch is completed successfully to try again later. | 66 // fetch is completed successfully to try again later. |
63 class LanguageListFetcher : public net::URLFetcherDelegate { | 67 class LanguageListFetcher : public net::URLFetcherDelegate { |
64 public: | 68 public: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 // Internal state. | 101 // Internal state. |
98 enum State state_; | 102 enum State state_; |
99 | 103 |
100 // URLFetcher instance. | 104 // URLFetcher instance. |
101 scoped_ptr<net::URLFetcher> fetcher_; | 105 scoped_ptr<net::URLFetcher> fetcher_; |
102 | 106 |
103 // Callback passed at Request(). It will be invoked when an asynchronous | 107 // Callback passed at Request(). It will be invoked when an asynchronous |
104 // fetch operation is finished. | 108 // fetch operation is finished. |
105 Callback callback_; | 109 Callback callback_; |
106 | 110 |
| 111 // Counts how many times did it try to fetch the language list. |
| 112 int retry_count_; |
| 113 |
107 DISALLOW_COPY_AND_ASSIGN(LanguageListFetcher); | 114 DISALLOW_COPY_AND_ASSIGN(LanguageListFetcher); |
108 }; | 115 }; |
109 | 116 |
110 // Callback function called when LanguageListFetcher::Request() is finished. | 117 // Callback function called when LanguageListFetcher::Request() is finished. |
111 void OnLanguageListFetchComplete(bool include_alpha_languages, | 118 void OnLanguageListFetchComplete(bool include_alpha_languages, |
112 bool success, | 119 bool success, |
113 const std::string& data); | 120 const std::string& data); |
114 | 121 |
115 // Updates |all_supported_languages_| to contain the union of | 122 // Updates |all_supported_languages_| to contain the union of |
116 // |supported_languages_| and |supported_alpha_languages_|. | 123 // |supported_languages_| and |supported_alpha_languages_|. |
(...skipping 13 matching lines...) Expand all Loading... |
130 // language list. | 137 // language list. |
131 scoped_ptr<LanguageListFetcher> language_list_fetcher_; | 138 scoped_ptr<LanguageListFetcher> language_list_fetcher_; |
132 | 139 |
133 // A LanguageListFetcher instance to fetch a server providing supported alpha | 140 // A LanguageListFetcher instance to fetch a server providing supported alpha |
134 // language list. | 141 // language list. |
135 scoped_ptr<LanguageListFetcher> alpha_language_list_fetcher_; | 142 scoped_ptr<LanguageListFetcher> alpha_language_list_fetcher_; |
136 | 143 |
137 // The last-updated time when the language list is sent. | 144 // The last-updated time when the language list is sent. |
138 base::Time last_updated_; | 145 base::Time last_updated_; |
139 | 146 |
| 147 // Helper class to know if it's allowed to make network resource requests. |
| 148 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; |
| 149 |
140 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList); | 150 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList); |
141 }; | 151 }; |
142 | 152 |
143 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ | 153 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ |
OLD | NEW |