| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/translate/translate_helper.h" | 5 #include "components/translate/content/renderer/translate_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/renderer/isolated_world_ids.h" | |
| 16 #include "components/translate/content/common/translate_messages.h" | 15 #include "components/translate/content/common/translate_messages.h" |
| 17 #include "components/translate/core/common/translate_constants.h" | 16 #include "components/translate/core/common/translate_constants.h" |
| 18 #include "components/translate/core/common/translate_metrics.h" | 17 #include "components/translate/core/common/translate_metrics.h" |
| 19 #include "components/translate/core/common/translate_util.h" | 18 #include "components/translate/core/common/translate_util.h" |
| 20 #include "components/translate/core/language_detection/language_detection_util.h
" | 19 #include "components/translate/core/language_detection/language_detection_util.h
" |
| 21 #include "content/public/common/content_constants.h" | 20 #include "content/public/common/content_constants.h" |
| 21 #include "content/public/common/url_constants.h" |
| 22 #include "content/public/renderer/render_thread.h" | 22 #include "content/public/renderer/render_thread.h" |
| 23 #include "content/public/renderer/render_view.h" | 23 #include "content/public/renderer/render_view.h" |
| 24 #include "extensions/common/constants.h" | |
| 25 #include "extensions/renderer/extension_groups.h" | |
| 26 #include "ipc/ipc_platform_file.h" | 24 #include "ipc/ipc_platform_file.h" |
| 27 #include "content/public/common/url_constants.h" | |
| 28 #include "third_party/WebKit/public/web/WebDocument.h" | 25 #include "third_party/WebKit/public/web/WebDocument.h" |
| 29 #include "third_party/WebKit/public/web/WebElement.h" | 26 #include "third_party/WebKit/public/web/WebElement.h" |
| 30 #include "third_party/WebKit/public/web/WebFrame.h" | 27 #include "third_party/WebKit/public/web/WebFrame.h" |
| 31 #include "third_party/WebKit/public/web/WebNode.h" | 28 #include "third_party/WebKit/public/web/WebNode.h" |
| 32 #include "third_party/WebKit/public/web/WebNodeList.h" | 29 #include "third_party/WebKit/public/web/WebNodeList.h" |
| 33 #include "third_party/WebKit/public/web/WebScriptSource.h" | 30 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 34 #include "third_party/WebKit/public/web/WebView.h" | 31 #include "third_party/WebKit/public/web/WebView.h" |
| 35 #include "third_party/WebKit/public/web/WebWidget.h" | 32 #include "third_party/WebKit/public/web/WebWidget.h" |
| 36 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 37 #include "v8/include/v8.h" | 34 #include "v8/include/v8.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 66 const char kAutoDetectionLanguage[] = "auto"; | 63 const char kAutoDetectionLanguage[] = "auto"; |
| 67 | 64 |
| 68 // Isolated world sets following content-security-policy. | 65 // Isolated world sets following content-security-policy. |
| 69 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; | 66 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; |
| 70 | 67 |
| 71 // Whether or not we have set the CLD callback yet. | 68 // Whether or not we have set the CLD callback yet. |
| 72 bool g_cld_callback_set = false; | 69 bool g_cld_callback_set = false; |
| 73 | 70 |
| 74 } // namespace | 71 } // namespace |
| 75 | 72 |
| 73 namespace translate { |
| 76 | 74 |
| 77 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
| 78 // TranslateHelper, public: | 76 // TranslateHelper, public: |
| 79 // | 77 // |
| 80 TranslateHelper::TranslateHelper(content::RenderView* render_view) | 78 TranslateHelper::TranslateHelper(content::RenderView* render_view, |
| 79 int world_id, |
| 80 int extension_group, |
| 81 const std::string& extension_scheme) |
| 81 : content::RenderViewObserver(render_view), | 82 : content::RenderViewObserver(render_view), |
| 82 page_seq_no_(0), | 83 page_seq_no_(0), |
| 83 translation_pending_(false), | 84 translation_pending_(false), |
| 84 weak_method_factory_(this), | 85 weak_method_factory_(this), |
| 85 cld_data_provider_(translate::CreateRendererCldDataProviderFor(this)), | 86 cld_data_provider_(CreateRendererCldDataProviderFor(this)), |
| 86 cld_data_polling_started_(false), | 87 cld_data_polling_started_(false), |
| 87 cld_data_polling_canceled_(false), | 88 cld_data_polling_canceled_(false), |
| 88 deferred_page_capture_(false), | 89 deferred_page_capture_(false), |
| 89 deferred_page_seq_no_(-1) { | 90 deferred_page_seq_no_(-1), |
| 91 world_id_(world_id), |
| 92 extension_group_(extension_group), |
| 93 extension_scheme_(extension_scheme) { |
| 90 } | 94 } |
| 91 | 95 |
| 92 TranslateHelper::~TranslateHelper() { | 96 TranslateHelper::~TranslateHelper() { |
| 93 CancelPendingTranslation(); | 97 CancelPendingTranslation(); |
| 94 CancelCldDataPolling(); | 98 CancelCldDataPolling(); |
| 95 } | 99 } |
| 96 | 100 |
| 97 void TranslateHelper::PrepareForUrl(const GURL& url) { | 101 void TranslateHelper::PrepareForUrl(const GURL& url) { |
| 98 ++page_seq_no_; | 102 ++page_seq_no_; |
| 99 Send(new ChromeViewHostMsg_TranslateAssignedSequenceNumber( | 103 Send(new ChromeViewHostMsg_TranslateAssignedSequenceNumber( |
| 100 routing_id(), page_seq_no_)); | 104 routing_id(), page_seq_no_)); |
| 101 deferred_page_capture_ = false; | 105 deferred_page_capture_ = false; |
| 102 deferred_page_seq_no_ = -1; | 106 deferred_page_seq_no_ = -1; |
| 103 deferred_contents_.clear(); | 107 deferred_contents_.clear(); |
| 104 if (cld_data_polling_started_) | 108 if (cld_data_polling_started_) |
| 105 return; | 109 return; |
| 106 | 110 |
| 107 // TODO(andrewhayden): Refactor translate_manager.cc's IsTranslatableURL to | 111 // TODO(andrewhayden): Refactor translate_manager.cc's IsTranslatableURL to |
| 108 // components/translate/core/common/translate_util.cc, and ignore any URL | 112 // components/translate/core/common/translate_util.cc, and ignore any URL |
| 109 // that fails that check. This will require moving unit tests and rewiring | 113 // that fails that check. This will require moving unit tests and rewiring |
| 110 // other function calls as well, so for now replicate the logic here. | 114 // other function calls as well, so for now replicate the logic here. |
| 111 if (url.is_empty()) | 115 if (url.is_empty()) |
| 112 return; | 116 return; |
| 113 if (url.SchemeIs(content::kChromeUIScheme)) | 117 if (url.SchemeIs(content::kChromeUIScheme)) |
| 114 return; | 118 return; |
| 115 if (url.SchemeIs(content::kChromeDevToolsScheme)) | 119 if (url.SchemeIs(content::kChromeDevToolsScheme)) |
| 116 return; | 120 return; |
| 117 if (url.SchemeIs(url::kFtpScheme)) | 121 if (url.SchemeIs(url::kFtpScheme)) |
| 118 return; | 122 return; |
| 119 if (url.SchemeIs(extensions::kExtensionScheme)) | 123 if (url.SchemeIs(extension_scheme_.c_str())) |
| 120 return; | 124 return; |
| 121 | 125 |
| 122 // Start polling for CLD data. | 126 // Start polling for CLD data. |
| 123 cld_data_polling_started_ = true; | 127 cld_data_polling_started_ = true; |
| 124 TranslateHelper::SendCldDataRequest(0, 1000); | 128 TranslateHelper::SendCldDataRequest(0, 1000); |
| 125 } | 129 } |
| 126 | 130 |
| 127 void TranslateHelper::PageCaptured(const base::string16& contents) { | 131 void TranslateHelper::PageCaptured(const base::string16& contents) { |
| 128 PageCapturedImpl(page_seq_no_, contents); | 132 PageCapturedImpl(page_seq_no_, contents); |
| 129 } | 133 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 WebDocument document = main_frame->document(); | 169 WebDocument document = main_frame->document(); |
| 166 std::string content_language = document.contentLanguage().utf8(); | 170 std::string content_language = document.contentLanguage().utf8(); |
| 167 WebElement html_element = document.documentElement(); | 171 WebElement html_element = document.documentElement(); |
| 168 std::string html_lang; | 172 std::string html_lang; |
| 169 // |html_element| can be null element, e.g. in | 173 // |html_element| can be null element, e.g. in |
| 170 // BrowserTest.WindowOpenClose. | 174 // BrowserTest.WindowOpenClose. |
| 171 if (!html_element.isNull()) | 175 if (!html_element.isNull()) |
| 172 html_lang = html_element.getAttribute("lang").utf8(); | 176 html_lang = html_element.getAttribute("lang").utf8(); |
| 173 std::string cld_language; | 177 std::string cld_language; |
| 174 bool is_cld_reliable; | 178 bool is_cld_reliable; |
| 175 std::string language = translate::DeterminePageLanguage( | 179 std::string language = DeterminePageLanguage( |
| 176 content_language, html_lang, contents, &cld_language, &is_cld_reliable); | 180 content_language, html_lang, contents, &cld_language, &is_cld_reliable); |
| 177 | 181 |
| 178 if (language.empty()) | 182 if (language.empty()) |
| 179 return; | 183 return; |
| 180 | 184 |
| 181 language_determined_time_ = base::TimeTicks::Now(); | 185 language_determined_time_ = base::TimeTicks::Now(); |
| 182 | 186 |
| 183 GURL url(document.url()); | 187 GURL url(document.url()); |
| 184 translate::LanguageDetectionDetails details; | 188 LanguageDetectionDetails details; |
| 185 details.time = base::Time::Now(); | 189 details.time = base::Time::Now(); |
| 186 details.url = url; | 190 details.url = url; |
| 187 details.content_language = content_language; | 191 details.content_language = content_language; |
| 188 details.cld_language = cld_language; | 192 details.cld_language = cld_language; |
| 189 details.is_cld_reliable = is_cld_reliable; | 193 details.is_cld_reliable = is_cld_reliable; |
| 190 details.html_root_language = html_lang; | 194 details.html_root_language = html_lang; |
| 191 details.adopted_language = language; | 195 details.adopted_language = language; |
| 192 | 196 |
| 193 // TODO(hajimehoshi): If this affects performance, it should be set only if | 197 // TODO(hajimehoshi): If this affects performance, it should be set only if |
| 194 // translate-internals tab exists. | 198 // translate-internals tab exists. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 return base::TimeDelta::FromMilliseconds(delayInMs); | 252 return base::TimeDelta::FromMilliseconds(delayInMs); |
| 249 } | 253 } |
| 250 | 254 |
| 251 void TranslateHelper::ExecuteScript(const std::string& script) { | 255 void TranslateHelper::ExecuteScript(const std::string& script) { |
| 252 WebFrame* main_frame = GetMainFrame(); | 256 WebFrame* main_frame = GetMainFrame(); |
| 253 if (!main_frame) | 257 if (!main_frame) |
| 254 return; | 258 return; |
| 255 | 259 |
| 256 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 260 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); |
| 257 main_frame->executeScriptInIsolatedWorld( | 261 main_frame->executeScriptInIsolatedWorld( |
| 258 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 262 world_id_, &source, 1, extension_group_); |
| 259 &source, | |
| 260 1, | |
| 261 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS); | |
| 262 } | 263 } |
| 263 | 264 |
| 264 bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, | 265 bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, |
| 265 bool fallback) { | 266 bool fallback) { |
| 266 WebFrame* main_frame = GetMainFrame(); | 267 WebFrame* main_frame = GetMainFrame(); |
| 267 if (!main_frame) | 268 if (!main_frame) |
| 268 return fallback; | 269 return fallback; |
| 269 | 270 |
| 270 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 271 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 271 WebVector<v8::Local<v8::Value> > results; | 272 WebVector<v8::Local<v8::Value> > results; |
| 272 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 273 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); |
| 273 main_frame->executeScriptInIsolatedWorld( | 274 main_frame->executeScriptInIsolatedWorld( |
| 274 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 275 world_id_, &source, 1, extension_group_, &results); |
| 275 &source, | |
| 276 1, | |
| 277 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, | |
| 278 &results); | |
| 279 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsBoolean()) { | 276 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsBoolean()) { |
| 280 NOTREACHED(); | 277 NOTREACHED(); |
| 281 return fallback; | 278 return fallback; |
| 282 } | 279 } |
| 283 | 280 |
| 284 return results[0]->BooleanValue(); | 281 return results[0]->BooleanValue(); |
| 285 } | 282 } |
| 286 | 283 |
| 287 std::string TranslateHelper::ExecuteScriptAndGetStringResult( | 284 std::string TranslateHelper::ExecuteScriptAndGetStringResult( |
| 288 const std::string& script) { | 285 const std::string& script) { |
| 289 WebFrame* main_frame = GetMainFrame(); | 286 WebFrame* main_frame = GetMainFrame(); |
| 290 if (!main_frame) | 287 if (!main_frame) |
| 291 return std::string(); | 288 return std::string(); |
| 292 | 289 |
| 293 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 290 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 294 WebVector<v8::Local<v8::Value> > results; | 291 WebVector<v8::Local<v8::Value> > results; |
| 295 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 292 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); |
| 296 main_frame->executeScriptInIsolatedWorld( | 293 main_frame->executeScriptInIsolatedWorld( |
| 297 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 294 world_id_, &source, 1, extension_group_, &results); |
| 298 &source, | |
| 299 1, | |
| 300 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, | |
| 301 &results); | |
| 302 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsString()) { | 295 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsString()) { |
| 303 NOTREACHED(); | 296 NOTREACHED(); |
| 304 return std::string(); | 297 return std::string(); |
| 305 } | 298 } |
| 306 | 299 |
| 307 v8::Local<v8::String> v8_str = results[0]->ToString(); | 300 v8::Local<v8::String> v8_str = results[0]->ToString(); |
| 308 int length = v8_str->Utf8Length() + 1; | 301 int length = v8_str->Utf8Length() + 1; |
| 309 scoped_ptr<char[]> str(new char[length]); | 302 scoped_ptr<char[]> str(new char[length]); |
| 310 v8_str->WriteUtf8(str.get(), length); | 303 v8_str->WriteUtf8(str.get(), length); |
| 311 return std::string(str.get()); | 304 return std::string(str.get()); |
| 312 } | 305 } |
| 313 | 306 |
| 314 double TranslateHelper::ExecuteScriptAndGetDoubleResult( | 307 double TranslateHelper::ExecuteScriptAndGetDoubleResult( |
| 315 const std::string& script) { | 308 const std::string& script) { |
| 316 WebFrame* main_frame = GetMainFrame(); | 309 WebFrame* main_frame = GetMainFrame(); |
| 317 if (!main_frame) | 310 if (!main_frame) |
| 318 return 0.0; | 311 return 0.0; |
| 319 | 312 |
| 320 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 313 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 321 WebVector<v8::Local<v8::Value> > results; | 314 WebVector<v8::Local<v8::Value> > results; |
| 322 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 315 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); |
| 323 main_frame->executeScriptInIsolatedWorld( | 316 main_frame->executeScriptInIsolatedWorld( |
| 324 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 317 world_id_, &source, 1, extension_group_, &results); |
| 325 &source, | |
| 326 1, | |
| 327 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, | |
| 328 &results); | |
| 329 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { | 318 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { |
| 330 NOTREACHED(); | 319 NOTREACHED(); |
| 331 return 0.0; | 320 return 0.0; |
| 332 } | 321 } |
| 333 | 322 |
| 334 return results[0]->NumberValue(); | 323 return results[0]->NumberValue(); |
| 335 } | 324 } |
| 336 | 325 |
| 337 //////////////////////////////////////////////////////////////////////////////// | 326 //////////////////////////////////////////////////////////////////////////////// |
| 338 // TranslateHelper, private: | 327 // TranslateHelper, private: |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 return; | 390 return; |
| 402 | 391 |
| 403 // Any pending translation is now irrelevant. | 392 // Any pending translation is now irrelevant. |
| 404 CancelPendingTranslation(); | 393 CancelPendingTranslation(); |
| 405 | 394 |
| 406 // Set our states. | 395 // Set our states. |
| 407 translation_pending_ = true; | 396 translation_pending_ = true; |
| 408 | 397 |
| 409 // If the source language is undetermined, we'll let the translate element | 398 // If the source language is undetermined, we'll let the translate element |
| 410 // detect it. | 399 // detect it. |
| 411 source_lang_ = (source_lang != translate::kUnknownLanguageCode) ? | 400 source_lang_ = (source_lang != kUnknownLanguageCode) ? source_lang |
| 412 source_lang : kAutoDetectionLanguage; | 401 : kAutoDetectionLanguage; |
| 413 target_lang_ = target_lang; | 402 target_lang_ = target_lang; |
| 414 | 403 |
| 415 translate::ReportUserActionDuration(language_determined_time_, | 404 ReportUserActionDuration(language_determined_time_, base::TimeTicks::Now()); |
| 416 base::TimeTicks::Now()); | |
| 417 | 405 |
| 418 GURL url(main_frame->document().url()); | 406 GURL url(main_frame->document().url()); |
| 419 translate::ReportPageScheme(url.scheme()); | 407 ReportPageScheme(url.scheme()); |
| 420 | 408 |
| 421 // Set up v8 isolated world with proper content-security-policy and | 409 // Set up v8 isolated world with proper content-security-policy and |
| 422 // security-origin. | 410 // security-origin. |
| 423 WebFrame* frame = GetMainFrame(); | 411 WebFrame* frame = GetMainFrame(); |
| 424 if (frame) { | 412 if (frame) { |
| 425 frame->setIsolatedWorldContentSecurityPolicy( | 413 frame->setIsolatedWorldContentSecurityPolicy( |
| 426 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 414 world_id_, WebString::fromUTF8(kContentSecurityPolicy)); |
| 427 WebString::fromUTF8(kContentSecurityPolicy)); | |
| 428 | 415 |
| 429 GURL security_origin = translate::GetTranslateSecurityOrigin(); | 416 GURL security_origin = GetTranslateSecurityOrigin(); |
| 430 frame->setIsolatedWorldSecurityOrigin( | 417 frame->setIsolatedWorldSecurityOrigin( |
| 431 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 418 world_id_, WebSecurityOrigin::create(security_origin)); |
| 432 WebSecurityOrigin::create(security_origin)); | |
| 433 } | 419 } |
| 434 | 420 |
| 435 if (!IsTranslateLibAvailable()) { | 421 if (!IsTranslateLibAvailable()) { |
| 436 // Evaluate the script to add the translation related method to the global | 422 // Evaluate the script to add the translation related method to the global |
| 437 // context of the page. | 423 // context of the page. |
| 438 ExecuteScript(translate_script); | 424 ExecuteScript(translate_script); |
| 439 DCHECK(IsTranslateLibAvailable()); | 425 DCHECK(IsTranslateLibAvailable()); |
| 440 } | 426 } |
| 441 | 427 |
| 442 TranslatePageImpl(page_seq_no, 0); | 428 TranslatePageImpl(page_seq_no, 0); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 458 | 444 |
| 459 void TranslateHelper::CheckTranslateStatus(int page_seq_no) { | 445 void TranslateHelper::CheckTranslateStatus(int page_seq_no) { |
| 460 // If this is not the same page, the translation has been canceled. If the | 446 // If this is not the same page, the translation has been canceled. If the |
| 461 // view is gone, the page is closing. | 447 // view is gone, the page is closing. |
| 462 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) | 448 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) |
| 463 return; | 449 return; |
| 464 | 450 |
| 465 // First check if there was an error. | 451 // First check if there was an error. |
| 466 if (HasTranslationFailed()) { | 452 if (HasTranslationFailed()) { |
| 467 // TODO(toyoshim): Check |errorCode| of translate.js and notify it here. | 453 // TODO(toyoshim): Check |errorCode| of translate.js and notify it here. |
| 468 NotifyBrowserTranslationFailed( | 454 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); |
| 469 translate::TranslateErrors::TRANSLATION_ERROR); | |
| 470 return; // There was an error. | 455 return; // There was an error. |
| 471 } | 456 } |
| 472 | 457 |
| 473 if (HasTranslationFinished()) { | 458 if (HasTranslationFinished()) { |
| 474 std::string actual_source_lang; | 459 std::string actual_source_lang; |
| 475 // Translation was successfull, if it was auto, retrieve the source | 460 // Translation was successfull, if it was auto, retrieve the source |
| 476 // language the Translate Element detected. | 461 // language the Translate Element detected. |
| 477 if (source_lang_ == kAutoDetectionLanguage) { | 462 if (source_lang_ == kAutoDetectionLanguage) { |
| 478 actual_source_lang = GetOriginalPageLanguage(); | 463 actual_source_lang = GetOriginalPageLanguage(); |
| 479 if (actual_source_lang.empty()) { | 464 if (actual_source_lang.empty()) { |
| 480 NotifyBrowserTranslationFailed( | 465 NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); |
| 481 translate::TranslateErrors::UNKNOWN_LANGUAGE); | |
| 482 return; | 466 return; |
| 483 } else if (actual_source_lang == target_lang_) { | 467 } else if (actual_source_lang == target_lang_) { |
| 484 NotifyBrowserTranslationFailed( | 468 NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); |
| 485 translate::TranslateErrors::IDENTICAL_LANGUAGES); | |
| 486 return; | 469 return; |
| 487 } | 470 } |
| 488 } else { | 471 } else { |
| 489 actual_source_lang = source_lang_; | 472 actual_source_lang = source_lang_; |
| 490 } | 473 } |
| 491 | 474 |
| 492 if (!translation_pending_) { | 475 if (!translation_pending_) { |
| 493 NOTREACHED(); | 476 NOTREACHED(); |
| 494 return; | 477 return; |
| 495 } | 478 } |
| 496 | 479 |
| 497 translation_pending_ = false; | 480 translation_pending_ = false; |
| 498 | 481 |
| 499 // Check JavaScript performance counters for UMA reports. | 482 // Check JavaScript performance counters for UMA reports. |
| 500 translate::ReportTimeToTranslate( | 483 ReportTimeToTranslate( |
| 501 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); | 484 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); |
| 502 | 485 |
| 503 // Notify the browser we are done. | 486 // Notify the browser we are done. |
| 504 render_view()->Send( | 487 render_view()->Send( |
| 505 new ChromeViewHostMsg_PageTranslated(render_view()->GetRoutingID(), | 488 new ChromeViewHostMsg_PageTranslated(render_view()->GetRoutingID(), |
| 506 actual_source_lang, | 489 actual_source_lang, |
| 507 target_lang_, | 490 target_lang_, |
| 508 translate::TranslateErrors::NONE)); | 491 TranslateErrors::NONE)); |
| 509 return; | 492 return; |
| 510 } | 493 } |
| 511 | 494 |
| 512 // The translation is still pending, check again later. | 495 // The translation is still pending, check again later. |
| 513 base::MessageLoop::current()->PostDelayedTask( | 496 base::MessageLoop::current()->PostDelayedTask( |
| 514 FROM_HERE, | 497 FROM_HERE, |
| 515 base::Bind(&TranslateHelper::CheckTranslateStatus, | 498 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 516 weak_method_factory_.GetWeakPtr(), page_seq_no), | 499 weak_method_factory_.GetWeakPtr(), page_seq_no), |
| 517 AdjustDelay(kTranslateStatusCheckDelayMs)); | 500 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 518 } | 501 } |
| 519 | 502 |
| 520 void TranslateHelper::TranslatePageImpl(int page_seq_no, int count) { | 503 void TranslateHelper::TranslatePageImpl(int page_seq_no, int count) { |
| 521 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); | 504 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); |
| 522 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) | 505 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) |
| 523 return; | 506 return; |
| 524 | 507 |
| 525 if (!IsTranslateLibReady()) { | 508 if (!IsTranslateLibReady()) { |
| 526 // The library is not ready, try again later, unless we have tried several | 509 // The library is not ready, try again later, unless we have tried several |
| 527 // times unsucessfully already. | 510 // times unsucessfully already. |
| 528 if (++count >= kMaxTranslateInitCheckAttempts) { | 511 if (++count >= kMaxTranslateInitCheckAttempts) { |
| 529 NotifyBrowserTranslationFailed( | 512 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); |
| 530 translate::TranslateErrors::INITIALIZATION_ERROR); | |
| 531 return; | 513 return; |
| 532 } | 514 } |
| 533 base::MessageLoop::current()->PostDelayedTask( | 515 base::MessageLoop::current()->PostDelayedTask( |
| 534 FROM_HERE, | 516 FROM_HERE, |
| 535 base::Bind(&TranslateHelper::TranslatePageImpl, | 517 base::Bind(&TranslateHelper::TranslatePageImpl, |
| 536 weak_method_factory_.GetWeakPtr(), | 518 weak_method_factory_.GetWeakPtr(), |
| 537 page_seq_no, count), | 519 page_seq_no, count), |
| 538 AdjustDelay(count * kTranslateInitCheckDelayMs)); | 520 AdjustDelay(count * kTranslateInitCheckDelayMs)); |
| 539 return; | 521 return; |
| 540 } | 522 } |
| 541 | 523 |
| 542 // The library is loaded, and ready for translation now. | 524 // The library is loaded, and ready for translation now. |
| 543 // Check JavaScript performance counters for UMA reports. | 525 // Check JavaScript performance counters for UMA reports. |
| 544 translate::ReportTimeToBeReady( | 526 ReportTimeToBeReady( |
| 545 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); | 527 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); |
| 546 translate::ReportTimeToLoad( | 528 ReportTimeToLoad( |
| 547 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); | 529 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
| 548 | 530 |
| 549 if (!StartTranslation()) { | 531 if (!StartTranslation()) { |
| 550 NotifyBrowserTranslationFailed( | 532 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); |
| 551 translate::TranslateErrors::TRANSLATION_ERROR); | |
| 552 return; | 533 return; |
| 553 } | 534 } |
| 554 // Check the status of the translation. | 535 // Check the status of the translation. |
| 555 base::MessageLoop::current()->PostDelayedTask( | 536 base::MessageLoop::current()->PostDelayedTask( |
| 556 FROM_HERE, | 537 FROM_HERE, |
| 557 base::Bind(&TranslateHelper::CheckTranslateStatus, | 538 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 558 weak_method_factory_.GetWeakPtr(), page_seq_no), | 539 weak_method_factory_.GetWeakPtr(), page_seq_no), |
| 559 AdjustDelay(kTranslateStatusCheckDelayMs)); | 540 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 560 } | 541 } |
| 561 | 542 |
| 562 void TranslateHelper::NotifyBrowserTranslationFailed( | 543 void TranslateHelper::NotifyBrowserTranslationFailed( |
| 563 translate::TranslateErrors::Type error) { | 544 TranslateErrors::Type error) { |
| 564 translation_pending_ = false; | 545 translation_pending_ = false; |
| 565 // Notify the browser there was an error. | 546 // Notify the browser there was an error. |
| 566 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 547 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 567 render_view()->GetRoutingID(), source_lang_, target_lang_, error)); | 548 render_view()->GetRoutingID(), source_lang_, target_lang_, error)); |
| 568 } | 549 } |
| 569 | 550 |
| 570 WebFrame* TranslateHelper::GetMainFrame() { | 551 WebFrame* TranslateHelper::GetMainFrame() { |
| 571 WebView* web_view = render_view()->GetWebView(); | 552 WebView* web_view = render_view()->GetWebView(); |
| 572 | 553 |
| 573 // When the tab is going to be closed, the web_view can be NULL. | 554 // When the tab is going to be closed, the web_view can be NULL. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 // be DEFERRED until CLD is ready. In the latter case, CLD is in dynamic mode | 627 // be DEFERRED until CLD is ready. In the latter case, CLD is in dynamic mode |
| 647 // and may eventually become available, triggering the RESUMED event; after | 628 // and may eventually become available, triggering the RESUMED event; after |
| 648 // this, everything should start being ON_TIME. This should never run more | 629 // this, everything should start being ON_TIME. This should never run more |
| 649 // than twice in a page load, under any conditions. | 630 // than twice in a page load, under any conditions. |
| 650 // Also note that language detection is triggered off of a delay AFTER the | 631 // Also note that language detection is triggered off of a delay AFTER the |
| 651 // page load completed event has fired, making this very much off the critical | 632 // page load completed event has fired, making this very much off the critical |
| 652 // path. | 633 // path. |
| 653 content::RenderThread::Get()->UpdateHistograms( | 634 content::RenderThread::Get()->UpdateHistograms( |
| 654 content::kHistogramSynchronizerReservedSequenceNumber); | 635 content::kHistogramSynchronizerReservedSequenceNumber); |
| 655 } | 636 } |
| 637 |
| 638 } // namespace translate |
| OLD | NEW |