Safe Browsing: Add ScopedBlockingCall(MAY_BLOCK) before remote call

The CL also adds a microsecond histogram to the call, to see how often
we'd get above the 10ms threshold in practice. A similar histogram
exists (SubresourceFilter.SafeBrowsing.CheckDispatchTime), but it is
only called for SubresourceFilter safe browsing checks on main frame
navigations.

Bug: 806981
Change-Id: If63590a4f543a1e65760ee5c7f35d8bc773cef10
Reviewed-on: https://chromium-review.googlesource.com/911750
Commit-Queue: Charlie Harrison <csharrison@chromium.org>
Reviewed-by: Varun Khaneja <vakh@chromium.org>
Reviewed-by: Robert Kaplow <rkaplow@chromium.org>
Reviewed-by: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#535954}
diff --git a/components/safe_browsing/android/safe_browsing_api_handler_bridge.cc b/components/safe_browsing/android/safe_browsing_api_handler_bridge.cc
index 3e3070f4..db16c9f2 100644
--- a/components/safe_browsing/android/safe_browsing_api_handler_bridge.cc
+++ b/components/safe_browsing/android/safe_browsing_api_handler_bridge.cc
@@ -17,6 +17,9 @@
 #include "base/metrics/histogram_macros.h"
 #include "base/task_scheduler/post_task.h"
 #include "base/task_scheduler/task_traits.h"
+#include "base/threading/scoped_blocking_call.h"
+#include "base/time/time.h"
+#include "base/timer/elapsed_timer.h"
 #include "base/trace_event/trace_event.h"
 #include "components/safe_browsing/android/safe_browsing_api_handler_util.h"
 #include "components/safe_browsing/db/v4_protocol_manager_util.h"
@@ -244,8 +247,15 @@
   ScopedJavaLocalRef<jintArray> j_threat_types =
       SBThreatTypeSetToJavaArray(env, threat_types);
 
+  // Increase parallelism by indicating that the lookup may block. Only the long
+  // tail of these calls block for more than 10ms, which is the current
+  // threshold for increasing worker capacity.
+  base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
+  base::ElapsedTimer check_timer;
   Java_SafeBrowsingApiBridge_startUriLookup(env, j_api_handler_, callback_id,
                                             j_url, j_threat_types);
+  UMA_HISTOGRAM_COUNTS_10M("SB2.RemoteCall.CheckDispatchTime",
+                           check_timer.Elapsed().InMicroseconds());
 }
 
 }  // namespace safe_browsing
diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml
index 4ad5475..f506fef2 100644
--- a/tools/metrics/histograms/histograms.xml
+++ b/tools/metrics/histograms/histograms.xml
@@ -75441,6 +75441,16 @@
   </summary>
 </histogram>
 
+<histogram name="SB2.RemoteCall.CheckDispatchTime" units="microseconds">
+  <owner>csharrison@chromium.org</owner>
+  <owner>vakh@chromium.org</owner>
+  <summary>
+    Wall time in microseconds for calling
+    Java_SafeBrowsingApiBridge_startUriLookup. Logged at every Safe Browsing
+    check on Android.
+  </summary>
+</histogram>
+
 <histogram name="SB2.RemoteCall.ChecksPending" units="calls">
   <obsolete>
     Deprecated in M64 (Nov 2017). No longer generated.