Crypto Histograms must be executed after the deriveBits operation

In r1227708 we have added use counters to measure the usage of the
deriveBits's 'length' parameter with 2 specific values. The counter to
detect non-zero truncation should be executed after the deriveBits
operation is completed, since only then we can know the size of the
derived key and whether the length's parameter value implies that the
key is being truncated.

In r1227708 we executed the counters just after calling Blink's platform
WebCryptoImpl::DeriveBits method, which is asynchronous. Hence, we were
checking for the warning before we actually set it in the WebCrypto
component's code.

This CL moves the call to the WebCryptoImpl::DoDeriveBitsReply func,
invoked as an async post-task. This changes requires to declare the
HistogramDeriveBitsTruncation function in the Blink public platform
as an exported symbol, so that it could be invoked from the webcrypto
component.

Bug: 1439774
Change-Id: I510e4bb9d35ed38da573bedb2e62ad35eebc6a89
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5328466
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Commit-Queue: Javier Fernandez <jfernandez@igalia.com>
Reviewed-by: David Benjamin <davidben@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1283786}
diff --git a/components/webcrypto/webcrypto_impl.cc b/components/webcrypto/webcrypto_impl.cc
index 83b2011..e747c5e 100644
--- a/components/webcrypto/webcrypto_impl.cc
+++ b/components/webcrypto/webcrypto_impl.cc
@@ -24,6 +24,7 @@
 #include "components/webcrypto/status.h"
 #include "third_party/blink/public/platform/web_crypto_key_algorithm.h"
 #include "third_party/blink/public/platform/web_string.h"
+#include "third_party/blink/public/web/web_crypto_histograms.h"
 
 namespace webcrypto {
 
@@ -580,7 +581,11 @@
 void DoDeriveBitsReply(std::unique_ptr<DeriveBitsState> state) {
   TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
                "DoDeriveBitsReply");
-  state->result.SetWarning(state->status.warning_type());
+  if (!state->status.IsError()) {
+    HistogramDeriveBitsTruncation(state->result.GetExecutionContext(),
+                                  state->length_bits,
+                                  state->status.warning_type());
+  }
   CompleteWithBufferOrError(state->status, state->derived_bytes,
                             &state->result);
 }
diff --git a/third_party/blink/public/BUILD.gn b/third_party/blink/public/BUILD.gn
index f165313..c8d740a0 100644
--- a/third_party/blink/public/BUILD.gn
+++ b/third_party/blink/public/BUILD.gn
@@ -295,6 +295,7 @@
     "web/web_console_message.h",
     "web/web_content_capture_client.h",
     "web/web_content_holder.h",
+    "web/web_crypto_histograms.h",
     "web/web_crypto_normalize.h",
     "web/web_css_origin.h",
     "web/web_custom_element.h",
diff --git a/third_party/blink/public/platform/web_crypto.h b/third_party/blink/public/platform/web_crypto.h
index 6ae7b61..eef598c 100644
--- a/third_party/blink/public/platform/web_crypto.h
+++ b/third_party/blink/public/platform/web_crypto.h
@@ -48,6 +48,7 @@
 class CryptoResult;
 class CryptoResultCancel;
 class WebString;
+class ExecutionContext;
 
 enum WebCryptoErrorType {
   kWebCryptoErrorTypeType,
@@ -94,7 +95,7 @@
   // This method can be called from any thread.
   bool Cancelled() const;
 
-  void SetWarning(WebCryptoWarningType code);
+  ExecutionContext* GetExecutionContext() const;
 
 #if INSIDE_BLINK
   WebCryptoResult(CryptoResult*, scoped_refptr<CryptoResultCancel>);
diff --git a/third_party/blink/renderer/modules/crypto/crypto_histograms.h b/third_party/blink/public/web/web_crypto_histograms.h
similarity index 92%
rename from third_party/blink/renderer/modules/crypto/crypto_histograms.h
rename to third_party/blink/public/web/web_crypto_histograms.h
index a82f4a9..11c9a1d 100644
--- a/third_party/blink/renderer/modules/crypto/crypto_histograms.h
+++ b/third_party/blink/public/web/web_crypto_histograms.h
@@ -2,8 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CRYPTO_CRYPTO_HISTOGRAMS_H_
-#define THIRD_PARTY_BLINK_RENDERER_MODULES_CRYPTO_CRYPTO_HISTOGRAMS_H_
+#ifndef THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_CRYPTO_HISTOGRAMS_H_
+#define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_CRYPTO_HISTOGRAMS_H_
 
 #include "third_party/blink/public/platform/web_crypto.h"
 
@@ -119,10 +119,10 @@
                               const WebCryptoAlgorithm&,
                               const WebCryptoKey&);
 
-void HistogramDeriveBitsTruncation(ExecutionContext*,
-                                   unsigned int,
-                                   WebCryptoWarningType);
-
+BLINK_EXPORT void
+HistogramDeriveBitsTruncation(ExecutionContext*,
+                              unsigned int,
+                              WebCryptoWarningType);
 }  // namespace blink
 
-#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_CRYPTO_CRYPTO_HISTOGRAMS_H_
+#endif  // THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_CRYPTO_HISTOGRAMS_H
diff --git a/third_party/blink/renderer/modules/crypto/BUILD.gn b/third_party/blink/renderer/modules/crypto/BUILD.gn
index 4798e44b..ab2537c 100644
--- a/third_party/blink/renderer/modules/crypto/BUILD.gn
+++ b/third_party/blink/renderer/modules/crypto/BUILD.gn
@@ -8,8 +8,6 @@
   sources = [
     "crypto.cc",
     "crypto.h",
-    "crypto_histograms.cc",
-    "crypto_histograms.h",
     "crypto_key.cc",
     "crypto_key.h",
     "crypto_result_impl.cc",
diff --git a/third_party/blink/renderer/modules/crypto/crypto_result_impl.h b/third_party/blink/renderer/modules/crypto/crypto_result_impl.h
index 619208e..3d39451e6 100644
--- a/third_party/blink/renderer/modules/crypto/crypto_result_impl.h
+++ b/third_party/blink/renderer/modules/crypto/crypto_result_impl.h
@@ -85,6 +85,9 @@
                            const WebCryptoKey& private_key) override;
   WebCryptoWarningType GetWarning() override { return warning_code_; }
   void SetWarning(WebCryptoWarningType code) override { warning_code_ = code; }
+  ExecutionContext* GetExecutionContext() const override {
+    return resolver_ ? resolver_->GetExecutionContext() : nullptr;
+  }
 
   void CompleteWithError(ExceptionState&);
 
diff --git a/third_party/blink/renderer/modules/crypto/subtle_crypto.cc b/third_party/blink/renderer/modules/crypto/subtle_crypto.cc
index 8d53ebb..e8690631 100644
--- a/third_party/blink/renderer/modules/crypto/subtle_crypto.cc
+++ b/third_party/blink/renderer/modules/crypto/subtle_crypto.cc
@@ -36,6 +36,7 @@
 #include "third_party/blink/public/platform/web_crypto.h"
 #include "third_party/blink/public/platform/web_crypto_algorithm.h"
 #include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
+#include "third_party/blink/public/web/web_crypto_histograms.h"
 #include "third_party/blink/renderer/bindings/core/v8/dictionary.h"
 #include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
 #include "third_party/blink/renderer/bindings/modules/v8/v8_json_web_key.h"
@@ -44,7 +45,6 @@
 #include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h"
 #include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer_view.h"
 #include "third_party/blink/renderer/core/typed_arrays/dom_array_piece.h"
-#include "third_party/blink/renderer/modules/crypto/crypto_histograms.h"
 #include "third_party/blink/renderer/modules/crypto/crypto_key.h"
 #include "third_party/blink/renderer/modules/crypto/crypto_result_impl.h"
 #include "third_party/blink/renderer/modules/crypto/crypto_utilities.h"
@@ -695,8 +695,6 @@
       normalized_algorithm, base_key->Key(), length_bits, result->Result(),
       execution_context->GetTaskRunner(blink::TaskType::kInternalWebCrypto));
 
-  HistogramDeriveBitsTruncation(execution_context, length_bits,
-                                result->GetWarning());
   return promise;
 }
 
diff --git a/third_party/blink/renderer/modules/exported/BUILD.gn b/third_party/blink/renderer/modules/exported/BUILD.gn
index 729b1590..d1525b2 100644
--- a/third_party/blink/renderer/modules/exported/BUILD.gn
+++ b/third_party/blink/renderer/modules/exported/BUILD.gn
@@ -7,6 +7,7 @@
   sources = [
     "web_ax_context.cc",
     "web_ax_object.cc",
+    "web_crypto_histograms.cc",
     "web_crypto_normalize.cc",
     "web_dom_file_system.cc",
     "web_dom_media_stream_track.cc",
diff --git a/third_party/blink/renderer/modules/crypto/crypto_histograms.cc b/third_party/blink/renderer/modules/exported/web_crypto_histograms.cc
similarity index 98%
rename from third_party/blink/renderer/modules/crypto/crypto_histograms.cc
rename to third_party/blink/renderer/modules/exported/web_crypto_histograms.cc
index 1b9ca45..24dd804 100644
--- a/third_party/blink/renderer/modules/crypto/crypto_histograms.cc
+++ b/third_party/blink/renderer/modules/exported/web_crypto_histograms.cc
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "third_party/blink/renderer/modules/crypto/crypto_histograms.h"
+#include "third_party/blink/public/web/web_crypto_histograms.h"
 
 #include "third_party/blink/public/platform/platform.h"
 #include "third_party/blink/public/platform/web_crypto_algorithm.h"
diff --git a/third_party/blink/renderer/platform/crypto_result.h b/third_party/blink/renderer/platform/crypto_result.h
index ed64e6fb..032c6b0 100644
--- a/third_party/blink/renderer/platform/crypto_result.h
+++ b/third_party/blink/renderer/platform/crypto_result.h
@@ -64,6 +64,7 @@
 
   virtual WebCryptoWarningType GetWarning() = 0;
   virtual void SetWarning(WebCryptoWarningType code) = 0;
+  virtual ExecutionContext* GetExecutionContext() const = 0;
 
   virtual void Trace(Visitor* visitor) const {}
 };
diff --git a/third_party/blink/renderer/platform/exported/web_crypto_result.cc b/third_party/blink/renderer/platform/exported/web_crypto_result.cc
index c5146d2..f1d70b3 100644
--- a/third_party/blink/renderer/platform/exported/web_crypto_result.cc
+++ b/third_party/blink/renderer/platform/exported/web_crypto_result.cc
@@ -80,8 +80,8 @@
   return cancel_->Cancelled();
 }
 
-void WebCryptoResult::SetWarning(WebCryptoWarningType code) {
-  impl_->SetWarning(code);
+ExecutionContext* WebCryptoResult::GetExecutionContext() const {
+  return impl_->GetExecutionContext();
 }
 
 WebCryptoResult::WebCryptoResult(CryptoResult* impl,