| 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/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/network_change_notifier.h" |
| 13 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 14 | 17 |
| 15 namespace net { | 18 namespace net { |
| 16 class URLFetcher; | 19 class URLFetcher; |
| 17 } | 20 } |
| 18 | 21 |
| 19 // The TranslateLanguageList class is responsible for maintaining the latest | 22 // The TranslateLanguageList class is responsible for maintaining the latest |
| 20 // supporting language list. | 23 // supporting language list. |
| 21 // This class is defined to be owned only by TranslateManager. | 24 // This class is defined to be owned only by TranslateManager. |
| 22 class TranslateLanguageList : public net::URLFetcherDelegate { | 25 class TranslateLanguageList |
| 26 : public net::NetworkChangeNotifier::NetworkChangeObserver { |
| 23 public: | 27 public: |
| 24 TranslateLanguageList(); | 28 TranslateLanguageList(); |
| 25 virtual ~TranslateLanguageList(); | 29 virtual ~TranslateLanguageList(); |
| 26 | 30 |
| 27 // net::URLFetcherDelegate implementation: | |
| 28 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 29 | |
| 30 // Fills |languages| with the list of languages that the translate server can | 31 // Fills |languages| with the list of languages that the translate server can |
| 31 // translate to and from. |languages| will include alpha languages. | 32 // translate to and from. |languages| will include alpha languages. |
| 32 void GetSupportedLanguages(std::vector<std::string>* languages); | 33 void GetSupportedLanguages(std::vector<std::string>* languages); |
| 33 | 34 |
| 34 // Returns the language code that can be used with the Translate method for a | 35 // Returns the language code that can be used with the Translate method for a |
| 35 // specified |chrome_locale|. | 36 // specified |chrome_locale|. |
| 36 std::string GetLanguageCode(const std::string& chrome_locale); | 37 std::string GetLanguageCode(const std::string& chrome_locale); |
| 37 | 38 |
| 38 // Returns true if |language| is supported by the translation server. It also | 39 // Returns true if |language| is supported by the translation server. It also |
| 39 // returns true against alpha languages. | 40 // returns true against alpha languages. |
| 40 bool IsSupportedLanguage(const std::string& language); | 41 bool IsSupportedLanguage(const std::string& language); |
| 41 | 42 |
| 42 // Returns true if |language| is supported by the translation server as a | 43 // Returns true if |language| is supported by the translation server as a |
| 43 // alpha language. | 44 // alpha language. |
| 44 bool IsAlphaLanguage(const std::string& language); | 45 bool IsAlphaLanguage(const std::string& language); |
| 45 | 46 |
| 46 // Fetches the language list from the translate server. It will not retry | 47 // Fetches the language list from the translate server. It will not retry |
| 47 // more than kMaxRetryLanguageListFetch times. | 48 // more than kMaxRetryLanguageListFetch times. Do nothing if the list is |
| 49 // already updated. |
| 48 void RequestLanguageList(); | 50 void RequestLanguageList(); |
| 49 | 51 |
| 52 // net::NetworkChangeObserver implementation: |
| 53 virtual void OnNetworkChanged( |
| 54 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 55 |
| 50 // static const values shared with our browser tests. | 56 // static const values shared with our browser tests. |
| 51 static const char kLanguageListCallbackName[]; | 57 static const char kLanguageListCallbackName[]; |
| 52 static const char kTargetLanguagesKey[]; | 58 static const char kTargetLanguagesKey[]; |
| 53 | 59 |
| 54 private: | 60 private: |
| 61 // The LanguageListFetcher class implements a client to fetch a server |
| 62 // supported language list. It also maintains state to represent if the fetch |
| 63 // is completed successfully to try again later. |
| 64 class LanguageListFetcher : public net::URLFetcherDelegate { |
| 65 public: |
| 66 // Callback type for Request(). |
| 67 typedef base::Callback<void(bool, bool, const std::string&)> Callback; |
| 68 |
| 69 // Represents internal state if the fetch is completed successfully. |
| 70 enum State { |
| 71 IDLE, // No fetch request was issued. |
| 72 REQUESTING, // A fetch request was issued, but not finished yet. |
| 73 COMPLETED, // The last fetch request was finished successfully. |
| 74 FAILED, // The last fetch request was finished with a failure. |
| 75 }; |
| 76 |
| 77 explicit LanguageListFetcher(bool include_alpha_languages); |
| 78 virtual ~LanguageListFetcher(); |
| 79 |
| 80 // Requests to fetch a server supported language list. |callback| will be |
| 81 // invoked when the function returns true, and the request is finished |
| 82 // asynchronously. |
| 83 // Returns false if the previous request is not finished, or the request |
| 84 // is omitted due to retry limitation. |
| 85 bool Request(const Callback& callback); |
| 86 |
| 87 // Gets internal state. |
| 88 State state() { return state_; } |
| 89 |
| 90 // net::URLFetcherDelegate implementation: |
| 91 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 92 |
| 93 private: |
| 94 // Represents if this instance should fetch a supported language list |
| 95 // including alpha languages. |
| 96 bool include_alpha_languages_; |
| 97 |
| 98 // Internal state. |
| 99 enum State state_; |
| 100 |
| 101 // URLFetcher instance. |
| 102 scoped_ptr<net::URLFetcher> fetcher_; |
| 103 |
| 104 // Callback passed at Request(). It will be invoked when asynchronous |
| 105 // fetch operation is finished. |
| 106 Callback callback_; |
| 107 |
| 108 // Counts how many times did it try to fetch the language list. |
| 109 int retry_count_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(LanguageListFetcher); |
| 112 }; |
| 113 |
| 114 // Callback function called when LanguageListFetcher::Request() is finished. |
| 115 void OnLanguageListFetchComplete(bool include_alpha_languages, |
| 116 bool success, |
| 117 const std::string& data); |
| 118 |
| 55 // Updates |all_supported_languages_| to contain the union of | 119 // Updates |all_supported_languages_| to contain the union of |
| 56 // |supported_languages_| and |supported_alpha_languages_|. | 120 // |supported_languages_| and |supported_alpha_languages_|. |
| 57 void UpdateSupportedLanguages(); | 121 void UpdateSupportedLanguages(); |
| 58 | 122 |
| 59 // The languages supported by the translation server. | 123 // The languages supported by the translation server. |
| 60 std::set<std::string> supported_languages_; | 124 std::set<std::string> supported_languages_; |
| 61 | 125 |
| 62 // The alpha languages supported by the translation server. | 126 // The alpha languages supported by the translation server. |
| 63 std::set<std::string> supported_alpha_languages_; | 127 std::set<std::string> supported_alpha_languages_; |
| 64 | 128 |
| 65 // All the languages supported by the translation server. It contains the | 129 // All the languages supported by the translation server. It contains the |
| 66 // union of |supported_languages_| and |supported_alpha_languages_|. | 130 // union of |supported_languages_| and |supported_alpha_languages_|. |
| 67 std::set<std::string> all_supported_languages_; | 131 std::set<std::string> all_supported_languages_; |
| 68 | 132 |
| 69 // An URLFetcher instance to fetch a server providing supported language list. | 133 // A LanguageListFetcher instance to fetch a server providing supported |
| 70 scoped_ptr<net::URLFetcher> language_list_fetcher_; | 134 // language list. |
| 135 scoped_ptr<LanguageListFetcher> language_list_fetcher_; |
| 71 | 136 |
| 72 // An URLFetcher instance to fetch a server providing supported alpha language | 137 // A LanguageListFetcher instance to fetch a server providing supported alpha |
| 73 // list. | 138 // language list. |
| 74 scoped_ptr<net::URLFetcher> alpha_language_list_fetcher_; | 139 scoped_ptr<LanguageListFetcher> alpha_language_list_fetcher_; |
| 75 | 140 |
| 76 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList); | 141 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList); |
| 77 }; | 142 }; |
| 78 | 143 |
| 79 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ | 144 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ |
| OLD | NEW |