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) { | |
jar (doing other things)
2014/07/16 16:44:44
nit: Although I'm not usually a big fan of the ter
andrewhayden
2014/07/17 08:36:19
Ha. OK, will do :)
Andrew Hayden (chromium.org)
2014/07/17 14:00:43
Changed my mind on this one, I like the comments.
| |
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 | |
Takashi Toyoshima
2014/07/17 02:09:31
Having comments on implementation details in sourc
Andrew Hayden (chromium.org)
2014/07/17 14:00:43
I moved it to the header file. I considered kickin
| |
623 // Note on metrics: | |
624 // | |
625 // This class provides metrics that allow tracking the user experience impact | |
626 // of non-static CldDataProvider implementations. For background on the data | |
627 // providers, please refer to the following documentation: | |
628 // http://www.chromium.org/developers/how-tos/compact-language-detector-cld-data -source-configuration | |
629 // | |
630 // Available metrics (from the LanguageDetectionTiming enum): | |
631 // 1. ON_TIME | |
632 // Recorded if PageCaptured(...) is invoked after CLD is available. This is | |
633 // the ideal case, indicating that CLD is available before it is needed. | |
634 // 2. DEFERRED | |
635 // Recorded if PageCaptured(...) is invoked before CLD is available. | |
636 // Sub-optimal case indicating that CLD wasn't available when it was needed, | |
637 // so the request for detection has been deferred until CLD is available or | |
638 // until the user navigates to a different page. | |
639 // 3. RESUMED | |
640 // Recorded if CLD becomes available after a language detection request was | |
641 // deferred, but before the user navigated to a different page. Language | |
642 // detection is ultimately completed, it just didn't happen on time. | |
643 // | |
644 // Note that there is NOT a metric that records the number of times that | |
645 // language detection had to be aborted because CLD never became available in | |
646 // time. This is because there is no reasonable way to cover all the cases | |
647 // under which this could occur, particularly the destruction of the renderer | |
648 // for which this object was created. However, this value can be synthetically | |
649 // derived, using the logic below. | |
650 // | |
651 // Every page load that triggers language detection will result in the | |
652 // recording of exactly one of the first two events: ON_TIME or DEFERRED. If | |
653 // CLD is available in time to satisfy the request, the third event (RESUMED) | |
654 // will be recorded; thus, the number of times when language detection | |
655 // ultimately fails because CLD isn't ever available is implied as the number of | |
656 // times that detection is deferred minus the number of times that language | |
657 // detection is late: | |
658 // | |
659 // count(FAILED) ~= count(DEFERRED) - count(RESUMED) | |
660 // | |
661 // Note that this is not 100% accurate: some renderer process are so short-lived | |
662 // that language detection wouldn't have been relevant anyway, and so a failure | |
663 // to detect the language in a timely manner might be completely innocuous. The | |
664 // overall problem with language detection is that it isn't possible to know | |
665 // whether it was required or not until after it has been performed! | |
666 // | |
667 // We use histograms for recording these metrics. On Android, the renderer can | |
668 // be killed without the chance to clean up or transmit these histograms, | |
669 // leading to dropped metrics. To work around this, this method forces an IPC | |
670 // message to be sent to the browser process immediately. | |
671 void TranslateHelper::RecordLanguageDetectionTiming(LanguageDetectionTiming | |
Alexei Svitkine (slow)
2014/07/16 14:50:40
Nit: Wrap after ( instead and indent next line 4.
andrewhayden
2014/07/17 08:36:19
Will do.
| |
672 timing) { | |
673 // The following comment is copied from page_load_histograms.cc, and applies | |
674 // just as equally here: | |
675 // | |
676 // Since there are currently no guarantees that renderer histograms will be | |
677 // sent to the browser, we initiate a PostTask here to be sure that we send | |
678 // the histograms we generated. Without this call, pages that don't have an | |
679 // on-close-handler might generate data that is lost when the renderer is | |
680 // shutdown abruptly (perchance because the user closed the tab). | |
681 VLOG(1) << "Language detection timing: " << timing; | |
Alexei Svitkine (slow)
2014/07/16 14:50:40
Do you need the VLOG(1) message or is this left ov
andrewhayden
2014/07/17 08:36:19
I put this here so that I don't have to scan throu
Alexei Svitkine (slow)
2014/07/17 13:20:25
You can filter chrome://histograms/ by substring -
Andrew Hayden (chromium.org)
2014/07/17 14:00:43
DVLOG it is, then.
| |
682 UMA_HISTOGRAM_ENUMERATION("Translate.LanguageDetectionTiming", timing, | |
683 kLanguageDetectionTimingMax); | |
684 content::RenderThread::Get()->UpdateHistograms( | |
jar (doing other things)
2014/07/16 16:44:44
How often (max) could this be called? Is it really
andrewhayden
2014/07/17 08:36:19
As above, in the great majority of cases this shou
Andrew Hayden (chromium.org)
2014/07/17 14:00:43
Performance comments added here.
| |
685 content::kHistogramSynchronizerReservedSequenceNumber); | |
686 } | |
OLD | NEW |