Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(626)

Side by Side Diff: chrome/browser/translate/chrome_translate_client.cc

Issue 2897563002: Implement client side logging for language detection. (Closed)
Patch Set: fix Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 specifics->set_navigation_id(entry_id);
skym 2017/05/23 22:45:42 So, this is kind of an open question, what's the b
renjieliu1 2017/05/24 00:33:17 thank you for the clarification!
80
81 sync_pb::LanguageDetection lang_detection;
82 auto* const lang = lang_detection.add_detected_languages();
skym 2017/05/23 22:45:42 I'm not a huge fan of this auto, but I suppose thi
renjieliu1 2017/05/24 00:33:17 Acknowledged.
83 lang->set_language_code(details.cld_language);
84 lang->set_is_reliable(details.is_cld_reliable);
85 // Only set adopted_language when it's different from cld_language.
86 if (details.adopted_language != details.cld_language) {
87 lang_detection.set_adopted_language(details.adopted_language);
skym 2017/05/23 22:45:42 I'm sorry, I should have commented about this when
renjieliu1 2017/05/24 00:33:17 That's a good suggestion! Changed the proto.
88 }
89 *specifics->mutable_language_detection() = lang_detection;
90 return specifics;
91 }
92
93 void LogLanguageDetectionEvent(
94 const content::WebContents* const web_contents,
95 const translate::LanguageDetectionDetails& details) {
96 auto* const profile =
97 Profile::FromBrowserContext(web_contents->GetBrowserContext());
98
99 if (profile == nullptr) {
skym 2017/05/23 22:45:41 Can this happen? Just convenience for tests?
renjieliu1 2017/05/24 00:33:17 you're right, previously I thought if user is not
100 return;
101 }
102
103 // Get user event service.
skym 2017/05/23 22:45:42 I'd omit this comment.
renjieliu1 2017/05/24 00:33:17 Done.
104 syncer::UserEventService* const user_event_service =
105 browser_sync::UserEventServiceFactory::GetForProfile(profile);
106
107 const auto* const entry =
108 web_contents->GetController().GetLastCommittedEntry();
109
110 // If entry is null, we don't record the page.
111 if (entry == nullptr) {
112 return;
skym 2017/05/23 22:45:42 Early return to avoid a single expression below se
renjieliu1 2017/05/24 00:33:17 Done.
113 }
114
115 user_event_service->RecordUserEvent(
116 ConstructLanguageDetectionEvent(entry->GetUniqueID(), details));
117 }
118
68 } // namespace 119 } // namespace
69 120
70 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); 121 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient);
71 122
72 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) 123 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents)
73 : content::WebContentsObserver(web_contents), 124 : content::WebContentsObserver(web_contents),
74 translate_driver_(&web_contents->GetController()), 125 translate_driver_(&web_contents->GetController()),
75 translate_manager_(new translate::TranslateManager( 126 translate_manager_(new translate::TranslateManager(
76 this, 127 this,
77 translate::TranslateRankerFactory::GetForBrowserContext( 128 translate::TranslateRankerFactory::GetForBrowserContext(
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 353
303 void ChromeTranslateClient::OnLanguageDetermined( 354 void ChromeTranslateClient::OnLanguageDetermined(
304 const translate::LanguageDetectionDetails& details) { 355 const translate::LanguageDetectionDetails& details) {
305 // TODO: Remove translate notifications and have the clients be 356 // TODO: Remove translate notifications and have the clients be
306 // ContentTranslateDriver::Observer directly instead. 357 // ContentTranslateDriver::Observer directly instead.
307 content::NotificationService::current()->Notify( 358 content::NotificationService::current()->Notify(
308 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 359 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
309 content::Source<content::WebContents>(web_contents()), 360 content::Source<content::WebContents>(web_contents()),
310 content::Details<const translate::LanguageDetectionDetails>(&details)); 361 content::Details<const translate::LanguageDetectionDetails>(&details));
311 362
363 LogLanguageDetectionEvent(web_contents(), details);
312 // Unless we have no language model (e.g., in incognito), notify the model 364 // Unless we have no language model (e.g., in incognito), notify the model
313 // about detected language of every page visited. 365 // about detected language of every page visited.
314 if (language_model_ && details.is_cld_reliable) 366 if (language_model_ && details.is_cld_reliable)
315 language_model_->OnPageVisited(details.cld_language); 367 language_model_->OnPageVisited(details.cld_language);
316 } 368 }
317 369
318 void ChromeTranslateClient::OnPageTranslated( 370 void ChromeTranslateClient::OnPageTranslated(
319 const std::string& original_lang, 371 const std::string& original_lang,
320 const std::string& translated_lang, 372 const std::string& translated_lang,
321 translate::TranslateErrors::Type error_type) { 373 translate::TranslateErrors::Type error_type) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 return ShowTranslateBubbleResult::SUCCESS; 416 return ShowTranslateBubbleResult::SUCCESS;
365 } 417 }
366 418
367 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, 419 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step,
368 error_type); 420 error_type);
369 #else 421 #else
370 NOTREACHED(); 422 NOTREACHED();
371 return ShowTranslateBubbleResult::SUCCESS; 423 return ShowTranslateBubbleResult::SUCCESS;
372 #endif 424 #endif
373 } 425 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698