Chromium Code Reviews| 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/user_metrics_action.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/renderer/render_thread.h" | |
| 20 #include "content/public/renderer/render_view.h" | 22 #include "content/public/renderer/render_view.h" |
| 21 #include "extensions/common/constants.h" | 23 #include "extensions/common/constants.h" |
| 22 #include "extensions/renderer/extension_groups.h" | 24 #include "extensions/renderer/extension_groups.h" |
| 23 #include "ipc/ipc_platform_file.h" | 25 #include "ipc/ipc_platform_file.h" |
| 24 #include "content/public/common/url_constants.h" | 26 #include "content/public/common/url_constants.h" |
| 25 #include "third_party/WebKit/public/web/WebDocument.h" | 27 #include "third_party/WebKit/public/web/WebDocument.h" |
| 26 #include "third_party/WebKit/public/web/WebElement.h" | 28 #include "third_party/WebKit/public/web/WebElement.h" |
| 27 #include "third_party/WebKit/public/web/WebFrame.h" | 29 #include "third_party/WebKit/public/web/WebFrame.h" |
| 28 #include "third_party/WebKit/public/web/WebNode.h" | 30 #include "third_party/WebKit/public/web/WebNode.h" |
| 29 #include "third_party/WebKit/public/web/WebNodeList.h" | 31 #include "third_party/WebKit/public/web/WebNodeList.h" |
| 30 #include "third_party/WebKit/public/web/WebScriptSource.h" | 32 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 31 #include "third_party/WebKit/public/web/WebView.h" | 33 #include "third_party/WebKit/public/web/WebView.h" |
| 32 #include "third_party/WebKit/public/web/WebWidget.h" | 34 #include "third_party/WebKit/public/web/WebWidget.h" |
| 33 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 34 #include "v8/include/v8.h" | 36 #include "v8/include/v8.h" |
| 35 | 37 |
| 36 using base::ASCIIToUTF16; | 38 using base::ASCIIToUTF16; |
| 37 using blink::WebDocument; | 39 using blink::WebDocument; |
| 38 using blink::WebElement; | 40 using blink::WebElement; |
| 39 using blink::WebFrame; | 41 using blink::WebFrame; |
| 40 using blink::WebNode; | 42 using blink::WebNode; |
| 41 using blink::WebNodeList; | 43 using blink::WebNodeList; |
| 42 using blink::WebScriptSource; | 44 using blink::WebScriptSource; |
| 43 using blink::WebSecurityOrigin; | 45 using blink::WebSecurityOrigin; |
| 44 using blink::WebString; | 46 using blink::WebString; |
| 45 using blink::WebVector; | 47 using blink::WebVector; |
| 46 using blink::WebView; | 48 using blink::WebView; |
| 47 | 49 |
| 50 // Note on metrics: | |
| 51 // | |
| 52 // This class provides metrics that allow tracking the user experience impact | |
| 53 // of non-static CldDataProvider implementations. For background on the data | |
| 54 // providers, please refer to the following documentation: | |
| 55 // http://www.chromium.org/developers/how-tos/compact-language-detector-cld-data -source-configuration | |
| 56 // | |
| 57 // Available metrics: | |
| 58 // 1. Translate.LanguageDetectionOnTime | |
| 59 // Recorded if PageCaptured(...) is invoked after CLD is available. | |
| 60 // This is the ideal case, indicating that CLD is available before it is | |
| 61 // needed. | |
| 62 // 2. Translate.LanguageDetectionDeferred | |
| 63 // Recorded if PageCaptured(...) is invoked before CLD is available. | |
| 64 // Sub-optimal case indicating that CLD wasn't available when it was | |
| 65 // needed, so the request for detection has been deferred until CLD is | |
| 66 // available or until the user navigates to a different page. | |
| 67 // 3. Translate.LanguageDetectionLate | |
| 68 // Recorded if CLD becomes available after a language detection request | |
| 69 // was deferred as above in (2), but before the user navigated to a | |
| 70 // different page. Language detection is ultimately completed, it just | |
| 71 // didn't happen on time. | |
| 72 // | |
| 73 // Note that there is NOT a metric that records the number of times that | |
| 74 // language detection had to be aborted because CLD never became available in | |
| 75 // time. This is because there is no reasonable way to cover all the cases | |
| 76 // under which this could occur, particularly the destruction of the renderer | |
| 77 // for which this object was created. However, this value can be synthetically | |
| 78 // derived, using the logic below. | |
| 79 // | |
| 80 // Every page load that triggers language detection will result in the | |
| 81 // recording of exactly one of the first two metrics in the list above: | |
| 82 // Translate.LanguageDetectionOnTime or Translate.LanguageDetectionDeferred. | |
| 83 // If CLD is available in time to satisfy the request, the third metric | |
| 84 // (Translate.LanguageDetectionLate) will be recorded; thus, the number of | |
| 85 // times when language detection ultimately fails because CLD isn't ever | |
| 86 // available is implied as the number of times that detection is deferred minus | |
| 87 // the number of times that language detection is late. Note that this, too, | |
| 88 // is not necessarily 100% relevant: some renderer process are so short-lived | |
| 89 // that language detection wouldn't have been relevant anywaysm and so a failure | |
|
Andrew Hayden (chromium.org)
2014/07/14 14:24:13
"anywaysm" -> "anyways,"
simonb1
2014/07/14 16:23:48
"anyways" -> "anyway" :-)
http://www.dailywritingt
| |
| 90 // to detect the language in a timely manner might be completely innocuous. | |
| 91 | |
| 48 namespace { | 92 namespace { |
| 49 | 93 |
| 50 // The delay in milliseconds that we'll wait before checking to see if the | 94 // The delay in milliseconds that we'll wait before checking to see if the |
| 51 // translate library injected in the page is ready. | 95 // translate library injected in the page is ready. |
| 52 const int kTranslateInitCheckDelayMs = 150; | 96 const int kTranslateInitCheckDelayMs = 150; |
| 53 | 97 |
| 54 // The maximum number of times we'll check to see if the translate library | 98 // The maximum number of times we'll check to see if the translate library |
| 55 // injected in the page is ready. | 99 // injected in the page is ready. |
| 56 const int kMaxTranslateInitCheckAttempts = 5; | 100 const int kMaxTranslateInitCheckAttempts = 5; |
| 57 | 101 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 81 weak_method_factory_(this), | 125 weak_method_factory_(this), |
| 82 cld_data_provider_(translate::CreateRendererCldDataProviderFor(this)), | 126 cld_data_provider_(translate::CreateRendererCldDataProviderFor(this)), |
| 83 cld_data_polling_started_(false), | 127 cld_data_polling_started_(false), |
| 84 cld_data_polling_canceled_(false), | 128 cld_data_polling_canceled_(false), |
| 85 deferred_page_capture_(false), | 129 deferred_page_capture_(false), |
| 86 deferred_page_seq_no_(-1) { | 130 deferred_page_seq_no_(-1) { |
| 87 } | 131 } |
| 88 | 132 |
| 89 TranslateHelper::~TranslateHelper() { | 133 TranslateHelper::~TranslateHelper() { |
| 90 CancelPendingTranslation(); | 134 CancelPendingTranslation(); |
| 91 CancelCldDataPolling(); | |
| 92 } | 135 } |
| 93 | 136 |
| 94 void TranslateHelper::PrepareForUrl(const GURL& url) { | 137 void TranslateHelper::PrepareForUrl(const GURL& url) { |
| 95 ++page_seq_no_; | 138 ++page_seq_no_; |
| 96 Send(new ChromeViewHostMsg_TranslateAssignedSequenceNumber( | 139 Send(new ChromeViewHostMsg_TranslateAssignedSequenceNumber( |
| 97 routing_id(), page_seq_no_)); | 140 routing_id(), page_seq_no_)); |
| 98 deferred_page_capture_ = false; | 141 deferred_page_capture_ = false; |
| 99 deferred_page_seq_no_ = -1; | 142 deferred_page_seq_no_ = -1; |
| 100 deferred_contents_.clear(); | 143 deferred_contents_.clear(); |
| 101 if (cld_data_polling_started_) | 144 if (cld_data_polling_started_) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 // header. The two actually have different meanings (despite the | 176 // header. The two actually have different meanings (despite the |
| 134 // original intent of http-equiv to be an equivalent) with the former | 177 // original intent of http-equiv to be an equivalent) with the former |
| 135 // being the language of the document and the latter being the | 178 // being the language of the document and the latter being the |
| 136 // language of the intended audience (a distinction really only | 179 // language of the intended audience (a distinction really only |
| 137 // relevant for things like langauge textbooks). This distinction | 180 // relevant for things like langauge textbooks). This distinction |
| 138 // shouldn't affect translation. | 181 // shouldn't affect translation. |
| 139 WebFrame* main_frame = GetMainFrame(); | 182 WebFrame* main_frame = GetMainFrame(); |
| 140 if (!main_frame || page_seq_no_ != page_seq_no) | 183 if (!main_frame || page_seq_no_ != page_seq_no) |
| 141 return; | 184 return; |
| 142 | 185 |
| 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()) { | 186 if (!cld_data_provider_->IsCldDataAvailable()) { |
| 146 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data | 187 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data |
| 147 // is loaded, if ever. | 188 // is loaded, if ever. |
| 148 deferred_page_capture_ = true; | 189 deferred_page_capture_ = true; |
| 149 deferred_page_seq_no_ = page_seq_no; | 190 deferred_page_seq_no_ = page_seq_no; |
| 150 deferred_contents_ = contents; | 191 deferred_contents_ = contents; |
| 192 content::RenderThread::Get()->RecordAction( | |
| 193 base::UserMetricsAction("Translate.LanguageDetectionDeferred")); | |
| 151 return; | 194 return; |
| 152 } | 195 } |
| 153 | 196 |
| 197 if (deferred_page_seq_no_ == -1) { | |
| 198 // CLD data was available before language detection was requested. | |
| 199 content::RenderThread::Get()->RecordAction( | |
| 200 base::UserMetricsAction("Translate.LanguageDetectionOnTime")); | |
| 201 } else { | |
| 202 // This is a request that was triggered because CLD data is now available | |
| 203 // and was previously deferred. | |
| 204 content::RenderThread::Get()->RecordAction( | |
| 205 base::UserMetricsAction("Translate.LanguageDetectionLate")); | |
| 206 } | |
| 207 | |
| 154 WebDocument document = main_frame->document(); | 208 WebDocument document = main_frame->document(); |
| 155 std::string content_language = document.contentLanguage().utf8(); | 209 std::string content_language = document.contentLanguage().utf8(); |
| 156 WebElement html_element = document.documentElement(); | 210 WebElement html_element = document.documentElement(); |
| 157 std::string html_lang; | 211 std::string html_lang; |
| 158 // |html_element| can be null element, e.g. in | 212 // |html_element| can be null element, e.g. in |
| 159 // BrowserTest.WindowOpenClose. | 213 // BrowserTest.WindowOpenClose. |
| 160 if (!html_element.isNull()) | 214 if (!html_element.isNull()) |
| 161 html_lang = html_element.getAttribute("lang").utf8(); | 215 html_lang = html_element.getAttribute("lang").utf8(); |
| 162 std::string cld_language; | 216 std::string cld_language; |
| 163 bool is_cld_reliable; | 217 bool is_cld_reliable; |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 601 } | 655 } |
| 602 | 656 |
| 603 void TranslateHelper::OnCldDataAvailable() { | 657 void TranslateHelper::OnCldDataAvailable() { |
| 604 if (deferred_page_capture_) { | 658 if (deferred_page_capture_) { |
| 605 deferred_page_capture_ = false; // Don't do this a second time. | 659 deferred_page_capture_ = false; // Don't do this a second time. |
| 606 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); | 660 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); |
| 607 deferred_page_seq_no_ = -1; // Clean up for sanity | 661 deferred_page_seq_no_ = -1; // Clean up for sanity |
| 608 deferred_contents_.clear(); // Clean up for sanity | 662 deferred_contents_.clear(); // Clean up for sanity |
| 609 } | 663 } |
| 610 } | 664 } |
| OLD | NEW |