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