| 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" |
| 39 #include "components/variations/service/variations_service.h" | 40 #include "components/variations/service/variations_service.h" |
| 40 #include "content/public/browser/notification_service.h" | 41 #include "content/public/browser/notification_service.h" |
| 41 #include "content/public/browser/render_view_host.h" | 42 #include "content/public/browser/render_view_host.h" |
| 42 #include "content/public/browser/web_contents.h" | 43 #include "content/public/browser/web_contents.h" |
| 43 #include "url/gurl.h" | 44 #include "url/gurl.h" |
| 44 | 45 |
| 46 namespace { |
| 47 |
| 48 metrics::TranslateEventProto::EventType BubbleResultToTranslateEvent( |
| 49 ShowTranslateBubbleResult result) { |
| 50 switch (result) { |
| 51 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID: |
| 52 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_INVALID; |
| 53 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED: |
| 54 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED; |
| 55 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE: |
| 56 return metrics::TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE; |
| 57 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE: |
| 58 return metrics::TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE; |
| 59 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE: |
| 60 return metrics::TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE; |
| 61 default: |
| 62 NOTREACHED(); |
| 63 return metrics::TranslateEventProto::UNKNOWN; |
| 64 } |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 45 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); | 69 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); |
| 46 | 70 |
| 47 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) | 71 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) |
| 48 : content::WebContentsObserver(web_contents), | 72 : content::WebContentsObserver(web_contents), |
| 49 translate_driver_(&web_contents->GetController()), | 73 translate_driver_(&web_contents->GetController()), |
| 50 translate_manager_( | 74 translate_manager_( |
| 51 new translate::TranslateManager(this, prefs::kAcceptLanguages)), | 75 new translate::TranslateManager(this, prefs::kAcceptLanguages)), |
| 52 language_model_( | 76 language_model_( |
| 53 LanguageModelFactory::GetInstance()->GetForBrowserContext( | 77 LanguageModelFactory::GetInstance()->GetForBrowserContext( |
| 54 web_contents->GetBrowserContext())) { | 78 web_contents->GetBrowserContext())) { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 return translate_manager_.get(); | 191 return translate_manager_.get(); |
| 168 } | 192 } |
| 169 | 193 |
| 170 void ChromeTranslateClient::ShowTranslateUI( | 194 void ChromeTranslateClient::ShowTranslateUI( |
| 171 translate::TranslateStep step, | 195 translate::TranslateStep step, |
| 172 const std::string& source_language, | 196 const std::string& source_language, |
| 173 const std::string& target_language, | 197 const std::string& target_language, |
| 174 translate::TranslateErrors::Type error_type, | 198 translate::TranslateErrors::Type error_type, |
| 175 bool triggered_from_menu) { | 199 bool triggered_from_menu) { |
| 176 DCHECK(web_contents()); | 200 DCHECK(web_contents()); |
| 201 DCHECK(translate_manager_); |
| 202 |
| 177 if (error_type != translate::TranslateErrors::NONE) | 203 if (error_type != translate::TranslateErrors::NONE) |
| 178 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; | 204 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; |
| 179 | 205 |
| 180 #if !defined(USE_AURA) | 206 #if !defined(USE_AURA) |
| 181 if (!TranslateService::IsTranslateBubbleEnabled()) { | 207 if (!TranslateService::IsTranslateBubbleEnabled()) { |
| 182 // Infobar UI. | 208 // Infobar UI. |
| 183 translate::TranslateInfoBarDelegate::Create( | 209 translate::TranslateInfoBarDelegate::Create( |
| 184 step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE, | 210 step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE, |
| 185 translate_manager_->GetWeakPtr(), | 211 translate_manager_->GetWeakPtr(), |
| 186 InfoBarService::FromWebContents(web_contents()), | 212 InfoBarService::FromWebContents(web_contents()), |
| 187 web_contents()->GetBrowserContext()->IsOffTheRecord(), step, | 213 web_contents()->GetBrowserContext()->IsOffTheRecord(), step, |
| 188 source_language, target_language, error_type, triggered_from_menu); | 214 source_language, target_language, error_type, triggered_from_menu); |
| 189 return; | 215 return; |
| 190 } | 216 } |
| 191 #endif | 217 #endif |
| 192 | 218 |
| 193 // Bubble UI. | 219 // Bubble UI. |
| 194 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { | 220 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { |
| 195 // TODO(droger): Move this logic out of UI code. | 221 // TODO(droger): Move this logic out of UI code. |
| 196 GetLanguageState().SetTranslateEnabled(true); | 222 GetLanguageState().SetTranslateEnabled(true); |
| 197 // In the new UI, continue offering translation after the user navigates to | 223 // In the new UI, continue offering translation after the user navigates to |
| 198 // another page. | 224 // another page. |
| 199 if (!base::FeatureList::IsEnabled(translate::kTranslateUI2016Q2) && | 225 if (!base::FeatureList::IsEnabled(translate::kTranslateUI2016Q2) && |
| 200 !GetLanguageState().HasLanguageChanged()) { | 226 !GetLanguageState().HasLanguageChanged()) { |
| 227 translate_manager_->RecordTranslateEvent( |
| 228 metrics::TranslateEventProto::MATCHES_PREVIOUS_LANGUAGE); |
| 201 return; | 229 return; |
| 202 } | 230 } |
| 203 | 231 |
| 204 if (!triggered_from_menu && | 232 if (!triggered_from_menu && |
| 205 GetTranslatePrefs()->IsTooOftenDenied(source_language)) | 233 GetTranslatePrefs()->IsTooOftenDenied(source_language)) { |
| 234 translate_manager_->RecordTranslateEvent( |
| 235 metrics::TranslateEventProto::LANGUAGE_DISABLED_BY_AUTO_BLACKLIST); |
| 206 return; | 236 return; |
| 237 } |
| 207 } | 238 } |
| 208 ShowBubble(step, error_type); | 239 ShowTranslateBubbleResult result = ShowBubble(step, error_type); |
| 240 if (result != ShowTranslateBubbleResult::SUCCESS && |
| 241 step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { |
| 242 translate_manager_->RecordTranslateEvent( |
| 243 BubbleResultToTranslateEvent(result)); |
| 244 } |
| 209 } | 245 } |
| 210 | 246 |
| 211 translate::TranslateDriver* ChromeTranslateClient::GetTranslateDriver() { | 247 translate::TranslateDriver* ChromeTranslateClient::GetTranslateDriver() { |
| 212 return &translate_driver_; | 248 return &translate_driver_; |
| 213 } | 249 } |
| 214 | 250 |
| 215 PrefService* ChromeTranslateClient::GetPrefs() { | 251 PrefService* ChromeTranslateClient::GetPrefs() { |
| 216 DCHECK(web_contents()); | 252 DCHECK(web_contents()); |
| 217 Profile* profile = | 253 Profile* profile = |
| 218 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 254 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 translate::PageTranslatedDetails details; | 334 translate::PageTranslatedDetails details; |
| 299 details.source_language = original_lang; | 335 details.source_language = original_lang; |
| 300 details.target_language = translated_lang; | 336 details.target_language = translated_lang; |
| 301 details.error_type = error_type; | 337 details.error_type = error_type; |
| 302 content::NotificationService::current()->Notify( | 338 content::NotificationService::current()->Notify( |
| 303 chrome::NOTIFICATION_PAGE_TRANSLATED, | 339 chrome::NOTIFICATION_PAGE_TRANSLATED, |
| 304 content::Source<content::WebContents>(web_contents()), | 340 content::Source<content::WebContents>(web_contents()), |
| 305 content::Details<translate::PageTranslatedDetails>(&details)); | 341 content::Details<translate::PageTranslatedDetails>(&details)); |
| 306 } | 342 } |
| 307 | 343 |
| 308 void ChromeTranslateClient::ShowBubble( | 344 ShowTranslateBubbleResult ChromeTranslateClient::ShowBubble( |
| 309 translate::TranslateStep step, | 345 translate::TranslateStep step, |
| 310 translate::TranslateErrors::Type error_type) { | 346 translate::TranslateErrors::Type error_type) { |
| 347 DCHECK(translate_manager_); |
| 311 // The bubble is implemented only on the desktop platforms. | 348 // The bubble is implemented only on the desktop platforms. |
| 312 #if !defined(OS_ANDROID) | 349 #if !defined(OS_ANDROID) |
| 313 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 350 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
| 314 | 351 |
| 315 // |browser| might be NULL when testing. In this case, Show(...) should be | 352 // |browser| might be NULL when testing. In this case, Show(...) should be |
| 316 // called because the implementation for testing is used. | 353 // called because the implementation for testing is used. |
| 317 if (!browser) { | 354 if (!browser) { |
| 318 TranslateBubbleFactory::Show(NULL, web_contents(), step, error_type); | 355 return TranslateBubbleFactory::Show(NULL, web_contents(), step, error_type); |
| 319 return; | |
| 320 } | 356 } |
| 321 | 357 |
| 322 if (web_contents() != browser->tab_strip_model()->GetActiveWebContents()) | 358 if (web_contents() != browser->tab_strip_model()->GetActiveWebContents()) |
| 323 return; | 359 return ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE; |
| 324 | 360 |
| 325 // This ShowBubble function is also used for upating the existing bubble. | 361 // This ShowBubble function is also used for updating the existing bubble. |
| 326 // However, with the bubble shown, any browser windows are NOT activated | 362 // However, with the bubble shown, any browser windows are NOT activated |
| 327 // because the bubble takes the focus from the other widgets including the | 363 // 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 | 364 // browser windows. So it is checked that |browser| is the last activated |
| 329 // browser, not is now activated. | 365 // browser, not is now activated. |
| 330 if (browser != chrome::FindLastActive()) | 366 if (browser != chrome::FindLastActive()) |
| 331 return; | 367 return ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE; |
| 332 | 368 |
| 333 // During auto-translating, the bubble should not be shown. | 369 // During auto-translating, the bubble should not be shown. |
| 334 if (step == translate::TRANSLATE_STEP_TRANSLATING || | 370 if (step == translate::TRANSLATE_STEP_TRANSLATING || |
| 335 step == translate::TRANSLATE_STEP_AFTER_TRANSLATE) { | 371 step == translate::TRANSLATE_STEP_AFTER_TRANSLATE) { |
| 336 if (GetLanguageState().InTranslateNavigation()) | 372 if (GetLanguageState().InTranslateNavigation()) |
| 337 return; | 373 return ShowTranslateBubbleResult::SUCCESS; |
| 338 } | 374 } |
| 339 | 375 |
| 340 TranslateBubbleFactory::Show(browser->window(), web_contents(), step, | 376 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, |
| 341 error_type); | 377 error_type); |
| 342 #else | 378 #else |
| 343 NOTREACHED(); | 379 NOTREACHED(); |
| 380 return ShowTranslateBubbleResult::SUCCESS; |
| 344 #endif | 381 #endif |
| 345 } | 382 } |
| OLD | NEW |