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()); |
| 82 |
| 83 sync_pb::LanguageDetection lang_detection; |
| 84 auto* const lang = lang_detection.add_detected_languages(); |
| 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. |
| 108 // The navigation entry can be null in situations like download or initial |
| 109 // blank page. |
| 110 if (entry != nullptr) { |
| 111 user_event_service->RecordUserEvent( |
| 112 ConstructLanguageDetectionEvent(entry->GetUniqueID(), details)); |
| 113 } |
| 114 } |
| 115 |
68 } // namespace | 116 } // namespace |
69 | 117 |
70 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); | 118 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); |
71 | 119 |
72 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) | 120 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) |
73 : content::WebContentsObserver(web_contents), | 121 : content::WebContentsObserver(web_contents), |
74 translate_driver_(&web_contents->GetController()), | 122 translate_driver_(&web_contents->GetController()), |
75 translate_manager_(new translate::TranslateManager( | 123 translate_manager_(new translate::TranslateManager( |
76 this, | 124 this, |
77 translate::TranslateRankerFactory::GetForBrowserContext( | 125 translate::TranslateRankerFactory::GetForBrowserContext( |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 | 350 |
303 void ChromeTranslateClient::OnLanguageDetermined( | 351 void ChromeTranslateClient::OnLanguageDetermined( |
304 const translate::LanguageDetectionDetails& details) { | 352 const translate::LanguageDetectionDetails& details) { |
305 // TODO: Remove translate notifications and have the clients be | 353 // TODO: Remove translate notifications and have the clients be |
306 // ContentTranslateDriver::Observer directly instead. | 354 // ContentTranslateDriver::Observer directly instead. |
307 content::NotificationService::current()->Notify( | 355 content::NotificationService::current()->Notify( |
308 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 356 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
309 content::Source<content::WebContents>(web_contents()), | 357 content::Source<content::WebContents>(web_contents()), |
310 content::Details<const translate::LanguageDetectionDetails>(&details)); | 358 content::Details<const translate::LanguageDetectionDetails>(&details)); |
311 | 359 |
| 360 LogLanguageDetectionEvent(web_contents(), details); |
312 // Unless we have no language model (e.g., in incognito), notify the model | 361 // Unless we have no language model (e.g., in incognito), notify the model |
313 // about detected language of every page visited. | 362 // about detected language of every page visited. |
314 if (language_model_ && details.is_cld_reliable) | 363 if (language_model_ && details.is_cld_reliable) |
315 language_model_->OnPageVisited(details.cld_language); | 364 language_model_->OnPageVisited(details.cld_language); |
316 } | 365 } |
317 | 366 |
318 void ChromeTranslateClient::OnPageTranslated( | 367 void ChromeTranslateClient::OnPageTranslated( |
319 const std::string& original_lang, | 368 const std::string& original_lang, |
320 const std::string& translated_lang, | 369 const std::string& translated_lang, |
321 translate::TranslateErrors::Type error_type) { | 370 translate::TranslateErrors::Type error_type) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 return ShowTranslateBubbleResult::SUCCESS; | 413 return ShowTranslateBubbleResult::SUCCESS; |
365 } | 414 } |
366 | 415 |
367 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, | 416 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, |
368 error_type); | 417 error_type); |
369 #else | 418 #else |
370 NOTREACHED(); | 419 NOTREACHED(); |
371 return ShowTranslateBubbleResult::SUCCESS; | 420 return ShowTranslateBubbleResult::SUCCESS; |
372 #endif | 421 #endif |
373 } | 422 } |
OLD | NEW |