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 <memory> | |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/infobars/infobar_service.h" | 16 #include "chrome/browser/infobars/infobar_service.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/sync/user_event_service_factory.h" | |
| 17 #include "chrome/browser/translate/language_model_factory.h" | 19 #include "chrome/browser/translate/language_model_factory.h" |
| 18 #include "chrome/browser/translate/translate_accept_languages_factory.h" | 20 #include "chrome/browser/translate/translate_accept_languages_factory.h" |
| 19 #include "chrome/browser/translate/translate_ranker_factory.h" | 21 #include "chrome/browser/translate/translate_ranker_factory.h" |
| 20 #include "chrome/browser/translate/translate_service.h" | 22 #include "chrome/browser/translate/translate_service.h" |
| 21 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_finder.h" | 24 #include "chrome/browser/ui/browser_finder.h" |
| 23 #include "chrome/browser/ui/browser_tabstrip.h" | 25 #include "chrome/browser/ui/browser_tabstrip.h" |
| 24 #include "chrome/browser/ui/browser_window.h" | 26 #include "chrome/browser/ui/browser_window.h" |
| 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 27 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 26 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | 28 #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
| 27 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
| 28 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 29 #include "chrome/grit/theme_resources.h" | 31 #include "chrome/grit/theme_resources.h" |
| 30 #include "components/metrics/proto/translate_event.pb.h" | 32 #include "components/metrics/proto/translate_event.pb.h" |
| 31 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 34 #include "components/sync/protocol/user_event_specifics.pb.h" | |
| 35 #include "components/sync/user_events/user_event_service.h" | |
| 32 #include "components/translate/core/browser/language_model.h" | 36 #include "components/translate/core/browser/language_model.h" |
| 33 #include "components/translate/core/browser/language_state.h" | 37 #include "components/translate/core/browser/language_state.h" |
| 34 #include "components/translate/core/browser/page_translated_details.h" | 38 #include "components/translate/core/browser/page_translated_details.h" |
| 35 #include "components/translate/core/browser/translate_accept_languages.h" | 39 #include "components/translate/core/browser/translate_accept_languages.h" |
| 36 #include "components/translate/core/browser/translate_download_manager.h" | 40 #include "components/translate/core/browser/translate_download_manager.h" |
| 37 #include "components/translate/core/browser/translate_infobar_delegate.h" | 41 #include "components/translate/core/browser/translate_infobar_delegate.h" |
| 38 #include "components/translate/core/browser/translate_manager.h" | 42 #include "components/translate/core/browser/translate_manager.h" |
| 39 #include "components/translate/core/browser/translate_prefs.h" | 43 #include "components/translate/core/browser/translate_prefs.h" |
| 40 #include "components/translate/core/common/language_detection_details.h" | 44 #include "components/translate/core/common/language_detection_details.h" |
| 41 #include "components/variations/service/variations_service.h" | 45 #include "components/variations/service/variations_service.h" |
| 46 #include "content/public/browser/navigation_entry.h" | |
| 42 #include "content/public/browser/notification_service.h" | 47 #include "content/public/browser/notification_service.h" |
| 43 #include "content/public/browser/render_view_host.h" | 48 #include "content/public/browser/render_view_host.h" |
| 44 #include "content/public/browser/web_contents.h" | 49 #include "content/public/browser/web_contents.h" |
| 45 #include "url/gurl.h" | 50 #include "url/gurl.h" |
| 46 | 51 |
| 47 namespace { | 52 namespace { |
| 48 | 53 |
| 49 metrics::TranslateEventProto::EventType BubbleResultToTranslateEvent( | 54 metrics::TranslateEventProto::EventType BubbleResultToTranslateEvent( |
| 50 ShowTranslateBubbleResult result) { | 55 ShowTranslateBubbleResult result) { |
| 51 switch (result) { | 56 switch (result) { |
| 52 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID: | 57 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID: |
| 53 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_INVALID; | 58 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_INVALID; |
| 54 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED: | 59 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED: |
| 55 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED; | 60 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED; |
| 56 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE: | 61 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE: |
| 57 return metrics::TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE; | 62 return metrics::TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE; |
| 58 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE: | 63 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE: |
| 59 return metrics::TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE; | 64 return metrics::TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE; |
| 60 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE: | 65 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE: |
| 61 return metrics::TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE; | 66 return metrics::TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE; |
| 62 default: | 67 default: |
| 63 NOTREACHED(); | 68 NOTREACHED(); |
| 64 return metrics::TranslateEventProto::UNKNOWN; | 69 return metrics::TranslateEventProto::UNKNOWN; |
| 65 } | 70 } |
| 66 } | 71 } |
| 67 | 72 |
| 73 std::unique_ptr<sync_pb::UserEventSpecifics> ConstructLanguageDetectionEvent( | |
| 74 const int entry_id, | |
| 75 const translate::LanguageDetectionDetails& details) { | |
| 76 auto specifics = base::MakeUnique<sync_pb::UserEventSpecifics>(); | |
| 77 specifics->set_event_time_usec(base::Time::Now().ToInternalValue()); | |
| 78 | |
| 79 // TODO(renjieliu): Revisit this field when the best way to identify | |
| 80 // navigations is determined. | |
| 81 specifics->set_navigation_id(base::Time::Now().ToInternalValue()); | |
|
groby-ooo-7-16
2017/05/25 21:35:29
Note: Time::Now() is not guaranteed to be increasi
napper
2017/05/25 21:49:26
See here for the ongoing discussion: https://docs.
renjieliu1
2017/05/26 00:26:45
Acknowledged.
renjieliu1
2017/05/26 00:26:45
currently the suggested way is to use time to iden
groby-ooo-7-16
2017/05/26 01:28:48
I'm not sure what your requirements are, so pick w
| |
| 82 | |
| 83 sync_pb::LanguageDetection lang_detection; | |
| 84 auto* const lang = lang_detection.add_detected_languages(); | |
|
groby-ooo-7-16
2017/05/25 21:35:29
Wait - isn't this a mutable pointer to a constant
napper
2017/05/25 21:49:26
Note that the pointer is const (so lang++ won't wo
renjieliu1
2017/05/26 00:26:45
this is to a const pointer to a mutable object
renjieliu1
2017/05/26 00:26:45
thanks for the clarification! :)
groby-ooo-7-16
2017/05/26 01:28:47
Indeed. Thank you. I can't believe I *still* read
| |
| 85 lang->set_language_code(details.cld_language); | |
| 86 lang->set_is_reliable(details.is_cld_reliable); | |
| 87 // Only set adopted_language when it's different from cld_language. | |
| 88 if (details.adopted_language != details.cld_language) { | |
| 89 lang_detection.set_adopted_language_code(details.adopted_language); | |
| 90 } | |
| 91 *specifics->mutable_language_detection() = lang_detection; | |
| 92 return specifics; | |
| 93 } | |
| 94 | |
| 95 void LogLanguageDetectionEvent( | |
| 96 const content::WebContents* const web_contents, | |
| 97 const translate::LanguageDetectionDetails& details) { | |
| 98 auto* const profile = | |
| 99 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 100 | |
| 101 syncer::UserEventService* const user_event_service = | |
| 102 browser_sync::UserEventServiceFactory::GetForProfile(profile); | |
| 103 | |
| 104 const auto* const entry = | |
| 105 web_contents->GetController().GetLastCommittedEntry(); | |
| 106 | |
| 107 // If entry is null, we don't record the page. | |
|
groby-ooo-7-16
2017/05/25 21:35:29
The logic is clear from the code - can you change
renjieliu1
2017/05/26 00:26:45
Added a comment.
I wonder are you suggesting web
groby-ooo-7-16
2017/05/26 01:28:48
No, I meant the NavigationObserver :) It gets noti
| |
| 108 if (entry != nullptr) { | |
| 109 user_event_service->RecordUserEvent( | |
| 110 ConstructLanguageDetectionEvent(entry->GetUniqueID(), details)); | |
| 111 } | |
| 112 } | |
| 113 | |
| 68 } // namespace | 114 } // namespace |
| 69 | 115 |
| 70 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); | 116 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); |
| 71 | 117 |
| 72 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) | 118 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) |
| 73 : content::WebContentsObserver(web_contents), | 119 : content::WebContentsObserver(web_contents), |
| 74 translate_driver_(&web_contents->GetController()), | 120 translate_driver_(&web_contents->GetController()), |
| 75 translate_manager_(new translate::TranslateManager( | 121 translate_manager_(new translate::TranslateManager( |
| 76 this, | 122 this, |
| 77 translate::TranslateRankerFactory::GetForBrowserContext( | 123 translate::TranslateRankerFactory::GetForBrowserContext( |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 | 348 |
| 303 void ChromeTranslateClient::OnLanguageDetermined( | 349 void ChromeTranslateClient::OnLanguageDetermined( |
| 304 const translate::LanguageDetectionDetails& details) { | 350 const translate::LanguageDetectionDetails& details) { |
| 305 // TODO: Remove translate notifications and have the clients be | 351 // TODO: Remove translate notifications and have the clients be |
| 306 // ContentTranslateDriver::Observer directly instead. | 352 // ContentTranslateDriver::Observer directly instead. |
| 307 content::NotificationService::current()->Notify( | 353 content::NotificationService::current()->Notify( |
| 308 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 354 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 309 content::Source<content::WebContents>(web_contents()), | 355 content::Source<content::WebContents>(web_contents()), |
| 310 content::Details<const translate::LanguageDetectionDetails>(&details)); | 356 content::Details<const translate::LanguageDetectionDetails>(&details)); |
| 311 | 357 |
| 358 LogLanguageDetectionEvent(web_contents(), details); | |
| 312 // Unless we have no language model (e.g., in incognito), notify the model | 359 // Unless we have no language model (e.g., in incognito), notify the model |
| 313 // about detected language of every page visited. | 360 // about detected language of every page visited. |
| 314 if (language_model_ && details.is_cld_reliable) | 361 if (language_model_ && details.is_cld_reliable) |
| 315 language_model_->OnPageVisited(details.cld_language); | 362 language_model_->OnPageVisited(details.cld_language); |
| 316 } | 363 } |
| 317 | 364 |
| 318 void ChromeTranslateClient::OnPageTranslated( | 365 void ChromeTranslateClient::OnPageTranslated( |
| 319 const std::string& original_lang, | 366 const std::string& original_lang, |
| 320 const std::string& translated_lang, | 367 const std::string& translated_lang, |
| 321 translate::TranslateErrors::Type error_type) { | 368 translate::TranslateErrors::Type error_type) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 return ShowTranslateBubbleResult::SUCCESS; | 411 return ShowTranslateBubbleResult::SUCCESS; |
| 365 } | 412 } |
| 366 | 413 |
| 367 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, | 414 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, |
| 368 error_type); | 415 error_type); |
| 369 #else | 416 #else |
| 370 NOTREACHED(); | 417 NOTREACHED(); |
| 371 return ShowTranslateBubbleResult::SUCCESS; | 418 return ShowTranslateBubbleResult::SUCCESS; |
| 372 #endif | 419 #endif |
| 373 } | 420 } |
| OLD | NEW |