Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 5 | #include "components/translate/content/renderer/translate_agent.h" |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 6 | |
Scott Little | f130e8e8 | 2020-08-07 16:01:37 | [diff] [blame] | 7 | #include <stddef.h> |
Fergal Daly | 74bfae2 | 2024-07-02 06:02:07 | [diff] [blame] | 8 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 9 | #include <string> |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 12 | #include "base/check_op.h" |
mcrouse | f0c2fb00 | 2021-10-13 00:40:48 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 15 | #include "base/functional/bind.h" |
Jon Napper | a7dc1b00 | 2017-06-26 01:01:21 | [diff] [blame] | 16 | #include "base/json/string_escape.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 17 | #include "base/location.h" |
Keita Suzuki | 54e166c | 2024-07-17 00:53:36 | [diff] [blame] | 18 | #include "base/metrics/histogram_functions.h" |
asvitkine | a0f05db | 2015-06-16 21:45:46 | [diff] [blame] | 19 | #include "base/metrics/histogram_macros.h" |
mcrouse | 4973894 | 2021-04-15 21:24:25 | [diff] [blame] | 20 | #include "base/metrics/histogram_macros_local.h" |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 21 | #include "base/no_destructor.h" |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 22 | #include "base/notreached.h" |
[email protected] | cbde0d9 | 2013-06-14 11:37:04 | [diff] [blame] | 23 | #include "base/strings/string_util.h" |
[email protected] | abfd149 | 2013-06-07 21:23:26 | [diff] [blame] | 24 | #include "base/strings/utf_string_conversions.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 25 | #include "base/task/single_thread_task_runner.h" |
Fergal Daly | 74bfae2 | 2024-07-02 06:02:07 | [diff] [blame] | 26 | #include "base/trace_event/trace_event.h" |
Fergal Daly | fbcc460 | 2024-08-28 15:00:31 | [diff] [blame] | 27 | #include "components/language_detection/content/renderer/language_detection_agent.h" |
Fergal Daly | fe7cf25 | 2025-01-20 02:28:42 | [diff] [blame] | 28 | #include "components/language_detection/core/constants.h" |
Fergal Daly | ab92791 | 2024-07-31 17:16:34 | [diff] [blame] | 29 | #include "components/language_detection/core/language_detection_provider.h" |
Karandeep Bhatia | 4a8b435 | 2020-07-16 06:57:13 | [diff] [blame] | 30 | #include "components/translate/content/renderer/isolated_world_util.h" |
[email protected] | eba93c9 | 2014-01-07 17:34:17 | [diff] [blame] | 31 | #include "components/translate/core/common/translate_metrics.h" |
| 32 | #include "components/translate/core/common/translate_util.h" |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 33 | #include "components/translate/core/language_detection/language_detection_model.h" |
[email protected] | bfa4c82a | 2014-05-13 23:45:02 | [diff] [blame] | 34 | #include "components/translate/core/language_detection/language_detection_util.h" |
[email protected] | 9ce23704 | 2014-07-17 18:09:56 | [diff] [blame] | 35 | #include "content/public/common/content_constants.h" |
mcrouse | f0c2fb00 | 2021-10-13 00:40:48 | [diff] [blame] | 36 | #include "content/public/common/content_switches.h" |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 37 | #include "content/public/common/url_constants.h" |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 38 | #include "content/public/renderer/render_frame.h" |
[email protected] | 9ce23704 | 2014-07-17 18:09:56 | [diff] [blame] | 39 | #include "content/public/renderer/render_thread.h" |
Lukasz Anforowicz | 9bc03e7 | 2024-07-14 20:25:02 | [diff] [blame] | 40 | #include "third_party/blink/public/platform/browser_interface_broker_proxy.h" |
Dave Tapuska | b8035cb | 2022-09-09 15:12:34 | [diff] [blame] | 41 | #include "third_party/blink/public/platform/scheduler/web_agent_group_scheduler.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 42 | #include "third_party/blink/public/web/web_document.h" |
| 43 | #include "third_party/blink/public/web/web_language_detection_details.h" |
| 44 | #include "third_party/blink/public/web/web_local_frame.h" |
| 45 | #include "third_party/blink/public/web/web_script_source.h" |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 46 | #include "url/gurl.h" |
mcrouse | 0d096d3 | 2021-02-11 01:58:46 | [diff] [blame] | 47 | #include "url/url_constants.h" |
[email protected] | 4d51d5bf | 2010-07-26 18:48:26 | [diff] [blame] | 48 | #include "v8/include/v8.h" |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 49 | |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 50 | using blink::WebDocument; |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 51 | using blink::WebLanguageDetectionDetails; |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 52 | using blink::WebLocalFrame; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 53 | using blink::WebScriptSource; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 54 | using blink::WebString; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 55 | |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 56 | namespace { |
| 57 | |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 58 | // The delay in milliseconds that we'll wait before checking to see if the |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 59 | // translate library injected in the page is ready. |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 60 | const int kTranslateInitCheckDelayMs = 150; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 61 | |
| 62 | // The maximum number of times we'll check to see if the translate library |
| 63 | // injected in the page is ready. |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 64 | const int kMaxTranslateInitCheckAttempts = 5; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 65 | |
| 66 | // The delay we wait in milliseconds before checking whether the translation has |
| 67 | // finished. |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 68 | const int kTranslateStatusCheckDelayMs = 400; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 69 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 70 | // Language name passed to the Translate element for it to detect the language. |
[email protected] | 3328125 | 2013-04-15 14:46:49 | [diff] [blame] | 71 | const char kAutoDetectionLanguage[] = "auto"; |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 72 | |
mcrouse | e29a9f1 | 2021-02-17 19:06:29 | [diff] [blame] | 73 | // The current CLD model version. |
| 74 | constexpr char kCLDModelVersion[] = "CLD3"; |
| 75 | |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 76 | // Returns the language detection model that is shared across the RenderFrames |
| 77 | // in the renderer. |
| 78 | translate::LanguageDetectionModel& GetLanguageDetectionModel() { |
Fergal Daly | 2e810a4 | 2024-07-31 14:08:06 | [diff] [blame] | 79 | static base::NoDestructor<translate::LanguageDetectionModel> instance( |
Sylvain Defresne | ae892c0 | 2024-09-20 14:29:09 | [diff] [blame] | 80 | language_detection::GetLanguageDetectionModel()); |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 81 | return *instance; |
| 82 | } |
| 83 | |
mcrouse | 15e59cc | 2021-10-20 17:38:30 | [diff] [blame] | 84 | // Returns if the language detection should be overridden so that a default |
| 85 | // result is returned immediately. |
| 86 | bool ShouldOverrideLanguageDetectionForTesting() { |
| 87 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 88 | if (command_line->HasSwitch(::switches::kOverrideLanguageDetection)) { |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 94 | } // namespace |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 95 | |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 96 | namespace translate { |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 97 | |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 98 | //////////////////////////////////////////////////////////////////////////////// |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 99 | // TranslateAgent, public: |
Devlin Cronin | c03d88df | 2022-01-20 21:29:20 | [diff] [blame] | 100 | TranslateAgent::TranslateAgent(content::RenderFrame* render_frame, int world_id) |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 101 | : content::RenderFrameObserver(render_frame), |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 102 | world_id_(world_id), |
Fergal Daly | cf6c44ac | 2024-11-22 08:34:45 | [diff] [blame] | 103 | translate_language_detection_model_(GetLanguageDetectionModel()), |
Fergal Daly | fbcc460 | 2024-08-28 15:00:31 | [diff] [blame] | 104 | language_detection_agent_( |
| 105 | IsTFLiteLanguageDetectionEnabled() |
Fergal Daly | cf6c44ac | 2024-11-22 08:34:45 | [diff] [blame] | 106 | ? new language_detection::LanguageDetectionAgent( |
| 107 | render_frame, |
| 108 | translate_language_detection_model_->tflite_model()) |
Fergal Daly | fbcc460 | 2024-08-28 15:00:31 | [diff] [blame] | 109 | : nullptr) { |
Karolina Soltys | c7fbc03 | 2018-10-25 10:10:08 | [diff] [blame] | 110 | translate_task_runner_ = this->render_frame()->GetTaskRunner( |
| 111 | blink::TaskType::kInternalTranslation); |
| 112 | } |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 113 | |
Sorin Jianu | 0373bcb9 | 2024-10-10 21:37:17 | [diff] [blame] | 114 | TranslateAgent::~TranslateAgent() = default; |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 115 | |
mcrouse | f0c2fb00 | 2021-10-13 00:40:48 | [diff] [blame] | 116 | void TranslateAgent::SeedLanguageDetectionModelForTesting( |
| 117 | base::File model_file) { |
Fergal Daly | 6ad84d9 | 2024-11-22 09:30:55 | [diff] [blame] | 118 | translate_language_detection_model_->tflite_model().UpdateWithFile( |
Fergal Daly | fbcc460 | 2024-08-28 15:00:31 | [diff] [blame] | 119 | std::move(model_file)); |
mcrouse | f0c2fb00 | 2021-10-13 00:40:48 | [diff] [blame] | 120 | } |
| 121 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 122 | void TranslateAgent::PrepareForUrl(const GURL& url) { |
Anthony Cui | e8788515 | 2023-11-29 22:16:31 | [diff] [blame] | 123 | // Navigated to a new url, reset current page translation. |
leon.han | b0780131 | 2016-08-12 11:47:57 | [diff] [blame] | 124 | ResetPage(); |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 125 | } |
| 126 | |
Zinovy Nis | fc466c15 | 2024-05-01 16:51:28 | [diff] [blame] | 127 | void TranslateAgent::PageCaptured( |
| 128 | scoped_refptr<const base::RefCountedString16> contents) { |
Fergal Daly | 74bfae2 | 2024-07-02 06:02:07 | [diff] [blame] | 129 | TRACE_EVENT("browser", "TranslateAgent::PageCaptured"); |
[email protected] | 25689773 | 2012-10-16 19:35:05 | [diff] [blame] | 130 | // Get the document language as set by WebKit from the http-equiv |
| 131 | // meta tag for "content-language". This may or may not also |
| 132 | // have a value derived from the actual Content-Language HTTP |
| 133 | // header. The two actually have different meanings (despite the |
| 134 | // original intent of http-equiv to be an equivalent) with the former |
| 135 | // being the language of the document and the latter being the |
| 136 | // language of the intended audience (a distinction really only |
Zinovy Nis | fc466c15 | 2024-05-01 16:51:28 | [diff] [blame] | 137 | // relevant for things like language textbooks). This distinction |
[email protected] | 25689773 | 2012-10-16 19:35:05 | [diff] [blame] | 138 | // shouldn't affect translation. |
Zinovy Nis | fc466c15 | 2024-05-01 16:51:28 | [diff] [blame] | 139 | if (!contents) { |
| 140 | return; |
| 141 | } |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 142 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 143 | if (!main_frame) |
[email protected] | effa54c | 2013-05-28 11:14:23 | [diff] [blame] | 144 | return; |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 145 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 146 | WebDocument document = main_frame->GetDocument(); |
mcrouse | 23992f1 | 2021-05-07 22:01:25 | [diff] [blame] | 147 | GURL url = GURL(document.Url()); |
| 148 | // Limit detection to URLs that only detect the language of the content if the |
| 149 | // page is potentially a candidate for translation. This should be strictly a |
| 150 | // subset of the conditions in TranslateService::IsTranslatableURL, however, |
| 151 | // due to layering they cannot be identical. Critically, this list should |
| 152 | // never filter anything that is eligible for translation. Under filtering is |
| 153 | // ok as the translate service will make the final call and only results in a |
| 154 | // slight overhead in running the model when unnecessary. |
| 155 | if (url.is_empty() || url.SchemeIs(content::kChromeUIScheme) || |
Eric Lawrence [MSFT] | 874a6879 | 2021-11-09 00:11:31 | [diff] [blame] | 156 | url.SchemeIs(content::kChromeDevToolsScheme) || url.IsAboutBlank()) { |
mcrouse | 23992f1 | 2021-05-07 22:01:25 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Zinovy Nis | fc466c15 | 2024-05-01 16:51:28 | [diff] [blame] | 160 | page_contents_length_ = contents->as_string().size(); |
Megan Jablonski | 2778578 | 2021-06-23 23:08:23 | [diff] [blame] | 161 | |
adithyas | 5e61528 | 2017-01-17 21:27:01 | [diff] [blame] | 162 | WebLanguageDetectionDetails web_detection_details = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 163 | WebLanguageDetectionDetails::CollectLanguageDetectionDetails(document); |
Victor Tan | 47cea9a | 2022-04-04 19:55:48 | [diff] [blame] | 164 | WebLanguageDetectionDetails::RecordAcceptLanguageAndXmlHtmlLangMetric( |
| 165 | document); |
| 166 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 167 | std::string content_language = web_detection_details.content_language.Utf8(); |
| 168 | std::string html_lang = web_detection_details.html_language.Utf8(); |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 169 | std::string model_detected_language; |
Michael Crouse | 30c287a1 | 2020-12-14 23:26:01 | [diff] [blame] | 170 | bool is_model_reliable = false; |
mcrouse | e29a9f1 | 2021-02-17 19:06:29 | [diff] [blame] | 171 | std::string detection_model_version; |
| 172 | float model_reliability_score = 0.0; |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 173 | |
mcrouse | 15e59cc | 2021-10-20 17:38:30 | [diff] [blame] | 174 | if (ShouldOverrideLanguageDetectionForTesting()) { |
| 175 | std::string language = "fr"; |
| 176 | LanguageDetectionDetails details; |
| 177 | details.adopted_language = language; |
Zinovy Nis | fc466c15 | 2024-05-01 16:51:28 | [diff] [blame] | 178 | details.contents = contents->as_string(); |
Anthony Cui | 0220ce0 | 2023-01-18 21:35:00 | [diff] [blame] | 179 | details.has_run_lang_detection = true; |
mcrouse | 15e59cc | 2021-10-20 17:38:30 | [diff] [blame] | 180 | ResetPage(); |
Vlad Batalov | 76821bcd | 2024-03-07 00:35:05 | [diff] [blame] | 181 | |
| 182 | last_details_ = std::move(details); |
| 183 | RenewPageRegistration(); |
mcrouse | 15e59cc | 2021-10-20 17:38:30 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
Anthony Cui | c526ba7 | 2023-01-13 19:02:25 | [diff] [blame] | 187 | LanguageDetectionDetails details; |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 188 | std::string language; |
Anthony Cui | e8788515 | 2023-11-29 22:16:31 | [diff] [blame] | 189 | if (page_contents_length_ == 0) { |
Tao Xiong | 6f6842bb2 | 2024-08-05 20:56:20 | [diff] [blame] | 190 | // If captured content is empty do not run language detection and |
| 191 | // only use page-provided languages. |
| 192 | language = translate::DeterminePageLanguageNoModel( |
| 193 | content_language, html_lang, |
Tao Xiong | e2fb7df | 2024-08-13 03:49:37 | [diff] [blame] | 194 | translate::LanguageVerificationType::kNoPageContent); |
Anthony Cui | c526ba7 | 2023-01-13 19:02:25 | [diff] [blame] | 195 | } else if (translate::IsTFLiteLanguageDetectionEnabled()) { |
Anthony Cui | e8788515 | 2023-11-29 22:16:31 | [diff] [blame] | 196 | // Use TFLite and page contents to assist with language detection. |
Fergal Daly | cf6c44ac | 2024-11-22 08:34:45 | [diff] [blame] | 197 | bool is_available = translate_language_detection_model_->IsAvailable(); |
Tao Xiong | e2fb7df | 2024-08-13 03:49:37 | [diff] [blame] | 198 | language = |
| 199 | is_available |
Fergal Daly | cf6c44ac | 2024-11-22 08:34:45 | [diff] [blame] | 200 | ? translate_language_detection_model_->DeterminePageLanguage( |
Tao Xiong | e2fb7df | 2024-08-13 03:49:37 | [diff] [blame] | 201 | content_language, html_lang, contents->as_string(), |
| 202 | &model_detected_language, &is_model_reliable, |
| 203 | model_reliability_score) |
| 204 | // If the model is not available do not run language |
| 205 | // detection and only use page-provided languages. |
| 206 | : translate::DeterminePageLanguageNoModel( |
| 207 | content_language, html_lang, |
| 208 | translate::LanguageVerificationType::kModelNotAvailable); |
mcrouse | ca5021c | 2021-02-19 00:34:14 | [diff] [blame] | 209 | UMA_HISTOGRAM_BOOLEAN( |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 210 | "LanguageDetection.TFLiteModel.WasModelAvailableForDetection", |
| 211 | is_available); |
mcrouse | fe71386 | 2021-08-17 22:22:44 | [diff] [blame] | 212 | UMA_HISTOGRAM_BOOLEAN( |
| 213 | "LanguageDetection.TFLiteModel.WasModelUnavailableDueToDeferredLoad", |
Fergal Daly | fbcc460 | 2024-08-28 15:00:31 | [diff] [blame] | 214 | !is_available && |
| 215 | language_detection_agent_->waiting_for_first_foreground()); |
Fergal Daly | cf6c44ac | 2024-11-22 08:34:45 | [diff] [blame] | 216 | detection_model_version = |
| 217 | translate_language_detection_model_->GetModelVersion(); |
Anthony Cui | c526ba7 | 2023-01-13 19:02:25 | [diff] [blame] | 218 | details.has_run_lang_detection = true; |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 219 | } else { |
Anthony Cui | e8788515 | 2023-11-29 22:16:31 | [diff] [blame] | 220 | // Use CLD3 and page contents to assist with language detection. |
mcrouse | e29a9f1 | 2021-02-17 19:06:29 | [diff] [blame] | 221 | language = DeterminePageLanguage( |
Zinovy Nis | fc466c15 | 2024-05-01 16:51:28 | [diff] [blame] | 222 | content_language, html_lang, contents->as_string(), |
| 223 | &model_detected_language, &is_model_reliable, model_reliability_score); |
mcrouse | e29a9f1 | 2021-02-17 19:06:29 | [diff] [blame] | 224 | detection_model_version = kCLDModelVersion; |
Anthony Cui | c526ba7 | 2023-01-13 19:02:25 | [diff] [blame] | 225 | details.has_run_lang_detection = true; |
Michael Crouse | 11e2a15 | 2020-12-12 20:50:34 | [diff] [blame] | 226 | } |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 227 | |
| 228 | if (language.empty()) |
| 229 | return; |
[email protected] | 93b9d69 | 2011-04-13 00:44:31 | [diff] [blame] | 230 | |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 231 | details.time = base::Time::Now(); |
adithyas | 5e61528 | 2017-01-17 21:27:01 | [diff] [blame] | 232 | details.url = web_detection_details.url; |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 233 | details.content_language = content_language; |
Michael Crouse | 0ebf0d5 | 2020-12-14 23:29:31 | [diff] [blame] | 234 | details.model_detected_language = model_detected_language; |
| 235 | details.is_model_reliable = is_model_reliable; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 236 | details.has_notranslate = web_detection_details.has_no_translate_meta; |
[email protected] | 20efbf2f | 2013-05-31 06:42:37 | [diff] [blame] | 237 | details.html_root_language = html_lang; |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 238 | details.adopted_language = language; |
mcrouse | e29a9f1 | 2021-02-17 19:06:29 | [diff] [blame] | 239 | details.model_reliability_score = model_reliability_score; |
| 240 | details.detection_model_version = detection_model_version; |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 241 | |
[email protected] | d861d97 | 2013-05-30 06:02:31 | [diff] [blame] | 242 | // TODO(hajimehoshi): If this affects performance, it should be set only if |
| 243 | // translate-internals tab exists. |
Zinovy Nis | fc466c15 | 2024-05-01 16:51:28 | [diff] [blame] | 244 | details.contents = contents->as_string(); |
[email protected] | d861d97 | 2013-05-30 06:02:31 | [diff] [blame] | 245 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 246 | // For the same render frame with the same url, each time when its texts are |
| 247 | // captured, it should be treated as a new page to do translation. |
leon.han | b0780131 | 2016-08-12 11:47:57 | [diff] [blame] | 248 | ResetPage(); |
Vlad Batalov | 76821bcd | 2024-03-07 00:35:05 | [diff] [blame] | 249 | |
| 250 | last_details_ = std::move(details); |
| 251 | RenewPageRegistration(); |
| 252 | } |
| 253 | |
| 254 | void TranslateAgent::RenewPageRegistration() { |
| 255 | if (!last_details_.has_value()) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 260 | if (!main_frame) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | LanguageDetectionDetails details = std::move(*last_details_); |
| 265 | |
| 266 | ResetPage(); |
Michael Martis | 088a00a | 2018-04-12 09:33:05 | [diff] [blame] | 267 | GetTranslateHandler()->RegisterPage( |
Julie Jeongeun Kim | 9b734c5 | 2019-09-25 16:19:56 | [diff] [blame] | 268 | receiver_.BindNewPipeAndPassRemote( |
| 269 | main_frame->GetTaskRunner(blink::TaskType::kInternalTranslation)), |
Vlad Batalov | 76821bcd | 2024-03-07 00:35:05 | [diff] [blame] | 270 | details, !details.has_notranslate && !details.adopted_language.empty()); |
| 271 | |
| 272 | last_details_ = std::move(details); |
[email protected] | 93b9d69 | 2011-04-13 00:44:31 | [diff] [blame] | 273 | } |
| 274 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 275 | void TranslateAgent::CancelPendingTranslation() { |
[email protected] | 81c9c66 | 2011-10-12 04:27:01 | [diff] [blame] | 276 | weak_method_factory_.InvalidateWeakPtrs(); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 277 | // Make sure to send the cancelled response back. |
| 278 | if (translate_callback_pending_) { |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 279 | std::move(translate_callback_pending_) |
| 280 | .Run(true, source_lang_, target_lang_, TranslateErrors::NONE); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 281 | } |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 282 | source_lang_.clear(); |
| 283 | target_lang_.clear(); |
| 284 | } |
| 285 | |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 286 | //////////////////////////////////////////////////////////////////////////////// |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 287 | // TranslateAgent, protected: |
| 288 | bool TranslateAgent::IsTranslateLibAvailable() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 289 | return ExecuteScriptAndGetBoolResult( |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 290 | "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 291 | "typeof cr.googleTranslate.translate == 'function'", |
| 292 | false); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 293 | } |
| 294 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 295 | bool TranslateAgent::IsTranslateLibReady() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 296 | return ExecuteScriptAndGetBoolResult("cr.googleTranslate.libReady", false); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 297 | } |
| 298 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 299 | bool TranslateAgent::HasTranslationFinished() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 300 | return ExecuteScriptAndGetBoolResult("cr.googleTranslate.finished", true); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 301 | } |
| 302 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 303 | bool TranslateAgent::HasTranslationFailed() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 304 | return ExecuteScriptAndGetBoolResult("cr.googleTranslate.error", true); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 305 | } |
| 306 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 307 | int64_t TranslateAgent::GetErrorCode() { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 308 | int64_t error_code = |
| 309 | ExecuteScriptAndGetIntegerResult("cr.googleTranslate.errorCode"); |
| 310 | DCHECK_LT(error_code, static_cast<int>(TranslateErrors::TRANSLATE_ERROR_MAX)); |
| 311 | return error_code; |
| 312 | } |
| 313 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 314 | bool TranslateAgent::StartTranslation() { |
Jon Napper | a7dc1b00 | 2017-06-26 01:01:21 | [diff] [blame] | 315 | return ExecuteScriptAndGetBoolResult( |
| 316 | BuildTranslationScript(source_lang_, target_lang_), false); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 317 | } |
| 318 | |
Evan Stade | 157a921 | 2021-04-20 00:09:14 | [diff] [blame] | 319 | std::string TranslateAgent::GetPageSourceLanguage() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 320 | return ExecuteScriptAndGetStringResult("cr.googleTranslate.sourceLang"); |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 321 | } |
| 322 | |
Doug Arnett | c1436ac | 2020-04-08 03:10:05 | [diff] [blame] | 323 | base::TimeDelta TranslateAgent::AdjustDelay(int delay_in_milliseconds) { |
| 324 | // Just converts |delay_in_milliseconds| without any modification in practical |
| 325 | // cases. Tests will override this function to return modified value. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 326 | return base::Milliseconds(delay_in_milliseconds); |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 327 | } |
| 328 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 329 | void TranslateAgent::ExecuteScript(const std::string& script) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 330 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 331 | if (!main_frame) |
| 332 | return; |
| 333 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 334 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Dave Tapuska | 306a7240 | 2021-04-09 14:20:02 | [diff] [blame] | 335 | main_frame->ExecuteScriptInIsolatedWorld( |
| 336 | world_id_, source, blink::BackForwardCacheAware::kAllow); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 337 | } |
| 338 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 339 | bool TranslateAgent::ExecuteScriptAndGetBoolResult(const std::string& script, |
| 340 | bool fallback) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 341 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 342 | if (!main_frame) |
| 343 | return fallback; |
| 344 | |
Dave Tapuska | b8035cb | 2022-09-09 15:12:34 | [diff] [blame] | 345 | v8::HandleScope handle_scope(main_frame->GetAgentGroupScheduler()->Isolate()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 346 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 347 | v8::Local<v8::Value> result = |
Dave Tapuska | 306a7240 | 2021-04-09 14:20:02 | [diff] [blame] | 348 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue( |
| 349 | world_id_, source, blink::BackForwardCacheAware::kAllow); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 350 | if (result.IsEmpty() || !result->IsBoolean()) { |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 351 | return fallback; |
| 352 | } |
| 353 | |
Ross McIlroy | 1c8eb14 | 2018-08-01 15:12:07 | [diff] [blame] | 354 | return result.As<v8::Boolean>()->Value(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 355 | } |
| 356 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 357 | std::string TranslateAgent::ExecuteScriptAndGetStringResult( |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 358 | const std::string& script) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 359 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 360 | if (!main_frame) |
| 361 | return std::string(); |
| 362 | |
Dave Tapuska | b8035cb | 2022-09-09 15:12:34 | [diff] [blame] | 363 | v8::Isolate* isolate = main_frame->GetAgentGroupScheduler()->Isolate(); |
Dan Elphick | c027e8ae | 2018-07-27 10:10:10 | [diff] [blame] | 364 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 365 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 366 | v8::Local<v8::Value> result = |
Dave Tapuska | 306a7240 | 2021-04-09 14:20:02 | [diff] [blame] | 367 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue( |
| 368 | world_id_, source, blink::BackForwardCacheAware::kAllow); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 369 | if (result.IsEmpty() || !result->IsString()) { |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 370 | return std::string(); |
| 371 | } |
| 372 | |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 373 | v8::Local<v8::String> v8_str = result.As<v8::String>(); |
Samuel Groß | 4fb6b66f | 2024-11-18 11:06:49 | [diff] [blame] | 374 | size_t length = v8_str->Utf8LengthV2(isolate); |
| 375 | if (length == 0) { |
Scott Little | f130e8e8 | 2020-08-07 16:01:37 | [diff] [blame] | 376 | return std::string(); |
Samuel Groß | 4fb6b66f | 2024-11-18 11:06:49 | [diff] [blame] | 377 | } |
Scott Little | f130e8e8 | 2020-08-07 16:01:37 | [diff] [blame] | 378 | |
Samuel Groß | 4fb6b66f | 2024-11-18 11:06:49 | [diff] [blame] | 379 | std::string str(length, '\0'); |
| 380 | v8_str->WriteUtf8V2(isolate, str.data(), length); |
Scott Little | f130e8e8 | 2020-08-07 16:01:37 | [diff] [blame] | 381 | return str; |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 382 | } |
| 383 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 384 | double TranslateAgent::ExecuteScriptAndGetDoubleResult( |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 385 | const std::string& script) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 386 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 387 | if (!main_frame) |
| 388 | return 0.0; |
| 389 | |
Dave Tapuska | b8035cb | 2022-09-09 15:12:34 | [diff] [blame] | 390 | v8::HandleScope handle_scope(main_frame->GetAgentGroupScheduler()->Isolate()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 391 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 392 | v8::Local<v8::Value> result = |
Dave Tapuska | 306a7240 | 2021-04-09 14:20:02 | [diff] [blame] | 393 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue( |
| 394 | world_id_, source, blink::BackForwardCacheAware::kAllow); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 395 | if (result.IsEmpty() || !result->IsNumber()) { |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 396 | return 0.0; |
| 397 | } |
| 398 | |
Ross McIlroy | 1c8eb14 | 2018-08-01 15:12:07 | [diff] [blame] | 399 | return result.As<v8::Number>()->Value(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 400 | } |
| 401 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 402 | int64_t TranslateAgent::ExecuteScriptAndGetIntegerResult( |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 403 | const std::string& script) { |
| 404 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 405 | if (!main_frame) |
| 406 | return 0; |
| 407 | |
Dave Tapuska | b8035cb | 2022-09-09 15:12:34 | [diff] [blame] | 408 | v8::HandleScope handle_scope(main_frame->GetAgentGroupScheduler()->Isolate()); |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 409 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 410 | v8::Local<v8::Value> result = |
Dave Tapuska | 306a7240 | 2021-04-09 14:20:02 | [diff] [blame] | 411 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue( |
| 412 | world_id_, source, blink::BackForwardCacheAware::kAllow); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 413 | if (result.IsEmpty() || !result->IsNumber()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
Ross McIlroy | 1c8eb14 | 2018-08-01 15:12:07 | [diff] [blame] | 417 | return result.As<v8::Integer>()->Value(); |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 418 | } |
| 419 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 420 | // mojom::TranslateAgent implementations. |
| 421 | void TranslateAgent::TranslateFrame(const std::string& translate_script, |
| 422 | const std::string& source_lang, |
| 423 | const std::string& target_lang, |
| 424 | TranslateFrameCallback callback) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 425 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 426 | if (!main_frame) { |
| 427 | // Cancelled. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 428 | std::move(callback).Run(true, source_lang, target_lang, |
| 429 | TranslateErrors::NONE); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 430 | return; // We navigated away, nothing to do. |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 431 | } |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 432 | |
[email protected] | 4a2387e | 2013-05-31 03:47:56 | [diff] [blame] | 433 | // A similar translation is already under way, nothing to do. |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 434 | if (translate_callback_pending_ && target_lang_ == target_lang) { |
| 435 | // This request is ignored. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 436 | std::move(callback).Run(true, source_lang, target_lang, |
| 437 | TranslateErrors::NONE); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 438 | return; |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 439 | } |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 440 | |
| 441 | // Any pending translation is now irrelevant. |
| 442 | CancelPendingTranslation(); |
| 443 | |
| 444 | // Set our states. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 445 | translate_callback_pending_ = std::move(callback); |
[email protected] | 4a2387e | 2013-05-31 03:47:56 | [diff] [blame] | 446 | |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 447 | // If the source language is undetermined, we'll let the translate element |
| 448 | // detect it. |
Fergal Daly | fe7cf25 | 2025-01-20 02:28:42 | [diff] [blame] | 449 | source_lang_ = (source_lang != language_detection::kUnknownLanguageCode) |
| 450 | ? source_lang |
| 451 | : kAutoDetectionLanguage; |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 452 | target_lang_ = target_lang; |
| 453 | |
Karandeep Bhatia | 4a8b435 | 2020-07-16 06:57:13 | [diff] [blame] | 454 | // Set up v8 isolated world. |
| 455 | EnsureIsolatedWorldInitialized(world_id_); |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 456 | |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 457 | if (!IsTranslateLibAvailable()) { |
| 458 | // Evaluate the script to add the translation related method to the global |
| 459 | // context of the page. |
| 460 | ExecuteScript(translate_script); |
| 461 | DCHECK(IsTranslateLibAvailable()); |
| 462 | } |
| 463 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 464 | TranslatePageImpl(0); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 465 | } |
| 466 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 467 | void TranslateAgent::RevertTranslation() { |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 468 | if (!IsTranslateLibAvailable()) { |
Peter Boström | cb0d5306 | 2024-06-04 18:45:31 | [diff] [blame] | 469 | DUMP_WILL_BE_NOTREACHED(); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 473 | CancelPendingTranslation(); |
| 474 | |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 475 | ExecuteScript("cr.googleTranslate.revert()"); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 476 | } |
| 477 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 478 | //////////////////////////////////////////////////////////////////////////////// |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 479 | // TranslateAgent, private: |
| 480 | void TranslateAgent::CheckTranslateStatus() { |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 481 | // First check if there was an error. |
| 482 | if (HasTranslationFailed()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 483 | NotifyBrowserTranslationFailed( |
Sylvain Defresne | de305ac | 2022-09-01 13:15:37 | [diff] [blame] | 484 | static_cast<translate::TranslateErrors>(GetErrorCode())); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 485 | return; // There was an error. |
| 486 | } |
| 487 | |
| 488 | if (HasTranslationFinished()) { |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 489 | std::string actual_source_lang; |
| 490 | // Translation was successfull, if it was auto, retrieve the source |
| 491 | // language the Translate Element detected. |
| 492 | if (source_lang_ == kAutoDetectionLanguage) { |
Evan Stade | 157a921 | 2021-04-20 00:09:14 | [diff] [blame] | 493 | actual_source_lang = GetPageSourceLanguage(); |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 494 | if (actual_source_lang.empty()) { |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 495 | NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); |
[email protected] | 579317e | 2010-06-30 20:13:48 | [diff] [blame] | 496 | return; |
| 497 | } else if (actual_source_lang == target_lang_) { |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 498 | NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | } else { |
| 502 | actual_source_lang = source_lang_; |
| 503 | } |
| 504 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 505 | if (!translate_callback_pending_) { |
Peter Boström | 77d2135 | 2024-11-13 22:26:11 | [diff] [blame] | 506 | NOTREACHED(); |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 507 | } |
| 508 | |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 509 | // Check JavaScript performance counters for UMA reports. |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 510 | ReportTimeToTranslate( |
[email protected] | 3a2ea8f | 2013-05-13 18:40:15 | [diff] [blame] | 511 | ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); |
Megan Jablonski | 2778578 | 2021-06-23 23:08:23 | [diff] [blame] | 512 | ReportTranslatedLanguageDetectionContentLength(page_contents_length_); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 513 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 514 | // Notify the browser we are done. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 515 | std::move(translate_callback_pending_) |
| 516 | .Run(false, actual_source_lang, target_lang_, TranslateErrors::NONE); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | |
| 520 | // The translation is still pending, check again later. |
Karolina Soltys | c7fbc03 | 2018-10-25 10:10:08 | [diff] [blame] | 521 | translate_task_runner_->PostDelayedTask( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 522 | FROM_HERE, |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 523 | base::BindOnce(&TranslateAgent::CheckTranslateStatus, |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 524 | weak_method_factory_.GetWeakPtr()), |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 525 | AdjustDelay(kTranslateStatusCheckDelayMs)); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 526 | } |
| 527 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 528 | void TranslateAgent::TranslatePageImpl(int count) { |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 529 | DCHECK_LT(count, kMaxTranslateInitCheckAttempts); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 530 | if (!IsTranslateLibReady()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 531 | // There was an error during initialization of library. |
Sylvain Defresne | de305ac | 2022-09-01 13:15:37 | [diff] [blame] | 532 | TranslateErrors error = |
| 533 | static_cast<translate::TranslateErrors>(GetErrorCode()); |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 534 | if (error != TranslateErrors::NONE) { |
| 535 | NotifyBrowserTranslationFailed(error); |
| 536 | return; |
| 537 | } |
| 538 | |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 539 | // The library is not ready, try again later, unless we have tried several |
thestig | 259626c | 2015-11-23 20:18:11 | [diff] [blame] | 540 | // times unsuccessfully already. |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 541 | if (++count >= kMaxTranslateInitCheckAttempts) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 542 | NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_TIMEOUT); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 543 | return; |
| 544 | } |
Hajime Hoshi | 701c6e1 | 2019-03-19 15:19:18 | [diff] [blame] | 545 | translate_task_runner_->PostDelayedTask( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 546 | FROM_HERE, |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 547 | base::BindOnce(&TranslateAgent::TranslatePageImpl, |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 548 | weak_method_factory_.GetWeakPtr(), count), |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 549 | AdjustDelay(count * kTranslateInitCheckDelayMs)); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 550 | return; |
| 551 | } |
| 552 | |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 553 | // The library is loaded, and ready for translation now. |
| 554 | // Check JavaScript performance counters for UMA reports. |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 555 | ReportTimeToBeReady( |
[email protected] | 3a2ea8f | 2013-05-13 18:40:15 | [diff] [blame] | 556 | ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 557 | ReportTimeToLoad( |
[email protected] | 3a2ea8f | 2013-05-13 18:40:15 | [diff] [blame] | 558 | ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 559 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 560 | if (!StartTranslation()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 561 | CheckTranslateStatus(); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | // Check the status of the translation. |
Karolina Soltys | c7fbc03 | 2018-10-25 10:10:08 | [diff] [blame] | 565 | translate_task_runner_->PostDelayedTask( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 566 | FROM_HERE, |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 567 | base::BindOnce(&TranslateAgent::CheckTranslateStatus, |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 568 | weak_method_factory_.GetWeakPtr()), |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 569 | AdjustDelay(kTranslateStatusCheckDelayMs)); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 570 | } |
| 571 | |
Sylvain Defresne | de305ac | 2022-09-01 13:15:37 | [diff] [blame] | 572 | void TranslateAgent::NotifyBrowserTranslationFailed(TranslateErrors error) { |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 573 | DCHECK(translate_callback_pending_); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 574 | // Notify the browser there was an error. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 575 | std::move(translate_callback_pending_) |
| 576 | .Run(false, source_lang_, target_lang_, error); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 577 | } |
| 578 | |
Julie Jeongeun Kim | b1afbe5 | 2019-09-26 02:39:48 | [diff] [blame] | 579 | const mojo::Remote<mojom::ContentTranslateDriver>& |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 580 | TranslateAgent::GetTranslateHandler() { |
Fergal Daly | cebc825 | 2024-07-29 18:50:52 | [diff] [blame] | 581 | if (translate_handler_) { |
| 582 | if (translate_handler_.is_connected()) { |
| 583 | return translate_handler_; |
| 584 | } |
| 585 | // The translate handler can become unbound or disconnected in testing |
| 586 | // so this catches that case and reconnects so `this` can connect to |
| 587 | // the driver in the browser. |
| 588 | translate_handler_.reset(); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 589 | } |
| 590 | |
Lukasz Anforowicz | 21b92848 | 2024-06-26 17:02:51 | [diff] [blame] | 591 | render_frame()->GetBrowserInterfaceBroker().GetInterface( |
mcrouse | f0c2fb00 | 2021-10-13 00:40:48 | [diff] [blame] | 592 | translate_handler_.BindNewPipeAndPassReceiver()); |
Michael Martis | 088a00a | 2018-04-12 09:33:05 | [diff] [blame] | 593 | return translate_handler_; |
[email protected] | 7faa5386 | 2010-06-16 00:09:13 | [diff] [blame] | 594 | } |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 595 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 596 | void TranslateAgent::ResetPage() { |
Vlad Batalov | 76821bcd | 2024-03-07 00:35:05 | [diff] [blame] | 597 | last_details_ = {}; |
Julie Jeongeun Kim | 9b734c5 | 2019-09-25 16:19:56 | [diff] [blame] | 598 | receiver_.reset(); |
leon.han | b0780131 | 2016-08-12 11:47:57 | [diff] [blame] | 599 | translate_callback_pending_.Reset(); |
| 600 | CancelPendingTranslation(); |
| 601 | } |
| 602 | |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 603 | void TranslateAgent::OnDestruct() { |
xjz | 694b50a9 | 2016-06-07 21:49:37 | [diff] [blame] | 604 | delete this; |
| 605 | } |
| 606 | |
Jon Napper | a7dc1b00 | 2017-06-26 01:01:21 | [diff] [blame] | 607 | /* static */ |
Doug Arnett | 2fd6cf2 | 2020-01-27 19:18:40 | [diff] [blame] | 608 | std::string TranslateAgent::BuildTranslationScript( |
Jon Napper | a7dc1b00 | 2017-06-26 01:01:21 | [diff] [blame] | 609 | const std::string& source_lang, |
| 610 | const std::string& target_lang) { |
| 611 | return "cr.googleTranslate.translate(" + |
| 612 | base::GetQuotedJSONString(source_lang) + "," + |
| 613 | base::GetQuotedJSONString(target_lang) + ")"; |
| 614 | } |
| 615 | |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 616 | } // namespace translate |