| 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 "chrome/renderer/translate/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/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/renderer/isolated_world_ids.h" | 15 #include "chrome/renderer/isolated_world_ids.h" |
| 15 #include "components/translate/content/common/translate_messages.h" | 16 #include "components/translate/content/common/translate_messages.h" |
| 16 #include "components/translate/core/common/translate_constants.h" | 17 #include "components/translate/core/common/translate_constants.h" |
| 17 #include "components/translate/core/common/translate_metrics.h" | 18 #include "components/translate/core/common/translate_metrics.h" |
| 18 #include "components/translate/core/common/translate_util.h" | 19 #include "components/translate/core/common/translate_util.h" |
| 19 #include "components/translate/core/language_detection/language_detection_util.h
" | 20 #include "components/translate/core/language_detection/language_detection_util.h
" |
| 21 #include "content/public/common/content_constants.h" |
| 22 #include "content/public/renderer/render_thread.h" |
| 20 #include "content/public/renderer/render_view.h" | 23 #include "content/public/renderer/render_view.h" |
| 21 #include "extensions/common/constants.h" | 24 #include "extensions/common/constants.h" |
| 22 #include "extensions/renderer/extension_groups.h" | 25 #include "extensions/renderer/extension_groups.h" |
| 23 #include "ipc/ipc_platform_file.h" | 26 #include "ipc/ipc_platform_file.h" |
| 24 #include "content/public/common/url_constants.h" | 27 #include "content/public/common/url_constants.h" |
| 25 #include "third_party/WebKit/public/web/WebDocument.h" | 28 #include "third_party/WebKit/public/web/WebDocument.h" |
| 26 #include "third_party/WebKit/public/web/WebElement.h" | 29 #include "third_party/WebKit/public/web/WebElement.h" |
| 27 #include "third_party/WebKit/public/web/WebFrame.h" | 30 #include "third_party/WebKit/public/web/WebFrame.h" |
| 28 #include "third_party/WebKit/public/web/WebNode.h" | 31 #include "third_party/WebKit/public/web/WebNode.h" |
| 29 #include "third_party/WebKit/public/web/WebNodeList.h" | 32 #include "third_party/WebKit/public/web/WebNodeList.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 // header. The two actually have different meanings (despite the | 136 // header. The two actually have different meanings (despite the |
| 134 // original intent of http-equiv to be an equivalent) with the former | 137 // original intent of http-equiv to be an equivalent) with the former |
| 135 // being the language of the document and the latter being the | 138 // being the language of the document and the latter being the |
| 136 // language of the intended audience (a distinction really only | 139 // language of the intended audience (a distinction really only |
| 137 // relevant for things like langauge textbooks). This distinction | 140 // relevant for things like langauge textbooks). This distinction |
| 138 // shouldn't affect translation. | 141 // shouldn't affect translation. |
| 139 WebFrame* main_frame = GetMainFrame(); | 142 WebFrame* main_frame = GetMainFrame(); |
| 140 if (!main_frame || page_seq_no_ != page_seq_no) | 143 if (!main_frame || page_seq_no_ != page_seq_no) |
| 141 return; | 144 return; |
| 142 | 145 |
| 143 // TODO(andrewhayden): UMA insertion point here: Track if data is available. | |
| 144 // TODO(andrewhayden): Retry insertion point here, retry till data available. | |
| 145 if (!cld_data_provider_->IsCldDataAvailable()) { | 146 if (!cld_data_provider_->IsCldDataAvailable()) { |
| 146 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data | 147 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data |
| 147 // is loaded, if ever. | 148 // is loaded, if ever. |
| 148 deferred_page_capture_ = true; | 149 deferred_page_capture_ = true; |
| 149 deferred_page_seq_no_ = page_seq_no; | 150 deferred_page_seq_no_ = page_seq_no; |
| 150 deferred_contents_ = contents; | 151 deferred_contents_ = contents; |
| 152 RecordLanguageDetectionTiming(DEFERRED); |
| 151 return; | 153 return; |
| 152 } | 154 } |
| 153 | 155 |
| 156 if (deferred_page_seq_no_ == -1) { |
| 157 // CLD data was available before language detection was requested. |
| 158 RecordLanguageDetectionTiming(ON_TIME); |
| 159 } else { |
| 160 // This is a request that was triggered because CLD data is now available |
| 161 // and was previously deferred. |
| 162 RecordLanguageDetectionTiming(RESUMED); |
| 163 } |
| 164 |
| 154 WebDocument document = main_frame->document(); | 165 WebDocument document = main_frame->document(); |
| 155 std::string content_language = document.contentLanguage().utf8(); | 166 std::string content_language = document.contentLanguage().utf8(); |
| 156 WebElement html_element = document.documentElement(); | 167 WebElement html_element = document.documentElement(); |
| 157 std::string html_lang; | 168 std::string html_lang; |
| 158 // |html_element| can be null element, e.g. in | 169 // |html_element| can be null element, e.g. in |
| 159 // BrowserTest.WindowOpenClose. | 170 // BrowserTest.WindowOpenClose. |
| 160 if (!html_element.isNull()) | 171 if (!html_element.isNull()) |
| 161 html_lang = html_element.getAttribute("lang").utf8(); | 172 html_lang = html_element.getAttribute("lang").utf8(); |
| 162 std::string cld_language; | 173 std::string cld_language; |
| 163 bool is_cld_reliable; | 174 bool is_cld_reliable; |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 } | 612 } |
| 602 | 613 |
| 603 void TranslateHelper::OnCldDataAvailable() { | 614 void TranslateHelper::OnCldDataAvailable() { |
| 604 if (deferred_page_capture_) { | 615 if (deferred_page_capture_) { |
| 605 deferred_page_capture_ = false; // Don't do this a second time. | 616 deferred_page_capture_ = false; // Don't do this a second time. |
| 606 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); | 617 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); |
| 607 deferred_page_seq_no_ = -1; // Clean up for sanity | 618 deferred_page_seq_no_ = -1; // Clean up for sanity |
| 608 deferred_contents_.clear(); // Clean up for sanity | 619 deferred_contents_.clear(); // Clean up for sanity |
| 609 } | 620 } |
| 610 } | 621 } |
| 622 |
| 623 void TranslateHelper::RecordLanguageDetectionTiming( |
| 624 LanguageDetectionTiming timing) { |
| 625 // The following comment is copied from page_load_histograms.cc, and applies |
| 626 // just as equally here: |
| 627 // |
| 628 // Since there are currently no guarantees that renderer histograms will be |
| 629 // sent to the browser, we initiate a PostTask here to be sure that we send |
| 630 // the histograms we generated. Without this call, pages that don't have an |
| 631 // on-close-handler might generate data that is lost when the renderer is |
| 632 // shutdown abruptly (perchance because the user closed the tab). |
| 633 DVLOG(1) << "Language detection timing: " << timing; |
| 634 UMA_HISTOGRAM_ENUMERATION("Translate.LanguageDetectionTiming", timing, |
| 635 LANGUAGE_DETECTION_TIMING_MAX_VALUE); |
| 636 |
| 637 // Note on performance: Under normal circumstances, this should get called |
| 638 // once per page load. The code will either manage to do it ON_TIME or will |
| 639 // be DEFERRED until CLD is ready. In the latter case, CLD is in dynamic mode |
| 640 // and may eventually become available, triggering the RESUMED event; after |
| 641 // this, everything should start being ON_TIME. This should never run more |
| 642 // than twice in a page load, under any conditions. |
| 643 // Also note that language detection is triggered off of a delay AFTER the |
| 644 // page load completed event has fired, making this very much off the critical |
| 645 // path. |
| 646 content::RenderThread::Get()->UpdateHistograms( |
| 647 content::kHistogramSynchronizerReservedSequenceNumber); |
| 648 } |
| OLD | NEW |