Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chrome_translate_client.h" | 5 #include "chrome/browser/translate/chrome_translate_client.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/infobars/infobar_service.h" | 15 #include "chrome/browser/infobars/infobar_service.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/translate/language_model_factory.h" | 17 #include "chrome/browser/translate/language_model_factory.h" |
| 18 #include "chrome/browser/translate/translate_accept_languages_factory.h" | 18 #include "chrome/browser/translate/translate_accept_languages_factory.h" |
| 19 #include "chrome/browser/translate/translate_service.h" | 19 #include "chrome/browser/translate/translate_service.h" |
| 20 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_finder.h" | 21 #include "chrome/browser/ui/browser_finder.h" |
| 22 #include "chrome/browser/ui/browser_tabstrip.h" | 22 #include "chrome/browser/ui/browser_tabstrip.h" |
| 23 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
| 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 25 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | 25 #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
| 26 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
| 27 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/grit/theme_resources.h" | 28 #include "chrome/grit/theme_resources.h" |
| 29 #include "components/metrics/proto/translate_event.pb.h" | |
| 29 #include "components/prefs/pref_service.h" | 30 #include "components/prefs/pref_service.h" |
| 30 #include "components/translate/core/browser/language_model.h" | 31 #include "components/translate/core/browser/language_model.h" |
| 31 #include "components/translate/core/browser/language_state.h" | 32 #include "components/translate/core/browser/language_state.h" |
| 32 #include "components/translate/core/browser/page_translated_details.h" | 33 #include "components/translate/core/browser/page_translated_details.h" |
| 33 #include "components/translate/core/browser/translate_accept_languages.h" | 34 #include "components/translate/core/browser/translate_accept_languages.h" |
| 34 #include "components/translate/core/browser/translate_download_manager.h" | 35 #include "components/translate/core/browser/translate_download_manager.h" |
| 35 #include "components/translate/core/browser/translate_infobar_delegate.h" | 36 #include "components/translate/core/browser/translate_infobar_delegate.h" |
| 36 #include "components/translate/core/browser/translate_manager.h" | 37 #include "components/translate/core/browser/translate_manager.h" |
| 37 #include "components/translate/core/browser/translate_prefs.h" | 38 #include "components/translate/core/browser/translate_prefs.h" |
| 38 #include "components/translate/core/common/language_detection_details.h" | 39 #include "components/translate/core/common/language_detection_details.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 return translate_manager_.get(); | 168 return translate_manager_.get(); |
| 168 } | 169 } |
| 169 | 170 |
| 170 void ChromeTranslateClient::ShowTranslateUI( | 171 void ChromeTranslateClient::ShowTranslateUI( |
| 171 translate::TranslateStep step, | 172 translate::TranslateStep step, |
| 172 const std::string& source_language, | 173 const std::string& source_language, |
| 173 const std::string& target_language, | 174 const std::string& target_language, |
| 174 translate::TranslateErrors::Type error_type, | 175 translate::TranslateErrors::Type error_type, |
| 175 bool triggered_from_menu) { | 176 bool triggered_from_menu) { |
| 176 DCHECK(web_contents()); | 177 DCHECK(web_contents()); |
| 178 DCHECK(translate_manager_); | |
| 179 | |
| 177 if (error_type != translate::TranslateErrors::NONE) | 180 if (error_type != translate::TranslateErrors::NONE) |
| 178 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; | 181 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; |
| 179 | 182 |
| 180 #if !defined(USE_AURA) | 183 #if !defined(USE_AURA) |
| 181 if (!TranslateService::IsTranslateBubbleEnabled()) { | 184 if (!TranslateService::IsTranslateBubbleEnabled()) { |
| 182 // Infobar UI. | 185 // Infobar UI. |
| 183 translate::TranslateInfoBarDelegate::Create( | 186 translate::TranslateInfoBarDelegate::Create( |
| 184 step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE, | 187 step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE, |
| 185 translate_manager_->GetWeakPtr(), | 188 translate_manager_->GetWeakPtr(), |
| 186 InfoBarService::FromWebContents(web_contents()), | 189 InfoBarService::FromWebContents(web_contents()), |
| 187 web_contents()->GetBrowserContext()->IsOffTheRecord(), step, | 190 web_contents()->GetBrowserContext()->IsOffTheRecord(), step, |
| 188 source_language, target_language, error_type, triggered_from_menu); | 191 source_language, target_language, error_type, triggered_from_menu); |
| 189 return; | 192 return; |
| 190 } | 193 } |
| 191 #endif | 194 #endif |
| 192 | 195 |
| 193 // Bubble UI. | 196 // Bubble UI. |
| 194 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { | 197 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { |
| 195 // TODO(droger): Move this logic out of UI code. | 198 // TODO(droger): Move this logic out of UI code. |
| 196 GetLanguageState().SetTranslateEnabled(true); | 199 GetLanguageState().SetTranslateEnabled(true); |
| 197 // In the new UI, continue offering translation after the user navigates to | 200 // In the new UI, continue offering translation after the user navigates to |
| 198 // another page. | 201 // another page. |
| 199 if (!base::FeatureList::IsEnabled(translate::kTranslateUI2016Q2) && | 202 if (!base::FeatureList::IsEnabled(translate::kTranslateUI2016Q2) && |
| 200 !GetLanguageState().HasLanguageChanged()) { | 203 !GetLanguageState().HasLanguageChanged()) { |
| 204 translate_manager_->RecordTranslateEvent( | |
| 205 metrics::TranslateEventProto::MATCHES_PREVIOUS_LANGUAGE); | |
| 201 return; | 206 return; |
| 202 } | 207 } |
| 203 | 208 |
| 204 if (!triggered_from_menu && | 209 if (!triggered_from_menu && |
| 205 GetTranslatePrefs()->IsTooOftenDenied(source_language)) | 210 GetTranslatePrefs()->IsTooOftenDenied(source_language)) { |
| 211 translate_manager_->RecordTranslateEvent( | |
| 212 metrics::TranslateEventProto::LANGUAGE_DISABLED_BY_AUTO_BLACKLIST); | |
| 206 return; | 213 return; |
| 214 } | |
| 207 } | 215 } |
| 208 ShowBubble(step, error_type); | 216 ShowTranslateBubbleResult result = ShowBubble(step, error_type); |
| 217 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { | |
| 218 switch (result) { | |
| 219 case ShowTranslateBubbleResult::SUCCESS: | |
| 220 break; | |
| 221 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID: | |
|
groby-ooo-7-16
2016/11/15 01:15:43
I am deeply saddened we have two enums we need to
Roger McFarlane (Chromium)
2016/11/16 05:51:31
I pulled the mapping out to a helper function in t
| |
| 222 translate_manager_->RecordTranslateEvent( | |
| 223 metrics::TranslateEventProto::BROWSER_WINDOW_IS_INVALID); | |
| 224 break; | |
| 225 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED: | |
| 226 translate_manager_->RecordTranslateEvent( | |
| 227 metrics::TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED); | |
| 228 break; | |
| 229 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE: | |
| 230 translate_manager_->RecordTranslateEvent( | |
| 231 metrics::TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE); | |
| 232 break; | |
| 233 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE: | |
| 234 translate_manager_->RecordTranslateEvent( | |
| 235 metrics::TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE); | |
| 236 break; | |
| 237 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE: | |
| 238 translate_manager_->RecordTranslateEvent( | |
| 239 metrics::TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE); | |
| 240 break; | |
| 241 } | |
| 242 } | |
| 209 } | 243 } |
| 210 | 244 |
| 211 translate::TranslateDriver* ChromeTranslateClient::GetTranslateDriver() { | 245 translate::TranslateDriver* ChromeTranslateClient::GetTranslateDriver() { |
| 212 return &translate_driver_; | 246 return &translate_driver_; |
| 213 } | 247 } |
| 214 | 248 |
| 215 PrefService* ChromeTranslateClient::GetPrefs() { | 249 PrefService* ChromeTranslateClient::GetPrefs() { |
| 216 DCHECK(web_contents()); | 250 DCHECK(web_contents()); |
| 217 Profile* profile = | 251 Profile* profile = |
| 218 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 252 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 translate::PageTranslatedDetails details; | 332 translate::PageTranslatedDetails details; |
| 299 details.source_language = original_lang; | 333 details.source_language = original_lang; |
| 300 details.target_language = translated_lang; | 334 details.target_language = translated_lang; |
| 301 details.error_type = error_type; | 335 details.error_type = error_type; |
| 302 content::NotificationService::current()->Notify( | 336 content::NotificationService::current()->Notify( |
| 303 chrome::NOTIFICATION_PAGE_TRANSLATED, | 337 chrome::NOTIFICATION_PAGE_TRANSLATED, |
| 304 content::Source<content::WebContents>(web_contents()), | 338 content::Source<content::WebContents>(web_contents()), |
| 305 content::Details<translate::PageTranslatedDetails>(&details)); | 339 content::Details<translate::PageTranslatedDetails>(&details)); |
| 306 } | 340 } |
| 307 | 341 |
| 308 void ChromeTranslateClient::ShowBubble( | 342 ShowTranslateBubbleResult ChromeTranslateClient::ShowBubble( |
| 309 translate::TranslateStep step, | 343 translate::TranslateStep step, |
| 310 translate::TranslateErrors::Type error_type) { | 344 translate::TranslateErrors::Type error_type) { |
| 345 DCHECK(translate_manager_); | |
| 311 // The bubble is implemented only on the desktop platforms. | 346 // The bubble is implemented only on the desktop platforms. |
| 312 #if !defined(OS_ANDROID) | 347 #if !defined(OS_ANDROID) |
| 313 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 348 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
| 314 | 349 |
| 315 // |browser| might be NULL when testing. In this case, Show(...) should be | 350 // |browser| might be NULL when testing. In this case, Show(...) should be |
| 316 // called because the implementation for testing is used. | 351 // called because the implementation for testing is used. |
| 317 if (!browser) { | 352 if (!browser) { |
| 318 TranslateBubbleFactory::Show(NULL, web_contents(), step, error_type); | 353 return TranslateBubbleFactory::Show(NULL, web_contents(), step, error_type); |
| 319 return; | |
| 320 } | 354 } |
| 321 | 355 |
| 322 if (web_contents() != browser->tab_strip_model()->GetActiveWebContents()) | 356 if (web_contents() != browser->tab_strip_model()->GetActiveWebContents()) |
| 323 return; | 357 return ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE; |
| 324 | 358 |
| 325 // This ShowBubble function is also used for upating the existing bubble. | 359 // This ShowBubble function is also used for updating the existing bubble. |
| 326 // However, with the bubble shown, any browser windows are NOT activated | 360 // However, with the bubble shown, any browser windows are NOT activated |
| 327 // because the bubble takes the focus from the other widgets including the | 361 // because the bubble takes the focus from the other widgets including the |
| 328 // browser windows. So it is checked that |browser| is the last activated | 362 // browser windows. So it is checked that |browser| is the last activated |
| 329 // browser, not is now activated. | 363 // browser, not is now activated. |
| 330 if (browser != chrome::FindLastActive()) | 364 if (browser != chrome::FindLastActive()) |
| 331 return; | 365 return ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE; |
| 332 | 366 |
| 333 // During auto-translating, the bubble should not be shown. | 367 // During auto-translating, the bubble should not be shown. |
| 334 if (step == translate::TRANSLATE_STEP_TRANSLATING || | 368 if (step == translate::TRANSLATE_STEP_TRANSLATING || |
| 335 step == translate::TRANSLATE_STEP_AFTER_TRANSLATE) { | 369 step == translate::TRANSLATE_STEP_AFTER_TRANSLATE) { |
| 336 if (GetLanguageState().InTranslateNavigation()) | 370 if (GetLanguageState().InTranslateNavigation()) |
| 337 return; | 371 return ShowTranslateBubbleResult::SUCCESS; |
| 338 } | 372 } |
| 339 | 373 |
| 340 TranslateBubbleFactory::Show(browser->window(), web_contents(), step, | 374 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, |
| 341 error_type); | 375 error_type); |
| 342 #else | 376 #else |
| 343 NOTREACHED(); | 377 NOTREACHED(); |
| 378 return ShowTranslateBubbleResult::SUCCESS; | |
| 344 #endif | 379 #endif |
| 345 } | 380 } |
| OLD | NEW |