Skip to content

Commit

Permalink
Revert "Remove remaining components/domain_reliability dependency on …
Browse files Browse the repository at this point in the history
…content/ and components/keyed_service."

This reverts commit bd611e3.

Reason for revert: It looks like this CL broke CFI bots:

https://ci.chromium.org/p/chromium/builders/luci.chromium.ci/Linux%20CFI/11301

Original change's description:
> Remove remaining components/domain_reliability dependency on content/ and components/keyed_service.
> 
> This way it can run in the network process, similar to Network Error Logging.
> 
> Bug: 853251
> Change-Id: I403a5dcc3cae12b592a444c7e23f71c552b58e1f
> Reviewed-on: https://chromium-review.googlesource.com/c/1334848
> Reviewed-by: Misha Efimov <[email protected]>
> Commit-Queue: John Abd-El-Malek <[email protected]>
> Cr-Commit-Position: refs/heads/master@{#608175}

[email protected],[email protected]

Change-Id: I57829e413ad57ec667882069f77bf2d97faec4a3
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 853251
Reviewed-on: https://chromium-review.googlesource.com/c/1337215
Reviewed-by: Kentaro Hara <[email protected]>
Commit-Queue: Kentaro Hara <[email protected]>
Cr-Commit-Position: refs/heads/master@{#608268}
  • Loading branch information
xharaken authored and Commit Bot committed Nov 15, 2018
1 parent 2043308 commit 0dde167
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,8 @@ class MockDomainReliabilityService : public DomainReliabilityService {

std::unique_ptr<DomainReliabilityMonitor> CreateMonitor(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
const domain_reliability::DomainReliabilityContext::UploadAllowedCallback&
upload_allowed_callback) override {
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
override {
NOTREACHED();
return std::unique_ptr<DomainReliabilityMonitor>();
}
Expand Down Expand Up @@ -546,21 +545,6 @@ class MockDomainReliabilityService : public DomainReliabilityService {
base::Callback<bool(const GURL&)> last_filter_;
};

class DomainReliablityKeyedServiceWrapper : public KeyedService {
public:
explicit DomainReliablityKeyedServiceWrapper(
std::unique_ptr<DomainReliabilityService> service)
: service_(std::move(service)) {}
~DomainReliablityKeyedServiceWrapper() override = default;

DomainReliabilityService* service() { return service_.get(); }

private:
std::unique_ptr<DomainReliabilityService> service_;

DISALLOW_COPY_AND_ASSIGN(DomainReliablityKeyedServiceWrapper);
};

struct TestingDomainReliabilityServiceFactoryUserData
: public base::SupportsUserData::Data {
TestingDomainReliabilityServiceFactoryUserData(
Expand Down Expand Up @@ -594,8 +578,7 @@ std::unique_ptr<KeyedService> TestingDomainReliabilityServiceFactoryFunction(
EXPECT_FALSE(data->attached);

data->attached = true;
return std::make_unique<DomainReliablityKeyedServiceWrapper>(
base::WrapUnique(data->service));
return base::WrapUnique(data->service);
}

std::unique_ptr<KeyedService> BuildProtocolHandlerRegistry(
Expand Down
23 changes: 2 additions & 21 deletions chrome/browser/domain_reliability/service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "chrome/common/chrome_switches.h"
#include "components/domain_reliability/service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"

#if defined(OS_CHROMEOS)
Expand All @@ -32,32 +31,15 @@ const char kFieldTrialValueEnable[] = "enable";
// Identifies Chrome as the source of Domain Reliability uploads it sends.
const char kUploadReporterString[] = "chrome";

class KeyedServiceWrapper : public KeyedService {
public:
explicit KeyedServiceWrapper(DomainReliabilityService* service)
: service_(service) {}
~KeyedServiceWrapper() override = default;

DomainReliabilityService* service() { return service_; }

private:
DomainReliabilityService* service_;

DISALLOW_COPY_AND_ASSIGN(KeyedServiceWrapper);
};

} // namespace

// static
DomainReliabilityService*
DomainReliabilityServiceFactory::GetForBrowserContext(
content::BrowserContext* context) {
auto* wrapper = static_cast<KeyedServiceWrapper*>(
return static_cast<DomainReliabilityService*>(
GetInstance()->GetServiceForBrowserContext(context,
/* create = */ true));
if (!wrapper)
return nullptr;
return wrapper->service();
}

// static
Expand All @@ -78,8 +60,7 @@ KeyedService* DomainReliabilityServiceFactory::BuildServiceInstanceFor(
if (!ShouldCreateService())
return NULL;

return new KeyedServiceWrapper(
DomainReliabilityService::Create(kUploadReporterString));
return DomainReliabilityService::Create(kUploadReporterString, context);
}

bool DomainReliabilityServiceFactory::ShouldCreateService() const {
Expand Down
30 changes: 1 addition & 29 deletions chrome/browser/profiles/profile_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/dom_storage_context.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/permission_type.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/url_data_source.h"
Expand Down Expand Up @@ -328,31 +326,6 @@ std::unique_ptr<service_manager::Service> CreateAppService(Profile* profile) {
}
#endif

void CheckDomainReliablityUploadAllowedOnUIThread(
Profile* profile,
const GURL& origin,
base::OnceCallback<void(bool)> callback) {
// Profile is safe since ProfileImpl always outlives IO thread.
content::PermissionController* permission_controller =
content::BrowserContext::GetPermissionController(profile);
DCHECK(permission_controller);
bool allowed = permission_controller->GetPermissionStatus(
content::PermissionType::BACKGROUND_SYNC, origin,
origin) == blink::mojom::PermissionStatus::GRANTED;
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::IO},
base::BindOnce(std::move(callback), allowed));
}

void CheckDomainReliablityUploadAllowedOnIOThread(
Profile* profile,
const GURL& origin,
base::OnceCallback<void(bool)> callback) {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(CheckDomainReliablityUploadAllowedOnUIThread, profile,
origin, std::move(callback)));
}

} // namespace

// static
Expand Down Expand Up @@ -1503,8 +1476,7 @@ ProfileImpl::CreateDomainReliabilityMonitor(PrefService* local_state) {

return service->CreateMonitor(
base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI}),
base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}),
base::BindRepeating(CheckDomainReliablityUploadAllowedOnIOThread, this));
base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}));
}

std::unique_ptr<service_manager::Service> ProfileImpl::CreateIdentityService() {
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/profiles/profile_impl_io_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ void ProfileImplIOData::Handle::Init(

io_data_->InitializeMetricsEnabledStateOnUIThread();
if (io_data_->lazy_params_->domain_reliability_monitor)
io_data_->lazy_params_->domain_reliability_monitor
->InitializeOnNetworkThread();
io_data_->lazy_params_->domain_reliability_monitor->MoveToNetworkThread();

io_data_->set_data_reduction_proxy_io_data(
CreateDataReductionProxyChromeIOData(
Expand Down
5 changes: 5 additions & 0 deletions components/domain_reliability/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ jumbo_component("domain_reliability") {
deps = [
":bake_in_configs",
"//base",
"//components/keyed_service/core",
"//content/public/browser",
"//content/public/common",
"//net",
"//url",
]
Expand Down Expand Up @@ -114,6 +117,8 @@ jumbo_source_set("unit_tests") {
":domain_reliability",
"//base",
"//base/test:test_support",
"//content/public/browser",
"//content/test:test_support",
"//net:test_support",
"//testing/gtest",
]
Expand Down
4 changes: 4 additions & 0 deletions components/domain_reliability/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@

include_rules = [
"+net",
"+components/keyed_service/core",
"+content/public/browser",
"+content/public/test",
"+third_party/blink/public",
]

18 changes: 9 additions & 9 deletions components/domain_reliability/monitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ DomainReliabilityMonitor::DomainReliabilityMonitor(
const std::string& upload_reporter_string,
const DomainReliabilityContext::UploadAllowedCallback&
upload_allowed_callback,
const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& network_thread)
: time_(new ActualTime()),
upload_reporter_string_(upload_reporter_string),
Expand All @@ -87,19 +87,19 @@ DomainReliabilityMonitor::DomainReliabilityMonitor(
DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()),
dispatcher_(time_.get()),
context_manager_(this),
main_task_runner_(main_thread),
pref_task_runner_(pref_thread),
network_task_runner_(network_thread),
moved_to_network_thread_(false),
discard_uploads_set_(false),
weak_factory_(this) {
DCHECK(OnMainThread());
DCHECK(OnPrefThread());
}

DomainReliabilityMonitor::DomainReliabilityMonitor(
const std::string& upload_reporter_string,
const DomainReliabilityContext::UploadAllowedCallback&
upload_allowed_callback,
const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& network_thread,
std::unique_ptr<MockableTime> time)
: time_(std::move(time)),
Expand All @@ -109,25 +109,25 @@ DomainReliabilityMonitor::DomainReliabilityMonitor(
DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()),
dispatcher_(time_.get()),
context_manager_(this),
main_task_runner_(main_thread),
pref_task_runner_(pref_thread),
network_task_runner_(network_thread),
moved_to_network_thread_(false),
discard_uploads_set_(false),
weak_factory_(this) {
DCHECK(OnMainThread());
DCHECK(OnPrefThread());
}

DomainReliabilityMonitor::~DomainReliabilityMonitor() {
if (moved_to_network_thread_) {
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
DCHECK(OnNetworkThread());
} else {
DCHECK(OnMainThread());
DCHECK(OnPrefThread());
}
}

void DomainReliabilityMonitor::InitializeOnNetworkThread() {
DCHECK(OnMainThread());
void DomainReliabilityMonitor::MoveToNetworkThread() {
DCHECK(OnPrefThread());
DCHECK(!moved_to_network_thread_);

network_task_runner_->PostTask(
Expand Down
22 changes: 11 additions & 11 deletions components/domain_reliability/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,40 @@ class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor
: public net::NetworkChangeNotifier::NetworkChangeObserver,
DomainReliabilityContext::Factory {
public:
// Creates a Monitor. |main_thread| is the current thread, which may or may
// not be the same as |network_thread|. |network_thread| is the thread
// Creates a Monitor. |local_state_pref_service| must live on |pref_thread|
// (which should be the current thread); |network_thread| is the thread
// on which requests will actually be monitored and reported.
DomainReliabilityMonitor(
const std::string& upload_reporter_string,
const DomainReliabilityContext::UploadAllowedCallback&
upload_allowed_callback,
const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& network_thread);

// Same, but specifies a mock interface for time functions for testing.
DomainReliabilityMonitor(
const std::string& upload_reporter_string,
const DomainReliabilityContext::UploadAllowedCallback&
upload_allowed_callback,
const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& network_thread,
std::unique_ptr<MockableTime> time);

// Must be called from the main thread if |InitializeOnNetworkThread| was not
// Must be called from the pref thread if |MoveToNetworkThread| was not
// called, or from the network thread if it was called.
~DomainReliabilityMonitor() override;

// Must be called before |InitURLRequestContext| on the same thread on which
// the Monitor was constructed. Moves (most of) the Monitor to the network
// thread passed in the constructor.
void InitializeOnNetworkThread();
void MoveToNetworkThread();

// All public methods below this point must be called on the network thread
// after |InitializeOnNetworkThread| is called on the main thread.
// after |MoveToNetworkThread| is called on the pref thread.

// Initializes the Monitor's URLRequestContextGetter.
//
// Must be called on the network thread, after |InitializeOnNetworkThread|.
// Must be called on the network thread, after |MoveToNetworkThread|.
void InitURLRequestContext(net::URLRequestContext* url_request_context);

// Same, but for unittests where the Getter is readily available.
Expand Down Expand Up @@ -177,8 +177,8 @@ class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor

void MaybeHandleHeader(const RequestInfo& info);

bool OnMainThread() const {
return main_task_runner_->BelongsToCurrentThread();
bool OnPrefThread() const {
return pref_task_runner_->BelongsToCurrentThread();
}
bool OnNetworkThread() const {
return network_task_runner_->BelongsToCurrentThread();
Expand All @@ -195,7 +195,7 @@ class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor
std::unique_ptr<DomainReliabilityUploader> uploader_;
DomainReliabilityContextManager context_manager_;

scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;

bool moved_to_network_thread_;
Expand Down
8 changes: 4 additions & 4 deletions components/domain_reliability/monitor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ class DomainReliabilityMonitorTest : public testing::Test {
typedef DomainReliabilityMonitor::RequestInfo RequestInfo;

DomainReliabilityMonitorTest()
: main_task_runner(new base::TestSimpleTaskRunner()),
: pref_task_runner_(new base::TestSimpleTaskRunner()),
network_task_runner_(new base::TestSimpleTaskRunner()),
url_request_context_getter_(
new net::TestURLRequestContextGetter(network_task_runner_)),
time_(new MockTime()),
monitor_("test-reporter",
DomainReliabilityContext::UploadAllowedCallback(),
main_task_runner,
pref_task_runner_,
network_task_runner_,
std::unique_ptr<MockableTime>(time_)) {
monitor_.InitializeOnNetworkThread();
monitor_.MoveToNetworkThread();
monitor_.InitURLRequestContext(url_request_context_getter_);
monitor_.SetDiscardUploads(false);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ class DomainReliabilityMonitorTest : public testing::Test {
return monitor_.AddContextForTesting(std::move(config));
}

scoped_refptr<base::TestSimpleTaskRunner> main_task_runner;
scoped_refptr<base::TestSimpleTaskRunner> pref_task_runner_;
scoped_refptr<base::TestSimpleTaskRunner> network_task_runner_;
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
MockTime* time_;
Expand Down
Loading

0 comments on commit 0dde167

Please sign in to comment.