1 // Copyright 2014 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 COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_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 "components/translate/core/browser/language_state.h"
17 #include "components/translate/core/common/translate_errors.h"
24 class TranslateClient
;
25 class TranslateDriver
;
27 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.
33 class TranslateManager
{
35 // |translate_client| is expected to outlive the TranslateManager.
36 // |accept_language_pref_name| is the path for the preference for the
38 TranslateManager(TranslateClient
* translate_client
,
39 const std::string
& accept_language_pref_name
);
40 virtual ~TranslateManager();
42 // Returns a weak pointer to this instance.
43 base::WeakPtr
<TranslateManager
> GetWeakPtr();
45 // Cannot return NULL.
46 TranslateClient
* translate_client() { return translate_client_
; }
48 // Sets the sequence number of the current page, for use while sending
49 // messages to the renderer.
50 void set_current_seq_no(int page_seq_no
) { page_seq_no_
= page_seq_no
; }
52 // Returns the language to translate to. The language returned is the
53 // first language found in the following list that is supported by the
54 // translation service:
56 // the accept-language list
57 // If no language is found then an empty string is returned.
58 static std::string
GetTargetLanguage(
59 const std::vector
<std::string
>& accept_languages_list
);
61 // Returns the language to automatically translate to. |original_language| is
62 // the webpage's original language.
63 static std::string
GetAutoTargetLanguage(const std::string
& original_language
,
64 TranslatePrefs
* translate_prefs
);
66 // Translates the page contents from |source_lang| to |target_lang|.
67 // The actual translation might be performed asynchronously if the translate
68 // script is not yet available.
69 void TranslatePage(const std::string
& source_lang
,
70 const std::string
& target_lang
,
71 bool triggered_from_menu
);
73 // Starts the translation process for the page in the |page_lang| language.
74 void InitiateTranslation(const std::string
& page_lang
);
76 // Shows the after translate or error infobar depending on the details.
77 void PageTranslated(const std::string
& source_lang
,
78 const std::string
& target_lang
,
79 TranslateErrors::Type error_type
);
81 // Reverts the contents of the page to its original language.
82 void RevertTranslation();
84 // Reports to the Google translate server that a page language was incorrectly
85 // detected. This call is initiated by the user selecting the "report" menu
86 // under options in the translate infobar.
87 void ReportLanguageDetectionError();
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
);
99 // Gets the LanguageState associated with the TranslateManager
100 LanguageState
& GetLanguageState();
102 // By default, don't offer to translate in builds lacking an API key. For
103 // testing, set to true to offer anyway.
104 static void SetIgnoreMissingKeyForTesting(bool ignore
);
107 // Sends a translation request to the TranslateDriver.
108 void DoTranslatePage(const std::string
& translate_script
,
109 const std::string
& source_lang
,
110 const std::string
& target_lang
);
112 // Called when the Translate script has been fetched.
113 // Initiates the translation.
114 void OnTranslateScriptFetchComplete(const std::string
& source_lang
,
115 const std::string
& target_lang
,
117 const std::string
& data
);
119 // Sequence number of the current page.
122 // Preference name for the Accept-Languages HTTP header.
123 std::string accept_languages_pref_name_
;
125 TranslateClient
* translate_client_
; // Weak.
126 TranslateDriver
* translate_driver_
; // Weak.
128 LanguageState language_state_
;
130 base::WeakPtrFactory
<TranslateManager
> weak_method_factory_
;
132 // By default, don't offer to translate in builds lacking an API key. For
133 // testing, set to true to offer anyway.
134 static bool ignore_missing_key_for_testing_
;
136 DISALLOW_COPY_AND_ASSIGN(TranslateManager
);
139 } // namespace translate
141 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_