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" | |
| 7 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/translate/translate_accept_languages_factory.h" | 11 #include "chrome/browser/translate/translate_accept_languages_factory.h" |
| 10 #include "chrome/browser/translate/translate_infobar_delegate.h" | 12 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 11 #include "chrome/browser/translate/translate_manager.h" | 13 #include "chrome/browser/translate/translate_manager.h" |
| 12 #include "chrome/browser/translate/translate_service.h" | 14 #include "chrome/browser/translate/translate_service.h" |
| 13 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_finder.h" | 16 #include "chrome/browser/ui/browser_finder.h" |
| 15 #include "chrome/browser/ui/browser_window.h" | 17 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | 19 #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
| 20 #include "chrome/common/chrome_paths.h" | |
| 18 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/common/render_messages.h" | 22 #include "chrome/common/render_messages.h" |
| 20 #include "components/translate/core/browser/page_translated_details.h" | 23 #include "components/translate/core/browser/page_translated_details.h" |
| 21 #include "components/translate/core/browser/translate_accept_languages.h" | 24 #include "components/translate/core/browser/translate_accept_languages.h" |
| 22 #include "components/translate/core/browser/translate_prefs.h" | 25 #include "components/translate/core/browser/translate_prefs.h" |
| 23 #include "components/translate/core/common/language_detection_details.h" | 26 #include "components/translate/core/common/language_detection_details.h" |
| 24 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/render_process_host.h" | |
| 29 #include "content/public/browser/render_view_host.h" | |
| 25 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 26 | 31 |
| 27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); | 32 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); |
| 28 | 33 |
| 29 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) | 34 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) |
| 30 : content::WebContentsObserver(web_contents), | 35 : content::WebContentsObserver(web_contents), |
| 31 translate_driver_(&web_contents->GetController()), | 36 translate_driver_(&web_contents->GetController()), |
| 32 translate_manager_(new TranslateManager(this)) {} | 37 translate_manager_(new TranslateManager(this)) {} |
| 33 | 38 |
| 34 TranslateTabHelper::~TranslateTabHelper() { | 39 TranslateTabHelper::~TranslateTabHelper() { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 error_type, | 110 error_type, |
| 106 original_profile->GetPrefs()); | 111 original_profile->GetPrefs()); |
| 107 } | 112 } |
| 108 | 113 |
| 109 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { | 114 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 110 bool handled = true; | 115 bool handled = true; |
| 111 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) | 116 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) |
| 112 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, | 117 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, |
| 113 OnLanguageDetermined) | 118 OnLanguageDetermined) |
| 114 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) | 119 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) |
| 120 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested) | |
| 115 IPC_MESSAGE_UNHANDLED(handled = false) | 121 IPC_MESSAGE_UNHANDLED(handled = false) |
| 116 IPC_END_MESSAGE_MAP() | 122 IPC_END_MESSAGE_MAP() |
| 117 | 123 |
| 118 return handled; | 124 return handled; |
| 119 } | 125 } |
| 120 | 126 |
| 121 void TranslateTabHelper::DidNavigateAnyFrame( | 127 void TranslateTabHelper::DidNavigateAnyFrame( |
| 122 const content::LoadCommittedDetails& details, | 128 const content::LoadCommittedDetails& details, |
| 123 const content::FrameNavigateParams& params) { | 129 const content::FrameNavigateParams& params) { |
| 124 // Let the LanguageState clear its state. | 130 // Let the LanguageState clear its state. |
| 125 translate_driver_.DidNavigate(details); | 131 translate_driver_.DidNavigate(details); |
| 126 } | 132 } |
| 127 | 133 |
| 128 void TranslateTabHelper::WebContentsDestroyed( | 134 void TranslateTabHelper::WebContentsDestroyed( |
| 129 content::WebContents* web_contents) { | 135 content::WebContents* web_contents) { |
| 130 // Translation process can be interrupted. | 136 // Translation process can be interrupted. |
| 131 // Destroying the TranslateManager now guarantees that it never has to deal | 137 // Destroying the TranslateManager now guarantees that it never has to deal |
| 132 // with NULL WebContents. | 138 // with NULL WebContents. |
| 133 translate_manager_.reset(); | 139 translate_manager_.reset(); |
| 134 } | 140 } |
| 135 | 141 |
| 142 // TODO(andrewhayden): It's annoying that during download of the data file, | |
| 143 // multiple renderers may be calling this method repeatedly, but there's not | |
| 144 // an obviously-better choice from a complexity standpoint: we have no idea | |
| 145 // if or when the file will become available, and the check is cheap. | |
| 146 void TranslateTabHelper::OnCLDDataRequested() { | |
| 147 static base::PlatformFile cached_platform_file = 0; | |
| 148 const int routing_id = GetWebContents()->GetRenderViewHost()->GetRoutingID(); | |
| 149 | |
| 150 // If we have cached the platform file, we reuse it. Else, we cache it now. | |
| 151 if (cached_platform_file == 0) { | |
| 152 // First, grab a reference to the user data directory | |
| 153 base::FilePath path; | |
| 154 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { | |
| 155 NOTREACHED(); | |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
i don't think this if () { NOTREACHED(); } adds mu
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
Done.
| |
| 156 } | |
| 157 | |
| 158 // Now derive the name of the CLD data file that we are looking for. | |
| 159 path = path.Append(chrome::kCLDDataFilename); | |
| 160 | |
| 161 // If the file exists, we can send an IPC-safe construct back to the | |
| 162 // renderer process immediately. If not (e.g., the file is being downloaded | |
| 163 // right now), then we will do nothing. It is up to the renderer process | |
| 164 // to call us again later. | |
| 165 if (!base::PathExists(path)) { | |
| 166 return; | |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
no { }
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
Done.
| |
| 167 } | |
| 168 | |
| 169 // Attempt to open the file for reading. | |
| 170 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | |
| 171 bool created = false; | |
| 172 base::PlatformFileError error; | |
| 173 cached_platform_file = base::CreatePlatformFile( | |
| 174 path, flags, &created, &error); | |
| 175 DCHECK(created == false); | |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
!created
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
Done.
| |
| 176 if (error != base::PLATFORM_FILE_OK) { | |
| 177 cached_platform_file = 0; | |
| 178 DCHECK(false) << "Failed to open CLD data file, error code=" << error; | |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
is it really necessary to crash here?
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
No, it isn't. I just put a comment in the code ins
| |
| 179 return; | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 // Get a handle to the renderer process for IPC. | |
| 184 base::ProcessHandle rendererProcessHandle = | |
| 185 GetWebContents()->GetRenderViewHost()->GetProcess()->GetHandle(); | |
| 186 // Prepare a file handle for transit to that renderer process. | |
| 187 IPC::PlatformFileForTransit ipcPlatformFile = IPC::GetFileHandleForProcess( | |
| 188 cached_platform_file, | |
| 189 rendererProcessHandle, | |
| 190 false); | |
| 191 // Send the file handle to the renderer via IPC. We're done! | |
| 192 Send(new ChromeViewMsg_CLDDataAvailable(routing_id, ipcPlatformFile)); | |
| 193 } | |
| 194 | |
| 136 void TranslateTabHelper::OnLanguageDetermined( | 195 void TranslateTabHelper::OnLanguageDetermined( |
| 137 const LanguageDetectionDetails& details, | 196 const LanguageDetectionDetails& details, |
| 138 bool page_needs_translation) { | 197 bool page_needs_translation) { |
| 139 translate_driver_.language_state().LanguageDetermined( | 198 translate_driver_.language_state().LanguageDetermined( |
| 140 details.adopted_language, page_needs_translation); | 199 details.adopted_language, page_needs_translation); |
| 141 | 200 |
| 142 content::NotificationService::current()->Notify( | 201 content::NotificationService::current()->Notify( |
| 143 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 202 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 144 content::Source<content::WebContents>(web_contents()), | 203 content::Source<content::WebContents>(web_contents()), |
| 145 content::Details<const LanguageDetectionDetails>(&details)); | 204 content::Details<const LanguageDetectionDetails>(&details)); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 if (GetLanguageState().InTranslateNavigation()) | 253 if (GetLanguageState().InTranslateNavigation()) |
| 195 return; | 254 return; |
| 196 } | 255 } |
| 197 | 256 |
| 198 TranslateBubbleFactory::Show( | 257 TranslateBubbleFactory::Show( |
| 199 browser->window(), web_contents(), step, error_type); | 258 browser->window(), web_contents(), step, error_type); |
| 200 #else | 259 #else |
| 201 NOTREACHED(); | 260 NOTREACHED(); |
| 202 #endif | 261 #endif |
| 203 } | 262 } |
| OLD | NEW |