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"
20 struct PageTranslatedDetails;
22 class TranslateClient;
23 class TranslateDriver;
24 struct TranslateErrorDetails;
25 class TranslateTabHelper;
31 // 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
33 // page translation the user requests.
34 // TranslateManager expects its associated TranslateTabHelper to always have a
35 // valid WebContents (i.e. the WebContents is never destroyed within the
36 // lifetime of TranslateManager).
38 class TranslateManager : public content::NotificationObserver {
40 // TranslateTabHelper is expected to outlive the TranslateManager.
41 // |accept_language_pref_name| is the path for the preference for the
43 TranslateManager(TranslateTabHelper* helper,
44 const std::string& accept_language_pref_name);
45 virtual ~TranslateManager();
47 // Returns true if the URL can be translated.
48 static bool IsTranslatableURL(const GURL& url);
50 // Returns the language to translate to. The language returned is the
51 // first language found in the following list that is supported by the
52 // translation service:
54 // the accept-language list
55 // If no language is found then an empty string is returned.
56 static std::string GetTargetLanguage(
57 const std::vector<std::string>& accept_languages_list);
59 // Returns the language to automatically translate to. |original_language| is
60 // the webpage's original language.
61 static std::string GetAutoTargetLanguage(const std::string& original_language,
64 // Translates the page contents from |source_lang| to |target_lang|.
65 // The actual translation might be performed asynchronously if the translate
66 // script is not yet available.
67 void TranslatePage(const std::string& source_lang,
68 const std::string& target_lang,
69 bool triggered_from_menu);
71 // Reverts the contents of the page to its original language.
72 void RevertTranslation();
74 // Reports to the Google translate server that a page language was incorrectly
75 // detected. This call is initiated by the user selecting the "report" menu
76 // under options in the translate infobar.
77 void ReportLanguageDetectionError();
79 // content::NotificationObserver implementation:
80 virtual void Observe(int type,
81 const content::NotificationSource& source,
82 const content::NotificationDetails& details) OVERRIDE;
84 // Number of attempts before waiting for a page to be fully reloaded.
85 void set_translate_max_reload_attemps(int attempts) {
86 max_reload_check_attempts_ = attempts;
89 // Callback types for translate errors.
90 typedef base::Callback<void(const TranslateErrorDetails&)>
91 TranslateErrorCallback;
92 typedef base::CallbackList<void(const TranslateErrorDetails&)>
93 TranslateErrorCallbackList;
95 // Registers a callback for translate errors.
96 static scoped_ptr<TranslateErrorCallbackList::Subscription>
97 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback);
100 // Starts the translation process for a page in the |page_lang| language.
101 void InitiateTranslation(const std::string& page_lang);
103 // Initiates translation once the page is finished loading.
104 void InitiateTranslationPosted(const std::string& page_lang, int attempt);
106 // Sends a translation request to the RenderView.
107 void DoTranslatePage(const std::string& translate_script,
108 const std::string& source_lang,
109 const std::string& target_lang);
111 // Shows the after translate or error infobar depending on the details.
112 void PageTranslated(PageTranslatedDetails* details);
114 // Called when the Translate script has been fetched.
115 // Initiates the translation.
116 void OnTranslateScriptFetchComplete(int page_id,
117 const std::string& source_lang,
118 const std::string& target_lang,
120 const std::string& data);
122 content::NotificationRegistrar notification_registrar_;
124 // Max number of attempts before checking if a page has been reloaded.
125 int max_reload_check_attempts_;
127 // Preference name for the Accept-Languages HTTP header.
128 std::string accept_languages_pref_name_;
130 // TODO(droger): Remove all uses of |translate_tab_helper_|, use
131 // TranslateClient and TranslateDriver instead.
132 TranslateTabHelper* translate_tab_helper_; // Weak.
133 TranslateClient* translate_client_; // Weak.
134 TranslateDriver* translate_driver_; // Weak.
136 base::WeakPtrFactory<TranslateManager> weak_method_factory_;
138 DISALLOW_COPY_AND_ASSIGN(TranslateManager);
141 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_