chromium/chrome/browser/safe_browsing/extension_telemetry/extension_telemetry_service.cc

// 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.

#include "chrome/browser/safe_browsing/extension_telemetry/extension_telemetry_service.h"

#include <sstream>
#include <vector>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/i18n/time_formatting.h"
#include "base/json/values_util.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/sequence_bound.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/enterprise/connectors/connectors_service.h"
#include "chrome/browser/enterprise/connectors/reporting/extension_telemetry_event_router.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/extension_telemetry/cookies_get_all_signal_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/cookies_get_signal_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/declarative_net_request_action_signal_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/declarative_net_request_signal_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/extension_js_callstacks.h"
#include "chrome/browser/safe_browsing/extension_telemetry/extension_signal.h"
#include "chrome/browser/safe_browsing/extension_telemetry/extension_telemetry_config_manager.h"
#include "chrome/browser/safe_browsing/extension_telemetry/extension_telemetry_file_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/extension_telemetry_persister.h"
#include "chrome/browser/safe_browsing/extension_telemetry/extension_telemetry_service_factory.h"
#include "chrome/browser/safe_browsing/extension_telemetry/extension_telemetry_uploader.h"
#include "chrome/browser/safe_browsing/extension_telemetry/potential_password_theft_signal_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/remote_host_contacted_signal_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/tabs_api_signal_processor.h"
#include "chrome/browser/safe_browsing/extension_telemetry/tabs_execute_script_signal_processor.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/safe_browsing/core/browser/sync/safe_browsing_primary_account_token_fetcher.h"
#include "components/safe_browsing/core/browser/sync/sync_utils.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/proto/csd.pb.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/blocklist_extension_prefs.h"
#include "extensions/browser/blocklist_state.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/path_util.h"
#include "extensions/common/file_util.h"
#include "extensions/common/mojom/manifest.mojom-shared.h"
#include "extensions/common/switches.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace safe_browsing {

namespace {

ExtensionManagement;
ManifestLocation;
RepeatedPtrField;
ExtensionInfo;
OffstoreExtensionVerdict;

// The ExtensionTelemetryService saves offstore extensions file data such as
// filenames and hashes in Prefs. This information is stored in the following
// dictionary format:
// {
//  ...
//  kExtensionTelemetryFileData : {
//    <extension_id_1> : {
//      "last_processed_timestamp" : <timestamp>,
//      "file_data" : {
//        <file_path_1> : <file_hash_1>,
//        <file_path_2> : <file_hash_2>,
//        ...
//        <manifest.json> : <file_contents>
//      }
//    },
//    <extension_id_2> : {
//      "last_processed_timestamp" : <timestamp>,
//      "file_data" : {
//        <file_path_1> : <file_hash_1>,
//        <file_path_2> : <file_hash_2>,
//        ...
//        <manifest.json> : <file_contents>
//      }
//    },
//    ...
//  },
//  ...
// }

constexpr char kFileDataProcessTimestampPref[] =;
constexpr char kFileDataDictPref[] =;
constexpr char kManifestFile[] =;

// Specifies the default for `num_checks_per_upload_interval_`
// NOTE: With a value of 1, the telemetry service will perform checks at the
// upload interval itself and will only write a report to disk if the upload
// fails.
constexpr int kNumChecksPerUploadInterval =;

// Specifies the upload interval for ESB telemetry reports.
base::TimeDelta kUploadIntervalSeconds =;

// Delay before the Telemetry Service checks its last upload time.
base::TimeDelta kStartupUploadCheckDelaySeconds =;

// Interval for extension telemetry to start another off-store extension
// file data collection process.
base::TimeDelta kOffstoreFileDataCollectionIntervalSeconds =;

// Initial delay for extension telemetry to start collecting
// off-store extension file data.
base::TimeDelta kOffstoreFileDataCollectionStartupDelaySeconds =;

// Limit the off-store file data collection duration.
base::TimeDelta kOffstoreFileDataCollectionDurationLimitSeconds =;

void RecordWhenFileWasPersisted(bool persisted_at_write_interval) {}

void RecordNumOffstoreExtensions(int num_extensions) {}

void RecordCollectionDuration(base::TimeDelta duration) {}

void RecordSignalTypeForEnterprise(ExtensionSignalType signal_type) {}

void RecordEnterpriseReportSize(size_t size) {}

static_assert;
ExtensionInfo::Type GetType(extensions::Manifest::Type type) {}

static_assert;
ExtensionInfo::InstallLocation GetInstallLocation(ManifestLocation location) {}

static_assert;
ExtensionTelemetryReportRequest::ManagementAuthorityTrustworthiness
GetManagementAuthorityTrustworthiness(
    policy::ManagementAuthorityTrustworthiness
        management_authority_trustworthiness) {}

ExtensionInfo::InstallationPolicy
ExtensionManagementInstallationModeToExtensionInfoInstallationPolicy(
    const ExtensionManagement::InstallationMode& installation_mode) {}

ExtensionInfo::BlocklistState BitMapBlocklistStateToExtensionInfoBlocklistState(
    const extensions::BitMapBlocklistState& state) {}

ExtensionInfo::BlocklistState GetBlocklistState(
    const extensions::ExtensionId extension_id,
    extensions::ExtensionPrefs* extension_prefs) {}

ExtensionInfo::BlocklistState GetExtensionTelemetryServiceBlocklistState(
    const extensions::ExtensionId extension_id,
    extensions::ExtensionPrefs* extension_prefs) {}

extensions::BlocklistState ConvertTelemetryResponseVerdictToBlocklistState(
    OffstoreExtensionVerdict::OffstoreExtensionVerdictType type) {}

// Checks for extensions specified in the -load-extension commandline switch.
// Creates and returns a set of extension objects for such extensions.
// NOTE: These extensions are not installed - the information is only collected
// for telemetry purposes.
// This function is executed on a separate thread from the UI thread since it
// involves reading the manifest files from disk.
extensions::ExtensionSet CollectCommandLineExtensionInfo() {}

// Retrieves the ExtensionTelemetryEventRouter associated with the profile.
enterprise_connectors::ExtensionTelemetryEventRouter*
GetExtensionTelemetryEventRouter(Profile* profile) {}

// Returns true if the signal type should be collected for enterprise telemetry.
bool CollectForEnterprise(ExtensionSignalType type) {}

}  // namespace

// Adds extension installation mode and managed status to extension telemetry
// reports.
BASE_FEATURE();

ExtensionTelemetryService::~ExtensionTelemetryService() = default;

// static
ExtensionTelemetryService* ExtensionTelemetryService::Get(Profile* profile) {}

ExtensionTelemetryService::ExtensionTelemetryService(
    Profile* profile,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
    :{}

void ExtensionTelemetryService::RecordSignalType(
    ExtensionSignalType signal_type) {}

void ExtensionTelemetryService::RecordSignalDiscarded(
    ExtensionSignalType signal_type) {}

void ExtensionTelemetryService::OnESBPrefChanged() {}

void ExtensionTelemetryService::OnEnterprisePolicyChanged() {}

// Telemetry features for ESB include:
// - ESB signals
// - Off-store data collection
// - Command line extensions file data
// - Telemetry Configuration
// - Persister
void ExtensionTelemetryService::SetEnabledForESB(bool enable) {}

// Telemetry features for enterprise include:
// - Enterprise signals
// - Off-store data collection
void ExtensionTelemetryService::SetEnabledForEnterprise(bool enable) {}

bool ExtensionTelemetryService::enabled() const {}

void ExtensionTelemetryService::Shutdown() {}

bool ExtensionTelemetryService::SignalDataPresent() const {}

bool ExtensionTelemetryService::IsSignalEnabled(
    const extensions::ExtensionId& extension_id,
    ExtensionSignalType signal_type) const {}

void ExtensionTelemetryService::AddSignal(
    std::unique_ptr<ExtensionSignal> signal) {}

void ExtensionTelemetryService::AddSignalHelper(
    const ExtensionSignal& signal,
    ExtensionStore& store,
    SignalSubscribers& subscribers) {}

std::unique_ptr<ExtensionTelemetryReportRequest>
ExtensionTelemetryService::CreateReportWithCommonFieldsPopulated() {}

void ExtensionTelemetryService::CreateAndUploadReport() {}

void ExtensionTelemetryService::CreateAndSendEnterpriseReport() {}

void ExtensionTelemetryService::OnUploadComplete(
    bool success,
    const std::string& response_data) {}

void ExtensionTelemetryService::UploadPersistedFile(std::string report) {}

void ExtensionTelemetryService::UploadReport(
    std::unique_ptr<std::string> report) {}

void ExtensionTelemetryService::StartUploadCheck() {}

void ExtensionTelemetryService::PersistOrUploadData() {}

std::unique_ptr<ExtensionTelemetryReportRequest>
ExtensionTelemetryService::CreateReport() {}

std::unique_ptr<ExtensionTelemetryReportRequest>
ExtensionTelemetryService::CreateReportForEnterprise() {}

std::unique_ptr<SafeBrowsingTokenFetcher>
ExtensionTelemetryService::GetTokenFetcher() {}

void ExtensionTelemetryService::DumpReportForTesting(
    const ExtensionTelemetryReportRequest& report) {}

ExtensionTelemetryService::OffstoreExtensionFileData::
    OffstoreExtensionFileData() = default;
ExtensionTelemetryService::OffstoreExtensionFileData::
    ~OffstoreExtensionFileData() = default;
ExtensionTelemetryService::OffstoreExtensionFileData::
    OffstoreExtensionFileData::OffstoreExtensionFileData(
        const OffstoreExtensionFileData& src) = default;

std::optional<ExtensionTelemetryService::OffstoreExtensionFileData>
ExtensionTelemetryService::RetrieveOffstoreFileDataForReport(
    const extensions::ExtensionId& extension_id) {}

std::unique_ptr<ExtensionInfo>
ExtensionTelemetryService::GetExtensionInfoForReport(
    const extensions::Extension& extension) {}

void ExtensionTelemetryService::SetUpSignalProcessorsAndSubscribersForESB() {}

void ExtensionTelemetryService::
    SetUpSignalProcessorsAndSubscribersForEnterprise() {}

ExtensionTelemetryService::OffstoreExtensionFileDataContext::
    OffstoreExtensionFileDataContext(
        const extensions::ExtensionId& extension_id,
        const base::FilePath& root_dir)
    :{}

ExtensionTelemetryService::OffstoreExtensionFileDataContext::
    OffstoreExtensionFileDataContext(
        const extensions::ExtensionId& extension_id,
        const base::FilePath& root_dir,
        const base::Time& last_processed_time)
    :{}

bool ExtensionTelemetryService::OffstoreExtensionFileDataContext::operator<(
    const OffstoreExtensionFileDataContext& other) const {}

void ExtensionTelemetryService::SetUpOffstoreFileDataCollection() {}

void ExtensionTelemetryService::StartOffstoreFileDataCollection() {}

void ExtensionTelemetryService::OnCommandLineExtensionsInfoCollected(
    extensions::ExtensionSet commandline_extensions) {}

void ExtensionTelemetryService::GetOffstoreExtensionDirs() {}

void ExtensionTelemetryService::RemoveStaleExtensionsFileDataFromPref() {}

void ExtensionTelemetryService::CollectOffstoreFileData() {}

void ExtensionTelemetryService::OnOffstoreFileDataCollected(
    base::flat_set<OffstoreExtensionFileDataContext>::iterator context,
    base::Value::Dict file_data) {}

void ExtensionTelemetryService::StopOffstoreFileDataCollection() {}

void ExtensionTelemetryService::ProcessOffstoreExtensionVerdicts(
    const ExtensionTelemetryReportResponse& response) {}

base::TimeDelta
ExtensionTelemetryService::GetOffstoreFileDataCollectionStartupDelaySeconds() {}

base::TimeDelta
ExtensionTelemetryService::GetOffstoreFileDataCollectionIntervalSeconds() {}

}  // namespace safe_browsing