diff --git a/atom/browser/BUILD.gn b/atom/browser/BUILD.gn index c8cadb33a5..939ff75866 100644 --- a/atom/browser/BUILD.gn +++ b/atom/browser/BUILD.gn @@ -26,17 +26,23 @@ source_set("browser") { # @todo(bridiver) fix circular dep # "//electron/muon/browser", "//base", + "//chrome/browser:resource_prefetch_predictor_proto", "//chrome/common:constants", "//storage/browser", "//storage/common", "//components/prefs", "//components/metrics", + "//components/safe_browsing/common:safe_browsing_prefs", ":importer", "//electron/vendor/ad-block/muon:ad_block", "//electron/vendor/tracking-protection/muon:tp_node_addon", ] sources = [ + "//chrome/browser/loader/safe_browsing_resource_throttle.h", + "//chrome/browser/loader/safe_browsing_resource_throttle.cc", + "//chrome/browser/safe_browsing/url_checker_delegate_impl.cc", + "//chrome/browser/safe_browsing/url_checker_delegate_impl.h", "api/atom_api_app.cc", "api/atom_api_app.h", "api/atom_api_autofill.cc", diff --git a/atom/browser/atom_resource_dispatcher_host_delegate.cc b/atom/browser/atom_resource_dispatcher_host_delegate.cc index 98c3dcce09..9a8ba40bb2 100644 --- a/atom/browser/atom_resource_dispatcher_host_delegate.cc +++ b/atom/browser/atom_resource_dispatcher_host_delegate.cc @@ -10,7 +10,16 @@ #include "atom/browser/web_contents_permission_helper.h" #include "atom/common/platform_util.h" #include "base/strings/utf_string_conversions.h" +#include "chrome/browser/browser_process_impl.h" +#include "chrome/browser/loader/predictor_resource_throttle.h" +#include "chrome/browser/loader/safe_browsing_resource_throttle.h" +#include "chrome/browser/safe_browsing/safe_browsing_service.h" +#include "components/offline_pages/features/features.h" +#include "components/policy/core/common/cloud/policy_header_io_helper.h" +#include "components/variations/net/variations_http_headers.h" #include "content/public/browser/browser_thread.h" +#include "content/public/browser/resource_request_info.h" + #include "net/base/escape.h" #include "net/ssl/client_cert_store.h" #include "url/gurl.h" @@ -24,6 +33,8 @@ #endif using content::BrowserThread; +using content::ResourceRequestInfo; +using content::ResourceType; namespace atom { @@ -61,7 +72,93 @@ void HandleExternalProtocolInUI( } // namespace -AtomResourceDispatcherHostDelegate::AtomResourceDispatcherHostDelegate() { +AtomResourceDispatcherHostDelegate::AtomResourceDispatcherHostDelegate() : + safe_browsing_(g_browser_process->safe_browsing_service()) { +} + +void AtomResourceDispatcherHostDelegate::RequestBeginning( + net::URLRequest* request, + content::ResourceContext* resource_context, + content::AppCacheService* appcache_service, + ResourceType resource_type, + std::vector>* throttles) { + if (safe_browsing_.get()) + safe_browsing_->OnResourceRequest(request); + + const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); + +#if BUILDFLAG(ENABLE_OFFLINE_PAGES) + // TODO(petewil): Unify the safe browsing request and the metrics observer + // request if possible so we only have to cross to the main thread once. + // http://crbug.com/712312. + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, + base::BindOnce(&NotifyUIThreadOfRequestStarted, + info->GetWebContentsGetterForRequest(), + info->GetResourceType())); +#endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) + +#if defined(OS_ANDROID) + if (resource_type != content::RESOURCE_TYPE_MAIN_FRAME) + InterceptNavigationDelegate::UpdateUserGestureCarryoverInfo(request); +#endif + +#if defined(OS_CHROMEOS) + // Check if we need to add merge session throttle. This throttle will postpone + // loading of XHR requests. + if (resource_type == content::RESOURCE_TYPE_XHR) { + // Add interstitial page while merge session process (cookie + // reconstruction from OAuth2 refresh token in ChromeOS login) is still in + // progress while we are attempting to load a google property. + if (!merge_session_throttling_utils::AreAllSessionMergedAlready() && + request->url().SchemeIsHTTPOrHTTPS()) { + throttles->push_back( + base::MakeUnique(request)); + } + } +#endif + + // Don't attempt to append headers to requests that have already started. + // TODO(stevet): Remove this once the request ordering issues are resolved + // in crbug.com/128048. + if (!request->is_pending()) { + net::HttpRequestHeaders headers; + headers.CopyFrom(request->extra_request_headers()); + request->SetExtraRequestHeaders(headers); + } + + AppendStandardResourceThrottles(request, + resource_context, + resource_type, + throttles); +#if BUILDFLAG(ENABLE_NACL) + AppendComponentUpdaterThrottles(request, *info, resource_context, + resource_type, throttles); +#endif // BUILDFLAG(ENABLE_NACL) +} + +void AtomResourceDispatcherHostDelegate::AppendStandardResourceThrottles( + net::URLRequest* request, + content::ResourceContext* resource_context, + ResourceType resource_type, + std::vector>* throttles) { + + // Insert either safe browsing or data reduction proxy throttle at the front + // of the list, so one of them gets to decide if the resource is safe. + content::ResourceThrottle* first_throttle = NULL; +#if defined(OS_ANDROID) + first_throttle = DataReductionProxyResourceThrottle::MaybeCreate( + request, resource_context, resource_type, safe_browsing_.get()); +#endif // defined(OS_ANDROID) + +#if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) + if (!first_throttle) { + first_throttle = MaybeCreateSafeBrowsingResourceThrottle( + request, resource_type, safe_browsing_.get()); + } +#endif // defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) + + if (first_throttle) + throttles->push_back(base::WrapUnique(first_throttle)); } bool AtomResourceDispatcherHostDelegate::HandleExternalProtocol( diff --git a/atom/browser/atom_resource_dispatcher_host_delegate.h b/atom/browser/atom_resource_dispatcher_host_delegate.h index f021e69549..6a409ae0b9 100644 --- a/atom/browser/atom_resource_dispatcher_host_delegate.h +++ b/atom/browser/atom_resource_dispatcher_host_delegate.h @@ -6,9 +6,15 @@ #define ATOM_BROWSER_ATOM_RESOURCE_DISPATCHER_HOST_DELEGATE_H_ #include +#include +#include "base/memory/ref_counted.h" #include "content/public/browser/resource_dispatcher_host_delegate.h" +namespace safe_browsing { +class SafeBrowsingService; +} + namespace atom { class AtomResourceDispatcherHostDelegate @@ -16,6 +22,15 @@ class AtomResourceDispatcherHostDelegate public: AtomResourceDispatcherHostDelegate(); + void RequestBeginning(net::URLRequest* request, + content::ResourceContext* resource_context, + content::AppCacheService* appcache_service, + content::ResourceType resource_type, + std::vector>* + throttles) override; + + + // content::ResourceDispatcherHostDelegate: bool HandleExternalProtocol( const GURL& url, @@ -25,6 +40,16 @@ class AtomResourceDispatcherHostDelegate net::URLRequest* request) override; std::unique_ptr CreateClientCertStore( content::ResourceContext* resource_context) override; + + protected: + virtual void AppendStandardResourceThrottles( + net::URLRequest* request, + content::ResourceContext* resource_context, + content::ResourceType resource_type, + std::vector>* throttles); + + private: + scoped_refptr safe_browsing_; }; } // namespace atom diff --git a/brave/browser/brave_browser_context.cc b/brave/browser/brave_browser_context.cc index dfee246b31..9f0468da22 100644 --- a/brave/browser/brave_browser_context.cc +++ b/brave/browser/brave_browser_context.cc @@ -36,6 +36,7 @@ #include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_filter.h" #include "components/pref_registry/pref_registry_syncable.h" +#include "components/safe_browsing/common/safe_browsing_prefs.h" #include "components/sync_preferences/pref_service_syncable.h" #include "components/sync_preferences/pref_service_syncable_factory.h" #include "components/user_prefs/user_prefs.h" @@ -449,6 +450,7 @@ void BraveBrowserContext::CreateProfilePrefs( pref_registry_->RegisterDictionaryPref(prefs::kPartitionPerHostZoomLevels); pref_registry_->RegisterBooleanPref(prefs::kPrintingEnabled, true); pref_registry_->RegisterBooleanPref(prefs::kPrintPreviewDisabled, false); + pref_registry_->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true); #if BUILDFLAG(ENABLE_PLUGINS) PluginInfoMessageFilter::RegisterUserPrefs(pref_registry_.get()); PepperFlashSettingsManager::RegisterProfilePrefs(pref_registry_.get()); @@ -724,4 +726,3 @@ AtomBrowserContext* AtomBrowserContext::From( } } // namespace atom - diff --git a/brave/browser/password_manager/brave_password_manager_client.cc b/brave/browser/password_manager/brave_password_manager_client.cc index 86684443c3..30875668d0 100644 --- a/brave/browser/password_manager/brave_password_manager_client.cc +++ b/brave/browser/password_manager/brave_password_manager_client.cc @@ -378,8 +378,11 @@ void BravePasswordManagerClient::CheckSafeBrowsingReputation( const GURL& form_action, const GURL& frame_url) {} +void BravePasswordManagerClient::LogPasswordReuseDetectedEvent() {} + void BravePasswordManagerClient::CheckProtectedPasswordEntry( - const std::string& password_saved_domain, + bool matches_sync_password, + const std::vector& matching_domains, bool password_field_exists) {} #endif diff --git a/brave/browser/password_manager/brave_password_manager_client.h b/brave/browser/password_manager/brave_password_manager_client.h index 4477db8fb4..b835d0594d 100644 --- a/brave/browser/password_manager/brave_password_manager_client.h +++ b/brave/browser/password_manager/brave_password_manager_client.h @@ -112,9 +112,12 @@ class BravePasswordManagerClient void CheckSafeBrowsingReputation(const GURL& form_action, const GURL& frame_url) override; void CheckProtectedPasswordEntry( - const std::string& password_saved_domain, + bool matches_sync_password, + const std::vector& matching_domains, bool password_field_exists) override; + void LogPasswordReuseDetectedEvent() override; + #endif password_manager::PasswordSyncState GetPasswordSyncState() const override; bool WasLastNavigationHTTPError() const override; diff --git a/chromium_src/BUILD.gn b/chromium_src/BUILD.gn index d30f13251d..f73e1969a9 100644 --- a/chromium_src/BUILD.gn +++ b/chromium_src/BUILD.gn @@ -147,7 +147,6 @@ source_set("browser") { ":password_manager", ":sessions", ":web_data", - "chrome/browser/safe_browsing", "//electron/brave/browser:tab_manager", "//base", "//chrome/common", @@ -163,7 +162,6 @@ source_set("browser") { "//components/prefs", "//components/ssl_config", "//components/spellcheck:build_features", - "//components/safe_browsing:csd_proto", "//components/storage_monitor", "//content/public/browser", "//electron/atom/browser", @@ -194,6 +192,8 @@ source_set("browser") { "//chrome/browser/extensions/global_shortcut_listener_mac.mm", "//chrome/browser/extensions/global_shortcut_listener_win.cc", "//chrome/browser/extensions/global_shortcut_listener_win.h", + "//chrome/browser/extensions/install_signer.cc", + "//chrome/browser/extensions/install_signer.h", "//chrome/browser/file_select_helper.cc", "//chrome/browser/file_select_helper_mac.mm", @@ -290,8 +290,118 @@ source_set("browser") { "chrome/browser/content_settings/host_content_settings_map_factory.cc", "chrome/browser/content_settings/host_content_settings_map_factory.h", - "//chrome/browser/ssl/security_state_tab_helper.cc", + "//chrome/browser/renderer_preferences_util.cc", + "//chrome/browser/renderer_preferences_util.h", + + "//chrome/browser/safe_browsing/chunk_range.cc", + "//chrome/browser/safe_browsing/chunk_range.h", + "//chrome/browser/safe_browsing/client_side_model_loader.cc", + "//chrome/browser/safe_browsing/client_side_model_loader.h", + "//chrome/browser/safe_browsing/client_side_detection_service.cc", + "//chrome/browser/safe_browsing/client_side_detection_service.h", + "//chrome/browser/safe_browsing/download_protection/check_client_download_request.cc", + "//chrome/browser/safe_browsing/download_protection/check_client_download_request.h", + "//chrome/browser/safe_browsing/download_protection/disk_image_type_sniffer_mac.cc", + "//chrome/browser/safe_browsing/download_protection/disk_image_type_sniffer_mac.h", + "//chrome/browser/safe_browsing/download_protection/download_feedback.cc", + "//chrome/browser/safe_browsing/download_protection/download_feedback.h", + "//chrome/browser/safe_browsing/download_protection/download_feedback_service.cc", + "//chrome/browser/safe_browsing/download_protection/download_feedback_service.h", + "//chrome/browser/safe_browsing/download_protection/download_protection_service.cc", + "//chrome/browser/safe_browsing/download_protection/download_protection_service.h", + "//chrome/browser/safe_browsing/download_protection/download_protection_util.cc", + "//chrome/browser/safe_browsing/download_protection/download_protection_util.h", + "//chrome/browser/safe_browsing/download_protection/download_url_sb_client.cc", + "//chrome/browser/safe_browsing/download_protection/download_url_sb_client.h", + "//chrome/browser/safe_browsing/download_protection/path_sanitizer.cc", + "//chrome/browser/safe_browsing/download_protection/path_sanitizer.h", + "//chrome/browser/safe_browsing/download_protection/ppapi_download_request.cc", + "//chrome/browser/safe_browsing/download_protection/ppapi_download_request.h", + "//chrome/browser/safe_browsing/download_protection/sandboxed_dmg_analyzer_mac.cc", + "//chrome/browser/safe_browsing/download_protection/sandboxed_dmg_analyzer_mac.h", + "//chrome/browser/safe_browsing/download_protection/sandboxed_zip_analyzer.cc", + "//chrome/browser/safe_browsing/download_protection/sandboxed_zip_analyzer.h", + "//chrome/browser/safe_browsing/download_protection/two_phase_uploader.cc", + "//chrome/browser/safe_browsing/download_protection/two_phase_uploader.h", + "//chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer.cc", + "//chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer.h", + "//chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer_mac.cc", + "//chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer_mac.h", + "//chrome/browser/safe_browsing/incident_reporting/binary_integrity_incident.cc", + "//chrome/browser/safe_browsing/incident_reporting/binary_integrity_incident.h", + "//chrome/browser/safe_browsing/incident_reporting/delayed_callback_runner.cc", + "//chrome/browser/safe_browsing/incident_reporting/delayed_callback_runner.h", + "//chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.cc", + "//chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.h", + "//chrome/browser/safe_browsing/incident_reporting/environment_data_collection.cc", + "//chrome/browser/safe_browsing/incident_reporting/environment_data_collection.h", + "//chrome/browser/safe_browsing/incident_reporting/extension_data_collection.cc", + "//chrome/browser/safe_browsing/incident_reporting/extension_data_collection.h", + "//chrome/browser/safe_browsing/incident_reporting/incident.cc", + "//chrome/browser/safe_browsing/incident_reporting/incident.h", + "//chrome/browser/safe_browsing/incident_reporting/incident_handler_util.cc", + "//chrome/browser/safe_browsing/incident_reporting/incident_handler_util.h", + "//chrome/browser/safe_browsing/incident_reporting/incident_report_uploader.cc", + "//chrome/browser/safe_browsing/incident_reporting/incident_report_uploader.h", + "//chrome/browser/safe_browsing/incident_reporting/incident_report_uploader_impl.cc", + "//chrome/browser/safe_browsing/incident_reporting/incident_report_uploader_impl.h", + "//chrome/browser/safe_browsing/incident_reporting/incident_reporting_service.cc", + "//chrome/browser/safe_browsing/incident_reporting/incident_reporting_service.h", + "//chrome/browser/safe_browsing/incident_reporting/last_download_finder.cc", + "//chrome/browser/safe_browsing/incident_reporting/last_download_finder.h", + "//chrome/browser/safe_browsing/incident_reporting/platform_state_store.cc", + "//chrome/browser/safe_browsing/incident_reporting/platform_state_store.h", + "//chrome/browser/safe_browsing/incident_reporting/preference_validation_delegate.cc", + "//chrome/browser/safe_browsing/incident_reporting/preference_validation_delegate.h", + "//chrome/browser/safe_browsing/incident_reporting/resource_request_detector.cc", + "//chrome/browser/safe_browsing/incident_reporting/resource_request_detector.h", + "//chrome/browser/safe_browsing/incident_reporting/resource_request_incident.cc", + "//chrome/browser/safe_browsing/incident_reporting/resource_request_incident.h", + "//chrome/browser/safe_browsing/incident_reporting/state_store.cc", + "//chrome/browser/safe_browsing/incident_reporting/state_store.h", + "//chrome/browser/safe_browsing/incident_reporting/tracked_preference_incident.cc", + "//chrome/browser/safe_browsing/incident_reporting/tracked_preference_incident.h", + "//chrome/browser/safe_browsing/notification_image_reporter.cc", + "//chrome/browser/safe_browsing/notification_image_reporter.h", + "//chrome/browser/safe_browsing/local_database_manager.cc", + "//chrome/browser/safe_browsing/local_database_manager.h", + "//chrome/browser/safe_browsing/permission_reporter.cc", + "//chrome/browser/safe_browsing/permission_reporter.h", + "//chrome/browser/safe_browsing/ping_manager.cc", + "//chrome/browser/safe_browsing/ping_manager.h", + "//chrome/browser/safe_browsing/protocol_manager.cc", + "//chrome/browser/safe_browsing/protocol_manager.h", + "//chrome/browser/safe_browsing/protocol_parser.cc", + "//chrome/browser/safe_browsing/protocol_parser.h", + "//chrome/browser/safe_browsing/ui_manager.cc", + "//chrome/browser/safe_browsing/ui_manager.h", + "chrome/browser/safe_browsing/safe_browsing_blocking_page.cc", + "//chrome/browser/safe_browsing/safe_browsing_blocking_page.h", + "//chrome/browser/safe_browsing/safe_browsing_database.cc", + "//chrome/browser/safe_browsing/safe_browsing_database.h", + "chrome/browser/safe_browsing/safe_browsing_service.cc", + "chrome/browser/safe_browsing/safe_browsing_service.h", + "//chrome/browser/safe_browsing/safe_browsing_store.cc", + "//chrome/browser/safe_browsing/safe_browsing_store.h", + "//chrome/browser/safe_browsing/safe_browsing_store_file.cc", + "//chrome/browser/safe_browsing/safe_browsing_store_file.h", + "//chrome/browser/safe_browsing/safe_browsing_navigation_observer.cc", + "//chrome/browser/safe_browsing/safe_browsing_navigation_observer.h", + "//chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager.cc", + "//chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager.h", + "//chrome/browser/safe_browsing/safe_browsing_util.cc", + "//chrome/browser/safe_browsing/safe_browsing_util.h", + "chrome/browser/safe_browsing/services_delegate_impl.cc", + "//chrome/browser/safe_browsing/services_delegate_impl.h", + "//chrome/browser/safe_browsing/signature_evaluator_mac.mm", + "//chrome/browser/safe_browsing/signature_evaluator_mac.h", + + "//chrome/browser/ssl/captive_portal_metrics_recorder.cc", + "//chrome/browser/ssl/captive_portal_metrics_recorder.h", + "chrome/browser/ssl/security_state_tab_helper.cc", "//chrome/browser/ssl/security_state_tab_helper.h", + "//chrome/browser/sync/user_event_service_factory.cc", + "//chrome/browser/sync/user_event_service_factory.h", "//chrome/browser/tab_contents/tab_util.cc", "//chrome/browser/tab_contents/tab_util.h", "//chrome/browser/win/chrome_process_finder.cc", @@ -369,9 +479,20 @@ source_set("browser") { deps += [ "//base", + "//chrome/browser/safe_browsing:chunk_proto", + "//chrome/common/safe_browsing:proto", + "//components/safe_browsing/password_protection", + "//components/safe_browsing:safe_browsing", + "//components/safe_browsing/browser:browser", + "//components/safe_browsing/triggers:triggers", + "//components/safe_browsing/db:v4_feature_list", + "//components/safe_browsing/db:prefix_set", + "//components/safe_browsing/db:metadata_proto", + "//content/public/browser:browser", "//net:extras", "//net:net_browser_services", "//skia", + "//third_party/cacheinvalidation", "//ui/base", "//ui/events", "//ui/gfx", diff --git a/chromium_src/chrome/browser/prerender/prerender_contents.cc b/chromium_src/chrome/browser/prerender/prerender_contents.cc index d73c4789c1..c2a105b409 100644 --- a/chromium_src/chrome/browser/prerender/prerender_contents.cc +++ b/chromium_src/chrome/browser/prerender/prerender_contents.cc @@ -7,6 +7,9 @@ #include #include "build/build_config.h" +#include "chrome/browser/prerender/prerender_manager.h" +#include "chrome/browser/prerender/prerender_manager_factory.h" +#include "content/public/browser/web_contents.h" #include "components/history/core/browser/history_types.h" namespace prerender { @@ -14,4 +17,38 @@ namespace prerender { void PrerenderContents::DidNavigate( const history::HistoryAddPageArgs& add_page_args) {} +void PrerenderContents::Destroy(FinalStatus final_status) { + DCHECK_NE(final_status, FINAL_STATUS_USED); + + if (prerendering_has_been_cancelled_) + return; + + SetFinalStatus(final_status); + + prerendering_has_been_cancelled_ = true; + prerender_manager_->MoveEntryToPendingDelete(this, final_status); +} + +void PrerenderContents::SetFinalStatus(FinalStatus final_status) { + DCHECK_GE(final_status, FINAL_STATUS_USED); + DCHECK_LT(final_status, FINAL_STATUS_MAX); + + DCHECK_EQ(FINAL_STATUS_MAX, final_status_); + + final_status_ = final_status; +} + +// static +PrerenderContents* PrerenderContents::FromWebContents( + content::WebContents* web_contents) { + if (!web_contents) + return NULL; + PrerenderManager* prerender_manager = + PrerenderManagerFactory::GetForBrowserContext( + web_contents->GetBrowserContext()); + if (!prerender_manager) + return NULL; + return prerender_manager->GetPrerenderContents(web_contents); +} + } // namespace prerender diff --git a/chromium_src/chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer.cc b/chromium_src/chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer.cc new file mode 100644 index 0000000000..3994dbe96d --- /dev/null +++ b/chromium_src/chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer.cc @@ -0,0 +1,60 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// 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/incident_reporting/binary_integrity_analyzer.h" + +#include +#include + +#include "base/bind.h" +#include "base/callback.h" +#include "base/files/file_util.h" +#include "base/memory/ptr_util.h" +#include "base/metrics/histogram_macros.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_util.h" +#include "base/time/time.h" +#include "build/build_config.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/safe_browsing/incident_reporting/binary_integrity_incident.h" +#include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" +#include "chrome/browser/safe_browsing/safe_browsing_service.h" +#include "components/safe_browsing/proto/csd.pb.h" + +namespace safe_browsing { + +void RecordSignatureVerificationTime(size_t file_index, + const base::TimeDelta& verification_time) { + static const char kHistogramName[] = "SBIRS.VerifyBinaryIntegrity."; + + base::HistogramBase* signature_verification_time_histogram = + base::Histogram::FactoryTimeGet( + std::string(kHistogramName) + base::SizeTToString(file_index), + base::TimeDelta::FromMilliseconds(1), + base::TimeDelta::FromSeconds(20), 50, + base::Histogram::kUmaTargetedHistogramFlag); + + signature_verification_time_histogram->AddTime(verification_time); +} + +void ClearBinaryIntegrityForFile(IncidentReceiver* incident_receiver, + const std::string& basename) { + std::unique_ptr + incident(new ClientIncidentReport_IncidentData_BinaryIntegrityIncident()); + incident->set_file_basename(basename); + incident_receiver->ClearIncidentForProcess( + base::MakeUnique(std::move(incident))); +} + +void RegisterBinaryIntegrityAnalysis() { +#if defined(OS_WIN) + scoped_refptr safe_browsing_service( + g_browser_process->safe_browsing_service()); + + safe_browsing_service->RegisterDelayedAnalysisCallback( + base::Bind(&VerifyBinaryIntegrity)); +#endif +} + +} // namespace safe_browsing diff --git a/chromium_src/chrome/browser/safe_browsing/incident_reporting/variations_seed_signature_analyzer.cc b/chromium_src/chrome/browser/safe_browsing/incident_reporting/variations_seed_signature_analyzer.cc new file mode 100644 index 0000000000..b858ae5a70 --- /dev/null +++ b/chromium_src/chrome/browser/safe_browsing/incident_reporting/variations_seed_signature_analyzer.cc @@ -0,0 +1,60 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// 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/incident_reporting/variations_seed_signature_analyzer.h" + +#include +#include + +#include "base/bind.h" +#include "base/location.h" +#include "base/memory/ptr_util.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" +#include "chrome/browser/safe_browsing/incident_reporting/variations_seed_signature_incident.h" +#include "chrome/browser/safe_browsing/safe_browsing_service.h" +#include "components/safe_browsing/proto/csd.pb.h" +#include "components/variations/service/variations_service.h" +#include "content/public/browser/browser_thread.h" + +namespace safe_browsing { + +namespace { + +void VerifyVariationsSeedSignatureOnUIThread( + std::unique_ptr incident_receiver) { + variations::VariationsService* variations_service = + g_browser_process->variations_service(); + if (!variations_service) + return; + std::string invalid_signature = + variations_service->GetInvalidVariationsSeedSignature(); + if (!invalid_signature.empty()) { + std::unique_ptr< + ClientIncidentReport_IncidentData_VariationsSeedSignatureIncident> + variations_seed_signature( + new ClientIncidentReport_IncidentData_VariationsSeedSignatureIncident()); + variations_seed_signature->set_variations_seed_signature(invalid_signature); + incident_receiver->AddIncidentForProcess( + base::MakeUnique( + std::move(variations_seed_signature))); + } +} + +} // namespace + +void RegisterVariationsSeedSignatureAnalysis() { + scoped_refptr safe_browsing_service( + g_browser_process->safe_browsing_service()); +} + +void VerifyVariationsSeedSignature( + std::unique_ptr incident_receiver) { + content::BrowserThread::PostTask( + content::BrowserThread::UI, FROM_HERE, + base::BindOnce(&VerifyVariationsSeedSignatureOnUIThread, + base::Passed(&incident_receiver))); +} + +} // namespace safe_browsing diff --git a/chromium_src/chrome/browser/safe_browsing/safe_browsing_service.cc b/chromium_src/chrome/browser/safe_browsing/safe_browsing_service.cc index 677d0da055..6a7808dda3 100644 --- a/chromium_src/chrome/browser/safe_browsing/safe_browsing_service.cc +++ b/chromium_src/chrome/browser/safe_browsing/safe_browsing_service.cc @@ -6,15 +6,590 @@ #include +#include + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/callback.h" +#include "base/command_line.h" +#include "base/lazy_instance.h" +#include "base/macros.h" +#include "base/memory/ptr_util.h" +#include "base/metrics/histogram_macros.h" +#include "base/path_service.h" +#include "base/strings/string_util.h" +#include "base/threading/thread.h" +#include "base/threading/thread_restrictions.h" +#include "base/trace_event/trace_event.h" +#include "build/build_config.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/chrome_notification_types.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_manager.h" +#include "chrome/browser/safe_browsing/ping_manager.h" +#include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager.h" #include "chrome/browser/safe_browsing/ui_manager.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/pref_names.h" +#include "chrome/common/safe_browsing/file_type_policies.h" +#include "components/prefs/pref_change_registrar.h" +#include "components/prefs/pref_service.h" +#include "components/safe_browsing/browser/safe_browsing_url_request_context_getter.h" +#include "components/safe_browsing/common/safebrowsing_constants.h" +#include "components/safe_browsing/common/safebrowsing_switches.h" +#include "components/safe_browsing/db/database_manager.h" +#include "components/safe_browsing/db/v4_feature_list.h" +#include "components/safe_browsing/db/v4_get_hash_protocol_manager.h" +#include "components/safe_browsing/db/v4_local_database_manager.h" +#include "components/safe_browsing/triggers/trigger_manager.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/notification_service.h" +#include "content/public/browser/resource_request_info.h" +#include "google_apis/google_api_keys.h" +#include "net/url_request/url_request_context_getter.h" +#include "services/preferences/public/interfaces/tracked_preference_validation_delegate.mojom.h" + +#if defined(OS_WIN) +#include "chrome/installer/util/browser_distribution.h" +#endif + +#if defined(SAFE_BROWSING_DB_LOCAL) +#include "chrome/browser/safe_browsing/local_database_manager.h" +#elif defined(SAFE_BROWSING_DB_REMOTE) +#include "components/safe_browsing/db/remote_database_manager.h" +#endif + +#if defined(FULL_SAFE_BROWSING) +#include "chrome/browser/safe_browsing/client_side_detection_service.h" +#include "chrome/browser/safe_browsing/download_protection/download_protection_service.h" +#include "chrome/browser/safe_browsing/incident_reporting/binary_integrity_analyzer.h" +#include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_service.h" +#include "chrome/browser/safe_browsing/incident_reporting/resource_request_detector.h" +#include "chrome/browser/safe_browsing/protocol_manager.h" +#include "components/safe_browsing/password_protection/password_protection_service.h" +#endif + +using content::BrowserThread; namespace safe_browsing { +// static +SafeBrowsingServiceFactory* SafeBrowsingService::factory_ = NULL; + +// The default SafeBrowsingServiceFactory. Global, made a singleton so we +// don't leak it. +class SafeBrowsingServiceFactoryImpl : public SafeBrowsingServiceFactory { + public: + SafeBrowsingService* CreateSafeBrowsingService() override { + return new SafeBrowsingService(V4FeatureList::V4UsageStatus::V4_INSTANTIATED); + } + + private: + friend struct base::LazyInstanceTraitsBase; + + SafeBrowsingServiceFactoryImpl() { } + + DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactoryImpl); +}; + +static base::LazyInstance::Leaky + g_safe_browsing_service_factory_impl = LAZY_INSTANCE_INITIALIZER; + +// static +base::FilePath SafeBrowsingService::GetCookieFilePathForTesting() { + return base::FilePath(SafeBrowsingService::GetBaseFilename().value() + + safe_browsing::kCookiesFile); +} + +// static +base::FilePath SafeBrowsingService::GetBaseFilename() { + base::FilePath path; + bool result = PathService::Get(chrome::DIR_USER_DATA, &path); + DCHECK(result); + return path.Append(safe_browsing::kSafeBrowsingBaseFilename); +} + + +// static +SafeBrowsingService* SafeBrowsingService::CreateSafeBrowsingService() { + if (!factory_) + factory_ = g_safe_browsing_service_factory_impl.Pointer(); + return factory_->CreateSafeBrowsingService(); +} + +SafeBrowsingService::SafeBrowsingService( + V4FeatureList::V4UsageStatus v4_usage_status) + : services_delegate_(ServicesDelegate::Create(this)), + estimated_extended_reporting_by_prefs_(SBER_LEVEL_OFF), + enabled_(false), + enabled_by_prefs_(false), + use_v4_only_(v4_usage_status == V4FeatureList::V4UsageStatus::V4_ONLY), + v4_enabled_(v4_usage_status == + V4FeatureList::V4UsageStatus::V4_INSTANTIATED || + v4_usage_status == V4FeatureList::V4UsageStatus::V4_ONLY) {} + +SafeBrowsingService::~SafeBrowsingService() { + // We should have already been shut down. If we're still enabled, then the + // database isn't going to be closed properly, which could lead to corruption. + DCHECK(!enabled_); +} + +void SafeBrowsingService::Initialize() { + // Ensure FileTypePolicies's Singleton is instantiated during startup. + // This guarantees we'll log UMA metrics about its state. + FileTypePolicies::GetInstance(); + + base::FilePath user_data_dir; + bool result = PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); + DCHECK(result); + + url_request_context_getter_ = new SafeBrowsingURLRequestContextGetter( + g_browser_process->system_request_context(), user_data_dir); + + ui_manager_ = CreateUIManager(); + + if (!use_v4_only_) { + database_manager_ = CreateDatabaseManager(); + } + + navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); + + services_delegate_->Initialize(v4_enabled_); + services_delegate_->InitializeCsdService(url_request_context_getter_.get()); + + // Needs to happen after |ui_manager_| is created. + CreateTriggerManager(); + + // Track profile creation and destruction. + profiles_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, + content::NotificationService::AllSources()); + profiles_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, + content::NotificationService::AllSources()); + + // Register all the delayed analysis to the incident reporting service. + RegisterAllDelayedAnalysis(); +} + +void SafeBrowsingService::ShutDown() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + shutdown_callback_list_.Notify(); + + // Remove Profile creation/destruction observers. + profiles_registrar_.RemoveAll(); + + // Delete the PrefChangeRegistrars, whose dtors also unregister |this| as an + // observer of the preferences. + prefs_map_.clear(); + + Stop(true); + + services_delegate_->ShutdownServices(); + + // Since URLRequestContextGetters are refcounted, can't count on clearing + // |url_request_context_getter_| to delete it, so need to shut it down first, + // which will cancel any requests that are currently using it, and prevent + // new requests from using it as well. + BrowserThread::PostNonNestableTask( + BrowserThread::IO, FROM_HERE, + base::BindOnce(&SafeBrowsingURLRequestContextGetter::ServiceShuttingDown, + url_request_context_getter_)); + + // Release the URLRequestContextGetter after passing it to the IOThread. It + // has to be released now rather than in the destructor because it can only + // be deleted on the IOThread, and the SafeBrowsingService outlives the IO + // thread. + url_request_context_getter_ = nullptr; +} + +bool SafeBrowsingService::DownloadBinHashNeeded() const { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + +#if defined(FULL_SAFE_BROWSING) + return database_manager()->IsDownloadProtectionEnabled() || + (download_protection_service() && + download_protection_service()->enabled()); +#else + return false; +#endif +} + +scoped_refptr +SafeBrowsingService::url_request_context() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + return url_request_context_getter_; +} + +void SafeBrowsingService::DisableQuicOnIOThread() { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + + url_request_context_getter_->DisableQuicOnIOThread(); +} + const scoped_refptr& SafeBrowsingService::ui_manager() const { + return ui_manager_; +} + +const scoped_refptr& +SafeBrowsingService::database_manager() const { + return use_v4_only_ ? v4_local_database_manager() : database_manager_; +} + +scoped_refptr +SafeBrowsingService::navigation_observer_manager() { + return navigation_observer_manager_; +} + +SafeBrowsingProtocolManager* SafeBrowsingService::protocol_manager() const { + DCHECK_CURRENTLY_ON(BrowserThread::IO); +#if defined(SAFE_BROWSING_DB_LOCAL) + DCHECK(!use_v4_only_); + return protocol_manager_.get(); +#else + return nullptr; +#endif +} + +SafeBrowsingPingManager* SafeBrowsingService::ping_manager() const { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + return ping_manager_.get(); +} + +const scoped_refptr& +SafeBrowsingService::v4_local_database_manager() const { + return services_delegate_->v4_local_database_manager(); +} + +TriggerManager* SafeBrowsingService::trigger_manager() const { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + return trigger_manager_.get(); +} + +PasswordProtectionService* SafeBrowsingService::GetPasswordProtectionService( + Profile* profile) const { + if (profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) + return services_delegate_->GetPasswordProtectionService(profile); return nullptr; } -void SafeBrowsingService::DisableQuicOnIOThread() {} +std::unique_ptr +SafeBrowsingService::CreatePreferenceValidationDelegate( + Profile* profile) const { + return services_delegate_->CreatePreferenceValidationDelegate(profile); +} + +void SafeBrowsingService::RegisterDelayedAnalysisCallback( + const DelayedAnalysisCallback& callback) { + services_delegate_->RegisterDelayedAnalysisCallback(callback); +} + +void SafeBrowsingService::AddDownloadManager( + content::DownloadManager* download_manager) { + services_delegate_->AddDownloadManager(download_manager); +} + +void SafeBrowsingService::OnResourceRequest(const net::URLRequest* request) { +#if defined(FULL_SAFE_BROWSING) + DCHECK_CURRENTLY_ON(BrowserThread::IO); + TRACE_EVENT1("loader", "SafeBrowsingService::OnResourceRequest", "url", + request->url().spec()); + + ResourceRequestInfo info = ResourceRequestDetector::GetRequestInfo(request); + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, + base::BindOnce(&SafeBrowsingService::ProcessResourceRequest, this, info)); +#endif +} + +SafeBrowsingUIManager* SafeBrowsingService::CreateUIManager() { + return new SafeBrowsingUIManager(this); +} + +SafeBrowsingDatabaseManager* SafeBrowsingService::CreateDatabaseManager() { +#if defined(SAFE_BROWSING_DB_LOCAL) + return new LocalSafeBrowsingDatabaseManager(this); +#elif defined(SAFE_BROWSING_DB_REMOTE) + return new RemoteSafeBrowsingDatabaseManager(); +#else + return NULL; +#endif +} + +void SafeBrowsingService::RegisterAllDelayedAnalysis() { +#if defined(FULL_SAFE_BROWSING) + RegisterBinaryIntegrityAnalysis(); +#endif +} + +SafeBrowsingProtocolConfig SafeBrowsingService::GetProtocolConfig() const { + SafeBrowsingProtocolConfig config; + config.client_name = GetProtocolConfigClientName(); + + base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); + config.disable_auto_update = + cmdline->HasSwitch(safe_browsing::switches::kSbDisableAutoUpdate) || + cmdline->HasSwitch(::switches::kDisableBackgroundNetworking); + config.url_prefix = kSbDefaultURLPrefix; + config.backup_connect_error_url_prefix = kSbBackupConnectErrorURLPrefix; + config.backup_http_error_url_prefix = kSbBackupHttpErrorURLPrefix; + config.backup_network_error_url_prefix = kSbBackupNetworkErrorURLPrefix; + + return config; +} + +V4ProtocolConfig +SafeBrowsingService::GetV4ProtocolConfig() const { + base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); + return V4ProtocolConfig( + GetProtocolConfigClientName(), + cmdline->HasSwitch(::switches::kDisableBackgroundNetworking), + google_apis::GetAPIKey(), ProtocolManagerHelper::Version()); +} + +std::string SafeBrowsingService::GetProtocolConfigClientName() const { + std::string client_name; + // On Windows, get the safe browsing client name from the browser + // distribution classes in installer util. These classes don't yet have + // an analog on non-Windows builds so just keep the name specified here. +#if defined(OS_WIN) + BrowserDistribution* dist = BrowserDistribution::GetDistribution(); + client_name = dist->GetSafeBrowsingName(); +#else +#if defined(GOOGLE_CHROME_BUILD) + client_name = "googlechrome"; +#else + client_name = "chromium"; +#endif + + // Mark client string to allow server to differentiate mobile. +#if defined(OS_ANDROID) + client_name.append("-a"); +#endif + +#endif // defined(OS_WIN) + + return client_name; +} + +// Any tests that create a DatabaseManager that isn't derived from +// LocalSafeBrowsingDatabaseManager should override this to return NULL. +SafeBrowsingProtocolManagerDelegate* +SafeBrowsingService::GetProtocolManagerDelegate() { +#if defined(SAFE_BROWSING_DB_LOCAL) + DCHECK(!use_v4_only_); + return static_cast( + database_manager_.get()); +#else + NOTREACHED(); + return NULL; +#endif +} + +void SafeBrowsingService::StartOnIOThread( + net::URLRequestContextGetter* url_request_context_getter) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + if (enabled_) + return; + enabled_ = true; + + SafeBrowsingProtocolConfig config = GetProtocolConfig(); + V4ProtocolConfig v4_config = GetV4ProtocolConfig(); + + services_delegate_->StartOnIOThread(url_request_context_getter, v4_config); + +#if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) + if (!use_v4_only_) { + DCHECK(database_manager_.get()); + database_manager_->StartOnIOThread(url_request_context_getter, v4_config); + } +#endif + +#if defined(SAFE_BROWSING_DB_LOCAL) + if (!use_v4_only_) { + SafeBrowsingProtocolManagerDelegate* protocol_manager_delegate = + GetProtocolManagerDelegate(); + if (protocol_manager_delegate) { + protocol_manager_ = SafeBrowsingProtocolManager::Create( + protocol_manager_delegate, url_request_context_getter, config); + protocol_manager_->Initialize(); + } + } +#endif + + DCHECK(!ping_manager_); + ping_manager_ = SafeBrowsingPingManager::Create( + url_request_context_getter, config); +} + +void SafeBrowsingService::StopOnIOThread(bool shutdown) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + +#if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) + if (!use_v4_only_) { + database_manager_->StopOnIOThread(shutdown); + } +#endif + ui_manager_->StopOnIOThread(shutdown); + + services_delegate_->StopOnIOThread(shutdown); + + if (enabled_) { + enabled_ = false; + +#if defined(SAFE_BROWSING_DB_LOCAL) + // This cancels all in-flight GetHash requests. Note that + // |database_manager_| relies on |protocol_manager_| so if the latter is + // destroyed, the former must be stopped. + protocol_manager_.reset(); +#endif + ping_manager_.reset(); + } +} + +void SafeBrowsingService::Start() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::BindOnce(&SafeBrowsingService::StartOnIOThread, this, + base::RetainedRef(url_request_context_getter_))); +} + +void SafeBrowsingService::Stop(bool shutdown) { + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::BindOnce(&SafeBrowsingService::StopOnIOThread, this, shutdown)); +} + +void SafeBrowsingService::Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + switch (type) { + case chrome::NOTIFICATION_PROFILE_CREATED: { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + Profile* profile = content::Source(source).ptr(); + services_delegate_->CreatePasswordProtectionService(profile); + if (!profile->IsOffTheRecord()) + AddPrefService(profile->GetPrefs()); + break; + } + case chrome::NOTIFICATION_PROFILE_DESTROYED: { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + Profile* profile = content::Source(source).ptr(); + services_delegate_->RemovePasswordProtectionService(profile); + if (!profile->IsOffTheRecord()) + RemovePrefService(profile->GetPrefs()); + break; + } + default: + NOTREACHED(); + } +} + +void SafeBrowsingService::AddPrefService(PrefService* pref_service) { + DCHECK(prefs_map_.find(pref_service) == prefs_map_.end()); + std::unique_ptr registrar = + base::MakeUnique(); + registrar->Init(pref_service); + registrar->Add( + prefs::kSafeBrowsingEnabled, + base::Bind(&SafeBrowsingService::RefreshState, base::Unretained(this))); + // ClientSideDetectionService will need to be refresh the models + // renderers have if extended-reporting changes. + registrar->Add( + prefs::kSafeBrowsingExtendedReportingEnabled, + base::Bind(&SafeBrowsingService::RefreshState, base::Unretained(this))); + registrar->Add( + prefs::kSafeBrowsingScoutReportingEnabled, + base::Bind(&SafeBrowsingService::RefreshState, base::Unretained(this))); + prefs_map_[pref_service] = std::move(registrar); + RefreshState(); + + // Initialize SafeBrowsing prefs on startup. + InitializeSafeBrowsingPrefs(pref_service); + + // Record the current pref state. + UMA_HISTOGRAM_BOOLEAN("SafeBrowsing.Pref.General", + pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)); + // Extended Reporting metrics are handled together elsewhere. + RecordExtendedReportingMetrics(*pref_service); +} + +void SafeBrowsingService::RemovePrefService(PrefService* pref_service) { + if (prefs_map_.find(pref_service) != prefs_map_.end()) { + prefs_map_.erase(pref_service); + RefreshState(); + } else { + NOTREACHED(); + } +} +std::unique_ptr +SafeBrowsingService::RegisterStateCallback( + const base::Callback& callback) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + return state_callback_list_.Add(callback); +} + +std::unique_ptr +SafeBrowsingService::RegisterShutdownCallback( + const base::Callback& callback) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + return shutdown_callback_list_.Add(callback); +} + +void SafeBrowsingService::RefreshState() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + // Check if any profile requires the service to be active. + enabled_by_prefs_ = false; + estimated_extended_reporting_by_prefs_ = SBER_LEVEL_OFF; + for (const auto& pref : prefs_map_) { + if (pref.first->GetBoolean(prefs::kSafeBrowsingEnabled)) { + enabled_by_prefs_ = true; + ExtendedReportingLevel erl = + safe_browsing::GetExtendedReportingLevel(*pref.first); + if (erl != SBER_LEVEL_OFF) { + estimated_extended_reporting_by_prefs_ = erl; + break; + } + } + } + + if (enabled_by_prefs_) + Start(); + else + Stop(false); + + state_callback_list_.Notify(); + + services_delegate_->RefreshState(enabled_by_prefs_); +} + +void SafeBrowsingService::SendSerializedDownloadReport( + const std::string& report) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::BindOnce(&SafeBrowsingService::OnSendSerializedDownloadReport, this, + report)); +} + +void SafeBrowsingService::OnSendSerializedDownloadReport( + const std::string& report) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + if (ping_manager()) + ping_manager()->ReportThreatDetails(report); +} + +void SafeBrowsingService::ProcessResourceRequest( + const ResourceRequestInfo& request) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + services_delegate_->ProcessResourceRequest(&request); +} + +void SafeBrowsingService::CreateTriggerManager() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + trigger_manager_ = base::MakeUnique(ui_manager_.get()); +} } // namespace safe_browsing diff --git a/muon/browser/muon_browser_process_impl.cc b/muon/browser/muon_browser_process_impl.cc index 20b9a7b041..493f7f05e6 100644 --- a/muon/browser/muon_browser_process_impl.cc +++ b/muon/browser/muon_browser_process_impl.cc @@ -6,25 +6,61 @@ #include "atom/browser/api/atom_api_app.h" #include "atom/browser/atom_resource_dispatcher_host_delegate.h" +#include "base/path_service.h" #include "brave/browser/component_updater/brave_component_updater_configurator.h" +//#include "chromium_src/chrome/browser/prefs/browser_prefs.cc" #include "chrome/browser/io_thread.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" +#include "chrome/browser/safe_browsing/safe_browsing_service.h" +#include "chrome/common/pref_names.h" #include "components/component_updater/component_updater_service.h" +#include "components/metrics/metrics_pref_names.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/resource_dispatcher_host.h" +using content::ResourceDispatcherHost; + MuonBrowserProcessImpl::MuonBrowserProcessImpl( base::SequencedTaskRunner* local_state_task_runner, const base::CommandLine& command_line) : - BrowserProcessImpl(local_state_task_runner, command_line) { + BrowserProcessImpl(local_state_task_runner, command_line), + created_safe_browsing_service_(false) { g_browser_process = this; } MuonBrowserProcessImpl::~MuonBrowserProcessImpl() { + if (safe_browsing_service_.get()) + safe_browsing_service()->ShutDown(); g_browser_process = NULL; } +safe_browsing::SafeBrowsingService* +MuonBrowserProcessImpl::safe_browsing_service() { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + if (!created_safe_browsing_service_) + CreateSafeBrowsingService(); + return safe_browsing_service_.get(); +} + +safe_browsing::ClientSideDetectionService* + MuonBrowserProcessImpl::safe_browsing_detection_service() { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + if (safe_browsing_service()) + return safe_browsing_service()->safe_browsing_detection_service(); + return NULL; +} + +void MuonBrowserProcessImpl::CreateSafeBrowsingService() { + DCHECK(!safe_browsing_service_); + // Set this flag to true so that we don't retry indefinitely to + // create the service class if there was an error. + created_safe_browsing_service_ = true; + safe_browsing_service_ = + safe_browsing::SafeBrowsingService::CreateSafeBrowsingService(); + safe_browsing_service_->Initialize(); +} + component_updater::ComponentUpdateService* MuonBrowserProcessImpl::component_updater( std::unique_ptr &component_updater, @@ -55,9 +91,20 @@ MuonBrowserProcessImpl::component_updater() { return component_updater(component_updater_, false); } +void MuonBrowserProcessImpl::ApplyAllowCrossOriginAuthPromptPolicy() { + bool value = local_state()->GetBoolean(prefs::kAllowCrossOriginAuthPrompt); + ResourceDispatcherHost::Get()->SetAllowCrossOriginAuthPrompt(value); +} + void MuonBrowserProcessImpl::ResourceDispatcherHostCreated() { resource_dispatcher_host_delegate_.reset( new atom::AtomResourceDispatcherHostDelegate); + content::ResourceDispatcherHost::Get()->SetDelegate( resource_dispatcher_host_delegate_.get()); + pref_change_registrar_.Add( + prefs::kAllowCrossOriginAuthPrompt, + base::Bind(&MuonBrowserProcessImpl::ApplyAllowCrossOriginAuthPromptPolicy, + base::Unretained(this))); + ApplyAllowCrossOriginAuthPromptPolicy(); } diff --git a/muon/browser/muon_browser_process_impl.h b/muon/browser/muon_browser_process_impl.h index 5d4de792fe..6ff5d20141 100644 --- a/muon/browser/muon_browser_process_impl.h +++ b/muon/browser/muon_browser_process_impl.h @@ -7,6 +7,7 @@ #include "build/build_config.h" #include "chrome/browser/browser_process_impl.h" +#include "net/socket/client_socket_pool_manager.h" namespace atom { namespace api { @@ -32,7 +33,16 @@ class MuonBrowserProcessImpl : public BrowserProcessImpl { component_updater::ComponentUpdateService* component_updater() override; void ResourceDispatcherHostCreated() override; + safe_browsing::SafeBrowsingService* safe_browsing_service() override; + safe_browsing::ClientSideDetectionService* safe_browsing_detection_service() + override; + private: + void ApplyAllowCrossOriginAuthPromptPolicy(); + void CreateSafeBrowsingService(); + void CreateSafeBrowsingDetectionService(); + + std::unique_ptr local_state_; atom::api::App* app_; // not owned std::unique_ptr @@ -46,6 +56,12 @@ class MuonBrowserProcessImpl : public BrowserProcessImpl { std::unique_ptr &, bool use_brave_server); + bool created_safe_browsing_service_; + scoped_refptr safe_browsing_service_; + + // Ensures that the observers of plugin/print disable/enable state + // notifications are properly added and removed. + PrefChangeRegistrar pref_change_registrar_; DISALLOW_COPY_AND_ASSIGN(MuonBrowserProcessImpl); };