OLD | NEW |
| (Empty) |
1 // Copyright 2012 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_MANAGER_H_ | |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback_list.h" | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "components/translate/core/common/translate_errors.h" | |
17 | |
18 class GURL; | |
19 class PrefService; | |
20 class TranslateClient; | |
21 class TranslateDriver; | |
22 struct TranslateErrorDetails; | |
23 class TranslateTabHelper; | |
24 | |
25 // The TranslateManager class is responsible for showing an info-bar when a page | |
26 // in a language different than the user language is loaded. It triggers the | |
27 // page translation the user requests. | |
28 // TranslateManager expects its associated TranslateTabHelper to always have a | |
29 // valid WebContents (i.e. the WebContents is never destroyed within the | |
30 // lifetime of TranslateManager). | |
31 | |
32 class TranslateManager { | |
33 public: | |
34 // TranslateTabHelper is expected to outlive the TranslateManager. | |
35 // |accept_language_pref_name| is the path for the preference for the | |
36 // accept-languages. | |
37 TranslateManager(TranslateTabHelper* helper, | |
38 const std::string& accept_language_pref_name); | |
39 virtual ~TranslateManager(); | |
40 | |
41 // Returns the language to translate to. The language returned is the | |
42 // first language found in the following list that is supported by the | |
43 // translation service: | |
44 // the UI language | |
45 // the accept-language list | |
46 // If no language is found then an empty string is returned. | |
47 static std::string GetTargetLanguage( | |
48 const std::vector<std::string>& accept_languages_list); | |
49 | |
50 // Returns the language to automatically translate to. |original_language| is | |
51 // the webpage's original language. | |
52 static std::string GetAutoTargetLanguage(const std::string& original_language, | |
53 PrefService* prefs); | |
54 | |
55 // Translates the page contents from |source_lang| to |target_lang|. | |
56 // The actual translation might be performed asynchronously if the translate | |
57 // script is not yet available. | |
58 void TranslatePage(const std::string& source_lang, | |
59 const std::string& target_lang, | |
60 bool triggered_from_menu); | |
61 | |
62 // Starts the translation process for a page in the |page_lang| language. | |
63 void InitiateTranslation(const std::string& page_lang); | |
64 | |
65 // Shows the after translate or error infobar depending on the details. | |
66 void PageTranslated(const std::string& source_lang, | |
67 const std::string& target_lang, | |
68 TranslateErrors::Type error_type); | |
69 | |
70 // Reverts the contents of the page to its original language. | |
71 void RevertTranslation(); | |
72 | |
73 // Reports to the Google translate server that a page language was incorrectly | |
74 // detected. This call is initiated by the user selecting the "report" menu | |
75 // under options in the translate infobar. | |
76 void ReportLanguageDetectionError(); | |
77 | |
78 // Callback types for translate errors. | |
79 typedef base::Callback<void(const TranslateErrorDetails&)> | |
80 TranslateErrorCallback; | |
81 typedef base::CallbackList<void(const TranslateErrorDetails&)> | |
82 TranslateErrorCallbackList; | |
83 | |
84 // Registers a callback for translate errors. | |
85 static scoped_ptr<TranslateErrorCallbackList::Subscription> | |
86 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); | |
87 | |
88 private: | |
89 // Sends a translation request to the TranslateDriver. | |
90 void DoTranslatePage(const std::string& translate_script, | |
91 const std::string& source_lang, | |
92 const std::string& target_lang); | |
93 | |
94 // Called when the Translate script has been fetched. | |
95 // Initiates the translation. | |
96 void OnTranslateScriptFetchComplete(int page_id, | |
97 const std::string& source_lang, | |
98 const std::string& target_lang, | |
99 bool success, | |
100 const std::string& data); | |
101 | |
102 // Preference name for the Accept-Languages HTTP header. | |
103 std::string accept_languages_pref_name_; | |
104 | |
105 // TODO(droger): Remove all uses of |translate_tab_helper_|, use | |
106 // TranslateClient and TranslateDriver instead. | |
107 TranslateTabHelper* translate_tab_helper_; // Weak. | |
108 TranslateClient* translate_client_; // Weak. | |
109 TranslateDriver* translate_driver_; // Weak. | |
110 | |
111 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | |
114 }; | |
115 | |
116 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | |
OLD | NEW |