[M73] Capture SafetyNetID if APK download telemetry report can be sent.

Flow:
1. AndroidTelemetryService::OnDownloadCreated called on UI thread.
   Calls MaybeCaptureSafetyNetId() on the UI thread.
2. If safety_net_id_on_ui_thread_ is already set, END.
   Otherwise, schedules SafeBrowsingDatabaseManager::GetSafetyNetId on
   the IO thread and the result of that is scheduled to go to
   AndroidTelemetryService::SetSafetyNetIdOnUIThread on the UI thread.
3. When SetSafetyNetIdOnUIThread() gets called, it sets
   safety_net_id_on_ui_thread_ on the UI thread.

Also, if chrome://safe-browsing is open, sends CRSBLOGs to DLOG also.

TBR=vakh@chromium.org

(cherry picked from commit 840d155daebddae95cc98d466361178223afdb90)

Bug: 907280
Change-Id: Id65ed62817f37ed911e5b3b8a623bc1e1c74ea5f
Reviewed-on: https://chromium-review.googlesource.com/c/1436186
Commit-Queue: Daniel Rubery <drubery@chromium.org>
Auto-Submit: Varun Khaneja <vakh@chromium.org>
Reviewed-by: Daniel Rubery <drubery@chromium.org>
Reviewed-by: Nathan Parker <nparker@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#626253}
Reviewed-on: https://chromium-review.googlesource.com/c/1452706
Reviewed-by: Varun Khaneja <vakh@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#178}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.cc b/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.cc
index e6c7959..a9e340d 100644
--- a/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.cc
+++ b/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.cc
@@ -8,6 +8,8 @@
 #include <memory>
 #include <utility>
 
+#include "base/bind.h"
+#include "base/feature_list.h"
 #include "base/files/file_path.h"
 #include "base/metrics/histogram_macros.h"
 #include "base/task/post_task.h"
@@ -18,6 +20,7 @@
 #include "components/keyed_service/core/service_access_type.h"
 #include "components/prefs/pref_service.h"
 #include "components/safe_browsing/common/safe_browsing_prefs.h"
+#include "components/safe_browsing/db/database_manager.h"
 #include "components/safe_browsing/features.h"
 #include "components/safe_browsing/ping_manager.h"
 #include "components/safe_browsing/web_ui/safe_browsing_ui.h"
@@ -90,7 +93,10 @@
 AndroidTelemetryService::AndroidTelemetryService(
     SafeBrowsingService* sb_service,
     Profile* profile)
-    : TelemetryService(), profile_(profile), sb_service_(sb_service) {
+    : TelemetryService(),
+      profile_(profile),
+      sb_service_(sb_service),
+      weak_ptr_factory_(this) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
   DCHECK(profile_);
   DCHECK(sb_service_);
@@ -125,6 +131,10 @@
     return;
   }
 
+  // The report can be sent. Try capturing the safety net ID. This should
+  // complete before the download completes, but is not guaranteed, That's OK.
+  MaybeCaptureSafetyNetId();
+
   item->AddObserver(this);
 }
 
@@ -224,6 +234,21 @@
       recent_navigations_to_collect, report->mutable_referrer_chain());
 }
 
+void AndroidTelemetryService::MaybeCaptureSafetyNetId() {
+  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+  DCHECK(sb_service_->database_manager());
+  if (!safety_net_id_on_ui_thread_.empty()) {
+    return;
+  }
+
+  base::PostTaskWithTraitsAndReplyWithResult(
+      FROM_HERE, {content::BrowserThread::IO},
+      base::BindOnce(&SafeBrowsingDatabaseManager::GetSafetyNetId,
+                     sb_service_->database_manager()),
+      base::BindOnce(&AndroidTelemetryService::SetSafetyNetIdOnUIThread,
+                     weak_ptr_factory_.GetWeakPtr()));
+}
+
 void AndroidTelemetryService::MaybeSendApkDownloadReport(
     download::DownloadItem* item) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
@@ -248,7 +273,6 @@
   mutable_download_item_info->set_length(item->GetReceivedBytes());
   mutable_download_item_info->set_file_basename(
       item->GetTargetFilePath().BaseName().value());
-  // TODO(vakh): Capture |safety_net_id_on_ui_thread_|. It is unset currently.
   report->set_safety_net_id(safety_net_id_on_ui_thread_);
 
   std::string serialized;
@@ -269,4 +293,9 @@
   RecordApkDownloadTelemetryOutcome(ApkDownloadTelemetryOutcome::SENT);
 }
 
+void AndroidTelemetryService::SetSafetyNetIdOnUIThread(const std::string& sid) {
+  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+  safety_net_id_on_ui_thread_ = sid;
+}
+
 }  // namespace safe_browsing
diff --git a/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.h b/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.h
index 0b17ac2..682813b 100644
--- a/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.h
+++ b/chrome/browser/safe_browsing/telemetry/android/android_telemetry_service.h
@@ -7,6 +7,7 @@
 
 #include <string>
 
+#include "base/memory/weak_ptr.h"
 #include "chrome/browser/safe_browsing/telemetry/telemetry_service.h"
 #include "components/download/public/common/download_item.h"
 #include "components/safe_browsing/proto/csd.pb.h"
@@ -59,11 +60,20 @@
   void FillReferrerChain(content::WebContents* web_contents,
                          ClientSafeBrowsingReportRequest* report);
 
+  // If |safety_net_id_on_ui_thread_| isn't already set, post a task on the IO
+  // thread to get the safety net ID of the device and then store that value on
+  // the UI thread in |safety_net_id_on_ui_thread_|.
+  void MaybeCaptureSafetyNetId();
+
   // Sets the relevant fields in an instance of
   // |ClientSafeBrowsingReportRequest| and sends it to the Safe Browsing
   // backend. The report may not be sent if the proto fails to serialize.
   void MaybeSendApkDownloadReport(download::DownloadItem* item);
 
+  // Gets called on the UI thread when the |safety_net_id| of the device has
+  // been captured. Sets |safety_net_id_on_ui_thread_| to the captured value.
+  void SetSafetyNetIdOnUIThread(const std::string& safety_net_id);
+
   // Helper method to get prefs from |profile_|.
   const PrefService* GetPrefs();
 
@@ -78,6 +88,8 @@
   // Unowned.
   SafeBrowsingService* sb_service_;
 
+  base::WeakPtrFactory<AndroidTelemetryService> weak_ptr_factory_;
+
   DISALLOW_COPY_AND_ASSIGN(AndroidTelemetryService);
 };
 
diff --git a/components/safe_browsing/web_ui/safe_browsing_ui.cc b/components/safe_browsing/web_ui/safe_browsing_ui.cc
index 0601e17..55ba006 100644
--- a/components/safe_browsing/web_ui/safe_browsing_ui.cc
+++ b/components/safe_browsing/web_ui/safe_browsing_ui.cc
@@ -1321,6 +1321,7 @@
 
 CrSBLogMessage::~CrSBLogMessage() {
   WebUIInfoSingleton::GetInstance()->LogMessage(stream_.str());
+  DLOG(WARNING) << stream_.str();
 }
 
 }  // namespace safe_browsing