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 "base/memory/weak_ptr.h" |
11 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 12 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
12 #include "components/translate/content/browser/content_translate_driver.h" | 13 #include "components/translate/content/browser/content_translate_driver.h" |
13 #include "components/translate/core/browser/translate_client.h" | 14 #include "components/translate/core/browser/translate_client.h" |
14 #include "components/translate/core/browser/translate_step.h" | 15 #include "components/translate/core/browser/translate_step.h" |
15 #include "components/translate/core/common/translate_errors.h" | 16 #include "components/translate/core/common/translate_errors.h" |
16 #include "content/public/browser/web_contents_observer.h" | 17 #include "content/public/browser/web_contents_observer.h" |
17 #include "content/public/browser/web_contents_user_data.h" | 18 #include "content/public/browser/web_contents_user_data.h" |
18 | 19 |
19 #if defined(CLD2_DYNAMIC_MODE) | 20 #if defined(CLD2_DYNAMIC_MODE) |
20 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
21 #include "base/lazy_instance.h" | 22 #include "base/lazy_instance.h" |
22 #include "base/memory/weak_ptr.h" | |
23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
24 #include "base/task_runner.h" | 24 #include "base/task_runner.h" |
25 #endif | 25 #endif |
26 | 26 |
27 namespace base { | 27 namespace base { |
28 class File; | 28 class File; |
29 } | 29 } |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 class BrowserContext; | 32 class BrowserContext; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 std::string* source, | 71 std::string* source, |
72 std::string* target); | 72 std::string* target); |
73 | 73 |
74 // Gets the associated TranslateManager. | 74 // Gets the associated TranslateManager. |
75 TranslateManager* GetTranslateManager(); | 75 TranslateManager* GetTranslateManager(); |
76 | 76 |
77 // Gets the associated WebContents. Returns NULL if the WebContents is being | 77 // Gets the associated WebContents. Returns NULL if the WebContents is being |
78 // destroyed. | 78 // destroyed. |
79 content::WebContents* GetWebContents(); | 79 content::WebContents* GetWebContents(); |
80 | 80 |
| 81 // Number of attempts before waiting for a page to be fully reloaded. |
| 82 void set_translate_max_reload_attempts(int attempts) { |
| 83 max_reload_check_attempts_ = attempts; |
| 84 } |
| 85 |
81 // TranslateClient implementation. | 86 // TranslateClient implementation. |
82 virtual TranslateDriver* GetTranslateDriver() OVERRIDE; | 87 virtual TranslateDriver* GetTranslateDriver() OVERRIDE; |
83 virtual PrefService* GetPrefs() OVERRIDE; | 88 virtual PrefService* GetPrefs() OVERRIDE; |
84 virtual scoped_ptr<TranslatePrefs> GetTranslatePrefs() OVERRIDE; | 89 virtual scoped_ptr<TranslatePrefs> GetTranslatePrefs() OVERRIDE; |
85 virtual TranslateAcceptLanguages* GetTranslateAcceptLanguages() OVERRIDE; | 90 virtual TranslateAcceptLanguages* GetTranslateAcceptLanguages() OVERRIDE; |
86 virtual void ShowTranslateUI(translate::TranslateStep step, | 91 virtual void ShowTranslateUI(translate::TranslateStep step, |
87 const std::string source_language, | 92 const std::string source_language, |
88 const std::string target_language, | 93 const std::string target_language, |
89 TranslateErrors::Type error_type, | 94 TranslateErrors::Type error_type, |
90 bool triggered_from_menu) OVERRIDE; | 95 bool triggered_from_menu) OVERRIDE; |
91 | 96 |
92 private: | 97 private: |
93 explicit TranslateTabHelper(content::WebContents* web_contents); | 98 explicit TranslateTabHelper(content::WebContents* web_contents); |
94 friend class content::WebContentsUserData<TranslateTabHelper>; | 99 friend class content::WebContentsUserData<TranslateTabHelper>; |
95 | 100 |
96 // content::WebContentsObserver implementation. | 101 // content::WebContentsObserver implementation. |
| 102 virtual void NavigationEntryCommitted( |
| 103 const content::LoadCommittedDetails& load_details) OVERRIDE; |
97 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 104 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
98 virtual void DidNavigateAnyFrame( | 105 virtual void DidNavigateAnyFrame( |
99 const content::LoadCommittedDetails& details, | 106 const content::LoadCommittedDetails& details, |
100 const content::FrameNavigateParams& params) OVERRIDE; | 107 const content::FrameNavigateParams& params) OVERRIDE; |
101 virtual void WebContentsDestroyed( | 108 virtual void WebContentsDestroyed( |
102 content::WebContents* web_contents) OVERRIDE; | 109 content::WebContents* web_contents) OVERRIDE; |
103 | 110 |
| 111 // Initiates translation once the page is finished loading. |
| 112 void InitiateTranslation(const std::string& page_lang, int attempt); |
104 void OnLanguageDetermined(const LanguageDetectionDetails& details, | 113 void OnLanguageDetermined(const LanguageDetectionDetails& details, |
105 bool page_needs_translation); | 114 bool page_needs_translation); |
106 void OnPageTranslated(int32 page_id, | 115 void OnPageTranslated(int32 page_id, |
107 const std::string& original_lang, | 116 const std::string& original_lang, |
108 const std::string& translated_lang, | 117 const std::string& translated_lang, |
109 TranslateErrors::Type error_type); | 118 TranslateErrors::Type error_type); |
110 | 119 |
111 #if defined(CLD2_DYNAMIC_MODE) | 120 #if defined(CLD2_DYNAMIC_MODE) |
112 // Called when we receive ChromeViewHostMsg_NeedCLDData from a renderer. | 121 // Called when we receive ChromeViewHostMsg_NeedCLDData from a renderer. |
113 // If we have already cached the data, responds immediately; else, enqueues | 122 // If we have already cached the data, responds immediately; else, enqueues |
(...skipping 11 matching lines...) Expand all Loading... |
125 static void HandleCLDDataRequest(); | 134 static void HandleCLDDataRequest(); |
126 | 135 |
127 // If the CLD data is ready, send it to the renderer. Briefly checks the lock. | 136 // If the CLD data is ready, send it to the renderer. Briefly checks the lock. |
128 void MaybeSendCLDDataAvailable(); | 137 void MaybeSendCLDDataAvailable(); |
129 | 138 |
130 // Sends the renderer a response containing the data file handle. No locking. | 139 // Sends the renderer a response containing the data file handle. No locking. |
131 void SendCLDDataAvailable(const base::File* handle, | 140 void SendCLDDataAvailable(const base::File* handle, |
132 const uint64 data_offset, | 141 const uint64 data_offset, |
133 const uint64 data_length); | 142 const uint64 data_length); |
134 | 143 |
135 // Necessary for binding the callback to HandleCLDDataRequest on the blocking | |
136 // pool. | |
137 base::WeakPtrFactory<TranslateTabHelper> weak_pointer_factory_; | |
138 | |
139 // The data file, cached as long as the process stays alive. | 144 // The data file, cached as long as the process stays alive. |
140 // We also track the offset at which the data starts, and its length. | 145 // We also track the offset at which the data starts, and its length. |
141 static base::File* s_cached_file_; // guarded by file_lock_ | 146 static base::File* s_cached_file_; // guarded by file_lock_ |
142 static uint64 s_cached_data_offset_; // guarded by file_lock_ | 147 static uint64 s_cached_data_offset_; // guarded by file_lock_ |
143 static uint64 s_cached_data_length_; // guarded by file_lock_ | 148 static uint64 s_cached_data_length_; // guarded by file_lock_ |
144 | 149 |
145 // Guards s_cached_file_ | 150 // Guards s_cached_file_ |
146 static base::LazyInstance<base::Lock> s_file_lock_; | 151 static base::LazyInstance<base::Lock> s_file_lock_; |
147 | 152 |
148 #endif | 153 #endif |
149 | 154 |
150 // Shows the translate bubble. | 155 // Shows the translate bubble. |
151 void ShowBubble(translate::TranslateStep step, | 156 void ShowBubble(translate::TranslateStep step, |
152 TranslateErrors::Type error_type); | 157 TranslateErrors::Type error_type); |
153 | 158 |
| 159 // Max number of attempts before checking if a page has been reloaded. |
| 160 int max_reload_check_attempts_; |
| 161 |
154 ContentTranslateDriver translate_driver_; | 162 ContentTranslateDriver translate_driver_; |
155 scoped_ptr<TranslateManager> translate_manager_; | 163 scoped_ptr<TranslateManager> translate_manager_; |
156 | 164 |
| 165 // Necessary for binding the callback to HandleCLDDataRequest on the blocking |
| 166 // pool and for delaying translation initialization until the page has |
| 167 // finished loading on a reload. |
| 168 base::WeakPtrFactory<TranslateTabHelper> weak_pointer_factory_; |
| 169 |
157 DISALLOW_COPY_AND_ASSIGN(TranslateTabHelper); | 170 DISALLOW_COPY_AND_ASSIGN(TranslateTabHelper); |
158 }; | 171 }; |
159 | 172 |
160 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ | 173 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ |
OLD | NEW |