[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 | |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 5 | #include "components/translate/content/renderer/translate_helper.h" |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 6 | |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 81c9c66 | 2011-10-12 04:27:01 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
Jon Napper | a7dc1b00 | 2017-06-26 01:01:21 | [diff] [blame] | 11 | #include "base/json/string_escape.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 12 | #include "base/location.h" |
[email protected] | b682591 | 2012-03-09 23:09:10 | [diff] [blame] | 13 | #include "base/logging.h" |
asvitkine | a0f05db | 2015-06-16 21:45:46 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
[email protected] | a19a16d8 | 2013-06-11 17:45:12 | [diff] [blame] | 16 | #include "base/strings/string16.h" |
[email protected] | cbde0d9 | 2013-06-14 11:37:04 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
[email protected] | abfd149 | 2013-06-07 21:23:26 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 19 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | eba93c9 | 2014-01-07 17:34:17 | [diff] [blame] | 20 | #include "components/translate/core/common/translate_constants.h" |
| 21 | #include "components/translate/core/common/translate_metrics.h" |
| 22 | #include "components/translate/core/common/translate_util.h" |
[email protected] | bfa4c82a | 2014-05-13 23:45:02 | [diff] [blame] | 23 | #include "components/translate/core/language_detection/language_detection_util.h" |
[email protected] | 9ce23704 | 2014-07-17 18:09:56 | [diff] [blame] | 24 | #include "content/public/common/content_constants.h" |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 25 | #include "content/public/common/url_constants.h" |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 26 | #include "content/public/renderer/render_frame.h" |
[email protected] | 9ce23704 | 2014-07-17 18:09:56 | [diff] [blame] | 27 | #include "content/public/renderer/render_thread.h" |
rockot | 734fb66 | 2016-10-15 16:41:30 | [diff] [blame] | 28 | #include "services/service_manager/public/cpp/interface_provider.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 29 | #include "third_party/blink/public/web/web_document.h" |
| 30 | #include "third_party/blink/public/web/web_language_detection_details.h" |
| 31 | #include "third_party/blink/public/web/web_local_frame.h" |
| 32 | #include "third_party/blink/public/web/web_script_source.h" |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 33 | #include "url/gurl.h" |
[email protected] | 4d51d5bf | 2010-07-26 18:48:26 | [diff] [blame] | 34 | #include "v8/include/v8.h" |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 35 | |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 36 | using blink::WebDocument; |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 37 | using blink::WebLocalFrame; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 38 | using blink::WebScriptSource; |
| 39 | using blink::WebSecurityOrigin; |
| 40 | using blink::WebString; |
| 41 | using blink::WebVector; |
adithyas | 5e61528 | 2017-01-17 21:27:01 | [diff] [blame] | 42 | using blink::WebLanguageDetectionDetails; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 43 | |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 44 | namespace { |
| 45 | |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 46 | // 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] | 47 | // translate library injected in the page is ready. |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 48 | const int kTranslateInitCheckDelayMs = 150; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 49 | |
| 50 | // The maximum number of times we'll check to see if the translate library |
| 51 | // injected in the page is ready. |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 52 | const int kMaxTranslateInitCheckAttempts = 5; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 53 | |
| 54 | // The delay we wait in milliseconds before checking whether the translation has |
| 55 | // finished. |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 56 | const int kTranslateStatusCheckDelayMs = 400; |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 57 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 58 | // Language name passed to the Translate element for it to detect the language. |
[email protected] | 3328125 | 2013-04-15 14:46:49 | [diff] [blame] | 59 | const char kAutoDetectionLanguage[] = "auto"; |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 60 | |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 61 | // Isolated world sets following content-security-policy. |
| 62 | const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; |
| 63 | |
[email protected] | ff9b1c1 | 2013-02-07 11:40:13 | [diff] [blame] | 64 | } // namespace |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 65 | |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 66 | namespace translate { |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 67 | |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 68 | //////////////////////////////////////////////////////////////////////////////// |
| 69 | // TranslateHelper, public: |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 70 | TranslateHelper::TranslateHelper(content::RenderFrame* render_frame, |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 71 | int world_id, |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 72 | const std::string& extension_scheme) |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 73 | : content::RenderFrameObserver(render_frame), |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 74 | world_id_(world_id), |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 75 | extension_scheme_(extension_scheme), |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 76 | binding_(this), |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 77 | weak_method_factory_(this) {} |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 78 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 79 | TranslateHelper::~TranslateHelper() { |
| 80 | } |
| 81 | |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 82 | void TranslateHelper::PrepareForUrl(const GURL& url) { |
leon.han | b0780131 | 2016-08-12 11:47:57 | [diff] [blame] | 83 | // Navigated to a new url, reset current page translation. |
| 84 | ResetPage(); |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | ae758199 | 2014-06-25 21:31:44 | [diff] [blame] | 87 | void TranslateHelper::PageCaptured(const base::string16& contents) { |
[email protected] | 25689773 | 2012-10-16 19:35:05 | [diff] [blame] | 88 | // Get the document language as set by WebKit from the http-equiv |
| 89 | // meta tag for "content-language". This may or may not also |
| 90 | // have a value derived from the actual Content-Language HTTP |
| 91 | // header. The two actually have different meanings (despite the |
| 92 | // original intent of http-equiv to be an equivalent) with the former |
| 93 | // being the language of the document and the latter being the |
| 94 | // language of the intended audience (a distinction really only |
| 95 | // relevant for things like langauge textbooks). This distinction |
| 96 | // shouldn't affect translation. |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 97 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 98 | if (!main_frame) |
[email protected] | effa54c | 2013-05-28 11:14:23 | [diff] [blame] | 99 | return; |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 100 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 101 | WebDocument document = main_frame->GetDocument(); |
adithyas | 5e61528 | 2017-01-17 21:27:01 | [diff] [blame] | 102 | WebLanguageDetectionDetails web_detection_details = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 103 | WebLanguageDetectionDetails::CollectLanguageDetectionDetails(document); |
| 104 | std::string content_language = web_detection_details.content_language.Utf8(); |
| 105 | std::string html_lang = web_detection_details.html_language.Utf8(); |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 106 | std::string cld_language; |
| 107 | bool is_cld_reliable; |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 108 | std::string language = DeterminePageLanguage( |
[email protected] | effa54c | 2013-05-28 11:14:23 | [diff] [blame] | 109 | content_language, html_lang, contents, &cld_language, &is_cld_reliable); |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 110 | |
| 111 | if (language.empty()) |
| 112 | return; |
[email protected] | 93b9d69 | 2011-04-13 00:44:31 | [diff] [blame] | 113 | |
[email protected] | 0f6afb4 | 2013-05-17 03:44:47 | [diff] [blame] | 114 | language_determined_time_ = base::TimeTicks::Now(); |
| 115 | |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 116 | LanguageDetectionDetails details; |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 117 | details.time = base::Time::Now(); |
adithyas | 5e61528 | 2017-01-17 21:27:01 | [diff] [blame] | 118 | details.url = web_detection_details.url; |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 119 | details.content_language = content_language; |
| 120 | details.cld_language = cld_language; |
| 121 | details.is_cld_reliable = is_cld_reliable; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 122 | details.has_notranslate = web_detection_details.has_no_translate_meta; |
[email protected] | 20efbf2f | 2013-05-31 06:42:37 | [diff] [blame] | 123 | details.html_root_language = html_lang; |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 124 | details.adopted_language = language; |
| 125 | |
[email protected] | d861d97 | 2013-05-30 06:02:31 | [diff] [blame] | 126 | // TODO(hajimehoshi): If this affects performance, it should be set only if |
| 127 | // translate-internals tab exists. |
| 128 | details.contents = contents; |
| 129 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 130 | // For the same render frame with the same url, each time when its texts are |
| 131 | // captured, it should be treated as a new page to do translation. |
leon.han | b0780131 | 2016-08-12 11:47:57 | [diff] [blame] | 132 | ResetPage(); |
Ken Rockot | 1caed9e | 2017-06-07 17:35:19 | [diff] [blame] | 133 | mojom::PagePtr page; |
| 134 | binding_.Bind(mojo::MakeRequest(&page)); |
Michael Martis | 088a00a | 2018-04-12 09:33:05 | [diff] [blame] | 135 | GetTranslateHandler()->RegisterPage( |
Ken Rockot | 1caed9e | 2017-06-07 17:35:19 | [diff] [blame] | 136 | std::move(page), details, !details.has_notranslate && !language.empty()); |
[email protected] | 93b9d69 | 2011-04-13 00:44:31 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 139 | void TranslateHelper::CancelPendingTranslation() { |
[email protected] | 81c9c66 | 2011-10-12 04:27:01 | [diff] [blame] | 140 | weak_method_factory_.InvalidateWeakPtrs(); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 141 | // Make sure to send the cancelled response back. |
| 142 | if (translate_callback_pending_) { |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 143 | std::move(translate_callback_pending_) |
| 144 | .Run(true, source_lang_, target_lang_, TranslateErrors::NONE); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 145 | } |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 146 | source_lang_.clear(); |
| 147 | target_lang_.clear(); |
| 148 | } |
| 149 | |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 150 | //////////////////////////////////////////////////////////////////////////////// |
| 151 | // TranslateHelper, protected: |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 152 | bool TranslateHelper::IsTranslateLibAvailable() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 153 | return ExecuteScriptAndGetBoolResult( |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 154 | "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 155 | "typeof cr.googleTranslate.translate == 'function'", false); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool TranslateHelper::IsTranslateLibReady() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 159 | return ExecuteScriptAndGetBoolResult("cr.googleTranslate.libReady", false); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool TranslateHelper::HasTranslationFinished() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 163 | return ExecuteScriptAndGetBoolResult("cr.googleTranslate.finished", true); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool TranslateHelper::HasTranslationFailed() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 167 | return ExecuteScriptAndGetBoolResult("cr.googleTranslate.error", true); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 168 | } |
| 169 | |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 170 | int64_t TranslateHelper::GetErrorCode() { |
| 171 | int64_t error_code = |
| 172 | ExecuteScriptAndGetIntegerResult("cr.googleTranslate.errorCode"); |
| 173 | DCHECK_LT(error_code, static_cast<int>(TranslateErrors::TRANSLATE_ERROR_MAX)); |
| 174 | return error_code; |
| 175 | } |
| 176 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 177 | bool TranslateHelper::StartTranslation() { |
Jon Napper | a7dc1b00 | 2017-06-26 01:01:21 | [diff] [blame] | 178 | return ExecuteScriptAndGetBoolResult( |
| 179 | BuildTranslationScript(source_lang_, target_lang_), false); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 182 | std::string TranslateHelper::GetOriginalPageLanguage() { |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 183 | return ExecuteScriptAndGetStringResult("cr.googleTranslate.sourceLang"); |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 186 | base::TimeDelta TranslateHelper::AdjustDelay(int delayInMs) { |
| 187 | // Just converts |delayInMs| without any modification in practical cases. |
| 188 | // Tests will override this function to return modified value. |
| 189 | return base::TimeDelta::FromMilliseconds(delayInMs); |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 192 | void TranslateHelper::ExecuteScript(const std::string& script) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 193 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 194 | if (!main_frame) |
| 195 | return; |
| 196 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 197 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 198 | main_frame->ExecuteScriptInIsolatedWorld(world_id_, source); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, |
| 202 | bool fallback) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 203 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 204 | if (!main_frame) |
| 205 | return fallback; |
| 206 | |
[email protected] | dc3d06e | 2013-09-06 12:21:03 | [diff] [blame] | 207 | v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 208 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 209 | v8::Local<v8::Value> result = |
| 210 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue(world_id_, source); |
| 211 | if (result.IsEmpty() || !result->IsBoolean()) { |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 212 | NOTREACHED(); |
| 213 | return fallback; |
| 214 | } |
| 215 | |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 216 | return result->BooleanValue(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | std::string TranslateHelper::ExecuteScriptAndGetStringResult( |
| 220 | const std::string& script) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 221 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 222 | if (!main_frame) |
| 223 | return std::string(); |
| 224 | |
Dan Elphick | c027e8ae | 2018-07-27 10:10:10 | [diff] [blame] | 225 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 226 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 227 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 228 | v8::Local<v8::Value> result = |
| 229 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue(world_id_, source); |
| 230 | if (result.IsEmpty() || !result->IsString()) { |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 231 | NOTREACHED(); |
| 232 | return std::string(); |
| 233 | } |
| 234 | |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 235 | v8::Local<v8::String> v8_str = result.As<v8::String>(); |
Dan Elphick | c027e8ae | 2018-07-27 10:10:10 | [diff] [blame] | 236 | int length = v8_str->Utf8Length(isolate) + 1; |
danakj | 396f876c | 2016-04-22 23:04:39 | [diff] [blame] | 237 | std::unique_ptr<char[]> str(new char[length]); |
Dan Elphick | c027e8ae | 2018-07-27 10:10:10 | [diff] [blame] | 238 | v8_str->WriteUtf8(isolate, str.get(), length); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 239 | return std::string(str.get()); |
| 240 | } |
| 241 | |
| 242 | double TranslateHelper::ExecuteScriptAndGetDoubleResult( |
| 243 | const std::string& script) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 244 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 245 | if (!main_frame) |
| 246 | return 0.0; |
| 247 | |
[email protected] | dc3d06e | 2013-09-06 12:21:03 | [diff] [blame] | 248 | v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 249 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 250 | v8::Local<v8::Value> result = |
| 251 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue(world_id_, source); |
| 252 | if (result.IsEmpty() || !result->IsNumber()) { |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 253 | NOTREACHED(); |
| 254 | return 0.0; |
| 255 | } |
| 256 | |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 257 | return result->NumberValue(); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 258 | } |
| 259 | |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 260 | int64_t TranslateHelper::ExecuteScriptAndGetIntegerResult( |
| 261 | const std::string& script) { |
| 262 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 263 | if (!main_frame) |
| 264 | return 0; |
| 265 | |
| 266 | v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 267 | WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 268 | v8::Local<v8::Value> result = |
| 269 | main_frame->ExecuteScriptInIsolatedWorldAndReturnValue(world_id_, source); |
| 270 | if (result.IsEmpty() || !result->IsNumber()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 271 | NOTREACHED(); |
| 272 | return 0; |
| 273 | } |
| 274 | |
Hiroshige Hayashizaki | e1352e6 | 2018-05-14 23:03:06 | [diff] [blame] | 275 | return result->IntegerValue(); |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 276 | } |
| 277 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 278 | // mojom::Page implementations. |
| 279 | void TranslateHelper::Translate(const std::string& translate_script, |
| 280 | const std::string& source_lang, |
| 281 | const std::string& target_lang, |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 282 | TranslateCallback callback) { |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 283 | WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 284 | if (!main_frame) { |
| 285 | // Cancelled. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 286 | std::move(callback).Run(true, source_lang, target_lang, |
| 287 | TranslateErrors::NONE); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 288 | return; // We navigated away, nothing to do. |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 289 | } |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 290 | |
[email protected] | 4a2387e | 2013-05-31 03:47:56 | [diff] [blame] | 291 | // A similar translation is already under way, nothing to do. |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 292 | if (translate_callback_pending_ && target_lang_ == target_lang) { |
| 293 | // This request is ignored. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 294 | std::move(callback).Run(true, source_lang, target_lang, |
| 295 | TranslateErrors::NONE); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 296 | return; |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 297 | } |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 298 | |
| 299 | // Any pending translation is now irrelevant. |
| 300 | CancelPendingTranslation(); |
| 301 | |
| 302 | // Set our states. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 303 | translate_callback_pending_ = std::move(callback); |
[email protected] | 4a2387e | 2013-05-31 03:47:56 | [diff] [blame] | 304 | |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 305 | // If the source language is undetermined, we'll let the translate element |
| 306 | // detect it. |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 307 | source_lang_ = (source_lang != kUnknownLanguageCode) ? source_lang |
| 308 | : kAutoDetectionLanguage; |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 309 | target_lang_ = target_lang; |
| 310 | |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 311 | ReportUserActionDuration(language_determined_time_, base::TimeTicks::Now()); |
[email protected] | 0f6afb4 | 2013-05-17 03:44:47 | [diff] [blame] | 312 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 313 | GURL url(main_frame->GetDocument().Url()); |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 314 | ReportPageScheme(url.scheme()); |
[email protected] | 595ec51 | 2013-05-23 16:40:30 | [diff] [blame] | 315 | |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 316 | // Set up v8 isolated world with proper content-security-policy and |
| 317 | // security-origin. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 318 | main_frame->SetIsolatedWorldContentSecurityPolicy( |
| 319 | world_id_, WebString::FromUTF8(kContentSecurityPolicy)); |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 320 | |
dglazkov | 2481477 | 2015-09-25 22:27:47 | [diff] [blame] | 321 | GURL security_origin = GetTranslateSecurityOrigin(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 322 | main_frame->SetIsolatedWorldSecurityOrigin( |
| 323 | world_id_, WebSecurityOrigin::Create(security_origin)); |
[email protected] | afa9cd92 | 2013-07-30 06:12:19 | [diff] [blame] | 324 | |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 325 | if (!IsTranslateLibAvailable()) { |
| 326 | // Evaluate the script to add the translation related method to the global |
| 327 | // context of the page. |
| 328 | ExecuteScript(translate_script); |
| 329 | DCHECK(IsTranslateLibAvailable()); |
| 330 | } |
| 331 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 332 | TranslatePageImpl(0); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 333 | } |
| 334 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 335 | void TranslateHelper::RevertTranslation() { |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 336 | if (!IsTranslateLibAvailable()) { |
| 337 | NOTREACHED(); |
| 338 | return; |
| 339 | } |
| 340 | |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 341 | CancelPendingTranslation(); |
| 342 | |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 343 | ExecuteScript("cr.googleTranslate.revert()"); |
[email protected] | 8127362 | 2011-02-02 03:56:13 | [diff] [blame] | 344 | } |
| 345 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 346 | //////////////////////////////////////////////////////////////////////////////// |
| 347 | // TranslateHelper, private: |
| 348 | void TranslateHelper::CheckTranslateStatus() { |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 349 | // First check if there was an error. |
| 350 | if (HasTranslationFailed()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 351 | NotifyBrowserTranslationFailed( |
| 352 | static_cast<translate::TranslateErrors::Type>(GetErrorCode())); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 353 | return; // There was an error. |
| 354 | } |
| 355 | |
| 356 | if (HasTranslationFinished()) { |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 357 | std::string actual_source_lang; |
| 358 | // Translation was successfull, if it was auto, retrieve the source |
| 359 | // language the Translate Element detected. |
| 360 | if (source_lang_ == kAutoDetectionLanguage) { |
| 361 | actual_source_lang = GetOriginalPageLanguage(); |
| 362 | if (actual_source_lang.empty()) { |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 363 | NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); |
[email protected] | 579317e | 2010-06-30 20:13:48 | [diff] [blame] | 364 | return; |
| 365 | } else if (actual_source_lang == target_lang_) { |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 366 | NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | } else { |
| 370 | actual_source_lang = source_lang_; |
| 371 | } |
| 372 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 373 | if (!translate_callback_pending_) { |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 374 | NOTREACHED(); |
| 375 | return; |
| 376 | } |
| 377 | |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 378 | // Check JavaScript performance counters for UMA reports. |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 379 | ReportTimeToTranslate( |
[email protected] | 3a2ea8f | 2013-05-13 18:40:15 | [diff] [blame] | 380 | ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 381 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 382 | // Notify the browser we are done. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 383 | std::move(translate_callback_pending_) |
| 384 | .Run(false, actual_source_lang, target_lang_, TranslateErrors::NONE); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | // The translation is still pending, check again later. |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 389 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame^] | 390 | FROM_HERE, |
| 391 | base::BindOnce(&TranslateHelper::CheckTranslateStatus, |
| 392 | weak_method_factory_.GetWeakPtr()), |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 393 | AdjustDelay(kTranslateStatusCheckDelayMs)); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 394 | } |
| 395 | |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 396 | void TranslateHelper::TranslatePageImpl(int count) { |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 397 | DCHECK_LT(count, kMaxTranslateInitCheckAttempts); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 398 | if (!IsTranslateLibReady()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 399 | // There was an error during initialization of library. |
| 400 | TranslateErrors::Type error = |
| 401 | static_cast<translate::TranslateErrors::Type>(GetErrorCode()); |
| 402 | if (error != TranslateErrors::NONE) { |
| 403 | NotifyBrowserTranslationFailed(error); |
| 404 | return; |
| 405 | } |
| 406 | |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 407 | // The library is not ready, try again later, unless we have tried several |
thestig | 259626c | 2015-11-23 20:18:11 | [diff] [blame] | 408 | // times unsuccessfully already. |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 409 | if (++count >= kMaxTranslateInitCheckAttempts) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 410 | NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_TIMEOUT); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 411 | return; |
| 412 | } |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 413 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame^] | 414 | FROM_HERE, |
| 415 | base::BindOnce(&TranslateHelper::TranslatePageImpl, |
| 416 | weak_method_factory_.GetWeakPtr(), count), |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 417 | AdjustDelay(count * kTranslateInitCheckDelayMs)); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 418 | return; |
| 419 | } |
| 420 | |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 421 | // The library is loaded, and ready for translation now. |
| 422 | // Check JavaScript performance counters for UMA reports. |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 423 | ReportTimeToBeReady( |
[email protected] | 3a2ea8f | 2013-05-13 18:40:15 | [diff] [blame] | 424 | ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 425 | ReportTimeToLoad( |
[email protected] | 3a2ea8f | 2013-05-13 18:40:15 | [diff] [blame] | 426 | ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
[email protected] | e0be0099 | 2013-04-26 13:15:29 | [diff] [blame] | 427 | |
[email protected] | d64b07b | 2010-04-20 22:14:06 | [diff] [blame] | 428 | if (!StartTranslation()) { |
gajendra.n | 5bcb90a | 2017-07-05 09:02:52 | [diff] [blame] | 429 | CheckTranslateStatus(); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 430 | return; |
| 431 | } |
| 432 | // Check the status of the translation. |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 433 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame^] | 434 | FROM_HERE, |
| 435 | base::BindOnce(&TranslateHelper::CheckTranslateStatus, |
| 436 | weak_method_factory_.GetWeakPtr()), |
[email protected] | 0dfc0e5 | 2013-04-15 14:53:00 | [diff] [blame] | 437 | AdjustDelay(kTranslateStatusCheckDelayMs)); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void TranslateHelper::NotifyBrowserTranslationFailed( |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 441 | TranslateErrors::Type error) { |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 442 | DCHECK(translate_callback_pending_); |
[email protected] | 85d252e | 2010-04-06 22:21:02 | [diff] [blame] | 443 | // Notify the browser there was an error. |
tzik | 9a17d7af | 2017-05-09 14:21:58 | [diff] [blame] | 444 | std::move(translate_callback_pending_) |
| 445 | .Run(false, source_lang_, target_lang_, error); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 446 | } |
| 447 | |
Michael Martis | 088a00a | 2018-04-12 09:33:05 | [diff] [blame] | 448 | const mojom::ContentTranslateDriverPtr& TranslateHelper::GetTranslateHandler() { |
| 449 | if (!translate_handler_) { |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 450 | render_frame()->GetRemoteInterfaces()->GetInterface( |
Michael Martis | 088a00a | 2018-04-12 09:33:05 | [diff] [blame] | 451 | mojo::MakeRequest(&translate_handler_)); |
leon.han | 3134fc6 | 2016-08-10 07:25:58 | [diff] [blame] | 452 | } |
| 453 | |
Michael Martis | 088a00a | 2018-04-12 09:33:05 | [diff] [blame] | 454 | return translate_handler_; |
[email protected] | 7faa5386 | 2010-06-16 00:09:13 | [diff] [blame] | 455 | } |
[email protected] | e5a7a2a | 2014-03-27 16:16:03 | [diff] [blame] | 456 | |
leon.han | b0780131 | 2016-08-12 11:47:57 | [diff] [blame] | 457 | void TranslateHelper::ResetPage() { |
| 458 | binding_.Close(); |
| 459 | translate_callback_pending_.Reset(); |
| 460 | CancelPendingTranslation(); |
| 461 | } |
| 462 | |
xjz | 694b50a9 | 2016-06-07 21:49:37 | [diff] [blame] | 463 | void TranslateHelper::OnDestruct() { |
| 464 | delete this; |
| 465 | } |
| 466 | |
Jon Napper | a7dc1b00 | 2017-06-26 01:01:21 | [diff] [blame] | 467 | /* static */ |
| 468 | std::string TranslateHelper::BuildTranslationScript( |
| 469 | const std::string& source_lang, |
| 470 | const std::string& target_lang) { |
| 471 | return "cr.googleTranslate.translate(" + |
| 472 | base::GetQuotedJSONString(source_lang) + "," + |
| 473 | base::GetQuotedJSONString(target_lang) + ")"; |
| 474 | } |
| 475 | |
droger | f1da180 | 2014-09-17 14:54:05 | [diff] [blame] | 476 | } // namespace translate |