OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_list.h" | 12 #include "base/callback_list.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "components/translate/core/common/translate_errors.h" | 16 #include "components/translate/core/common/translate_errors.h" |
17 #include "content/public/browser/notification_observer.h" | |
18 #include "content/public/browser/notification_registrar.h" | |
19 | 17 |
20 class GURL; | 18 class GURL; |
21 class PrefService; | 19 class PrefService; |
22 class TranslateClient; | 20 class TranslateClient; |
23 class TranslateDriver; | 21 class TranslateDriver; |
24 struct TranslateErrorDetails; | 22 struct TranslateErrorDetails; |
25 class TranslateTabHelper; | 23 class TranslateTabHelper; |
26 | 24 |
27 namespace content { | 25 namespace content { |
28 class WebContents; | 26 class WebContents; |
29 } | 27 } |
30 | 28 |
31 // The TranslateManager class is responsible for showing an info-bar when a page | 29 // The TranslateManager class is responsible for showing an info-bar when a page |
32 // in a language different than the user language is loaded. It triggers the | 30 // in a language different than the user language is loaded. It triggers the |
33 // page translation the user requests. | 31 // page translation the user requests. |
34 // TranslateManager expects its associated TranslateTabHelper to always have a | 32 // TranslateManager expects its associated TranslateTabHelper to always have a |
35 // valid WebContents (i.e. the WebContents is never destroyed within the | 33 // valid WebContents (i.e. the WebContents is never destroyed within the |
36 // lifetime of TranslateManager). | 34 // lifetime of TranslateManager). |
37 | 35 |
38 class TranslateManager : public content::NotificationObserver { | 36 class TranslateManager { |
39 public: | 37 public: |
40 // TranslateTabHelper is expected to outlive the TranslateManager. | 38 // TranslateTabHelper is expected to outlive the TranslateManager. |
41 // |accept_language_pref_name| is the path for the preference for the | 39 // |accept_language_pref_name| is the path for the preference for the |
42 // accept-languages. | 40 // accept-languages. |
43 TranslateManager(TranslateTabHelper* helper, | 41 TranslateManager(TranslateTabHelper* helper, |
44 const std::string& accept_language_pref_name); | 42 const std::string& accept_language_pref_name); |
45 virtual ~TranslateManager(); | 43 virtual ~TranslateManager(); |
46 | 44 |
47 // Returns true if the URL can be translated. | 45 // Returns true if the URL can be translated. |
48 static bool IsTranslatableURL(const GURL& url); | 46 static bool IsTranslatableURL(const GURL& url); |
(...skipping 28 matching lines...) Expand all Loading... |
77 TranslateErrors::Type error_type); | 75 TranslateErrors::Type error_type); |
78 | 76 |
79 // Reverts the contents of the page to its original language. | 77 // Reverts the contents of the page to its original language. |
80 void RevertTranslation(); | 78 void RevertTranslation(); |
81 | 79 |
82 // Reports to the Google translate server that a page language was incorrectly | 80 // Reports to the Google translate server that a page language was incorrectly |
83 // detected. This call is initiated by the user selecting the "report" menu | 81 // detected. This call is initiated by the user selecting the "report" menu |
84 // under options in the translate infobar. | 82 // under options in the translate infobar. |
85 void ReportLanguageDetectionError(); | 83 void ReportLanguageDetectionError(); |
86 | 84 |
87 // content::NotificationObserver implementation: | |
88 virtual void Observe(int type, | |
89 const content::NotificationSource& source, | |
90 const content::NotificationDetails& details) OVERRIDE; | |
91 | |
92 // Number of attempts before waiting for a page to be fully reloaded. | |
93 void set_translate_max_reload_attemps(int attempts) { | |
94 max_reload_check_attempts_ = attempts; | |
95 } | |
96 | |
97 // Callback types for translate errors. | 85 // Callback types for translate errors. |
98 typedef base::Callback<void(const TranslateErrorDetails&)> | 86 typedef base::Callback<void(const TranslateErrorDetails&)> |
99 TranslateErrorCallback; | 87 TranslateErrorCallback; |
100 typedef base::CallbackList<void(const TranslateErrorDetails&)> | 88 typedef base::CallbackList<void(const TranslateErrorDetails&)> |
101 TranslateErrorCallbackList; | 89 TranslateErrorCallbackList; |
102 | 90 |
103 // Registers a callback for translate errors. | 91 // Registers a callback for translate errors. |
104 static scoped_ptr<TranslateErrorCallbackList::Subscription> | 92 static scoped_ptr<TranslateErrorCallbackList::Subscription> |
105 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); | 93 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); |
106 | 94 |
107 private: | 95 private: |
108 | 96 |
109 // Initiates translation once the page is finished loading. | |
110 void InitiateTranslationPosted(const std::string& page_lang, int attempt); | |
111 | |
112 // Sends a translation request to the RenderView. | 97 // Sends a translation request to the RenderView. |
113 void DoTranslatePage(const std::string& translate_script, | 98 void DoTranslatePage(const std::string& translate_script, |
114 const std::string& source_lang, | 99 const std::string& source_lang, |
115 const std::string& target_lang); | 100 const std::string& target_lang); |
116 | 101 |
117 // Called when the Translate script has been fetched. | 102 // Called when the Translate script has been fetched. |
118 // Initiates the translation. | 103 // Initiates the translation. |
119 void OnTranslateScriptFetchComplete(int page_id, | 104 void OnTranslateScriptFetchComplete(int page_id, |
120 const std::string& source_lang, | 105 const std::string& source_lang, |
121 const std::string& target_lang, | 106 const std::string& target_lang, |
122 bool success, | 107 bool success, |
123 const std::string& data); | 108 const std::string& data); |
124 | 109 |
125 content::NotificationRegistrar notification_registrar_; | |
126 | |
127 // Max number of attempts before checking if a page has been reloaded. | |
128 int max_reload_check_attempts_; | |
129 | |
130 // Preference name for the Accept-Languages HTTP header. | 110 // Preference name for the Accept-Languages HTTP header. |
131 std::string accept_languages_pref_name_; | 111 std::string accept_languages_pref_name_; |
132 | 112 |
133 // TODO(droger): Remove all uses of |translate_tab_helper_|, use | 113 // TODO(droger): Remove all uses of |translate_tab_helper_|, use |
134 // TranslateClient and TranslateDriver instead. | 114 // TranslateClient and TranslateDriver instead. |
135 TranslateTabHelper* translate_tab_helper_; // Weak. | 115 TranslateTabHelper* translate_tab_helper_; // Weak. |
136 TranslateClient* translate_client_; // Weak. | 116 TranslateClient* translate_client_; // Weak. |
137 TranslateDriver* translate_driver_; // Weak. | 117 TranslateDriver* translate_driver_; // Weak. |
138 | 118 |
139 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | 119 base::WeakPtrFactory<TranslateManager> weak_method_factory_; |
140 | 120 |
141 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 121 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
142 }; | 122 }; |
143 | 123 |
144 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 124 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
OLD | NEW |