Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / translate / translate_manager.h
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.
4
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
6 #define CHROME_BROWSER_TRANSLATE_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 "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18
19 template <typename T> struct DefaultSingletonTraits;
20 class GURL;
21 struct PageTranslatedDetails;
22 class PrefService;
23 struct TranslateErrorDetails;
24
25 namespace content {
26 class WebContents;
27 }
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 // It is a singleton.
33
34 class TranslateManager : public content::NotificationObserver {
35  public:
36   // Returns the singleton instance.
37   static TranslateManager* GetInstance();
38
39   virtual ~TranslateManager();
40
41   // Returns true if the URL can be translated.
42   static bool IsTranslatableURL(const GURL& url);
43
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:
47   //     the UI language
48   //     the accept-language list
49   // If no language is found then an empty string is returned.
50   static std::string GetTargetLanguage(PrefService* prefs);
51
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,
55                                            PrefService* prefs);
56
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);
63
64   // Reverts the contents of the page in |web_contents| to its original
65   // language.
66   void RevertTranslation(content::WebContents* web_contents);
67
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);
72
73   // content::NotificationObserver implementation:
74   virtual void Observe(int type,
75                        const content::NotificationSource& source,
76                        const content::NotificationDetails& details) OVERRIDE;
77
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;
81   }
82
83   // Callback types for translate errors.
84   typedef base::Callback<void(const TranslateErrorDetails&)>
85       TranslateErrorCallback;
86   typedef base::CallbackList<void(const TranslateErrorDetails&)>
87       TranslateErrorCallbackList;
88
89   // Registers a callback for translate errors.
90   static scoped_ptr<TranslateErrorCallbackList::Subscription>
91       RegisterTranslateErrorCallback(const TranslateErrorCallback& callback);
92
93  protected:
94   TranslateManager();
95
96  private:
97   friend struct DefaultSingletonTraits<TranslateManager>;
98
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;
104     int render_view_id;
105     int page_id;
106     std::string source_lang;
107     std::string target_lang;
108   };
109
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);
114
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);
119
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);
125
126   // Shows the after translate or error infobar depending on the details.
127   void PageTranslated(content::WebContents* web_contents,
128                       PageTranslatedDetails* details);
129
130   void OnTranslateScriptFetchComplete(PendingRequest request,
131                                       bool success,
132                                       const std::string& data);
133
134   content::NotificationRegistrar notification_registrar_;
135
136   // Max number of attempts before checking if a page has been reloaded.
137   int max_reload_check_attempts_;
138
139   base::WeakPtrFactory<TranslateManager> weak_method_factory_;
140
141   DISALLOW_COPY_AND_ASSIGN(TranslateManager);
142 };
143
144 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_