Bug 1345437 - Gather telemetry for the encoding of external scripts;r=hsivonen
authorDavid Teller <dteller@mozilla.com>
Wed, 08 Mar 2017 14:08:36 +0100
changeset 394863 cb308a76ccd58381b1e18ba297d86af52353f220
parent 394862 9a57f9a7b87a23cc461134d2a74c0ca08ed50cdb
child 394864 093813e9bbbcbd38ac1dbfcb0a19fcc8fdcf3349
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsivonen
bugs1345437
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1345437 - Gather telemetry for the encoding of external scripts;r=hsivonen MozReview-Commit-ID: 2zjxBWxBGPK
dom/base/nsScriptLoader.cpp
dom/base/nsScriptLoader.h
toolkit/components/telemetry/Histograms.json
--- a/dom/base/nsScriptLoader.cpp
+++ b/dom/base/nsScriptLoader.cpp
@@ -2452,17 +2452,18 @@ nsScriptLoader::ConvertToUTF16(nsIChanne
     unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
   }
 
   if (!unicodeDecoder) {
     // Curiously, there are various callers that don't pass aDocument. The
     // fallback in the old code was ISO-8859-1, which behaved like
     // windows-1252. Saying windows-1252 for clarity and for compliance
     // with the Encoding Standard.
-    unicodeDecoder = EncodingUtils::DecoderForEncoding("windows-1252");
+    charset = "windows-1252";
+    unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
   }
 
   int32_t unicodeLength = 0;
 
   nsresult rv =
     unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData),
                                  aLength, &unicodeLength);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -2479,16 +2480,21 @@ nsScriptLoader::ConvertToUTF16(nsIChanne
                                &unicodeLength);
   MOZ_ASSERT(NS_SUCCEEDED(rv));
   aLengthOut = unicodeLength;
   if (NS_FAILED(rv)) {
     js_free(aBufOut);
     aBufOut = nullptr;
     aLengthOut = 0;
   }
+  if (charset.Length() == 0) {
+    charset = "?";
+  }
+  mozilla::Telemetry::Accumulate(mozilla::Telemetry::DOM_SCRIPT_SRC_ENCODING,
+    charset);
   return rv;
 }
 
 nsresult
 nsScriptLoader::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
                                  nsScriptLoadRequest* aRequest,
                                  nsresult aChannelStatus,
                                  nsresult aSRIStatus,
@@ -2970,49 +2976,66 @@ nsScriptLoadHandler::EnsureDecoder(nsIIn
                                    bool aEndOfStream)
 {
   // Check if decoder has already been created.
   if (mDecoder) {
     return true;
   }
 
   nsAutoCString charset;
-
+  if (!EnsureDecoder(aLoader, aData, aDataLength, aEndOfStream, charset)) {
+    return false;
+  }
+  if (charset.Length() == 0) {
+    charset = "?";
+  }
+  mozilla::Telemetry::Accumulate(mozilla::Telemetry::DOM_SCRIPT_SRC_ENCODING,
+    charset);
+  return true;
+}
+
+bool
+nsScriptLoadHandler::EnsureDecoder(nsIIncrementalStreamLoader *aLoader,
+                                   const uint8_t* aData,
+                                   uint32_t aDataLength,
+                                   bool aEndOfStream,
+                                   nsCString& oCharset)
+{
   // JavaScript modules are always UTF-8.
   if (mRequest->IsModuleRequest()) {
-    charset = "UTF-8";
-    mDecoder = EncodingUtils::DecoderForEncoding(charset);
+    oCharset = "UTF-8";
+    mDecoder = EncodingUtils::DecoderForEncoding(oCharset);
     return true;
   }
 
   // Determine if BOM check should be done.  This occurs either
   // if end-of-stream has been reached, or at least 3 bytes have
   // been read from input.
   if (!aEndOfStream && (aDataLength < 3)) {
     return false;
   }
 
   // Do BOM detection.
-  if (nsContentUtils::CheckForBOM(aData, aDataLength, charset)) {
-    mDecoder = EncodingUtils::DecoderForEncoding(charset);
+  if (nsContentUtils::CheckForBOM(aData, aDataLength, oCharset)) {
+    mDecoder = EncodingUtils::DecoderForEncoding(oCharset);
     return true;
   }
 
   // BOM detection failed, check content stream for charset.
   nsCOMPtr<nsIRequest> req;
   nsresult rv = aLoader->GetRequest(getter_AddRefs(req));
   NS_ASSERTION(req, "StreamLoader's request went away prematurely");
   NS_ENSURE_SUCCESS(rv, false);
 
   nsCOMPtr<nsIChannel> channel = do_QueryInterface(req);
 
   if (channel &&
-      NS_SUCCEEDED(channel->GetContentCharset(charset)) &&
-      EncodingUtils::FindEncodingForLabel(charset, charset)) {
-    mDecoder = EncodingUtils::DecoderForEncoding(charset);
+      NS_SUCCEEDED(channel->GetContentCharset(oCharset)) &&
+      EncodingUtils::FindEncodingForLabel(oCharset, oCharset)) {
+    mDecoder = EncodingUtils::DecoderForEncoding(oCharset);
     return true;
   }
 
   // Check the hint charset from the script element or preload
   // request.
   nsAutoString hintCharset;
   if (!mRequest->IsPreload()) {
     mRequest->mElement->GetScriptCharset(hintCharset);
@@ -3021,34 +3044,35 @@ nsScriptLoadHandler::EnsureDecoder(nsIIn
       mScriptLoader->mPreloads.IndexOf(mRequest, 0,
             nsScriptLoader::PreloadRequestComparator());
 
     NS_ASSERTION(i != mScriptLoader->mPreloads.NoIndex,
                  "Incorrect preload bookkeeping");
     hintCharset = mScriptLoader->mPreloads[i].mCharset;
   }
 
-  if (EncodingUtils::FindEncodingForLabel(hintCharset, charset)) {
-    mDecoder = EncodingUtils::DecoderForEncoding(charset);
+  if (EncodingUtils::FindEncodingForLabel(hintCharset, oCharset)) {
+    mDecoder = EncodingUtils::DecoderForEncoding(oCharset);
     return true;
   }
 
   // Get the charset from the charset of the document.
   if (mScriptLoader->mDocument) {
-    charset = mScriptLoader->mDocument->GetDocumentCharacterSet();
-    mDecoder = EncodingUtils::DecoderForEncoding(charset);
+    oCharset = mScriptLoader->mDocument->GetDocumentCharacterSet();
+    mDecoder = EncodingUtils::DecoderForEncoding(oCharset);
     return true;
   }
 
   // Curiously, there are various callers that don't pass aDocument. The
   // fallback in the old code was ISO-8859-1, which behaved like
   // windows-1252. Saying windows-1252 for clarity and for compliance
   // with the Encoding Standard.
-  charset = "windows-1252";
-  mDecoder = EncodingUtils::DecoderForEncoding(charset);
+  oCharset = "windows-1252";
+  mDecoder = EncodingUtils::DecoderForEncoding(oCharset);
+
   return true;
 }
 
 NS_IMETHODIMP
 nsScriptLoadHandler::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
                                       nsISupports* aContext,
                                       nsresult aStatus,
                                       uint32_t aDataLength,
--- a/dom/base/nsScriptLoader.h
+++ b/dom/base/nsScriptLoader.h
@@ -684,16 +684,19 @@ private:
   /*
    * Discover the charset by looking at the stream data, the script
    * tag, and other indicators.  Returns true if charset has been
    * discovered.
    */
   bool EnsureDecoder(nsIIncrementalStreamLoader *aLoader,
                      const uint8_t* aData, uint32_t aDataLength,
                      bool aEndOfStream);
+  bool EnsureDecoder(nsIIncrementalStreamLoader *aLoader,
+                     const uint8_t* aData, uint32_t aDataLength,
+                     bool aEndOfStream, nsCString& oCharset);
 
   // ScriptLoader which will handle the parsed script.
   RefPtr<nsScriptLoader>        mScriptLoader;
 
   // The nsScriptLoadRequest for this load. Decoded data are accumulated on it.
   RefPtr<nsScriptLoadRequest>   mRequest;
 
   // SRI data verifier.
--- a/toolkit/components/telemetry/Histograms.json
+++ b/toolkit/components/telemetry/Histograms.json
@@ -10535,16 +10535,24 @@
   "PLUGIN_DRAWING_MODEL": {
     "alert_emails": ["danderson@mozilla.com"],
     "expires_in_version": "never",
     "kind": "enumerated",
     "bug_numbers": [1229961],
     "n_values": 12,
     "description": "Plugin drawing model. 0 when windowed, otherwise NPDrawingModel + 1."
   },
+  "DOM_SCRIPT_SRC_ENCODING": {
+    "alert_emails": ["dteller@mozilla.com"],
+    "expires_in_version": "never",
+    "kind": "count",
+    "keyed": true,
+    "bug_numbers": [1344152],
+    "description": "Encoding used in a <script src>."
+  },
   "WEB_NOTIFICATION_REQUEST_PERMISSION_CALLBACK": {
     "alert_emails": ["push@mozilla.com"],
     "expires_in_version": "55",
     "bug_numbers": [1241278],
     "kind": "boolean",
     "description": "Usage of the deprecated Notification.requestPermission() callback argument"
   },
   "VIDEO_FASTSEEK_USED": {