Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/translate/translate_language_list.h

Issue 15949022: Translate: language list smart updater (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: done Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.
MAD 2013/06/07 13:29:24 This comment is a bit misleading... The state is n
Takashi Toyoshima 2013/06/07 14:42:55 I see. Is it enough to say just 'Represents intern
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 request is finished asynchronously.
82 void Request(const Callback& callback);
83
84 // Gets internal state.
85 State state() { return state_; }
86
87 // net::URLFetcherDelegate implementation:
88 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
89
90 private:
91 // Represents if this instance should fetch a supported language list
92 // including alpha languages.
93 bool include_alpha_languages_;
94
95 // Internal state.
96 enum State state_;
97
98 // URLFetcher instance.
99 scoped_ptr<net::URLFetcher> fetcher_;
100
101 // Callback passed at Request(). It will be invoked when asynchronous
102 // fetch operation is finished.
103 Callback callback_;
104
105 DISALLOW_COPY_AND_ASSIGN(LanguageListFetcher);
106 };
107
108 // Callback function called when LanguageListFetcher::Request() is finished.
109 void OnLanguageListFetchComplete(bool include_alpha_languages,
110 bool success,
111 const std::string& data);
112
55 // Updates |all_supported_languages_| to contain the union of 113 // Updates |all_supported_languages_| to contain the union of
56 // |supported_languages_| and |supported_alpha_languages_|. 114 // |supported_languages_| and |supported_alpha_languages_|.
57 void UpdateSupportedLanguages(); 115 void UpdateSupportedLanguages();
58 116
59 // The languages supported by the translation server. 117 // The languages supported by the translation server.
60 std::set<std::string> supported_languages_; 118 std::set<std::string> supported_languages_;
61 119
62 // The alpha languages supported by the translation server. 120 // The alpha languages supported by the translation server.
63 std::set<std::string> supported_alpha_languages_; 121 std::set<std::string> supported_alpha_languages_;
64 122
65 // All the languages supported by the translation server. It contains the 123 // All the languages supported by the translation server. It contains the
66 // union of |supported_languages_| and |supported_alpha_languages_|. 124 // union of |supported_languages_| and |supported_alpha_languages_|.
67 std::set<std::string> all_supported_languages_; 125 std::set<std::string> all_supported_languages_;
68 126
69 // An URLFetcher instance to fetch a server providing supported language list. 127 // A LanguageListFetcher instance to fetch a server providing supported
70 scoped_ptr<net::URLFetcher> language_list_fetcher_; 128 // language list.
129 scoped_ptr<LanguageListFetcher> language_list_fetcher_;
71 130
72 // An URLFetcher instance to fetch a server providing supported alpha language 131 // A LanguageListFetcher instance to fetch a server providing supported alpha
73 // list. 132 // language list.
74 scoped_ptr<net::URLFetcher> alpha_language_list_fetcher_; 133 scoped_ptr<LanguageListFetcher> alpha_language_list_fetcher_;
75 134
76 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList); 135 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList);
77 }; 136 };
78 137
79 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_ 138 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698