1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_DOWNLOAD_REQUEST_MAKER_H_
#define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_DOWNLOAD_REQUEST_MAKER_H_
#include <memory>
#include <optional>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/safe_browsing/download_protection/file_analyzer.h"
#include "components/download/public/common/download_item.h"
#include "components/history/core/browser/history_service.h"
#include "components/safe_browsing/content/browser/safe_browsing_navigation_observer_manager.h"
#include "components/safe_browsing/core/common/proto/csd.pb.h"
#include "content/public/browser/file_system_access_write_item.h"
namespace safe_browsing {
// This class encapsulate the process of populating all the fields in a Safe
// Browsing download ping.
class DownloadRequestMaker {
public:
// Details about a DownloadRequestMaker run, to pass back to the caller.
struct RequestCreationDetails {
// What type of file inspection was performed.
DownloadFileType::InspectionType inspection_type = DownloadFileType::NONE;
};
using Callback =
base::OnceCallback<void(std::unique_ptr<ClientDownloadRequest>)>;
using CallbackWithDetails =
base::OnceCallback<void(RequestCreationDetails,
std::unique_ptr<ClientDownloadRequest>)>;
// URL and referrer of the window the download was started from.
struct TabUrls {
GURL url;
GURL referrer;
};
static std::unique_ptr<DownloadRequestMaker> CreateFromDownloadItem(
scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor,
download::DownloadItem* item,
base::optional_ref<const std::string> password = std::nullopt);
static std::unique_ptr<DownloadRequestMaker> CreateFromFileSystemAccess(
scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor,
const content::FileSystemAccessWriteItem& item);
DownloadRequestMaker(
scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor,
content::BrowserContext* browser_context,
TabUrls tab_urls,
base::FilePath target_file_name,
base::FilePath full_path,
GURL source_url,
std::string sha256_hash,
int64_t length,
const std::vector<ClientDownloadRequest::Resource>& resources,
bool is_user_initiated,
ReferrerChainData* referrer_chain_data,
base::optional_ref<const std::string> password,
const std::string& previous_token,
base::OnceCallback<void(const FileAnalyzer::Results&)>
on_results_callback);
DownloadRequestMaker(const DownloadRequestMaker&) = delete;
DownloadRequestMaker& operator=(const DownloadRequestMaker&) = delete;
~DownloadRequestMaker();
// Starts filling in fields in the download ping. Will run the callback with
// the fully-populated ping.
void Start(Callback callback);
// Same as above but also returns a RequestCreationDetails to the caller.
void Start(CallbackWithDetails callback);
private:
// Callback when |file_analyzer_| is done analyzing the download.
void OnFileFeatureExtractionDone(FileAnalyzer::Results results);
// Helper function to get the tab redirects from the history service.
void GetTabRedirects();
// Callback when the history service has retrieved the tab redirects.
void OnGotTabRedirects(history::RedirectList redirect_list);
// Populates the tailored info field for tailored warnings.
void PopulateTailoredInfo();
raw_ptr<content::BrowserContext> browser_context_;
std::unique_ptr<ClientDownloadRequest> request_;
const scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor_;
const std::unique_ptr<FileAnalyzer> file_analyzer_ =
std::make_unique<FileAnalyzer>(binary_feature_extractor_);
base::CancelableTaskTracker request_tracker_; // For HistoryService lookup.
// The current URL for the WebContents that initiated the download, and its
// referrer.
TabUrls tab_urls_;
// The ultimate destination/filename for the download. This is used to
// determine the filetype and populate request fields requiring a
// human-readable filename. On platforms where the target file *path* does not
// contain file name but is instead a different identifier (e.g. a content-URI
// on Android), this should be populated with the file *name* i.e. a display
// name for the download.
const base::FilePath target_file_name_;
// The current path to the file contents.
const base::FilePath full_path_;
const std::optional<std::string> password_;
// Callback used for handling behavior specific to download items of file
// system accesses.
base::OnceCallback<void(const FileAnalyzer::Results&)> on_results_callback_;
CallbackWithDetails callback_;
RequestCreationDetails details_;
base::WeakPtrFactory<DownloadRequestMaker> weakptr_factory_{this};
};
} // namespace safe_browsing
#endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_DOWNLOAD_REQUEST_MAKER_H_
|