OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 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_TAB_HELPER_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 11 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
12 #include "components/translate/content/browser/content_translate_driver.h" | 12 #include "components/translate/content/browser/content_translate_driver.h" |
13 #include "components/translate/core/common/translate_errors.h" | 13 #include "components/translate/core/common/translate_errors.h" |
14 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
15 #include "content/public/browser/web_contents_user_data.h" | 15 #include "content/public/browser/web_contents_user_data.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 class BrowserContext; | 18 class BrowserContext; |
19 class WebContents; | 19 class WebContents; |
20 } | 20 } |
21 | 21 |
22 struct LanguageDetectionDetails; | 22 struct LanguageDetectionDetails; |
23 class PrefService; | 23 class PrefService; |
24 class TranslateAcceptLanguages; | 24 class TranslateAcceptLanguages; |
25 class TranslatePrefs; | 25 class TranslatePrefs; |
| 26 class TranslateManager; |
26 | 27 |
27 class TranslateTabHelper | 28 class TranslateTabHelper |
28 : public content::WebContentsObserver, | 29 : public content::WebContentsObserver, |
29 public content::WebContentsUserData<TranslateTabHelper> { | 30 public content::WebContentsUserData<TranslateTabHelper> { |
30 public: | 31 public: |
31 virtual ~TranslateTabHelper(); | 32 virtual ~TranslateTabHelper(); |
32 | 33 |
33 // Gets the LanguageState associated with the page. | 34 // Gets the LanguageState associated with the page. |
34 LanguageState& GetLanguageState(); | 35 LanguageState& GetLanguageState(); |
35 | 36 |
36 // Returns the ContentTranslateDriver instance associated with this | 37 // Returns the ContentTranslateDriver instance associated with this |
37 // WebContents. | 38 // WebContents. |
38 ContentTranslateDriver& translate_driver() { return translate_driver_; } | 39 ContentTranslateDriver& translate_driver() { return translate_driver_; } |
39 | 40 |
40 // Helper method to return a new TranslatePrefs instance. | 41 // Helper method to return a new TranslatePrefs instance. |
41 static scoped_ptr<TranslatePrefs> CreateTranslatePrefs(PrefService* prefs); | 42 static scoped_ptr<TranslatePrefs> CreateTranslatePrefs(PrefService* prefs); |
42 | 43 |
43 // Helper method to return the TranslateAcceptLanguages instance associated | 44 // Helper method to return the TranslateAcceptLanguages instance associated |
44 // with |browser_context|. | 45 // with |browser_context|. |
45 static TranslateAcceptLanguages* GetTranslateAcceptLanguages( | 46 static TranslateAcceptLanguages* GetTranslateAcceptLanguages( |
46 content::BrowserContext* browser_context); | 47 content::BrowserContext* browser_context); |
47 | 48 |
| 49 // Helper method to return the TranslateManager instance associated with |
| 50 // |web_contents|, or NULL if there is no such associated instance. |
| 51 static TranslateManager* GetManagerFromWebContents( |
| 52 content::WebContents* web_contents); |
| 53 |
| 54 // Gets the associated TranslateManager. |
| 55 TranslateManager* GetTranslateManager(); |
| 56 |
| 57 // Gets the associated WebContents. Returns NULL if the WebContents is being |
| 58 // destroyed. |
| 59 content::WebContents* GetWebContents(); |
| 60 |
48 // Denotes which state the user is in with respect to translate. | 61 // Denotes which state the user is in with respect to translate. |
49 enum TranslateStep { | 62 enum TranslateStep { |
50 BEFORE_TRANSLATE, | 63 BEFORE_TRANSLATE, |
51 TRANSLATING, | 64 TRANSLATING, |
52 AFTER_TRANSLATE, | 65 AFTER_TRANSLATE, |
53 TRANSLATE_ERROR | 66 TRANSLATE_ERROR |
54 }; | 67 }; |
55 | 68 |
56 // Called when the embedder should present UI to the user corresponding to the | 69 // Called when the embedder should present UI to the user corresponding to the |
57 // user's current |step|. | 70 // user's current |step|. |
58 void ShowTranslateUI(TranslateStep step, | 71 void ShowTranslateUI(TranslateStep step, |
59 content::WebContents* web_contents, | |
60 const std::string source_language, | 72 const std::string source_language, |
61 const std::string target_language, | 73 const std::string target_language, |
62 TranslateErrors::Type error_type); | 74 TranslateErrors::Type error_type); |
63 | 75 |
64 private: | 76 private: |
65 explicit TranslateTabHelper(content::WebContents* web_contents); | 77 explicit TranslateTabHelper(content::WebContents* web_contents); |
66 friend class content::WebContentsUserData<TranslateTabHelper>; | 78 friend class content::WebContentsUserData<TranslateTabHelper>; |
67 | 79 |
68 // content::WebContentsObserver implementation. | 80 // content::WebContentsObserver implementation. |
69 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 81 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
70 virtual void DidNavigateAnyFrame( | 82 virtual void DidNavigateAnyFrame( |
71 const content::LoadCommittedDetails& details, | 83 const content::LoadCommittedDetails& details, |
72 const content::FrameNavigateParams& params) OVERRIDE; | 84 const content::FrameNavigateParams& params) OVERRIDE; |
| 85 virtual void WebContentsDestroyed( |
| 86 content::WebContents* web_contents) OVERRIDE; |
73 | 87 |
74 void OnLanguageDetermined(const LanguageDetectionDetails& details, | 88 void OnLanguageDetermined(const LanguageDetectionDetails& details, |
75 bool page_needs_translation); | 89 bool page_needs_translation); |
76 void OnPageTranslated(int32 page_id, | 90 void OnPageTranslated(int32 page_id, |
77 const std::string& original_lang, | 91 const std::string& original_lang, |
78 const std::string& translated_lang, | 92 const std::string& translated_lang, |
79 TranslateErrors::Type error_type); | 93 TranslateErrors::Type error_type); |
80 | 94 |
81 // Shows the translate bubble. | 95 // Shows the translate bubble. |
82 void ShowBubble(content::WebContents* web_contents, | 96 void ShowBubble(TranslateStep step, TranslateErrors::Type error_type); |
83 TranslateStep step, | |
84 TranslateErrors::Type error_type); | |
85 | 97 |
86 ContentTranslateDriver translate_driver_; | 98 ContentTranslateDriver translate_driver_; |
| 99 scoped_ptr<TranslateManager> translate_manager_; |
87 | 100 |
88 DISALLOW_COPY_AND_ASSIGN(TranslateTabHelper); | 101 DISALLOW_COPY_AND_ASSIGN(TranslateTabHelper); |
89 }; | 102 }; |
90 | 103 |
91 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ | 104 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ |
OLD | NEW |