Bug 1333140 - Collect telemetry about (non-)HTTPS usage of crypto.subtle r=keeler data-r=bsmedberg
authorTim Taubert <ttaubert@mozilla.com>
Wed, 31 May 2017 10:35:37 +0200
changeset 409652 917caf00a8b48819476e54def1e1fcdd06d00299
parent 409651 34d32278afbfa8a25c6a68e9b62b5efa3b4aeef2
child 409653 ab34e949f80ec0d74fe93f5eb0229e6d9c21ec2c
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)
reviewerskeeler
bugs1333140
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 1333140 - Collect telemetry about (non-)HTTPS usage of crypto.subtle r=keeler data-r=bsmedberg
dom/base/SubtleCrypto.cpp
dom/base/SubtleCrypto.h
toolkit/components/telemetry/Histograms.json
--- a/dom/base/SubtleCrypto.cpp
+++ b/dom/base/SubtleCrypto.cpp
@@ -4,49 +4,65 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/SubtleCrypto.h"
 
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/SubtleCryptoBinding.h"
 #include "mozilla/dom/WebCryptoTask.h"
+#include "mozilla/Telemetry.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SubtleCrypto, mParent)
 NS_IMPL_CYCLE_COLLECTING_ADDREF(SubtleCrypto)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(SubtleCrypto)
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SubtleCrypto)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 
 
 SubtleCrypto::SubtleCrypto(nsIGlobalObject* aParent)
   : mParent(aParent)
+  , mRecordedTelemetry(false)
 {
 }
 
 JSObject*
 SubtleCrypto::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return SubtleCryptoBinding::Wrap(aCx, this, aGivenProto);
 }
 
+void
+SubtleCrypto::RecordTelemetryOnce() {
+  if (mRecordedTelemetry) {
+    return;
+  }
+
+  mRecordedTelemetry = true;
+  JSObject* global = mParent->GetGlobalJSObject();
+  bool isSecure = JS_GetIsSecureContext(js::GetObjectCompartment(global));
+  Telemetry::Accumulate(Telemetry::WEBCRYPTO_METHOD_SECURE, isSecure);
+}
+
 #define SUBTLECRYPTO_METHOD_BODY(Operation, aRv, ...)                   \
   MOZ_ASSERT(mParent);                                                  \
-  RefPtr<Promise> p = Promise::Create(mParent, aRv);                  \
+  RefPtr<Promise> p = Promise::Create(mParent, aRv);                    \
   if (aRv.Failed()) {                                                   \
     return nullptr;                                                     \
   }                                                                     \
-  RefPtr<WebCryptoTask> task = WebCryptoTask::Create ## Operation ## Task(__VA_ARGS__); \
-  task->DispatchWithPromise(p); \
+  RecordTelemetryOnce();                                                \
+  RefPtr<WebCryptoTask> task =                                          \
+    WebCryptoTask::Create ## Operation ## Task(__VA_ARGS__);            \
+  task->DispatchWithPromise(p);                                         \
   return p.forget();
 
 already_AddRefed<Promise>
 SubtleCrypto::Encrypt(JSContext* cx,
                       const ObjectOrString& algorithm,
                       CryptoKey& key,
                       const CryptoOperationData& data,
                       ErrorResult& aRv)
--- a/dom/base/SubtleCrypto.h
+++ b/dom/base/SubtleCrypto.h
@@ -114,15 +114,18 @@ public:
                                       CryptoKey& unwrappingKey,
                                       const ObjectOrString& unwrapAlgorithm,
                                       const ObjectOrString& unwrappedKeyAlgorithm,
                                       bool extractable,
                                       const Sequence<nsString>& keyUsages,
                                       ErrorResult& aRv);
 
 private:
+  void RecordTelemetryOnce();
+
   nsCOMPtr<nsIGlobalObject> mParent;
+  bool mRecordedTelemetry;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_SubtleCrypto_h
--- a/toolkit/components/telemetry/Histograms.json
+++ b/toolkit/components/telemetry/Histograms.json
@@ -10799,16 +10799,24 @@
   },
   "WEBCRYPTO_METHOD": {
     "record_in_processes": ["main", "content"],
     "expires_in_version": "never",
     "kind": "enumerated",
     "n_values": 20,
     "description": "Methods invoked under window.crypto.subtle (0=encrypt, 1=decrypt, 2=sign, 3=verify, 4=digest, 5=generateKey, 6=deriveKey, 7=deriveBits, 8=importKey, 9=exportKey, 10=wrapKey, 11=unwrapKey)"
   },
+  "WEBCRYPTO_METHOD_SECURE": {
+    "alert_emails": ["seceng-telemetry@mozilla.com"],
+    "record_in_processes": ["main", "content"],
+    "expires_in_version": "60",
+    "kind": "boolean",
+    "bug_numbers": [1333140],
+    "description": "Whether a method invoked under window.crypto.subtle is in a secure context"
+  },
   "WEBCRYPTO_ALG": {
     "record_in_processes": ["main", "content"],
     "expires_in_version": "never",
     "kind": "enumerated",
     "n_values": 30,
     "description": "Algorithms used with WebCrypto (see table in WebCryptoTask.cpp)"
   },
   "MASTER_PASSWORD_ENABLED": {