| 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 "ios/chrome/browser/translate/chrome_ios_translate_client.h" | 5 #include "ios/chrome/browser/translate/chrome_ios_translate_client.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/feature_list.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "components/infobars/core/infobar.h" | 13 #include "components/infobars/core/infobar.h" |
| 13 #include "components/metrics/proto/translate_event.pb.h" | 14 #include "components/metrics/proto/translate_event.pb.h" |
| 14 #include "components/prefs/pref_service.h" | 15 #include "components/prefs/pref_service.h" |
| 16 #include "components/sync/driver/sync_driver_switches.h" |
| 17 #include "components/sync/protocol/user_event_specifics.pb.h" |
| 18 #include "components/sync/user_events/user_event_service.h" |
| 15 #include "components/translate/core/browser/page_translated_details.h" | 19 #include "components/translate/core/browser/page_translated_details.h" |
| 16 #include "components/translate/core/browser/translate_accept_languages.h" | 20 #include "components/translate/core/browser/translate_accept_languages.h" |
| 17 #include "components/translate/core/browser/translate_infobar_delegate.h" | 21 #include "components/translate/core/browser/translate_infobar_delegate.h" |
| 18 #include "components/translate/core/browser/translate_manager.h" | 22 #include "components/translate/core/browser/translate_manager.h" |
| 19 #include "components/translate/core/browser/translate_prefs.h" | 23 #include "components/translate/core/browser/translate_prefs.h" |
| 20 #include "components/translate/core/browser/translate_step.h" | 24 #include "components/translate/core/browser/translate_step.h" |
| 21 #include "components/translate/core/common/language_detection_details.h" | 25 #include "components/translate/core/common/language_detection_details.h" |
| 26 #include "components/translate/core/common/translation_logging_helper.h" |
| 22 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 27 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 23 #include "ios/chrome/browser/infobars/infobar.h" | 28 #include "ios/chrome/browser/infobars/infobar.h" |
| 24 #include "ios/chrome/browser/infobars/infobar_controller.h" | 29 #include "ios/chrome/browser/infobars/infobar_controller.h" |
| 25 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | 30 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 26 #include "ios/chrome/browser/pref_names.h" | 31 #include "ios/chrome/browser/pref_names.h" |
| 32 #import "ios/chrome/browser/sync/ios_user_event_service_factory.h" |
| 27 #import "ios/chrome/browser/translate/after_translate_infobar_controller.h" | 33 #import "ios/chrome/browser/translate/after_translate_infobar_controller.h" |
| 28 #import "ios/chrome/browser/translate/before_translate_infobar_controller.h" | 34 #import "ios/chrome/browser/translate/before_translate_infobar_controller.h" |
| 29 #include "ios/chrome/browser/translate/language_model_factory.h" | 35 #include "ios/chrome/browser/translate/language_model_factory.h" |
| 30 #import "ios/chrome/browser/translate/never_translate_infobar_controller.h" | 36 #import "ios/chrome/browser/translate/never_translate_infobar_controller.h" |
| 31 #include "ios/chrome/browser/translate/translate_accept_languages_factory.h" | 37 #include "ios/chrome/browser/translate/translate_accept_languages_factory.h" |
| 32 #import "ios/chrome/browser/translate/translate_message_infobar_controller.h" | 38 #import "ios/chrome/browser/translate/translate_message_infobar_controller.h" |
| 33 #include "ios/chrome/browser/translate/translate_ranker_factory.h" | 39 #include "ios/chrome/browser/translate/translate_ranker_factory.h" |
| 34 #include "ios/chrome/browser/translate/translate_service_ios.h" | 40 #include "ios/chrome/browser/translate/translate_service_ios.h" |
| 35 #include "ios/chrome/grit/ios_theme_resources.h" | 41 #include "ios/chrome/grit/ios_theme_resources.h" |
| 36 #include "ios/web/public/browser_state.h" | 42 #include "ios/web/public/browser_state.h" |
| 43 #include "ios/web/public/navigation_item.h" |
| 44 #include "ios/web/public/navigation_manager.h" |
| 37 #include "ios/web/public/web_state/web_state.h" | 45 #include "ios/web/public/web_state/web_state.h" |
| 38 #include "url/gurl.h" | 46 #include "url/gurl.h" |
| 39 | 47 |
| 40 #if !defined(__has_feature) || !__has_feature(objc_arc) | 48 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 41 #error "This file requires ARC support." | 49 #error "This file requires ARC support." |
| 42 #endif | 50 #endif |
| 43 | 51 |
| 44 DEFINE_WEB_STATE_USER_DATA_KEY(ChromeIOSTranslateClient); | 52 DEFINE_WEB_STATE_USER_DATA_KEY(ChromeIOSTranslateClient); |
| 45 | 53 |
| 46 ChromeIOSTranslateClient::ChromeIOSTranslateClient(web::WebState* web_state) | 54 ChromeIOSTranslateClient::ChromeIOSTranslateClient(web::WebState* web_state) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 break; | 108 break; |
| 101 default: | 109 default: |
| 102 NOTREACHED(); | 110 NOTREACHED(); |
| 103 } | 111 } |
| 104 infobar->SetController(controller); | 112 infobar->SetController(controller); |
| 105 // TODO(crbug.com/703565): remove std::move() once Xcode 9.0+ is required. | 113 // TODO(crbug.com/703565): remove std::move() once Xcode 9.0+ is required. |
| 106 return std::move(infobar); | 114 return std::move(infobar); |
| 107 } | 115 } |
| 108 | 116 |
| 109 void ChromeIOSTranslateClient::RecordTranslateEvent( | 117 void ChromeIOSTranslateClient::RecordTranslateEvent( |
| 110 const metrics::TranslateEventProto&) { | 118 const metrics::TranslateEventProto& translate_event) { |
| 111 // TODO(crbug.com/728491): Implementing gaia-keyed logging. | 119 if (!base::FeatureList::IsEnabled(switches::kSyncUserTranslationEvents)) |
| 120 return; |
| 121 |
| 122 syncer::UserEventService* const user_event_service = |
| 123 IOSUserEventServiceFactory::GetForBrowserState( |
| 124 ios::ChromeBrowserState::FromBrowserState( |
| 125 web_state()->GetBrowserState())); |
| 126 const auto* const item = |
| 127 web_state()->GetNavigationManager()->GetLastCommittedItem(); |
| 128 |
| 129 // If entry is null, we don't record the page. |
| 130 // The navigation entry can be null in situations like download or initial |
| 131 // blank page. |
| 132 if (item == nullptr) |
| 133 return; |
| 134 |
| 135 auto specifics = base::MakeUnique<sync_pb::UserEventSpecifics>(); |
| 136 // We only log the event we care about. |
| 137 const bool needs_logging = translate::ConstructTranslateEvent( |
| 138 item->GetTimestamp().ToInternalValue(), translate_event, specifics.get()); |
| 139 if (needs_logging) { |
| 140 user_event_service->RecordUserEvent(std::move(specifics)); |
| 141 } |
| 112 } | 142 } |
| 113 | 143 |
| 114 void ChromeIOSTranslateClient::ShowTranslateUI( | 144 void ChromeIOSTranslateClient::ShowTranslateUI( |
| 115 translate::TranslateStep step, | 145 translate::TranslateStep step, |
| 116 const std::string& source_language, | 146 const std::string& source_language, |
| 117 const std::string& target_language, | 147 const std::string& target_language, |
| 118 translate::TranslateErrors::Type error_type, | 148 translate::TranslateErrors::Type error_type, |
| 119 bool triggered_from_menu) { | 149 bool triggered_from_menu) { |
| 120 DCHECK(web_state()); | 150 DCHECK(web_state()); |
| 121 if (error_type != translate::TranslateErrors::NONE) | 151 if (error_type != translate::TranslateErrors::NONE) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 const GURL& report_url) { | 204 const GURL& report_url) { |
| 175 NOTREACHED(); | 205 NOTREACHED(); |
| 176 } | 206 } |
| 177 | 207 |
| 178 void ChromeIOSTranslateClient::WebStateDestroyed() { | 208 void ChromeIOSTranslateClient::WebStateDestroyed() { |
| 179 // Translation process can be interrupted. | 209 // Translation process can be interrupted. |
| 180 // Destroying the TranslateManager now guarantees that it never has to deal | 210 // Destroying the TranslateManager now guarantees that it never has to deal |
| 181 // with nullptr WebState. | 211 // with nullptr WebState. |
| 182 translate_manager_.reset(); | 212 translate_manager_.reset(); |
| 183 } | 213 } |
| OLD | NEW |