Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: chrome/browser/translate/translate_manager.h

Issue 166963002: TranslateManager is no longer a singleton (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@temp
Patch Set: rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/callback_list.h" 12 #include "base/callback_list.h"
13 #include "base/gtest_prod_util.h" 13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
16 #include "content/public/browser/notification_observer.h" 16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h" 17 #include "content/public/browser/notification_registrar.h"
18 18
19 template <typename T> struct DefaultSingletonTraits;
20 class GURL; 19 class GURL;
21 struct PageTranslatedDetails; 20 struct PageTranslatedDetails;
22 class PrefService; 21 class PrefService;
23 struct TranslateErrorDetails; 22 struct TranslateErrorDetails;
23 class TranslateTabHelper;
24 24
25 namespace content { 25 namespace content {
26 class WebContents; 26 class WebContents;
27 } 27 }
28 28
29 // The TranslateManager class is responsible for showing an info-bar when a page 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 30 // in a language different than the user language is loaded. It triggers the
31 // page translation the user requests. 31 // page translation the user requests.
32 // It is a singleton. 32 // It is a singleton.
blundell 2014/02/17 15:26:28 Kill
33 33
34 class TranslateManager : public content::NotificationObserver { 34 class TranslateManager : public content::NotificationObserver {
35 public: 35 public:
36 // Returns the singleton instance. 36 explicit TranslateManager(TranslateTabHelper* helper);
37 static TranslateManager* GetInstance();
38
39 virtual ~TranslateManager(); 37 virtual ~TranslateManager();
40 38
41 // Returns true if the URL can be translated. 39 // Returns true if the URL can be translated.
42 static bool IsTranslatableURL(const GURL& url); 40 static bool IsTranslatableURL(const GURL& url);
43 41
44 // Returns the language to translate to. The language returned is the 42 // Returns the language to translate to. The language returned is the
45 // first language found in the following list that is supported by the 43 // first language found in the following list that is supported by the
46 // translation service: 44 // translation service:
47 // the UI language 45 // the UI language
48 // the accept-language list 46 // the accept-language list
49 // If no language is found then an empty string is returned. 47 // If no language is found then an empty string is returned.
50 static std::string GetTargetLanguage(PrefService* prefs); 48 static std::string GetTargetLanguage(PrefService* prefs);
51 49
52 // Returns the language to automatically translate to. |original_language| is 50 // Returns the language to automatically translate to. |original_language| is
53 // the webpage's original language. 51 // the webpage's original language.
54 static std::string GetAutoTargetLanguage(const std::string& original_language, 52 static std::string GetAutoTargetLanguage(const std::string& original_language,
55 PrefService* prefs); 53 PrefService* prefs);
56 54
57 // Translates the page contents from |source_lang| to |target_lang|. 55 // Translates the page contents from |source_lang| to |target_lang|.
58 // The actual translation might be performed asynchronously if the translate 56 // The actual translation might be performed asynchronously if the translate
59 // script is not yet available. 57 // script is not yet available.
60 void TranslatePage(content::WebContents* web_contents, 58 void TranslatePage(const std::string& source_lang,
61 const std::string& source_lang,
62 const std::string& target_lang); 59 const std::string& target_lang);
63 60
64 // Reverts the contents of the page in |web_contents| to its original 61 // Reverts the contents of the page to its original
65 // language. 62 // language.
66 void RevertTranslation(content::WebContents* web_contents); 63 void RevertTranslation();
67 64
68 // Reports to the Google translate server that a page language was incorrectly 65 // 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 66 // detected. This call is initiated by the user selecting the "report" menu
70 // under options in the translate infobar. 67 // under options in the translate infobar.
71 void ReportLanguageDetectionError(content::WebContents* web_contents); 68 void ReportLanguageDetectionError();
72 69
73 // content::NotificationObserver implementation: 70 // content::NotificationObserver implementation:
74 virtual void Observe(int type, 71 virtual void Observe(int type,
75 const content::NotificationSource& source, 72 const content::NotificationSource& source,
76 const content::NotificationDetails& details) OVERRIDE; 73 const content::NotificationDetails& details) OVERRIDE;
77 74
78 // Number of attempts before waiting for a page to be fully reloaded. 75 // Number of attempts before waiting for a page to be fully reloaded.
79 void set_translate_max_reload_attemps(int attempts) { 76 void set_translate_max_reload_attemps(int attempts) {
80 max_reload_check_attempts_ = attempts; 77 max_reload_check_attempts_ = attempts;
81 } 78 }
82 79
83 // Callback types for translate errors. 80 // Callback types for translate errors.
84 typedef base::Callback<void(const TranslateErrorDetails&)> 81 typedef base::Callback<void(const TranslateErrorDetails&)>
85 TranslateErrorCallback; 82 TranslateErrorCallback;
86 typedef base::CallbackList<void(const TranslateErrorDetails&)> 83 typedef base::CallbackList<void(const TranslateErrorDetails&)>
87 TranslateErrorCallbackList; 84 TranslateErrorCallbackList;
88 85
89 // Registers a callback for translate errors. 86 // Registers a callback for translate errors.
90 static scoped_ptr<TranslateErrorCallbackList::Subscription> 87 static scoped_ptr<TranslateErrorCallbackList::Subscription>
91 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); 88 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback);
92 89
93 protected: 90 private:
94 TranslateManager(); 91 // Starts the translation process for a page in the |page_lang| language.
92 void InitiateTranslation(const std::string& page_lang);
95 93
96 private: 94 // If the tab is loading, this does nothing, otherwise it calls
97 friend struct DefaultSingletonTraits<TranslateManager>; 95 // InitiateTranslation.
96 void InitiateTranslationPosted(const std::string& page_lang, int attempt);
98 97
99 // Structure that describes a translate request. 98 // Sends a translation request.
100 // Translation may be deferred while the translate script is being retrieved 99 void DoTranslatePage(const std::string& translate_script,
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, 100 const std::string& source_lang,
124 const std::string& target_lang); 101 const std::string& target_lang);
125 102
126 // Shows the after translate or error infobar depending on the details. 103 // Shows the after translate or error infobar depending on the details.
127 void PageTranslated(content::WebContents* web_contents, 104 void PageTranslated(PageTranslatedDetails* details);
128 PageTranslatedDetails* details);
129 105
130 void OnTranslateScriptFetchComplete(PendingRequest request, 106 void OnTranslateScriptFetchComplete(int page_id,
107 const std::string& source_lang,
108 const std::string& target_lang,
131 bool success, 109 bool success,
132 const std::string& data); 110 const std::string& data);
133 111
134 content::NotificationRegistrar notification_registrar_; 112 content::NotificationRegistrar notification_registrar_;
135 113
136 // Max number of attempts before checking if a page has been reloaded. 114 // Max number of attempts before checking if a page has been reloaded.
137 int max_reload_check_attempts_; 115 int max_reload_check_attempts_;
138 116
117 TranslateTabHelper* translate_tab_helper_; // Weak
118
139 base::WeakPtrFactory<TranslateManager> weak_method_factory_; 119 base::WeakPtrFactory<TranslateManager> weak_method_factory_;
140 120
141 DISALLOW_COPY_AND_ASSIGN(TranslateManager); 121 DISALLOW_COPY_AND_ASSIGN(TranslateManager);
142 }; 122 };
143 123
144 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ 124 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698