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

Side by Side Diff: chrome/browser/translate/translate_tab_helper.cc

Issue 187393005: Make it possible to read CLD data from a file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hack around lack of common IsTranslatableURL Created 6 years, 9 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 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/path_service.h"
8 #include "base/platform_file.h"
9 #include "base/synchronization/lock.h"
10 #include "base/task_runner.h"
7 #include "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/translate/translate_accept_languages_factory.h" 13 #include "chrome/browser/translate/translate_accept_languages_factory.h"
10 #include "chrome/browser/translate/translate_infobar_delegate.h" 14 #include "chrome/browser/translate/translate_infobar_delegate.h"
11 #include "chrome/browser/translate/translate_manager.h" 15 #include "chrome/browser/translate/translate_manager.h"
12 #include "chrome/browser/translate/translate_service.h" 16 #include "chrome/browser/translate/translate_service.h"
13 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h" 18 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_window.h" 19 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/browser/ui/translate/translate_bubble_factory.h" 21 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
22 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
19 #include "chrome/common/render_messages.h" 24 #include "chrome/common/render_messages.h"
20 #include "components/translate/core/browser/page_translated_details.h" 25 #include "components/translate/core/browser/page_translated_details.h"
21 #include "components/translate/core/browser/translate_accept_languages.h" 26 #include "components/translate/core/browser/translate_accept_languages.h"
22 #include "components/translate/core/browser/translate_prefs.h" 27 #include "components/translate/core/browser/translate_prefs.h"
23 #include "components/translate/core/common/language_detection_details.h" 28 #include "components/translate/core/common/language_detection_details.h"
29 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/notification_service.h" 30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
26 34
27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); 35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper);
28 36
37 #if defined(CLD2_DYNAMIC_MODE)
38 // Statics defined in the .h file:
39 base::PlatformFile TranslateTabHelper::s_cached_platform_file_ = 0;
bulach 2014/03/14 15:30:58 nit: NULL is probably cleaerer
Andrew Hayden (chromium.org) 2014/03/20 16:12:53 The compiler disagrees with you. I shall side with
40 base::Lock TranslateTabHelper::s_file_lock_;
41 #endif
42
29 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) 43 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents)
30 : content::WebContentsObserver(web_contents), 44 : content::WebContentsObserver(web_contents),
45 #if defined(CLD2_DYNAMIC_MODE)
46 weak_pointer_factory_(this),
47 #endif
31 translate_driver_(&web_contents->GetController()), 48 translate_driver_(&web_contents->GetController()),
32 translate_manager_(new TranslateManager(this)) {} 49 translate_manager_(new TranslateManager(this)) {}
33 50
34 TranslateTabHelper::~TranslateTabHelper() { 51 TranslateTabHelper::~TranslateTabHelper() {
35 } 52 }
36 53
37 LanguageState& TranslateTabHelper::GetLanguageState() { 54 LanguageState& TranslateTabHelper::GetLanguageState() {
38 return translate_driver_.language_state(); 55 return translate_driver_.language_state();
39 } 56 }
40 57
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 error_type, 122 error_type,
106 original_profile->GetPrefs()); 123 original_profile->GetPrefs());
107 } 124 }
108 125
109 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { 126 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) {
110 bool handled = true; 127 bool handled = true;
111 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) 128 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message)
112 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, 129 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined,
113 OnLanguageDetermined) 130 OnLanguageDetermined)
114 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) 131 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated)
132 #if defined(CLD2_DYNAMIC_MODE)
133 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested)
134 #endif
115 IPC_MESSAGE_UNHANDLED(handled = false) 135 IPC_MESSAGE_UNHANDLED(handled = false)
116 IPC_END_MESSAGE_MAP() 136 IPC_END_MESSAGE_MAP()
117 137
118 return handled; 138 return handled;
119 } 139 }
120 140
121 void TranslateTabHelper::DidNavigateAnyFrame( 141 void TranslateTabHelper::DidNavigateAnyFrame(
122 const content::LoadCommittedDetails& details, 142 const content::LoadCommittedDetails& details,
123 const content::FrameNavigateParams& params) { 143 const content::FrameNavigateParams& params) {
124 // Let the LanguageState clear its state. 144 // Let the LanguageState clear its state.
125 translate_driver_.DidNavigate(details); 145 translate_driver_.DidNavigate(details);
126 } 146 }
127 147
128 void TranslateTabHelper::WebContentsDestroyed( 148 void TranslateTabHelper::WebContentsDestroyed(
129 content::WebContents* web_contents) { 149 content::WebContents* web_contents) {
130 // Translation process can be interrupted. 150 // Translation process can be interrupted.
131 // Destroying the TranslateManager now guarantees that it never has to deal 151 // Destroying the TranslateManager now guarantees that it never has to deal
132 // with NULL WebContents. 152 // with NULL WebContents.
133 translate_manager_.reset(); 153 translate_manager_.reset();
134 } 154 }
135 155
156 #if defined(CLD2_DYNAMIC_MODE)
157 void TranslateTabHelper::OnCLDDataRequested() {
158 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
159 // Quickly try to read s_cached_platform_file_. If it's non-zero, we have
160 // cached the CLD data file and can answer immediately; else, we'll make
161 // a request to the blocking pool to look up the file and cache it.
162 // The value of s_cached_platform_file_ becomes non-zero is only set once,
163 // so when it becomes non-zero we are guaranteed it will not change again.
164 if (!s_file_lock_.Try())
bulach 2014/03/14 15:30:58 nit: use base::AutoLock, is a more common pattern.
Andrew Hayden (chromium.org) 2014/03/19 12:22:23 I explicitly chose Lock.try() instead. AutoLock wi
165 return; // We'll get another request later, whatevs!
166 const base::PlatformFile handle = s_cached_platform_file_;
167 s_file_lock_.Release();
168
169 if (handle == 0) {
bulach 2014/03/14 15:30:58 nit: NULL
Andrew Hayden (chromium.org) 2014/03/20 16:12:53 See previous comment, re: compiler.
170 // We don't have the data file yet. Queue an asynchronous caching attempt.
171 // The caching attempt happens in the blocking pool because it may involve
172 // arbitrary filesystem access.
173 content::BrowserThread::PostBlockingPoolTask(
174 FROM_HERE,
175 base::Bind(&TranslateTabHelper::HandleCLDDataRequest,
176 weak_pointer_factory_.GetWeakPtr()));
bulach 2014/03/14 15:30:58 hmm... WeakPtr can't be dereferenced across thread
bulach 2014/03/19 11:18:51 btw, I forgot to mention "Post(BlockingPool)TaskAn
Andrew Hayden (chromium.org) 2014/03/19 12:22:23 But I was under the impression that the weakptr wa
177 } else {
178 // We do have the data available. Respond to the request.
179 SendCLDDataAvailable(handle);
180 }
181 }
182
183 void TranslateTabHelper::SendCLDDataAvailable(const base::PlatformFile handle) {
184 // WARNING: This method can be invoked from either the IO thread or the
185 // blocking pool. Do not use the synchronous invocation style.
186
187 // We have the data available, respond to the request.
188 IPC::PlatformFileForTransit ipc_platform_file =
189 IPC::GetFileHandleForProcess(
190 handle,
191 GetWebContents()->GetRenderViewHost()->GetProcess()->GetHandle(),
192 false);
193 Send(new ChromeViewMsg_CLDDataAvailable(
194 GetWebContents()->GetRenderViewHost()->GetRoutingID(),
195 ipc_platform_file));
196 }
197
198 void TranslateTabHelper::HandleCLDDataRequest() {
199 // Because this function involves arbitrary file system access, it must run
200 // on the blocking pool.
201 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
202 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
203
204 base::AutoLock lock(s_file_lock_);
bulach 2014/03/14 15:30:58 locks are expensive, make them scoped: { base::A
Andrew Hayden (chromium.org) 2014/03/19 12:22:23 The only other code that depends upon this lock is
205 if (s_cached_platform_file_ != 0)
206 return; // already done, duplicate request
207
208 base::FilePath path;
209 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) {
210 LOG(WARNING) << "Unable to locate user data directory";
211 return; // Chrome isn't properly installed.
212 }
213
214 // If the file exists, we can send an IPC-safe construct back to the
215 // renderer process immediately.
216 path = path.Append(chrome::kCLDDataFilename);
217 if (!base::PathExists(path))
218 return;
219
220 // Attempt to open the file for reading. It exists, so we should succeed.
221 bool created = false;
222 base::PlatformFileError error;
223 s_cached_platform_file_ = base::CreatePlatformFile(
224 path,
225 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
226 &created, &error);
227 DCHECK(!created);
228 if (error != base::PLATFORM_FILE_OK) {
229 s_cached_platform_file_ = 0;
230 LOG(WARNING) << "CLD data file exists but cannot be opened";
231 return;
232 }
233
234 // If we get this far, we've just cached the file. Respond immediately.
235 lock.~AutoLock(); // Release the lock now rather than after responding
bulach 2014/03/14 15:30:58 as above, create smaller scopes instead..
Andrew Hayden (chromium.org) 2014/03/19 12:22:23 See reply above.
236 SendCLDDataAvailable(s_cached_platform_file_);
237 }
238 #endif // defined(CLD2_DYNAMIC_MODE)
239
136 void TranslateTabHelper::OnLanguageDetermined( 240 void TranslateTabHelper::OnLanguageDetermined(
137 const LanguageDetectionDetails& details, 241 const LanguageDetectionDetails& details,
138 bool page_needs_translation) { 242 bool page_needs_translation) {
139 translate_driver_.language_state().LanguageDetermined( 243 translate_driver_.language_state().LanguageDetermined(
140 details.adopted_language, page_needs_translation); 244 details.adopted_language, page_needs_translation);
141 245
142 content::NotificationService::current()->Notify( 246 content::NotificationService::current()->Notify(
143 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 247 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
144 content::Source<content::WebContents>(web_contents()), 248 content::Source<content::WebContents>(web_contents()),
145 content::Details<const LanguageDetectionDetails>(&details)); 249 content::Details<const LanguageDetectionDetails>(&details));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 if (GetLanguageState().InTranslateNavigation()) 298 if (GetLanguageState().InTranslateNavigation())
195 return; 299 return;
196 } 300 }
197 301
198 TranslateBubbleFactory::Show( 302 TranslateBubbleFactory::Show(
199 browser->window(), web_contents(), step, error_type); 303 browser->window(), web_contents(), step, error_type);
200 #else 304 #else
201 NOTREACHED(); 305 NOTREACHED();
202 #endif 306 #endif
203 } 307 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698