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