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

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: Review comments 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.
33 32
34 class TranslateManager : public content::NotificationObserver { 33 class TranslateManager : public content::NotificationObserver {
35 public: 34 public:
36 // Returns the singleton instance. 35 explicit TranslateManager(TranslateTabHelper* helper);
37 static TranslateManager* GetInstance();
38
39 virtual ~TranslateManager(); 36 virtual ~TranslateManager();
40 37
41 // Returns true if the URL can be translated. 38 // Returns true if the URL can be translated.
42 static bool IsTranslatableURL(const GURL& url); 39 static bool IsTranslatableURL(const GURL& url);
43 40
44 // Returns the language to translate to. The language returned is the 41 // Returns the language to translate to. The language returned is the
45 // first language found in the following list that is supported by the 42 // first language found in the following list that is supported by the
46 // translation service: 43 // translation service:
47 // the UI language 44 // the UI language
48 // the accept-language list 45 // the accept-language list
49 // If no language is found then an empty string is returned. 46 // If no language is found then an empty string is returned.
50 static std::string GetTargetLanguage(PrefService* prefs); 47 static std::string GetTargetLanguage(PrefService* prefs);
51 48
52 // Returns the language to automatically translate to. |original_language| is 49 // Returns the language to automatically translate to. |original_language| is
53 // the webpage's original language. 50 // the webpage's original language.
54 static std::string GetAutoTargetLanguage(const std::string& original_language, 51 static std::string GetAutoTargetLanguage(const std::string& original_language,
55 PrefService* prefs); 52 PrefService* prefs);
56 53
57 // Translates the page contents from |source_lang| to |target_lang|. 54 // Translates the page contents from |source_lang| to |target_lang|.
58 // The actual translation might be performed asynchronously if the translate 55 // The actual translation might be performed asynchronously if the translate
59 // script is not yet available. 56 // script is not yet available.
60 void TranslatePage(content::WebContents* web_contents, 57 void TranslatePage(const std::string& source_lang,
61 const std::string& source_lang,
62 const std::string& target_lang); 58 const std::string& target_lang);
63 59
64 // Reverts the contents of the page in |web_contents| to its original 60 // Reverts the contents of the page to its original
blundell 2014/02/18 09:21:46 nit: Can fit on one line.
65 // language. 61 // language.
66 void RevertTranslation(content::WebContents* web_contents); 62 void RevertTranslation();
67 63
68 // Reports to the Google translate server that a page language was incorrectly 64 // 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 65 // detected. This call is initiated by the user selecting the "report" menu
70 // under options in the translate infobar. 66 // under options in the translate infobar.
71 void ReportLanguageDetectionError(content::WebContents* web_contents); 67 void ReportLanguageDetectionError();
72 68
73 // content::NotificationObserver implementation: 69 // content::NotificationObserver implementation:
74 virtual void Observe(int type, 70 virtual void Observe(int type,
75 const content::NotificationSource& source, 71 const content::NotificationSource& source,
76 const content::NotificationDetails& details) OVERRIDE; 72 const content::NotificationDetails& details) OVERRIDE;
77 73
78 // Number of attempts before waiting for a page to be fully reloaded. 74 // Number of attempts before waiting for a page to be fully reloaded.
79 void set_translate_max_reload_attemps(int attempts) { 75 void set_translate_max_reload_attemps(int attempts) {
80 max_reload_check_attempts_ = attempts; 76 max_reload_check_attempts_ = attempts;
81 } 77 }
82 78
83 // Callback types for translate errors. 79 // Callback types for translate errors.
84 typedef base::Callback<void(const TranslateErrorDetails&)> 80 typedef base::Callback<void(const TranslateErrorDetails&)>
85 TranslateErrorCallback; 81 TranslateErrorCallback;
86 typedef base::CallbackList<void(const TranslateErrorDetails&)> 82 typedef base::CallbackList<void(const TranslateErrorDetails&)>
87 TranslateErrorCallbackList; 83 TranslateErrorCallbackList;
88 84
89 // Registers a callback for translate errors. 85 // Registers a callback for translate errors.
90 static scoped_ptr<TranslateErrorCallbackList::Subscription> 86 static scoped_ptr<TranslateErrorCallbackList::Subscription>
91 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); 87 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback);
92 88
93 protected: 89 private:
94 TranslateManager(); 90 // Starts the translation process for a page in the |page_lang| language.
91 void InitiateTranslation(const std::string& page_lang);
95 92
96 private: 93 // If the tab is loading, this does nothing, otherwise it calls
blundell 2014/02/18 09:21:46 Is this the right comment change? It seems to be s
97 friend struct DefaultSingletonTraits<TranslateManager>; 94 // InitiateTranslation.
95 void InitiateTranslationPosted(const std::string& page_lang, int attempt);
98 96
99 // Structure that describes a translate request. 97 // Sends a translation request.
blundell 2014/02/18 09:21:46 This comment is not very clear at this point.
100 // Translation may be deferred while the translate script is being retrieved 98 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, 99 const std::string& source_lang,
124 const std::string& target_lang); 100 const std::string& target_lang);
125 101
126 // Shows the after translate or error infobar depending on the details. 102 // Shows the after translate or error infobar depending on the details.
127 void PageTranslated(content::WebContents* web_contents, 103 void PageTranslated(PageTranslatedDetails* details);
128 PageTranslatedDetails* details);
129 104
130 void OnTranslateScriptFetchComplete(PendingRequest request, 105 void OnTranslateScriptFetchComplete(int page_id,
blundell 2014/02/18 09:21:46 Could you add a comment to this method?
106 const std::string& source_lang,
107 const std::string& target_lang,
131 bool success, 108 bool success,
132 const std::string& data); 109 const std::string& data);
133 110
134 content::NotificationRegistrar notification_registrar_; 111 content::NotificationRegistrar notification_registrar_;
135 112
136 // Max number of attempts before checking if a page has been reloaded. 113 // Max number of attempts before checking if a page has been reloaded.
137 int max_reload_check_attempts_; 114 int max_reload_check_attempts_;
138 115
116 TranslateTabHelper* translate_tab_helper_; // Weak
117
139 base::WeakPtrFactory<TranslateManager> weak_method_factory_; 118 base::WeakPtrFactory<TranslateManager> weak_method_factory_;
140 119
141 DISALLOW_COPY_AND_ASSIGN(TranslateManager); 120 DISALLOW_COPY_AND_ASSIGN(TranslateManager);
142 }; 121 };
143 122
144 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ 123 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698