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

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

Issue 2913593002: Implementation of translation event logging. (Closed)
Patch Set: fix Created 3 years, 6 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
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 <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 24 matching lines...) Expand all
35 #include "components/sync/user_events/user_event_service.h" 35 #include "components/sync/user_events/user_event_service.h"
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/translation_logging_helper.h"
45 #include "components/variations/service/variations_service.h" 46 #include "components/variations/service/variations_service.h"
46 #include "content/public/browser/navigation_entry.h" 47 #include "content/public/browser/navigation_entry.h"
47 #include "content/public/browser/notification_service.h" 48 #include "content/public/browser/notification_service.h"
48 #include "content/public/browser/render_view_host.h" 49 #include "content/public/browser/render_view_host.h"
49 #include "content/public/browser/web_contents.h" 50 #include "content/public/browser/web_contents.h"
50 #include "url/gurl.h" 51 #include "url/gurl.h"
51 52
52 namespace { 53 namespace {
54 using metrics::TranslateEventProto;
53 55
54 metrics::TranslateEventProto::EventType BubbleResultToTranslateEvent( 56 TranslateEventProto::EventType BubbleResultToTranslateEvent(
55 ShowTranslateBubbleResult result) { 57 ShowTranslateBubbleResult result) {
56 switch (result) { 58 switch (result) {
57 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID: 59 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_VALID:
58 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_INVALID; 60 return TranslateEventProto::BROWSER_WINDOW_IS_INVALID;
59 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED: 61 case ShowTranslateBubbleResult::BROWSER_WINDOW_MINIMIZED:
60 return metrics::TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED; 62 return TranslateEventProto::BROWSER_WINDOW_IS_MINIMIZED;
61 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE: 63 case ShowTranslateBubbleResult::BROWSER_WINDOW_NOT_ACTIVE:
62 return metrics::TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE; 64 return TranslateEventProto::BROWSER_WINDOW_NOT_ACTIVE;
63 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE: 65 case ShowTranslateBubbleResult::WEB_CONTENTS_NOT_ACTIVE:
64 return metrics::TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE; 66 return TranslateEventProto::WEB_CONTENTS_NOT_ACTIVE;
65 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE: 67 case ShowTranslateBubbleResult::EDITABLE_FIELD_IS_ACTIVE:
66 return metrics::TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE; 68 return TranslateEventProto::EDITABLE_FIELD_IS_ACTIVE;
67 default: 69 default:
68 NOTREACHED(); 70 NOTREACHED();
69 return metrics::TranslateEventProto::UNKNOWN; 71 return metrics::TranslateEventProto::UNKNOWN;
70 } 72 }
71 } 73 }
72 74
75 // ========== LOG LANGUAGE DETECTION EVENT ==============
76
73 std::unique_ptr<sync_pb::UserEventSpecifics> ConstructLanguageDetectionEvent( 77 std::unique_ptr<sync_pb::UserEventSpecifics> ConstructLanguageDetectionEvent(
74 const int entry_id, 78 const int entry_id,
75 const translate::LanguageDetectionDetails& details) { 79 const translate::LanguageDetectionDetails& details) {
76 auto specifics = base::MakeUnique<sync_pb::UserEventSpecifics>(); 80 auto specifics = base::MakeUnique<sync_pb::UserEventSpecifics>();
77 specifics->set_event_time_usec(base::Time::Now().ToInternalValue()); 81 specifics->set_event_time_usec(base::Time::Now().ToInternalValue());
78 82
79 // TODO(renjieliu): Revisit this field when the best way to identify 83 // TODO(renjieliu): Revisit this field when the best way to identify
80 // navigations is determined. 84 // navigations is determined.
81 specifics->set_navigation_id(base::Time::Now().ToInternalValue()); 85 specifics->set_navigation_id(base::Time::Now().ToInternalValue());
82 86
(...skipping 23 matching lines...) Expand all
106 110
107 // If entry is null, we don't record the page. 111 // If entry is null, we don't record the page.
108 // The navigation entry can be null in situations like download or initial 112 // The navigation entry can be null in situations like download or initial
109 // blank page. 113 // blank page.
110 if (entry != nullptr) { 114 if (entry != nullptr) {
111 user_event_service->RecordUserEvent( 115 user_event_service->RecordUserEvent(
112 ConstructLanguageDetectionEvent(entry->GetUniqueID(), details)); 116 ConstructLanguageDetectionEvent(entry->GetUniqueID(), details));
113 } 117 }
114 } 118 }
115 119
120 // ========== LOG TRANSLATE EVENT ==============
121
122 void LogTranslateEvent(const content::WebContents* const web_contents,
123 const metrics::TranslateEventProto& translate_event) {
124 DCHECK(web_contents);
125 auto* const profile =
126 Profile::FromBrowserContext(web_contents->GetBrowserContext());
127
128 syncer::UserEventService* const user_event_service =
129 browser_sync::UserEventServiceFactory::GetForProfile(profile);
130
131 const auto* const entry =
132 web_contents->GetController().GetLastCommittedEntry();
133
134 // If entry is null, we don't record the page.
135 // The navigation entry can be null in situations like download or initial
136 // blank page.
137 if (entry == nullptr)
138 return;
139
140 auto specifics = base::MakeUnique<sync_pb::UserEventSpecifics>();
141 // We only log the event we care about.
142 const bool needs_logging =
143 translate::ConstructTranslateEvent(translate_event, specifics.get());
144 if (needs_logging) {
145 user_event_service->RecordUserEvent(std::move(specifics));
146 }
147 }
148
116 } // namespace 149 } // namespace
117 150
118 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); 151 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient);
119 152
120 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) 153 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents)
121 : content::WebContentsObserver(web_contents), 154 : content::WebContentsObserver(web_contents),
122 translate_driver_(&web_contents->GetController()), 155 translate_driver_(&web_contents->GetController()),
123 translate_manager_(new translate::TranslateManager( 156 translate_manager_(new translate::TranslateManager(
124 this, 157 this,
125 translate::TranslateRankerFactory::GetForBrowserContext( 158 translate::TranslateRankerFactory::GetForBrowserContext(
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 if (!auto_translate_language.empty()) { 243 if (!auto_translate_language.empty()) {
211 *target = auto_translate_language; 244 *target = auto_translate_language;
212 return; 245 return;
213 } 246 }
214 } 247 }
215 248
216 *target = 249 *target =
217 translate::TranslateManager::GetTargetLanguage(translate_prefs.get()); 250 translate::TranslateManager::GetTargetLanguage(translate_prefs.get());
218 } 251 }
219 252
253 void ChromeTranslateClient::RecordTranslateEvent(
254 const TranslateEventProto& translate_event) {
255 LogTranslateEvent(web_contents(), translate_event);
256 }
257
220 // static 258 // static
221 void ChromeTranslateClient::BindContentTranslateDriver( 259 void ChromeTranslateClient::BindContentTranslateDriver(
222 content::RenderFrameHost* render_frame_host, 260 content::RenderFrameHost* render_frame_host,
223 const service_manager::BindSourceInfo& source_info, 261 const service_manager::BindSourceInfo& source_info,
224 translate::mojom::ContentTranslateDriverRequest request) { 262 translate::mojom::ContentTranslateDriverRequest request) {
225 content::WebContents* web_contents = 263 content::WebContents* web_contents =
226 content::WebContents::FromRenderFrameHost(render_frame_host); 264 content::WebContents::FromRenderFrameHost(render_frame_host);
227 if (!web_contents) 265 if (!web_contents)
228 return; 266 return;
229 267
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 return ShowTranslateBubbleResult::SUCCESS; 451 return ShowTranslateBubbleResult::SUCCESS;
414 } 452 }
415 453
416 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step, 454 return TranslateBubbleFactory::Show(browser->window(), web_contents(), step,
417 error_type); 455 error_type);
418 #else 456 #else
419 NOTREACHED(); 457 NOTREACHED();
420 return ShowTranslateBubbleResult::SUCCESS; 458 return ShowTranslateBubbleResult::SUCCESS;
421 #endif 459 #endif
422 } 460 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698