• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
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"
18 
19 class GURL;
20 class PrefService;
21 
22 namespace translate {
23 
24 class TranslateClient;
25 class TranslateDriver;
26 class TranslatePrefs;
27 struct TranslateErrorDetails;
28 
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.
32 
33 class TranslateManager {
34  public:
35   // |translate_client| is expected to outlive the TranslateManager.
36   // |accept_language_pref_name| is the path for the preference for the
37   // accept-languages.
38   TranslateManager(TranslateClient* translate_client,
39                    const std::string& accept_language_pref_name);
40   virtual ~TranslateManager();
41 
42   // Returns a weak pointer to this instance.
43   base::WeakPtr<TranslateManager> GetWeakPtr();
44 
45   // Cannot return NULL.
translate_client()46   TranslateClient* translate_client() { return translate_client_; }
47 
48   // Sets the sequence number of the current page, for use while sending
49   // messages to the renderer.
set_current_seq_no(int page_seq_no)50   void set_current_seq_no(int page_seq_no) { page_seq_no_ = page_seq_no; }
51 
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:
55   //     the UI language
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);
60 
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);
65 
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);
72 
73   // Starts the translation process for the page in the |page_lang| language.
74   void InitiateTranslation(const std::string& page_lang);
75 
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);
80 
81   // Reverts the contents of the page to its original language.
82   void RevertTranslation();
83 
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();
88 
89   // Callback types for translate errors.
90   typedef base::Callback<void(const TranslateErrorDetails&)>
91       TranslateErrorCallback;
92   typedef base::CallbackList<void(const TranslateErrorDetails&)>
93       TranslateErrorCallbackList;
94 
95   // Registers a callback for translate errors.
96   static scoped_ptr<TranslateErrorCallbackList::Subscription>
97       RegisterTranslateErrorCallback(const TranslateErrorCallback& callback);
98 
99   // Gets the LanguageState associated with the TranslateManager
100   LanguageState& GetLanguageState();
101 
102  private:
103   // Sends a translation request to the TranslateDriver.
104   void DoTranslatePage(const std::string& translate_script,
105                        const std::string& source_lang,
106                        const std::string& target_lang);
107 
108   // Called when the Translate script has been fetched.
109   // Initiates the translation.
110   void OnTranslateScriptFetchComplete(const std::string& source_lang,
111                                       const std::string& target_lang,
112                                       bool success,
113                                       const std::string& data);
114 
115   // Sequence number of the current page.
116   int page_seq_no_;
117 
118   // Preference name for the Accept-Languages HTTP header.
119   std::string accept_languages_pref_name_;
120 
121   TranslateClient* translate_client_;  // Weak.
122   TranslateDriver* translate_driver_;  // Weak.
123 
124   LanguageState language_state_;
125 
126   base::WeakPtrFactory<TranslateManager> weak_method_factory_;
127 
128   DISALLOW_COPY_AND_ASSIGN(TranslateManager);
129 };
130 
131 }  // namespace translate
132 
133 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_
134