• 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 class TranslateClient;
22 class TranslateDriver;
23 class TranslatePrefs;
24 struct TranslateErrorDetails;
25 
26 // The TranslateManager class is responsible for showing an info-bar when a page
27 // in a language different than the user language is loaded.  It triggers the
28 // page translation the user requests.
29 
30 class TranslateManager {
31  public:
32   // |translate_client| is expected to outlive the TranslateManager.
33   // |accept_language_pref_name| is the path for the preference for the
34   // accept-languages.
35   TranslateManager(TranslateClient* translate_client,
36                    const std::string& accept_language_pref_name);
37   virtual ~TranslateManager();
38 
39   // Returns a weak pointer to this instance.
40   base::WeakPtr<TranslateManager> GetWeakPtr();
41 
42   // Cannot return NULL.
translate_client()43   TranslateClient* translate_client() { return translate_client_; }
44 
45   // Returns the language to translate to. The language returned is the
46   // first language found in the following list that is supported by the
47   // translation service:
48   //     the UI language
49   //     the accept-language list
50   // If no language is found then an empty string is returned.
51   static std::string GetTargetLanguage(
52       const std::vector<std::string>& accept_languages_list);
53 
54   // Returns the language to automatically translate to. |original_language| is
55   // the webpage's original language.
56   static std::string GetAutoTargetLanguage(const std::string& original_language,
57                                            TranslatePrefs* translate_prefs);
58 
59   // Translates the page contents from |source_lang| to |target_lang|.
60   // The actual translation might be performed asynchronously if the translate
61   // script is not yet available.
62   void TranslatePage(const std::string& source_lang,
63                      const std::string& target_lang,
64                      bool triggered_from_menu);
65 
66   // Starts the translation process for a page in the |page_lang| language.
67   void InitiateTranslation(const std::string& page_lang);
68 
69   // Shows the after translate or error infobar depending on the details.
70   void PageTranslated(const std::string& source_lang,
71                       const std::string& target_lang,
72                       TranslateErrors::Type error_type);
73 
74   // Reverts the contents of the page to its original language.
75   void RevertTranslation();
76 
77   // Reports to the Google translate server that a page language was incorrectly
78   // detected.  This call is initiated by the user selecting the "report" menu
79   // under options in the translate infobar.
80   void ReportLanguageDetectionError();
81 
82   // Callback types for translate errors.
83   typedef base::Callback<void(const TranslateErrorDetails&)>
84       TranslateErrorCallback;
85   typedef base::CallbackList<void(const TranslateErrorDetails&)>
86       TranslateErrorCallbackList;
87 
88   // Registers a callback for translate errors.
89   static scoped_ptr<TranslateErrorCallbackList::Subscription>
90       RegisterTranslateErrorCallback(const TranslateErrorCallback& callback);
91 
92   // Gets the LanguageState associated with the TranslateManager
93   LanguageState& GetLanguageState();
94 
95  private:
96   // Sends a translation request to the TranslateDriver.
97   void DoTranslatePage(const std::string& translate_script,
98                        const std::string& source_lang,
99                        const std::string& target_lang);
100 
101   // Called when the Translate script has been fetched.
102   // Initiates the translation.
103   void OnTranslateScriptFetchComplete(int page_id,
104                                       const std::string& source_lang,
105                                       const std::string& target_lang,
106                                       bool success,
107                                       const std::string& data);
108 
109   // Preference name for the Accept-Languages HTTP header.
110   std::string accept_languages_pref_name_;
111 
112   TranslateClient* translate_client_;  // Weak.
113   TranslateDriver* translate_driver_;  // Weak.
114 
115   LanguageState language_state_;
116 
117   base::WeakPtrFactory<TranslateManager> weak_method_factory_;
118 
119   DISALLOW_COPY_AND_ASSIGN(TranslateManager);
120 };
121 
122 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_
123