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 "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
18 | 18 |
19 template <typename T> struct DefaultSingletonTraits; | |
20 class GURL; | 19 class GURL; |
21 struct PageTranslatedDetails; | 20 struct PageTranslatedDetails; |
22 class PrefService; | 21 class PrefService; |
23 struct TranslateErrorDetails; | 22 struct TranslateErrorDetails; |
23 class TranslateTabHelper; | |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 class WebContents; | 26 class WebContents; |
27 } | 27 } |
28 | 28 |
29 // 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 |
30 // 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 |
31 // page translation the user requests. | 31 // page translation the user requests. |
32 // It is a singleton. | |
33 | 32 |
34 class TranslateManager : public content::NotificationObserver { | 33 class TranslateManager : public content::NotificationObserver { |
35 public: | 34 public: |
36 // Returns the singleton instance. | 35 // TranslateTabHelper is expected to outlive the TranslateManager. |
37 static TranslateManager* GetInstance(); | 36 explicit TranslateManager(TranslateTabHelper* helper); |
38 | |
39 virtual ~TranslateManager(); | 37 virtual ~TranslateManager(); |
40 | 38 |
41 // Returns true if the URL can be translated. | 39 // Returns true if the URL can be translated. |
42 static bool IsTranslatableURL(const GURL& url); | 40 static bool IsTranslatableURL(const GURL& url); |
43 | 41 |
44 // Returns the language to translate to. The language returned is the | 42 // Returns the language to translate to. The language returned is the |
45 // first language found in the following list that is supported by the | 43 // first language found in the following list that is supported by the |
46 // translation service: | 44 // translation service: |
47 // the UI language | 45 // the UI language |
48 // the accept-language list | 46 // the accept-language list |
49 // If no language is found then an empty string is returned. | 47 // If no language is found then an empty string is returned. |
50 static std::string GetTargetLanguage(PrefService* prefs); | 48 static std::string GetTargetLanguage(PrefService* prefs); |
51 | 49 |
52 // Returns the language to automatically translate to. |original_language| is | 50 // Returns the language to automatically translate to. |original_language| is |
53 // the webpage's original language. | 51 // the webpage's original language. |
54 static std::string GetAutoTargetLanguage(const std::string& original_language, | 52 static std::string GetAutoTargetLanguage(const std::string& original_language, |
55 PrefService* prefs); | 53 PrefService* prefs); |
56 | 54 |
57 // Translates the page contents from |source_lang| to |target_lang|. | 55 // Translates the page contents from |source_lang| to |target_lang|. |
58 // The actual translation might be performed asynchronously if the translate | 56 // The actual translation might be performed asynchronously if the translate |
59 // script is not yet available. | 57 // script is not yet available. |
60 void TranslatePage(content::WebContents* web_contents, | 58 void TranslatePage(const std::string& source_lang, |
61 const std::string& source_lang, | |
62 const std::string& target_lang); | 59 const std::string& target_lang); |
63 | 60 |
64 // Reverts the contents of the page in |web_contents| to its original | 61 // Reverts the contents of the page to its original language. |
65 // language. | 62 void RevertTranslation(); |
66 void RevertTranslation(content::WebContents* web_contents); | |
67 | 63 |
68 // Reports to the Google translate server that a page language was incorrectly | 64 // Reports to the Google translate server that a page language was incorrectly |
69 // detected. This call is initiated by the user selecting the "report" menu | 65 // detected. This call is initiated by the user selecting the "report" menu |
70 // under options in the translate infobar. | 66 // under options in the translate infobar. |
71 void ReportLanguageDetectionError(content::WebContents* web_contents); | 67 void ReportLanguageDetectionError(); |
72 | 68 |
73 // content::NotificationObserver implementation: | 69 // content::NotificationObserver implementation: |
74 virtual void Observe(int type, | 70 virtual void Observe(int type, |
75 const content::NotificationSource& source, | 71 const content::NotificationSource& source, |
76 const content::NotificationDetails& details) OVERRIDE; | 72 const content::NotificationDetails& details) OVERRIDE; |
77 | 73 |
78 // Number of attempts before waiting for a page to be fully reloaded. | 74 // Number of attempts before waiting for a page to be fully reloaded. |
79 void set_translate_max_reload_attemps(int attempts) { | 75 void set_translate_max_reload_attemps(int attempts) { |
80 max_reload_check_attempts_ = attempts; | 76 max_reload_check_attempts_ = attempts; |
81 } | 77 } |
82 | 78 |
83 // Callback types for translate errors. | 79 // Callback types for translate errors. |
84 typedef base::Callback<void(const TranslateErrorDetails&)> | 80 typedef base::Callback<void(const TranslateErrorDetails&)> |
85 TranslateErrorCallback; | 81 TranslateErrorCallback; |
86 typedef base::CallbackList<void(const TranslateErrorDetails&)> | 82 typedef base::CallbackList<void(const TranslateErrorDetails&)> |
87 TranslateErrorCallbackList; | 83 TranslateErrorCallbackList; |
88 | 84 |
89 // Registers a callback for translate errors. | 85 // Registers a callback for translate errors. |
90 static scoped_ptr<TranslateErrorCallbackList::Subscription> | 86 static scoped_ptr<TranslateErrorCallbackList::Subscription> |
91 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); | 87 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); |
92 | 88 |
93 protected: | 89 private: |
94 TranslateManager(); | 90 // Starts the translation process for a page in the |page_lang| language. |
91 void InitiateTranslation(const std::string& page_lang); | |
95 | 92 |
96 private: | 93 // Initiates translation once the page is finished loading. |
97 friend struct DefaultSingletonTraits<TranslateManager>; | 94 void InitiateTranslationPosted(const std::string& page_lang, int attempt); |
98 | 95 |
99 // Structure that describes a translate request. | 96 // Sends a translation request to the RenderView. |
100 // Translation may be deferred while the translate script is being retrieved | 97 void DoTranslatePage(const std::string& translate_script, |
101 // from the translate server. | |
102 struct PendingRequest { | |
103 int render_process_id; | |
104 int render_view_id; | |
105 int page_id; | |
106 std::string source_lang; | |
107 std::string target_lang; | |
108 }; | |
109 | |
110 // Starts the translation process on |tab| containing the page in the | |
111 // |page_lang| language. | |
112 void InitiateTranslation(content::WebContents* web_contents, | |
113 const std::string& page_lang); | |
114 | |
115 // If the tab identified by |process_id| and |render_id| has been closed, this | |
116 // does nothing, otherwise it calls InitiateTranslation. | |
117 void InitiateTranslationPosted(int process_id, int render_id, | |
118 const std::string& page_lang, int attempt); | |
119 | |
120 // Sends a translation request to the RenderView of |web_contents|. | |
121 void DoTranslatePage(content::WebContents* web_contents, | |
122 const std::string& translate_script, | |
123 const std::string& source_lang, | 98 const std::string& source_lang, |
124 const std::string& target_lang); | 99 const std::string& target_lang); |
125 | 100 |
126 // Shows the after translate or error infobar depending on the details. | 101 // Shows the after translate or error infobar depending on the details. |
127 void PageTranslated(content::WebContents* web_contents, | 102 void PageTranslated(PageTranslatedDetails* details); |
128 PageTranslatedDetails* details); | |
129 | 103 |
130 void OnTranslateScriptFetchComplete(PendingRequest request, | 104 // Called when the Translate script has been fetched. |
105 // Initiates the translation. | |
106 void OnTranslateScriptFetchComplete(int page_id, | |
107 const std::string& source_lang, | |
108 const std::string& target_lang, | |
131 bool success, | 109 bool success, |
132 const std::string& data); | 110 const std::string& data); |
133 | 111 |
134 content::NotificationRegistrar notification_registrar_; | 112 content::NotificationRegistrar notification_registrar_; |
135 | 113 |
136 // Max number of attempts before checking if a page has been reloaded. | 114 // Max number of attempts before checking if a page has been reloaded. |
137 int max_reload_check_attempts_; | 115 int max_reload_check_attempts_; |
138 | 116 |
117 TranslateTabHelper* translate_tab_helper_; // Weak | |
MAD
2014/02/18 20:29:12
Please add a '.' at the end of the comment.
droger
2014/02/19 09:17:29
Done.
| |
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 |