Chromium Code Reviews| 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/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 // Statics defined in the .h file: | |
| 38 base::PlatformFile TranslateTabHelper::s_cached_platform_file_ = 0; | |
| 39 base::Lock TranslateTabHelper::s_file_lock_; | |
| 40 | |
| 29 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) | 41 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) |
| 30 : content::WebContentsObserver(web_contents), | 42 : content::WebContentsObserver(web_contents), |
| 43 weak_pointer_factory_(this), | |
| 31 translate_driver_(&web_contents->GetController()), | 44 translate_driver_(&web_contents->GetController()), |
| 32 translate_manager_(new TranslateManager(this)) {} | 45 translate_manager_(new TranslateManager(this)) {} |
| 33 | 46 |
| 34 TranslateTabHelper::~TranslateTabHelper() { | 47 TranslateTabHelper::~TranslateTabHelper() { |
| 35 } | 48 } |
| 36 | 49 |
| 37 LanguageState& TranslateTabHelper::GetLanguageState() { | 50 LanguageState& TranslateTabHelper::GetLanguageState() { |
| 38 return translate_driver_.language_state(); | 51 return translate_driver_.language_state(); |
| 39 } | 52 } |
| 40 | 53 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 error_type, | 118 error_type, |
| 106 original_profile->GetPrefs()); | 119 original_profile->GetPrefs()); |
| 107 } | 120 } |
| 108 | 121 |
| 109 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { | 122 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 110 bool handled = true; | 123 bool handled = true; |
| 111 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) | 124 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) |
| 112 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, | 125 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, |
| 113 OnLanguageDetermined) | 126 OnLanguageDetermined) |
| 114 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) | 127 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) |
| 128 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested) | |
| 115 IPC_MESSAGE_UNHANDLED(handled = false) | 129 IPC_MESSAGE_UNHANDLED(handled = false) |
| 116 IPC_END_MESSAGE_MAP() | 130 IPC_END_MESSAGE_MAP() |
| 117 | 131 |
| 118 return handled; | 132 return handled; |
| 119 } | 133 } |
| 120 | 134 |
| 121 void TranslateTabHelper::DidNavigateAnyFrame( | 135 void TranslateTabHelper::DidNavigateAnyFrame( |
| 122 const content::LoadCommittedDetails& details, | 136 const content::LoadCommittedDetails& details, |
| 123 const content::FrameNavigateParams& params) { | 137 const content::FrameNavigateParams& params) { |
| 124 // Let the LanguageState clear its state. | 138 // Let the LanguageState clear its state. |
| 125 translate_driver_.DidNavigate(details); | 139 translate_driver_.DidNavigate(details); |
| 126 } | 140 } |
| 127 | 141 |
| 128 void TranslateTabHelper::WebContentsDestroyed( | 142 void TranslateTabHelper::WebContentsDestroyed( |
| 129 content::WebContents* web_contents) { | 143 content::WebContents* web_contents) { |
| 130 // Translation process can be interrupted. | 144 // Translation process can be interrupted. |
| 131 // Destroying the TranslateManager now guarantees that it never has to deal | 145 // Destroying the TranslateManager now guarantees that it never has to deal |
| 132 // with NULL WebContents. | 146 // with NULL WebContents. |
| 133 translate_manager_.reset(); | 147 translate_manager_.reset(); |
| 134 } | 148 } |
| 135 | 149 |
| 150 void TranslateTabHelper::OnCLDDataRequested() { | |
| 151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 152 // Quickly try to read s_cached_platform_file_. If it's non-zero, we have | |
| 153 // cached the CLD data file and can answer immediately; else, we'll make | |
| 154 // a request to the blocking pool to look up the file and cache it. | |
| 155 // The value of s_cached_platform_file_ becomes non-zero is only set once, | |
| 156 // so when it becomes non-zero we are guaranteed it will not change again. | |
| 157 if (!s_file_lock_.Try()) return; // We'll get another request later, whatevs! | |
|
palmer
2014/03/13 17:59:50
Nit: Style for C++ is to put "return;" on its own
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 158 const base::PlatformFile handle = s_cached_platform_file_; | |
| 159 s_file_lock_.Release(); | |
| 160 | |
| 161 if (handle == 0) { | |
| 162 // We don't have the data file yet. Queue an asynchronous caching attempt. | |
| 163 // The caching attempt happens in the blocking pool because it may involve | |
| 164 // arbitrary filesystem access. | |
| 165 content::BrowserThread::PostBlockingPoolTask( | |
| 166 FROM_HERE, | |
| 167 base::Bind(&TranslateTabHelper::HandleCLDDataRequest, | |
| 168 weak_pointer_factory_.GetWeakPtr())); | |
| 169 } else { | |
| 170 // We do have the data available. Respond to the request. | |
| 171 SendCLDDataAvailable(handle); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void TranslateTabHelper::SendCLDDataAvailable(const base::PlatformFile handle) { | |
| 176 // WARNING: This method can be invoked from either the IO thread or the | |
| 177 // blocking pool. Do not use the synchronous invocation style. | |
| 178 | |
| 179 // We have the data available, respond to the request. | |
| 180 IPC::PlatformFileForTransit ipc_platform_file = | |
| 181 IPC::GetFileHandleForProcess( | |
| 182 handle, | |
| 183 GetWebContents()->GetRenderViewHost()->GetProcess()->GetHandle(), | |
| 184 false); | |
| 185 Send(new ChromeViewMsg_CLDDataAvailable( | |
| 186 GetWebContents()->GetRenderViewHost()->GetRoutingID(), | |
| 187 ipc_platform_file)); | |
| 188 } | |
| 189 | |
| 190 void TranslateTabHelper::HandleCLDDataRequest() { | |
| 191 // Because this function involves arbitrary file system access, it must run | |
| 192 // on the blocking pool. | |
| 193 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 194 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 195 | |
| 196 base::AutoLock lock(s_file_lock_); // will release when we return | |
|
palmer
2014/03/13 17:59:50
This comment is superfluous.
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 197 if (s_cached_platform_file_ != 0) return; // already done, duplicate request | |
|
palmer
2014/03/13 17:59:50
Style: "return;" on its own line.
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 198 | |
| 199 base::FilePath path; | |
| 200 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) | |
| 201 return; // Chrome isn't properly installed. | |
|
palmer
2014/03/13 17:59:50
DCHECK? CHECK or Warn the user in release builds?
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done. I took the DCHECK out because of other comme
| |
| 202 | |
| 203 // If the file exists, we can send an IPC-safe construct back to the | |
| 204 // renderer process immediately. If not (e.g., the file is being downloaded | |
| 205 // right now), then we will do nothing. It is up to the renderer process | |
|
palmer
2014/03/13 17:59:50
Rather than repeat the comments to this effect, I
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 206 // to call us again later. | |
| 207 path = path.Append(chrome::kCLDDataFilename); | |
| 208 if (!base::PathExists(path)) | |
| 209 return; // File doesn't exist | |
|
palmer
2014/03/13 17:59:50
This comment is superfluous.
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 210 | |
| 211 // Attempt to open the file for reading. It exists, so we should succeed. | |
| 212 bool created = false; | |
| 213 base::PlatformFileError error; | |
| 214 s_cached_platform_file_ = base::CreatePlatformFile( | |
| 215 path, | |
| 216 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
| 217 &created, &error); | |
| 218 DCHECK(!created); | |
| 219 if (error != base::PLATFORM_FILE_OK) { | |
| 220 s_cached_platform_file_ = 0; | |
| 221 return; // File exists, but we can't open it. | |
|
palmer
2014/03/13 17:59:50
This comment is superfluous.
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 222 } | |
| 223 | |
| 224 // If we get this far, we've just cached the file. Respond immediately. | |
| 225 lock.~AutoLock(); | |
|
palmer
2014/03/13 17:59:50
Not a problem, I don't think, but it "feels" a lit
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
I wanted to release the lock as quickly as possibl
| |
| 226 SendCLDDataAvailable(s_cached_platform_file_); | |
| 227 } | |
| 228 | |
| 136 void TranslateTabHelper::OnLanguageDetermined( | 229 void TranslateTabHelper::OnLanguageDetermined( |
| 137 const LanguageDetectionDetails& details, | 230 const LanguageDetectionDetails& details, |
| 138 bool page_needs_translation) { | 231 bool page_needs_translation) { |
| 139 translate_driver_.language_state().LanguageDetermined( | 232 translate_driver_.language_state().LanguageDetermined( |
| 140 details.adopted_language, page_needs_translation); | 233 details.adopted_language, page_needs_translation); |
| 141 | 234 |
| 142 content::NotificationService::current()->Notify( | 235 content::NotificationService::current()->Notify( |
| 143 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 236 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 144 content::Source<content::WebContents>(web_contents()), | 237 content::Source<content::WebContents>(web_contents()), |
| 145 content::Details<const LanguageDetectionDetails>(&details)); | 238 content::Details<const LanguageDetectionDetails>(&details)); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 if (GetLanguageState().InTranslateNavigation()) | 287 if (GetLanguageState().InTranslateNavigation()) |
| 195 return; | 288 return; |
| 196 } | 289 } |
| 197 | 290 |
| 198 TranslateBubbleFactory::Show( | 291 TranslateBubbleFactory::Show( |
| 199 browser->window(), web_contents(), step, error_type); | 292 browser->window(), web_contents(), step, error_type); |
| 200 #else | 293 #else |
| 201 NOTREACHED(); | 294 NOTREACHED(); |
| 202 #endif | 295 #endif |
| 203 } | 296 } |
| OLD | NEW |