// 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 "components/domain_reliability/service.h" #include #include "base/bind.h" #include "base/callback.h" #include "base/location.h" #include "base/memory/weak_ptr.h" #include "base/single_thread_task_runner.h" #include "base/task_runner_util.h" #include "base/threading/thread_task_runner_handle.h" #include "components/domain_reliability/context.h" #include "components/domain_reliability/monitor.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/permission_manager.h" #include "content/public/browser/permission_type.h" #include "net/url_request/url_request_context_getter.h" #include "third_party/blink/public/platform/modules/permissions/permission_status.mojom.h" #include "url/gurl.h" namespace domain_reliability { namespace { void AddContextForTestingOnNetworkTaskRunner( base::WeakPtr monitor, std::unique_ptr config) { if (!monitor) return; monitor->AddContextForTesting(std::move(config)); } std::unique_ptr GetWebUIDataOnNetworkTaskRunner( base::WeakPtr monitor) { if (!monitor) { base::DictionaryValue* dict = new base::DictionaryValue(); dict->SetString("error", "no_monitor"); return std::unique_ptr(dict); } return monitor->GetWebUIData(); } } // namespace class DomainReliabilityServiceImpl : public DomainReliabilityService { public: explicit DomainReliabilityServiceImpl( const std::string& upload_reporter_string, content::BrowserContext* browser_context) : upload_reporter_string_(upload_reporter_string), browser_context_(browser_context), weak_factory_(this) {} ~DomainReliabilityServiceImpl() override {} // DomainReliabilityService implementation: std::unique_ptr CreateMonitor( scoped_refptr ui_task_runner, scoped_refptr network_task_runner) override { DCHECK(!network_task_runner_.get()); std::unique_ptr monitor( new DomainReliabilityMonitor( upload_reporter_string_, base::Bind( &DomainReliabilityServiceImpl::CheckUploadAllowedOnIOThread, weak_factory_.GetWeakPtr(), base::RetainedRef(ui_task_runner), base::RetainedRef(network_task_runner)), ui_task_runner, network_task_runner)); monitor_ = monitor->MakeWeakPtr(); network_task_runner_ = network_task_runner; return monitor; } void ClearBrowsingData( DomainReliabilityClearMode clear_mode, const base::Callback& origin_filter, const base::Closure& callback) override { DCHECK(network_task_runner_.get()); network_task_runner_->PostTaskAndReply( FROM_HERE, base::Bind(&DomainReliabilityMonitor::ClearBrowsingData, monitor_, clear_mode, origin_filter), callback); } void GetWebUIData(const base::Callback)>& callback) const override { DCHECK(network_task_runner_.get()); PostTaskAndReplyWithResult( network_task_runner_.get(), FROM_HERE, base::Bind(&GetWebUIDataOnNetworkTaskRunner, monitor_), callback); } void SetDiscardUploadsForTesting(bool discard_uploads) override { DCHECK(network_task_runner_.get()); network_task_runner_->PostTask( FROM_HERE, base::Bind(&DomainReliabilityMonitor::SetDiscardUploads, monitor_, discard_uploads)); } void AddContextForTesting( std::unique_ptr config) override { DCHECK(network_task_runner_.get()); network_task_runner_->PostTask( FROM_HERE, base::BindOnce(&AddContextForTestingOnNetworkTaskRunner, monitor_, std::move(config))); } void ForceUploadsForTesting() override { DCHECK(network_task_runner_.get()); network_task_runner_->PostTask( FROM_HERE, base::Bind(&DomainReliabilityMonitor::ForceUploadsForTesting, monitor_)); } private: static void CheckUploadAllowedOnIOThread( base::WeakPtr service, scoped_refptr ui_task_runner, scoped_refptr network_task_runner, const GURL& origin, base::OnceCallback callback) { ui_task_runner->PostTask( FROM_HERE, base::BindOnce( &DomainReliabilityServiceImpl::CheckUploadAllowedOnUIThread, service, base::RetainedRef(network_task_runner), origin, std::move(callback))); } void CheckUploadAllowedOnUIThread( base::SingleThreadTaskRunner* network_task_runner, const GURL& origin, base::OnceCallback callback) { bool allowed = false; content::PermissionManager* permission_manager = browser_context_->GetPermissionManager(); if (permission_manager) { allowed = permission_manager->GetPermissionStatus( content::PermissionType::BACKGROUND_SYNC, origin, origin) == blink::mojom::PermissionStatus::GRANTED; } network_task_runner->PostTask(FROM_HERE, base::BindOnce(std::move(callback), allowed)); } std::string upload_reporter_string_; content::BrowserContext* browser_context_; base::WeakPtr monitor_; scoped_refptr network_task_runner_; base::WeakPtrFactory weak_factory_; }; // static DomainReliabilityService* DomainReliabilityService::Create( const std::string& upload_reporter_string, content::BrowserContext* browser_context) { return new DomainReliabilityServiceImpl(upload_reporter_string, browser_context); } DomainReliabilityService::~DomainReliabilityService() {} DomainReliabilityService::DomainReliabilityService() {} } // namespace domain_reliability