| 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 <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #include "components/translate/core/browser/language_model.h" | 36 #include "components/translate/core/browser/language_model.h" |
| 37 #include "components/translate/core/browser/language_state.h" | 37 #include "components/translate/core/browser/language_state.h" |
| 38 #include "components/translate/core/browser/page_translated_details.h" | 38 #include "components/translate/core/browser/page_translated_details.h" |
| 39 #include "components/translate/core/browser/translate_accept_languages.h" | 39 #include "components/translate/core/browser/translate_accept_languages.h" |
| 40 #include "components/translate/core/browser/translate_download_manager.h" | 40 #include "components/translate/core/browser/translate_download_manager.h" |
| 41 #include "components/translate/core/browser/translate_infobar_delegate.h" | 41 #include "components/translate/core/browser/translate_infobar_delegate.h" |
| 42 #include "components/translate/core/browser/translate_manager.h" | 42 #include "components/translate/core/browser/translate_manager.h" |
| 43 #include "components/translate/core/browser/translate_prefs.h" | 43 #include "components/translate/core/browser/translate_prefs.h" |
| 44 #include "components/translate/core/common/language_detection_details.h" | 44 #include "components/translate/core/common/language_detection_details.h" |
| 45 #include "components/translate/core/common/language_detection_logging_helper.h" | 45 #include "components/translate/core/common/language_detection_logging_helper.h" |
| 46 #include "components/translate/core/common/translation_logging_helper.h" |
| 46 #include "components/variations/service/variations_service.h" | 47 #include "components/variations/service/variations_service.h" |
| 47 #include "content/public/browser/navigation_entry.h" | 48 #include "content/public/browser/navigation_entry.h" |
| 48 #include "content/public/browser/notification_service.h" | 49 #include "content/public/browser/notification_service.h" |
| 49 #include "content/public/browser/render_view_host.h" | 50 #include "content/public/browser/render_view_host.h" |
| 50 #include "content/public/browser/web_contents.h" | 51 #include "content/public/browser/web_contents.h" |
| 51 #include "url/gurl.h" | 52 #include "url/gurl.h" |
| 52 | 53 |
| 53 namespace { | 54 namespace { |
| 55 using metrics::TranslateEventProto; |
| 54 | 56 |
| 55 metrics::TranslateEventProto::EventType BubbleResultToTranslateEvent( | 57 TranslateEventProto::EventType BubbleResultToTranslateEvent( |
| 56 ShowTranslateBubbleResult result) { | 58 ShowTranslateBubbleResult result) { |
| 57 switch (result) { | 59 switch (result) { |
| 58 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID: | 60 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID: |
| 59 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_INVALID; | 61 return TranslateEventProto::BROWSER_WINDOW_IS_INVALID; |
| 60 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED: | 62 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED: |
| 61 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED; | 63 return TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED; |
| 62 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE: | 64 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE: |
| 63 return metrics::TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE; | 65 return TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE; |
| 64 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE: | 66 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE: |
| 65 return metrics::TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE; | 67 return TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE; |
| 66 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE: | 68 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE: |
| 67 return metrics::TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE; | 69 return TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE; |
| 68 default: | 70 default: |
| 69 NOTREACHED(); | 71 NOTREACHED(); |
| 70 return metrics::TranslateEventProto::UNKNOWN; | 72 return metrics::TranslateEventProto::UNKNOWN; |
| 71 } | 73 } |
| 72 } | 74 } |
| 73 | 75 |
| 76 // ========== LOG LANGUAGE DETECTION EVENT ============== |
| 77 |
| 74 void LogLanguageDetectionEvent( | 78 void LogLanguageDetectionEvent( |
| 75 const content::WebContents* const web_contents, | 79 const content::WebContents* const web_contents, |
| 76 const translate::LanguageDetectionDetails& details) { | 80 const translate::LanguageDetectionDetails& details) { |
| 77 auto* const profile = | 81 auto* const profile = |
| 78 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 82 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 79 | 83 |
| 80 syncer::UserEventService* const user_event_service = | 84 syncer::UserEventService* const user_event_service = |
| 81 browser_sync::UserEventServiceFactory::GetForProfile(profile); | 85 browser_sync::UserEventServiceFactory::GetForProfile(profile); |
| 82 | 86 |
| 83 const auto* const entry = | 87 const auto* const entry = |
| 84 web_contents->GetController().GetLastCommittedEntry(); | 88 web_contents->GetController().GetLastCommittedEntry(); |
| 85 | 89 |
| 86 // If entry is null, we don't record the page. | 90 // If entry is null, we don't record the page. |
| 87 // The navigation entry can be null in situations like download or initial | 91 // The navigation entry can be null in situations like download or initial |
| 88 // blank page. | 92 // blank page. |
| 89 if (entry != nullptr) { | 93 if (entry != nullptr) { |
| 90 user_event_service->RecordUserEvent( | 94 user_event_service->RecordUserEvent( |
| 91 translate::ConstructLanguageDetectionEvent(details)); | 95 translate::ConstructLanguageDetectionEvent(details)); |
| 92 } | 96 } |
| 93 } | 97 } |
| 94 | 98 |
| 99 // ========== LOG TRANSLATE EVENT ============== |
| 100 |
| 101 void LogTranslateEvent(const content::WebContents* const web_contents, |
| 102 const metrics::TranslateEventProto& translate_event) { |
| 103 DCHECK(web_contents); |
| 104 auto* const profile = |
| 105 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 106 |
| 107 syncer::UserEventService* const user_event_service = |
| 108 browser_sync::UserEventServiceFactory::GetForProfile(profile); |
| 109 |
| 110 const auto* const entry = |
| 111 web_contents->GetController().GetLastCommittedEntry(); |
| 112 |
| 113 // If entry is null, we don't record the page. |
| 114 // The navigation entry can be null in situations like download or initial |
| 115 // blank page. |
| 116 if (entry == nullptr) |
| 117 return; |
| 118 |
| 119 auto specifics = base::MakeUnique<sync_pb::UserEventSpecifics>(); |
| 120 // We only log the event we care about. |
| 121 const bool needs_logging = translate::ConstructTranslateEvent( |
| 122 entry->GetTimestamp().ToInternalValue(), translate_event, |
| 123 specifics.get()); |
| 124 if (needs_logging) { |
| 125 user_event_service->RecordUserEvent(std::move(specifics)); |
| 126 } |
| 127 } |
| 128 |
| 95 } // namespace | 129 } // namespace |
| 96 | 130 |
| 97 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); | 131 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); |
| 98 | 132 |
| 99 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) | 133 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) |
| 100 : content::WebContentsObserver(web_contents), | 134 : content::WebContentsObserver(web_contents), |
| 101 translate_driver_(&web_contents->GetController()), | 135 translate_driver_(&web_contents->GetController()), |
| 102 translate_manager_(new translate::TranslateManager( | 136 translate_manager_(new translate::TranslateManager( |
| 103 this, | 137 this, |
| 104 translate::TranslateRankerFactory::GetForBrowserContext( | 138 translate::TranslateRankerFactory::GetForBrowserContext( |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 if (!auto_translate_language.empty()) { | 223 if (!auto_translate_language.empty()) { |
| 190 *target = auto_translate_language; | 224 *target = auto_translate_language; |
| 191 return; | 225 return; |
| 192 } | 226 } |
| 193 } | 227 } |
| 194 | 228 |
| 195 *target = | 229 *target = |
| 196 translate::TranslateManager::GetTargetLanguage(translate_prefs.get()); | 230 translate::TranslateManager::GetTargetLanguage(translate_prefs.get()); |
| 197 } | 231 } |
| 198 | 232 |
| 233 void ChromeTranslateClient::RecordTranslateEvent( |
| 234 const TranslateEventProto& translate_event) { |
| 235 LogTranslateEvent(web_contents(), translate_event); |
| 236 } |
| 237 |
| 199 // static | 238 // static |
| 200 void ChromeTranslateClient::BindContentTranslateDriver( | 239 void ChromeTranslateClient::BindContentTranslateDriver( |
| 201 content::RenderFrameHost* render_frame_host, | 240 content::RenderFrameHost* render_frame_host, |
| 202 const service_manager::BindSourceInfo& source_info, | 241 const service_manager::BindSourceInfo& source_info, |
| 203 translate::mojom::ContentTranslateDriverRequest request) { | 242 translate::mojom::ContentTranslateDriverRequest request) { |
| 204 content::WebContents* web_contents = | 243 content::WebContents* web_contents = |
| 205 content::WebContents::FromRenderFrameHost(render_frame_host); | 244 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 206 if (!web_contents) | 245 if (!web_contents) |
| 207 return; | 246 return; |
| 208 | 247 |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 return ShowTranslateBubbleResult::SUCCESS; | 431 return ShowTranslateBubbleResult::SUCCESS; |
| 393 } | 432 } |
| 394 | 433 |
| 395 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, | 434 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, |
| 396 error_type); | 435 error_type); |
| 397 #else | 436 #else |
| 398 NOTREACHED(); | 437 NOTREACHED(); |
| 399 return ShowTranslateBubbleResult::SUCCESS; | 438 return ShowTranslateBubbleResult::SUCCESS; |
| 400 #endif | 439 #endif |
| 401 } | 440 } |
| OLD | NEW |