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