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 #include "chrome/browser/translate/translate_tab_helper.h" | 5 #include "chrome/browser/translate/translate_tab_helper.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/path_service.h" | |
10 #include "base/platform_file.h" | |
11 #include "base/synchronization/lock.h" | |
12 #include "base/task_runner.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/translate/translate_accept_languages_factory.h" | 15 #include "chrome/browser/translate/translate_accept_languages_factory.h" |
11 #include "chrome/browser/translate/translate_infobar_delegate.h" | 16 #include "chrome/browser/translate/translate_infobar_delegate.h" |
12 #include "chrome/browser/translate/translate_manager.h" | 17 #include "chrome/browser/translate/translate_manager.h" |
13 #include "chrome/browser/translate/translate_service.h" | 18 #include "chrome/browser/translate/translate_service.h" |
14 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_finder.h" | 20 #include "chrome/browser/ui/browser_finder.h" |
16 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | 23 #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
24 #include "chrome/common/chrome_paths.h" | |
19 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
20 #include "chrome/common/render_messages.h" | 26 #include "chrome/common/render_messages.h" |
21 #include "components/translate/core/browser/page_translated_details.h" | 27 #include "components/translate/core/browser/page_translated_details.h" |
22 #include "components/translate/core/browser/translate_accept_languages.h" | 28 #include "components/translate/core/browser/translate_accept_languages.h" |
23 #include "components/translate/core/browser/translate_prefs.h" | 29 #include "components/translate/core/browser/translate_prefs.h" |
24 #include "components/translate/core/common/language_detection_details.h" | 30 #include "components/translate/core/common/language_detection_details.h" |
31 #include "content/public/browser/browser_thread.h" | |
25 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
33 #include "content/public/browser/render_process_host.h" | |
34 #include "content/public/browser/render_view_host.h" | |
26 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
27 | 36 |
28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); | 37 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); |
29 | 38 |
39 #if defined(CLD2_DYNAMIC_MODE) | |
40 // Statics defined in the .h file: | |
41 base::PlatformFile TranslateTabHelper::s_cached_platform_file_ = 0; | |
bulach
2014/03/23 20:57:02
nit: sorry, I just found that it should be kInvali
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
42 base::LazyInstance<base::Lock> TranslateTabHelper::s_file_lock_ | |
43 = LAZY_INSTANCE_INITIALIZER; | |
44 #endif | |
45 | |
30 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) | 46 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) |
31 : content::WebContentsObserver(web_contents), | 47 : content::WebContentsObserver(web_contents), |
48 #if defined(CLD2_DYNAMIC_MODE) | |
49 weak_pointer_factory_(this), | |
50 #endif | |
32 translate_driver_(&web_contents->GetController()), | 51 translate_driver_(&web_contents->GetController()), |
33 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)) {} | 52 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)) {} |
34 | 53 |
35 TranslateTabHelper::~TranslateTabHelper() { | 54 TranslateTabHelper::~TranslateTabHelper() { |
36 } | 55 } |
37 | 56 |
38 LanguageState& TranslateTabHelper::GetLanguageState() { | 57 LanguageState& TranslateTabHelper::GetLanguageState() { |
39 return translate_driver_.GetLanguageState(); | 58 return translate_driver_.GetLanguageState(); |
40 } | 59 } |
41 | 60 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 TranslateDriver* TranslateTabHelper::GetTranslateDriver() { | 131 TranslateDriver* TranslateTabHelper::GetTranslateDriver() { |
113 return &translate_driver_; | 132 return &translate_driver_; |
114 } | 133 } |
115 | 134 |
116 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { | 135 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { |
117 bool handled = true; | 136 bool handled = true; |
118 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) | 137 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) |
119 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, | 138 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, |
120 OnLanguageDetermined) | 139 OnLanguageDetermined) |
121 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) | 140 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) |
141 #if defined(CLD2_DYNAMIC_MODE) | |
142 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested) | |
143 #endif | |
122 IPC_MESSAGE_UNHANDLED(handled = false) | 144 IPC_MESSAGE_UNHANDLED(handled = false) |
123 IPC_END_MESSAGE_MAP() | 145 IPC_END_MESSAGE_MAP() |
124 | 146 |
125 return handled; | 147 return handled; |
126 } | 148 } |
127 | 149 |
128 void TranslateTabHelper::DidNavigateAnyFrame( | 150 void TranslateTabHelper::DidNavigateAnyFrame( |
129 const content::LoadCommittedDetails& details, | 151 const content::LoadCommittedDetails& details, |
130 const content::FrameNavigateParams& params) { | 152 const content::FrameNavigateParams& params) { |
131 // Let the LanguageState clear its state. | 153 // Let the LanguageState clear its state. |
132 translate_driver_.DidNavigate(details); | 154 translate_driver_.DidNavigate(details); |
133 } | 155 } |
134 | 156 |
135 void TranslateTabHelper::WebContentsDestroyed( | 157 void TranslateTabHelper::WebContentsDestroyed( |
136 content::WebContents* web_contents) { | 158 content::WebContents* web_contents) { |
137 // Translation process can be interrupted. | 159 // Translation process can be interrupted. |
138 // Destroying the TranslateManager now guarantees that it never has to deal | 160 // Destroying the TranslateManager now guarantees that it never has to deal |
139 // with NULL WebContents. | 161 // with NULL WebContents. |
140 translate_manager_.reset(); | 162 translate_manager_.reset(); |
141 } | 163 } |
142 | 164 |
165 #if defined(CLD2_DYNAMIC_MODE) | |
166 void TranslateTabHelper::OnCLDDataRequested() { | |
167 // Quickly try to read s_cached_platform_file_. If it's non-zero, we have | |
168 // cached the CLD data file and can answer immediately; else, we'll make | |
169 // a request to the blocking pool to look up the file and cache it. | |
170 // The value of s_cached_platform_file_ becomes non-zero is only set once, | |
171 // so when it becomes non-zero we are guaranteed it will not change again. | |
172 if (!s_file_lock_.Get().Try()) | |
173 return; // We'll get another request later, whatevs! | |
174 const base::PlatformFile handle = s_cached_platform_file_; | |
175 s_file_lock_.Get().Release(); | |
176 | |
177 if (handle == 0) { | |
bulach
2014/03/23 20:57:02
nit: s/0/kInvalidPlatformFileValue/
I've seen the
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
178 // We don't have the data file yet. Queue an asynchronous caching attempt. | |
bulach
2014/03/23 20:57:02
nit: I've got reviewers asking to not have pronoun
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done, and reviewed the other .h/.cc files as well
| |
179 // The caching attempt happens in the blocking pool because it may involve | |
180 // arbitrary filesystem access. | |
181 // After the caching attempt is made, we call MaybeSendCLDDataAvailable | |
182 // to pass the file handle to the renderer. This only results in an IPC | |
183 // message if the caching attempt was successful. | |
184 content::BrowserThread::PostBlockingPoolTaskAndReply( | |
185 FROM_HERE, | |
186 base::Bind(&TranslateTabHelper::HandleCLDDataRequest), | |
187 base::Bind(&TranslateTabHelper::MaybeSendCLDDataAvailable, | |
188 weak_pointer_factory_.GetWeakPtr())); | |
189 } else { | |
190 // We do have the data available. Respond to the request. | |
191 SendCLDDataAvailable(handle); | |
192 } | |
193 } | |
194 | |
195 void TranslateTabHelper::MaybeSendCLDDataAvailable() { | |
196 if (!s_file_lock_.Get().Try()) | |
bulach
2014/03/23 20:57:02
I don't quite follow why it's just trying rather t
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
As discussed offline, because in terms of performa
| |
197 return; // We'll get another request later, whatevs! | |
198 const base::PlatformFile handle = s_cached_platform_file_; | |
199 s_file_lock_.Get().Release(); | |
200 if (handle == 0) | |
201 return; // Nope, not ready yet | |
202 SendCLDDataAvailable(handle); | |
203 } | |
204 | |
205 void TranslateTabHelper::SendCLDDataAvailable(const base::PlatformFile handle) { | |
bulach
2014/03/23 20:57:02
nit: can this be a const &?
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
It's a file descriptor (integer) under the covers.
| |
206 // WARNING: This method can be invoked from either the IO thread or the | |
207 // blocking pool. Do not use the synchronous invocation style. | |
bulach
2014/03/23 20:57:02
not clear, synchronous invocation style of what?
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Cleared up, I hope.
| |
208 | |
209 // We have the data available, respond to the request. | |
210 IPC::PlatformFileForTransit ipc_platform_file = | |
211 IPC::GetFileHandleForProcess( | |
212 handle, | |
ulas
2014/03/21 21:08:32
Pls also allow offset+length for flexibility. Perh
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Will do this shortly, after I address Marcus' larg
| |
213 GetWebContents()->GetRenderViewHost()->GetProcess()->GetHandle(), | |
214 false); | |
215 Send(new ChromeViewMsg_CLDDataAvailable( | |
216 GetWebContents()->GetRenderViewHost()->GetRoutingID(), | |
217 ipc_platform_file)); | |
218 } | |
219 | |
220 void TranslateTabHelper::HandleCLDDataRequest() { | |
221 // Because this function involves arbitrary file system access, it must run | |
222 // on the blocking pool. | |
223 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
224 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
225 | |
226 base::AutoLock lock(s_file_lock_.Get()); | |
bulach
2014/03/23 20:57:02
nit: use a separate block here:
{
base::AutoLock
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
227 if (s_cached_platform_file_ != 0) | |
228 return; // already done, duplicate request | |
229 | |
230 base::FilePath path; | |
231 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { | |
232 LOG(WARNING) << "Unable to locate user data directory"; | |
233 return; // Chrome isn't properly installed. | |
234 } | |
235 | |
236 // If the file exists, we can send an IPC-safe construct back to the | |
237 // renderer process immediately. | |
238 path = path.Append(chrome::kCLDDataFilename); | |
239 if (!base::PathExists(path)) | |
240 return; | |
241 | |
242 // Attempt to open the file for reading. It exists, so we should succeed. | |
243 bool created = false; | |
244 base::PlatformFileError error; | |
245 s_cached_platform_file_ = base::CreatePlatformFile( | |
bulach
2014/03/23 20:57:02
then here, use a local variable.
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
246 path, | |
247 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
248 &created, &error); | |
249 DCHECK(!created); | |
250 if (error != base::PLATFORM_FILE_OK) { | |
251 s_cached_platform_file_ = 0; | |
252 LOG(WARNING) << "CLD data file exists but cannot be opened"; | |
253 return; | |
254 } | |
bulach
2014/03/23 20:57:02
so finally, only here use the lock again to actual
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
255 } | |
256 #endif // defined(CLD2_DYNAMIC_MODE) | |
257 | |
143 void TranslateTabHelper::OnLanguageDetermined( | 258 void TranslateTabHelper::OnLanguageDetermined( |
144 const LanguageDetectionDetails& details, | 259 const LanguageDetectionDetails& details, |
145 bool page_needs_translation) { | 260 bool page_needs_translation) { |
146 translate_driver_.GetLanguageState().LanguageDetermined( | 261 translate_driver_.GetLanguageState().LanguageDetermined( |
147 details.adopted_language, page_needs_translation); | 262 details.adopted_language, page_needs_translation); |
148 | 263 |
149 content::NotificationService::current()->Notify( | 264 content::NotificationService::current()->Notify( |
150 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 265 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
151 content::Source<content::WebContents>(web_contents()), | 266 content::Source<content::WebContents>(web_contents()), |
152 content::Details<const LanguageDetectionDetails>(&details)); | 267 content::Details<const LanguageDetectionDetails>(&details)); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 if (GetLanguageState().InTranslateNavigation()) | 316 if (GetLanguageState().InTranslateNavigation()) |
202 return; | 317 return; |
203 } | 318 } |
204 | 319 |
205 TranslateBubbleFactory::Show( | 320 TranslateBubbleFactory::Show( |
206 browser->window(), web_contents(), step, error_type); | 321 browser->window(), web_contents(), step, error_type); |
207 #else | 322 #else |
208 NOTREACHED(); | 323 NOTREACHED(); |
209 #endif | 324 #endif |
210 } | 325 } |
OLD | NEW |