Commit 840d155d authored by Varun Khaneja's avatar Varun Khaneja Committed by Commit Bot

Capture Safety Net ID if the 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.

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: default avatarDaniel Rubery <drubery@chromium.org>
Reviewed-by: default avatarNathan Parker <nparker@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626253}
parent d003ce09
......@@ -9,6 +9,7 @@
#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"
......@@ -19,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"
......@@ -91,7 +93,10 @@ void RecordApkDownloadTelemetryIncompleteReason(
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_);
......@@ -126,6 +131,10 @@ void AndroidTelemetryService::OnDownloadCreated(
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);
}
......@@ -225,6 +234,21 @@ void AndroidTelemetryService::FillReferrerChain(
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);
......@@ -249,7 +273,6 @@ void AndroidTelemetryService::MaybeSendApkDownloadReport(
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;
......@@ -270,4 +293,9 @@ void AndroidTelemetryService::MaybeSendApkDownloadReport(
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
......@@ -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 @@ class AndroidTelemetryService : public download::DownloadItem::Observer,
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 @@ class AndroidTelemetryService : public download::DownloadItem::Observer,
// Unowned.
SafeBrowsingService* sb_service_;
base::WeakPtrFactory<AndroidTelemetryService> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(AndroidTelemetryService);
};
......
......@@ -1322,6 +1322,7 @@ CrSBLogMessage::CrSBLogMessage() {}
CrSBLogMessage::~CrSBLogMessage() {
WebUIInfoSingleton::GetInstance()->LogMessage(stream_.str());
DLOG(WARNING) << stream_.str();
}
} // namespace safe_browsing
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment