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

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

Issue 2602003: Refactored the translate infobars. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Synced Created 10 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
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER2_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER2_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
13 #include "base/lazy_instance.h"
14 #include "base/singleton.h"
15 #include "base/task.h"
16 #include "chrome/common/net/url_fetcher.h"
17 #include "chrome/common/notification_observer.h"
18 #include "chrome/common/notification_registrar.h"
19 #include "chrome/common/translate_errors.h"
20
21 class GURL;
22 struct PageTranslatedDetails;
23 class PrefService;
24 class TabContents;
25 class TranslateInfoBarDelegate2;
26
27 // The TranslateManager class is responsible for showing an info-bar when a page
28 // in a language different than the user language is loaded. It triggers the
29 // page translation the user requests.
30 // It is a singleton.
31
32 class TranslateManager2 : public NotificationObserver,
33 public URLFetcher::Delegate {
34 public:
35 virtual ~TranslateManager2();
36
37 // Translates the page contents from |source_lang| to |target_lang|.
38 // The actual translation might be performed asynchronously if the translate
39 // script is not yet available.
40 void TranslatePage(TabContents* tab_contents,
41 const std::string& source_lang,
42 const std::string& target_lang);
43
44 // Reverts the contents of the page in |tab_contents| to its original
45 // language.
46 void RevertTranslation(TabContents* tab_contents);
47
48 // Clears the translate script, so it will be fetched next time we translate.
49 // Currently used by unit-tests.
50 void ClearTranslateScript() { translate_script_.clear(); }
51
52 // NotificationObserver implementation:
53 virtual void Observe(NotificationType type,
54 const NotificationSource& source,
55 const NotificationDetails& details);
56
57 // URLFetcher::Delegate implementation:
58 virtual void OnURLFetchComplete(const URLFetcher* source,
59 const GURL& url,
60 const URLRequestStatus& status,
61 int response_code,
62 const ResponseCookies& cookies,
63 const std::string& data);
64
65 // Convenience method to know if a tab is showing a translate infobar.
66 static bool IsShowingTranslateInfobar(TabContents* tab);
67
68 // Returns true if the URL can be translated, if it is not an internal URL
69 // (chrome:// and others).
70 static bool IsTranslatableURL(const GURL& url);
71
72 // Fills |languages| with the list of languages that the translate server can
73 // translate to and from.
74 static void GetSupportedLanguages(std::vector<std::string>* languages);
75
76 // Returns the language code that can be used with the Translate method for a
77 // specified |chrome_locale|.
78 static std::string GetLanguageCode(const std::string& chrome_locale);
79
80 // Returns true if |language| is supported by the translation server.
81 static bool IsSupportedLanguage(const std::string& language);
82
83 protected:
84 TranslateManager2();
85
86 private:
87 friend struct DefaultSingletonTraits<TranslateManager2>;
88
89 // Structure that describes a translate request.
90 // Translation may be deferred while the translate script is being retrieved
91 // from the translate server.
92 struct PendingRequest {
93 int render_process_id;
94 int render_view_id;
95 int page_id;
96 std::string source_lang;
97 std::string target_lang;
98 };
99
100 // Starts the translation process on |tab| containing the page in the
101 // |page_lang| language.
102 void InitiateTranslation(TabContents* tab, const std::string& page_lang);
103
104 // If the tab identified by |process_id| and |render_id| has been closed, this
105 // does nothing, otherwise it calls InitiateTranslation.
106 void InitiateTranslationPosted(int process_id,
107 int render_id,
108 const std::string& page_lang);
109
110 // Sends a translation request to the RenderView of |tab_contents|.
111 void DoTranslatePage(TabContents* tab_contents,
112 const std::string& translate_script,
113 const std::string& source_lang,
114 const std::string& target_lang);
115
116 // Shows the after translate or error infobar depending on the details.
117 void PageTranslated(TabContents* tab, PageTranslatedDetails* details);
118
119 // Returns true if the passed language has been configured by the user as an
120 // accept language.
121 bool IsAcceptLanguage(TabContents* tab, const std::string& language);
122
123 // Initializes the |accept_languages_| language table based on the associated
124 // preference in |prefs|.
125 void InitAcceptLanguages(PrefService* prefs);
126
127 // Fetches the JS translate script (the script that is injected in the page
128 // to translate it).
129 void RequestTranslateScript();
130
131 // Shows the specified translate |infobar| in the given |tab|. If a current
132 // translate infobar is showing, it just replaces it with the new one.
133 void ShowInfoBar(TabContents* tab, TranslateInfoBarDelegate2* infobar);
134
135 // Returns the language to translate to, which is the language the UI is
136 // configured in. Returns an empty string if that language is not supported
137 // by the translation service.
138 static std::string GetTargetLanguage();
139
140 // Returns the translate info bar showing in |tab| or NULL if none is showing.
141 static TranslateInfoBarDelegate2* GetTranslateInfoBarDelegate2(
142 TabContents* tab);
143
144 NotificationRegistrar notification_registrar_;
145
146 // A map that associates a profile with its parsed "accept languages".
147 typedef std::set<std::string> LanguageSet;
148 typedef std::map<PrefService*, LanguageSet> PrefServiceLanguagesMap;
149 PrefServiceLanguagesMap accept_languages_;
150
151 ScopedRunnableMethodFactory<TranslateManager2> method_factory_;
152
153 // The JS injected in the page to do the translation.
154 std::string translate_script_;
155
156 // Whether the translate JS is currently being retrieved.
157 bool translate_script_request_pending_;
158
159 // The list of pending translate requests. Translate requests are queued when
160 // the translate script is not ready and has to be fetched from the translate
161 // server.
162 std::vector<PendingRequest> pending_requests_;
163
164 // The languages supported by the translation server.
165 static base::LazyInstance<std::set<std::string> > supported_languages_;
166
167 DISALLOW_COPY_AND_ASSIGN(TranslateManager2);
168 };
169
170 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER2_H_
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_infobar_view.h ('k') | chrome/browser/translate/translate_manager2.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698