Commit d001b3f5 authored by proberge's avatar proberge Committed by Commit Bot

Refactor sw_reporter_installer and reporter_runner

The sw_reporter_installer was getting too large and confusing.
SwReporterInstallerPolicy is not an appropriate class to hold information
about user-initiated cleanups. This CL is preliminary work for a
redesigned version of
https://chromium-review.googlesource.com/c/chromium/src/+/810086

Bug: 776538
Change-Id: I3833a06aec23fc83eba05ed2a36e014875730d3b
Reviewed-on: https://chromium-review.googlesource.com/817620Reviewed-by: default avatarSorin Jianu <sorin@chromium.org>
Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Reviewed-by: default avatarJoe Mason <joenotcharles@chromium.org>
Commit-Queue: proberge <proberge@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524450}
parent f1d7d41d
...@@ -49,15 +49,12 @@ enum SoftwareReporterExperimentError { ...@@ -49,15 +49,12 @@ enum SoftwareReporterExperimentError {
}; };
// Callback for running the software reporter after it is downloaded. // Callback for running the software reporter after it is downloaded.
using SwReporterRunner = base::Callback<void( using OnComponentReadyCallback = base::Callback<void(
safe_browsing::SwReporterInvocationType invocation_type,
safe_browsing::SwReporterInvocationSequence&& invocations)>; safe_browsing::SwReporterInvocationSequence&& invocations)>;
class SwReporterInstallerPolicy : public ComponentInstallerPolicy { class SwReporterInstallerPolicy : public ComponentInstallerPolicy {
public: public:
SwReporterInstallerPolicy( explicit SwReporterInstallerPolicy(const OnComponentReadyCallback& callback);
const SwReporterRunner& reporter_runner,
safe_browsing::SwReporterInvocationType invocation_type);
~SwReporterInstallerPolicy() override; ~SwReporterInstallerPolicy() override;
// ComponentInstallerPolicy implementation. // ComponentInstallerPolicy implementation.
...@@ -81,20 +78,11 @@ class SwReporterInstallerPolicy : public ComponentInstallerPolicy { ...@@ -81,20 +78,11 @@ class SwReporterInstallerPolicy : public ComponentInstallerPolicy {
private: private:
friend class SwReporterInstallerTest; friend class SwReporterInstallerTest;
SwReporterRunner reporter_runner_; OnComponentReadyCallback on_component_ready_callback_;
const safe_browsing::SwReporterInvocationType invocation_type_;
DISALLOW_COPY_AND_ASSIGN(SwReporterInstallerPolicy); DISALLOW_COPY_AND_ASSIGN(SwReporterInstallerPolicy);
}; };
// Installs the SwReporter component and runs the reporter once it's available.
// Once ready, this may trigger either a periodic or a user-initiated run of
// the reporter, depending on |invocation_type|.
void RegisterSwReporterComponentWithParams(
safe_browsing::SwReporterInvocationType invocation_type,
ComponentUpdateService* cus);
// Call once during startup to make the component update service aware of the // Call once during startup to make the component update service aware of the
// SwReporter. Once ready, this may trigger a periodic run of the reporter. // SwReporter. Once ready, this may trigger a periodic run of the reporter.
void RegisterSwReporterComponent(ComponentUpdateService* cus); void RegisterSwReporterComponent(ComponentUpdateService* cus);
......
...@@ -178,7 +178,9 @@ class ReporterRunnerTest ...@@ -178,7 +178,9 @@ class ReporterRunnerTest
SwReporterInvocationSequence CreateInvocationSequence( SwReporterInvocationSequence CreateInvocationSequence(
const SwReporterInvocationSequence::Queue& container) { const SwReporterInvocationSequence::Queue& container) {
return SwReporterInvocationSequence(base::Version("1.2.3"), container); SwReporterInvocationSequence sequence(base::Version("1.2.3"));
sequence.mutable_container() = container;
return sequence;
} }
// Schedules a single reporter to run. // Schedules a single reporter to run.
...@@ -209,7 +211,7 @@ class ReporterRunnerTest ...@@ -209,7 +211,7 @@ class ReporterRunnerTest
OnReporterSequenceDone(Eq(expected_result))) OnReporterSequenceDone(Eq(expected_result)))
.WillOnce(InvokeWithoutArgs(&on_sequence_done, &Waiter::Signal)); .WillOnce(InvokeWithoutArgs(&on_sequence_done, &Waiter::Signal));
auto invocations = CreateInvocationSequence(container); auto invocations = CreateInvocationSequence(container);
RunSwReporters(invocation_type_, std::move(invocations)); RunSwReportersForTesting(invocation_type_, std::move(invocations));
on_sequence_done.Wait(); on_sequence_done.Wait();
} }
...@@ -413,7 +415,8 @@ class ReporterRunnerTest ...@@ -413,7 +415,8 @@ class ReporterRunnerTest
InvokeWithoutArgs(&first_sequence_done, &Waiter::Signal))); InvokeWithoutArgs(&first_sequence_done, &Waiter::Signal)));
SwReporterInvocationSequence::Queue invocations1({CreateInvocation(path1)}); SwReporterInvocationSequence::Queue invocations1({CreateInvocation(path1)});
RunSwReporters(invocation_type1, CreateInvocationSequence(invocations1)); RunSwReportersForTesting(invocation_type1,
CreateInvocationSequence(invocations1));
EXPECT_CALL( EXPECT_CALL(
mock_chrome_cleaner_controller_, mock_chrome_cleaner_controller_,
...@@ -421,7 +424,8 @@ class ReporterRunnerTest ...@@ -421,7 +424,8 @@ class ReporterRunnerTest
.WillOnce(InvokeWithoutArgs(&second_sequence_done, &Waiter::Signal)); .WillOnce(InvokeWithoutArgs(&second_sequence_done, &Waiter::Signal));
SwReporterInvocationSequence::Queue invocations2({CreateInvocation(path2)}); SwReporterInvocationSequence::Queue invocations2({CreateInvocation(path2)});
RunSwReporters(invocation_type2, CreateInvocationSequence(invocations2)); RunSwReportersForTesting(invocation_type2,
CreateInvocationSequence(invocations2));
first_sequence_done.Wait(); first_sequence_done.Wait();
......
...@@ -1044,9 +1044,8 @@ void SwReporterInvocation::set_cleaner_logs_upload_enabled( ...@@ -1044,9 +1044,8 @@ void SwReporterInvocation::set_cleaner_logs_upload_enabled(
} }
SwReporterInvocationSequence::SwReporterInvocationSequence( SwReporterInvocationSequence::SwReporterInvocationSequence(
const base::Version& version, const base::Version& version)
const Queue& container) : version_(version) {
: version_(version), container_(container) {
// Notify the cleaner controller once this sequence completes. Don't retain // Notify the cleaner controller once this sequence completes. Don't retain
// a reference to the controller object, since it's guaranteed to outlive the // a reference to the controller object, since it's guaranteed to outlive the
// sequence. // sequence.
...@@ -1070,6 +1069,11 @@ void SwReporterInvocationSequence::operator=( ...@@ -1070,6 +1069,11 @@ void SwReporterInvocationSequence::operator=(
on_sequence_done_ = std::move(invocations_sequence.on_sequence_done_); on_sequence_done_ = std::move(invocations_sequence.on_sequence_done_);
} }
void SwReporterInvocationSequence::PushInvocation(
const SwReporterInvocation& invocation) {
container_.push(invocation);
}
void SwReporterInvocationSequence::NotifySequenceDone( void SwReporterInvocationSequence::NotifySequenceDone(
SwReporterInvocationResult result) { SwReporterInvocationResult result) {
if (on_sequence_done_) if (on_sequence_done_)
...@@ -1090,8 +1094,8 @@ SwReporterInvocationSequence::mutable_container() { ...@@ -1090,8 +1094,8 @@ SwReporterInvocationSequence::mutable_container() {
return container_; return container_;
} }
void RunSwReporters(SwReporterInvocationType invocation_type, void RunSwReportersForTesting(SwReporterInvocationType invocation_type,
SwReporterInvocationSequence&& invocations) { SwReporterInvocationSequence&& invocations) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!invocations.container().empty()); DCHECK(!invocations.container().empty());
...@@ -1099,6 +1103,15 @@ void RunSwReporters(SwReporterInvocationType invocation_type, ...@@ -1099,6 +1103,15 @@ void RunSwReporters(SwReporterInvocationType invocation_type,
std::move(invocations)); std::move(invocations));
} }
void OnSwReporterReady(SwReporterInvocationSequence&& invocations) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!invocations.container().empty());
// TODO(crbug.com/776538): Handle user-initiated cleanups.
ReporterRunner::MaybeStartInvocations(SwReporterInvocationType::kPeriodicRun,
std::move(invocations));
}
void SetSwReporterTestingDelegate(SwReporterTestingDelegate* delegate) { void SetSwReporterTestingDelegate(SwReporterTestingDelegate* delegate) {
g_testing_delegate_ = delegate; g_testing_delegate_ = delegate;
} }
......
...@@ -160,11 +160,13 @@ class SwReporterInvocationSequence { ...@@ -160,11 +160,13 @@ class SwReporterInvocationSequence {
public: public:
using Queue = std::queue<SwReporterInvocation>; using Queue = std::queue<SwReporterInvocation>;
SwReporterInvocationSequence(const base::Version& version = base::Version(), explicit SwReporterInvocationSequence(
const Queue& container = Queue()); const base::Version& version = base::Version());
SwReporterInvocationSequence(SwReporterInvocationSequence&& queue); SwReporterInvocationSequence(SwReporterInvocationSequence&& queue);
virtual ~SwReporterInvocationSequence(); virtual ~SwReporterInvocationSequence();
void PushInvocation(const SwReporterInvocation& invocation);
void operator=(SwReporterInvocationSequence&& queue); void operator=(SwReporterInvocationSequence&& queue);
void NotifySequenceDone(SwReporterInvocationResult result); void NotifySequenceDone(SwReporterInvocationResult result);
...@@ -182,7 +184,13 @@ class SwReporterInvocationSequence { ...@@ -182,7 +184,13 @@ class SwReporterInvocationSequence {
OnReporterSequenceDone on_sequence_done_; OnReporterSequenceDone on_sequence_done_;
}; };
// Tries to run the sw_reporter component. If this runs successfully, than any // This is used only by tests and exists solely to make the browser tests happy
// while we implement user-initiated runs.
// TODO(proberge): Remove this by Q1 2018.
void RunSwReportersForTesting(SwReporterInvocationType invocation_type,
SwReporterInvocationSequence&& invocations);
// Tries to run the given invocations. If this runs successfully, than any
// calls made in the next |kDaysBetweenSuccessfulSwReporterRuns| days will be // calls made in the next |kDaysBetweenSuccessfulSwReporterRuns| days will be
// ignored. // ignored.
// //
...@@ -190,8 +198,7 @@ class SwReporterInvocationSequence { ...@@ -190,8 +198,7 @@ class SwReporterInvocationSequence {
// executions of the tool with different command lines. |invocations| is the // executions of the tool with different command lines. |invocations| is the
// queue of SwReporters to execute as a single "run". When a new try is // queue of SwReporters to execute as a single "run". When a new try is
// scheduled the entire queue is executed. // scheduled the entire queue is executed.
void RunSwReporters(SwReporterInvocationType invocation_type, void OnSwReporterReady(SwReporterInvocationSequence&& invocations);
SwReporterInvocationSequence&& invocations);
// A delegate used by tests to implement test doubles (e.g., stubs, fakes, or // A delegate used by tests to implement test doubles (e.g., stubs, fakes, or
// mocks). // mocks).
......
...@@ -210,12 +210,8 @@ void ChromeCleanupHandler::HandleStartScanning(const base::ListValue* args) { ...@@ -210,12 +210,8 @@ void ChromeCleanupHandler::HandleStartScanning(const base::ListValue* args) {
// The state is propagated to all open tabs and should be consistent. // The state is propagated to all open tabs and should be consistent.
DCHECK_EQ(controller_->logs_enabled(), allow_logs_upload); DCHECK_EQ(controller_->logs_enabled(), allow_logs_upload);
// TODO(crbug.com/776538): Force an on-demand update of the component. // TODO(crbug.com/776538): Force a proper on-demand update of the component.
component_updater::RegisterSwReporterComponentWithParams( component_updater::RegisterSwReporterComponent(
allow_logs_upload ? safe_browsing::SwReporterInvocationType::
kUserInitiatedWithLogsAllowed
: safe_browsing::SwReporterInvocationType::
kUserInitiatedWithLogsDisallowed,
g_browser_process->component_updater()); g_browser_process->component_updater());
base::RecordAction( base::RecordAction(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment