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.
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
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 "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
19 template <typename T> struct DefaultSingletonTraits;
21 struct PageTranslatedDetails;
23 struct TranslateErrorDetails;
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
31 // page translation the user requests.
34 class TranslateManager : public content::NotificationObserver {
36 // Returns the singleton instance.
37 static TranslateManager* GetInstance();
39 virtual ~TranslateManager();
41 // Returns true if the URL can be translated.
42 static bool IsTranslatableURL(const GURL& url);
44 // Returns the language to translate to. The language returned is the
45 // first language found in the following list that is supported by the
46 // translation service:
48 // the accept-language list
49 // If no language is found then an empty string is returned.
50 static std::string GetTargetLanguage(PrefService* prefs);
52 // Returns the language to automatically translate to. |original_language| is
53 // the webpage's original language.
54 static std::string GetAutoTargetLanguage(const std::string& original_language,
57 // Translates the page contents from |source_lang| to |target_lang|.
58 // The actual translation might be performed asynchronously if the translate
59 // script is not yet available.
60 void TranslatePage(content::WebContents* web_contents,
61 const std::string& source_lang,
62 const std::string& target_lang);
64 // Reverts the contents of the page in |web_contents| to its original
66 void RevertTranslation(content::WebContents* web_contents);
68 // 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
70 // under options in the translate infobar.
71 void ReportLanguageDetectionError(content::WebContents* web_contents);
73 // content::NotificationObserver implementation:
74 virtual void Observe(int type,
75 const content::NotificationSource& source,
76 const content::NotificationDetails& details) OVERRIDE;
78 // Number of attempts before waiting for a page to be fully reloaded.
79 void set_translate_max_reload_attemps(int attempts) {
80 max_reload_check_attempts_ = attempts;
83 // Callback types for translate errors.
84 typedef base::Callback<void(const TranslateErrorDetails&)>
85 TranslateErrorCallback;
86 typedef base::CallbackList<void(const TranslateErrorDetails&)>
87 TranslateErrorCallbackList;
89 // Registers a callback for translate errors.
90 static scoped_ptr<TranslateErrorCallbackList::Subscription>
91 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback);
97 friend struct DefaultSingletonTraits<TranslateManager>;
99 // Structure that describes a translate request.
100 // Translation may be deferred while the translate script is being retrieved
101 // from the translate server.
102 struct PendingRequest {
103 int render_process_id;
106 std::string source_lang;
107 std::string target_lang;
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);
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);
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,
124 const std::string& target_lang);
126 // Shows the after translate or error infobar depending on the details.
127 void PageTranslated(content::WebContents* web_contents,
128 PageTranslatedDetails* details);
130 void OnTranslateScriptFetchComplete(PendingRequest request,
132 const std::string& data);
134 content::NotificationRegistrar notification_registrar_;
136 // Max number of attempts before checking if a page has been reloaded.
137 int max_reload_check_attempts_;
139 base::WeakPtrFactory<TranslateManager> weak_method_factory_;
141 DISALLOW_COPY_AND_ASSIGN(TranslateManager);
144 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_