blob: e32039e461341653244ca6cbac986e293479bca1 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2020 The Chromium Authors
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:132// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_ENTERPRISE_CONNECTORS_CONNECTORS_MANAGER_H_
6#define CHROME_BROWSER_ENTERPRISE_CONNECTORS_CONNECTORS_MANAGER_H_
7
Keishi Hattori0e45c022021-11-27 09:25:528#include "base/memory/raw_ptr.h"
Dominique Fauteux-Chapleaud280fd52021-02-03 20:06:009#include "chrome/browser/enterprise/connectors/analysis/analysis_service_settings.h"
Dominique Fauteux-Chapleau5ae64242020-04-17 13:18:2610#include "chrome/browser/enterprise/connectors/common.h"
Shanthanu Bhardwajadf4f342d2022-07-19 17:16:5511#include "chrome/browser/enterprise/connectors/reporting/extension_install_event_router.h"
Dominique Fauteux-Chapleaud280fd52021-02-03 20:06:0012#include "chrome/browser/enterprise/connectors/reporting/reporting_service_settings.h"
Dominique Fauteux-Chapleau76b366a2020-05-28 17:15:0613#include "chrome/browser/enterprise/connectors/service_provider_config.h"
Dominique Fauteux-Chapleau29775702020-04-30 15:50:3614#include "components/prefs/pref_change_registrar.h"
Dominique Fauteux-Chapleau130242bf2020-12-07 19:07:4815#include "components/prefs/pref_service.h"
Anton Bikineev46bbb972021-05-15 17:53:5316#include "third_party/abseil-cpp/absl/types/optional.h"
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1317#include "url/gurl.h"
18
Steffen Seckler6ef24b82022-07-22 08:46:0719namespace storage {
20class FileSystemURL;
21}
22
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1323namespace enterprise_connectors {
Haifan Wang30d3f692022-08-18 14:51:2424class BrowserCrashEventRouter;
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1325
Dominique Fauteux-Chapleau130242bf2020-12-07 19:07:4826// Manages access to Connector policies for a given profile. This class is
27// responsible for caching the Connector policies, validate them against
28// approved service providers and provide a simple interface to them.
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1329class ConnectorsManager {
30 public:
Roger Tawa445a9972020-05-20 22:44:0231 // Maps used to cache connectors settings.
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:4532 using AnalysisConnectorsSettings =
33 std::map<AnalysisConnector, std::vector<AnalysisServiceSettings>>;
Roger Tawa445a9972020-05-20 22:44:0234 using ReportingConnectorsSettings =
35 std::map<ReportingConnector, std::vector<ReportingServiceSettings>>;
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:4536
Haifan Wang30d3f692022-08-18 14:51:2437 ConnectorsManager(
38 std::unique_ptr<BrowserCrashEventRouter> browser_crash_event_router,
Shanthanu Bhardwaje943c4f2022-12-21 16:32:1339 std::unique_ptr<ExtensionInstallEventRouter> extension_install_router,
Haifan Wang30d3f692022-08-18 14:51:2440 PrefService* pref_service,
41 const ServiceProviderConfig* config,
42 bool observe_prefs = true);
Dominique Fauteux-Chapleau130242bf2020-12-07 19:07:4843 ~ConnectorsManager();
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1344
Roger Tawa445a9972020-05-20 22:44:0245 // Validates which settings should be applied to a reporting event
Dominique Fauteux-Chapleauc088fba2020-10-26 20:34:0846 // against cached policies. Cache the policy value the first time this is
47 // called for every different connector.
Anton Bikineev46bbb972021-05-15 17:53:5348 absl::optional<ReportingSettings> GetReportingSettings(
Roger Tawa445a9972020-05-20 22:44:0249 ReportingConnector connector);
50
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1351 // Validates which settings should be applied to an analysis connector event
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:4552 // against cached policies. This function will prioritize new connector
53 // policies over legacy ones if they are set.
Anton Bikineev46bbb972021-05-15 17:53:5354 absl::optional<AnalysisSettings> GetAnalysisSettings(
Dominique Fauteux-Chapleauc08c8c962020-05-07 18:46:2655 const GURL& url,
56 AnalysisConnector connector);
Steffen Seckler6ef24b82022-07-22 08:46:0757#if BUILDFLAG(IS_CHROMEOS_ASH)
58 absl::optional<AnalysisSettings> GetAnalysisSettings(
59 content::BrowserContext* context,
60 const storage::FileSystemURL& source_url,
61 const storage::FileSystemURL& destination_url,
62 AnalysisConnector connector);
63#endif // BUILDFLAG(IS_CHROMEOS_ASH)
Dominique Fauteux-Chapleauc08c8c962020-05-07 18:46:2664
Dominique Fauteux-Chapleauc08c8c962020-05-07 18:46:2665 // Checks if the corresponding connector is enabled.
Dominique Fauteux-Chapleauadf0b112020-05-25 13:24:0366 bool IsConnectorEnabled(AnalysisConnector connector) const;
67 bool IsConnectorEnabled(ReportingConnector connector) const;
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1368
Nancy Xiao8f7c4a32023-04-26 16:16:2769#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
70 // Check if the corresponding connector is enabled for any local agent.
71 bool IsConnectorEnabledForLocalAgent(AnalysisConnector connector) const;
72#endif
73
Dominique Fauteux-Chapleauadf0b112020-05-25 13:24:0374 bool DelayUntilVerdict(AnalysisConnector connector);
Anthony Vallee-Dubois86baa7fc42021-06-29 15:38:5575 absl::optional<std::u16string> GetCustomMessage(AnalysisConnector connector,
76 const std::string& tag);
77 absl::optional<GURL> GetLearnMoreUrl(AnalysisConnector connector,
78 const std::string& tag);
Sneha Nagpaul4325c7852022-07-19 13:51:3879 bool GetBypassJustificationRequired(AnalysisConnector connector,
80 const std::string& tag);
Dominique Fauteux-Chapleauccf53092020-04-08 17:15:2881
Anthony Vallee-Dubois53913ec2021-02-22 16:46:4482 std::vector<std::string> GetAnalysisServiceProviderNames(
83 AnalysisConnector connector);
Anthony Vallee-Dubois9a6a0402021-02-24 14:46:1684 std::vector<std::string> GetReportingServiceProviderNames(
85 ReportingConnector connector);
Anthony Vallee-Dubois53913ec2021-02-22 16:46:4486
Roger Tawa671dc432022-08-23 15:58:2187 std::vector<const AnalysisConfig*> GetAnalysisServiceConfigs(
88 AnalysisConnector connector);
89
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:4590 // Public testing functions.
91 const AnalysisConnectorsSettings& GetAnalysisConnectorsSettingsForTesting()
92 const;
Roger Tawa445a9972020-05-20 22:44:0293 const ReportingConnectorsSettings& GetReportingConnectorsSettingsForTesting()
94 const;
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:4595
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:1396 private:
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:4597 // Validates which settings should be applied to an analysis connector event
98 // against connector policies. Cache the policy value the first time this is
99 // called for every different connector.
Anton Bikineev46bbb972021-05-15 17:53:53100 absl::optional<AnalysisSettings> GetAnalysisSettingsFromConnectorPolicy(
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:45101 const GURL& url,
Dominique Fauteux-Chapleauc08c8c962020-05-07 18:46:26102 AnalysisConnector connector);
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:45103
104 // Read and cache the policy corresponding to |connector|.
Roger Tawa3583e9c2023-02-24 14:48:24105 void CacheAnalysisConnectorPolicy(AnalysisConnector connector) const;
Roger Tawa445a9972020-05-20 22:44:02106 void CacheReportingConnectorPolicy(ReportingConnector connector);
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:45107
Nancy Xiao8f7c4a32023-04-26 16:16:27108#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
109 // Close connection with local agent if all the relevant connectors are turned
110 // off for it.
111 void MaybeCloseLocalContentAnalysisAgentConnection();
112#endif // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
113
114 // Re-cache analysis connector policy and update local agent connection if
115 // needed.
116 void OnPrefChanged(AnalysisConnector connector);
117
Dominique Fauteux-Chapleau130242bf2020-12-07 19:07:48118 // Sets up |pref_change_registrar_|. Used by the constructor and
119 // SetUpForTesting.
120 void StartObservingPrefs(PrefService* pref_service);
Dominique Fauteux-Chapleau045c37d2020-05-05 12:51:40121 void StartObservingPref(AnalysisConnector connector);
Roger Tawa445a9972020-05-20 22:44:02122 void StartObservingPref(ReportingConnector connector);
Dominique Fauteux-Chapleau29775702020-04-30 15:50:36123
Dominique Fauteux-Chapleau12381552020-09-21 18:45:55124 // Validates which settings should be applied to an analysis connector event
125 // against connector policies. Cache the policy value the first time this is
126 // called for every different connector.
Anton Bikineev46bbb972021-05-15 17:53:53127 absl::optional<ReportingSettings> GetReportingSettingsFromConnectorPolicy(
Dominique Fauteux-Chapleau12381552020-09-21 18:45:55128 ReportingConnector connector);
129
Roger Tawa3583e9c2023-02-24 14:48:24130 PrefService* prefs() { return pref_change_registrar_.prefs(); }
131
132 const PrefService* prefs() const { return pref_change_registrar_.prefs(); }
133
Dominique Fauteux-Chapleau76b366a2020-05-28 17:15:06134 // Cached values of available service providers. This information validates
135 // the Connector policies have a valid provider.
Dominique Fauteux-Chapleau5bc510e52022-06-02 12:29:23136 raw_ptr<const ServiceProviderConfig> service_provider_config_;
Dominique Fauteux-Chapleau76b366a2020-05-28 17:15:06137
Dominique Fauteux-Chapleaucb08fe42020-04-23 19:57:45138 // Cached values of the connector policies. Updated when a connector is first
Roger Tawa3583e9c2023-02-24 14:48:24139 // used or when a policy is updated. Analysis connectors settings are
140 // mutable because they maybe updated by a call to IsConnectorEnabled(),
141 // which is a const method.
142 mutable AnalysisConnectorsSettings analysis_connector_settings_;
Roger Tawa445a9972020-05-20 22:44:02143 ReportingConnectorsSettings reporting_connector_settings_;
Dominique Fauteux-Chapleau29775702020-04-30 15:50:36144
145 // Used to track changes of connector policies and propagate them in
146 // |connector_settings_|.
147 PrefChangeRegistrar pref_change_registrar_;
Shanthanu Bhardwajadf4f342d2022-07-19 17:16:55148
Haifan Wang30d3f692022-08-18 14:51:24149 // A router to report browser crash events via the reporting pipeline.
150 std::unique_ptr<BrowserCrashEventRouter> browser_crash_event_router_;
151
Shanthanu Bhardwajadf4f342d2022-07-19 17:16:55152 // An observer to report extension install events via the reporting pipeline.
Shanthanu Bhardwaje943c4f2022-12-21 16:32:13153 std::unique_ptr<ExtensionInstallEventRouter> extension_install_event_router_;
Dominique Fauteux-Chapleauc2d0a172020-04-01 20:04:13154};
155
156} // namespace enterprise_connectors
157
158#endif // CHROME_BROWSER_ENTERPRISE_CONNECTORS_CONNECTORS_MANAGER_H_