Chromium Code Reviews| Index: chrome/browser/safe_browsing/srt_fetcher_win.cc |
| diff --git a/chrome/browser/safe_browsing/srt_fetcher_win.cc b/chrome/browser/safe_browsing/srt_fetcher_win.cc |
| index f2f98355f54f0865b9782a261e22085495606bc9..063e864dfeeae605cc67ce5f37c1eb9eeb552ce7 100644 |
| --- a/chrome/browser/safe_browsing/srt_fetcher_win.cc |
| +++ b/chrome/browser/safe_browsing/srt_fetcher_win.cc |
| @@ -36,6 +36,7 @@ |
| #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/profiles/profile_io_data.h" |
| +#include "chrome/browser/safe_browsing/srt_chrome_prompt_impl.h" |
| #include "chrome/browser/safe_browsing/srt_client_info_win.h" |
| #include "chrome/browser/safe_browsing/srt_global_error_win.h" |
| #include "chrome/browser/ui/browser_finder.h" |
| @@ -50,11 +51,16 @@ |
| #include "components/variations/net/variations_http_headers.h" |
| #include "components/version_info/version_info.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "mojo/edk/embedder/connection_params.h" |
| +#include "mojo/edk/embedder/pending_process_connection.h" |
| +#include "mojo/edk/embedder/platform_channel_pair.h" |
| +#include "mojo/public/cpp/system/message_pipe.h" |
| #include "net/base/load_flags.h" |
| #include "net/http/http_status_code.h" |
| #include "net/url_request/url_fetcher.h" |
| #include "net/url_request/url_fetcher_delegate.h" |
| #include "net/url_request/url_request_context_getter.h" |
| +#include "third_party/pipa/src/chrome_cleaner/public/interfaces/chrome_prompt.mojom.h" |
| using content::BrowserThread; |
| @@ -70,6 +76,9 @@ const wchar_t kCleanerSubKey[] = L"Cleaner"; |
| const wchar_t kEndTimeValueName[] = L"EndTime"; |
| const wchar_t kStartTimeValueName[] = L"StartTime"; |
| +const base::Feature kInBrowserCleanerUIFeature{ |
| + "InBrowserCleanerUI", base::FEATURE_DISABLED_BY_DEFAULT}; |
| + |
| namespace { |
| // Used to send UMA information about missing start and end time registry |
| @@ -165,6 +174,12 @@ const char kEngineErrorCodeMetricName[] = "SoftwareReporter.EngineErrorCode"; |
| // SoftwareReporterLogsUploadResult enum defined in the histograms.xml file. |
| const int kSwReporterLogsUploadResultMax = 30; |
| +constexpr char kChromeMojoPipeTokenSwitch[] = "chrome-mojo-pipe-token"; |
| + |
| +// DO NOT SUBMIT. This is for debugging only and will be removed before landing |
| +// this CL. |
| +constexpr bool kDebugOnlyForceReporterRun = true; |
| + |
| // Reports metrics about the software reporter via UMA (and sometimes Rappor). |
| class UMAHistogramReporter { |
| public: |
| @@ -553,19 +568,75 @@ void DisplaySRTPrompt(const base::FilePath& download_path) { |
| global_error->ShowBubbleView(browser); |
| } |
| -// This function is called from a worker thread to launch the SwReporter and |
| -// wait for termination to collect its exit code. This task could be |
| -// interrupted by a shutdown at any time, so it shouldn't depend on anything |
| -// external that could be shut down beforehand. |
| -int LaunchAndWaitForExit(const SwReporterInvocation& invocation) { |
| +// Class responsible for launching the reporter process and waiting for its |
| +// completion. If feature InBrowserCleanerUI is enabled, this object will also |
| +// be responsible for starting the ChromePromptImpl object in the IO thread and |
| +// controlling its lifetime. |
| +// |
| +// Expected lifecycle of a SwReporterProcess: |
| +// - created in the UI thread before the reporter process launch is posted |
|
grt (UTC plus 2)
2017/03/31 11:32:33
nit: "on" a thread rather than "in" (prepositions,
ftirelo
2017/03/31 16:37:37
Done :-)
(I'm afraid that now I don't know how to
|
| +// (method ScheduleNextInvocation); |
| +// - deleted in the UI thread once ReporterDone() finishes (the method is |
| +// called after the reporter process exits). |
| +// |
| +// If feature InBrowserCleanerUI feature is enabled, the following tasks will |
| +// be posted in sequence to the IO Thread and will retain the SwReporterProcess |
| +// object: |
| +// - creation of a ChromePromptImpl object right the reporter process is |
|
Joe Mason
2017/03/31 08:06:23
Looks like there are some missing words in this se
ftirelo
2017/03/31 16:37:37
Done.
|
| +// launched (that object will be responsible for handling IPC requests from |
| +// the reporter process); |
| +// - deletion of the ChromePromptImpl object on ReporterDone(). |
| +// As a consequence, the SwReporterProcess object can outlive ReporterDone() |
| +// and will only be deleted after the ChromePromptImpl object is released in |
| +// the IO thread. |
| +class SwReporterProcess : public base::RefCounted<SwReporterProcess> { |
| + public: |
| + explicit SwReporterProcess(const SwReporterInvocation& invocation) |
| + : invocation_(invocation) {} |
| + ~SwReporterProcess() = default; |
| + |
| + // This function is called from a worker thread to launch the SwReporter and |
| + // wait for termination to collect its exit code. This task could be |
| + // interrupted by a shutdown at any time, so it shouldn't depend on anything |
| + // external that could be shut down beforehand. |
| + int LaunchAndWaitForExitOnBackgroundThread(); |
| + |
| + // Schedules to delete the instance of ChromePromptImpl in the IO thread. |
| + void ReleaseChromePromptImpl(); |
| + |
| + const SwReporterInvocation& invocation() const { return invocation_; } |
| + |
| + private: |
| + // Starts a new IPC service implementing the ChromePrompt interface and |
| + // launches a new reporter process that can connect to the IPC. |
| + base::Process LaunchConnectedReporterProcess(); |
| + |
| + // Starts a new instance of ChromePromptImpl to receive requests from the |
| + // reporter. Must be run in the IO thread. |
| + void CreateChromePromptImpl(mojo::ScopedMessagePipeHandle my_pipe); |
| + |
| + // The invocation for the current reporter process. |
| + SwReporterInvocation invocation_; |
| + |
| + // Implementation of the ChromePrompt service to be used by the current |
| + // reporter process. |
|
grt (UTC plus 2)
2017/03/31 11:32:33
please document that this member may only be acces
ftirelo
2017/03/31 16:37:37
Done.
|
| + std::unique_ptr<ChromePromptImpl> chrome_prompt_impl_; |
| +}; |
| + |
| +int SwReporterProcess::LaunchAndWaitForExitOnBackgroundThread() { |
| if (g_testing_delegate_) |
| - return g_testing_delegate_->LaunchReporter(invocation); |
| + return g_testing_delegate_->LaunchReporter(invocation_); |
| + |
| base::Process reporter_process = |
| - base::LaunchProcess(invocation.command_line, base::LaunchOptions()); |
| + base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature) |
| + ? LaunchConnectedReporterProcess() |
| + : base::LaunchProcess(invocation_.command_line, |
| + base::LaunchOptions()); |
| + |
| // This exit code is used to identify that a reporter run didn't happen, so |
| // the result should be ignored and a rerun scheduled for the usual delay. |
| int exit_code = kReporterFailureExitCode; |
| - UMAHistogramReporter uma(invocation.suffix); |
| + UMAHistogramReporter uma(invocation_.suffix); |
| if (reporter_process.IsValid()) { |
| uma.RecordReporterStep(SW_REPORTER_START_EXECUTION); |
| bool success = reporter_process.WaitForExit(&exit_code); |
| @@ -576,6 +647,61 @@ int LaunchAndWaitForExit(const SwReporterInvocation& invocation) { |
| return exit_code; |
| } |
| +base::Process SwReporterProcess::LaunchConnectedReporterProcess() { |
| + DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)); |
| + |
| + mojo::edk::PendingProcessConnection pending_process_connection; |
| + std::string mojo_pipe_token; |
| + mojo::ScopedMessagePipeHandle mojo_pipe = |
| + pending_process_connection.CreateMessagePipe(&mojo_pipe_token); |
| + invocation_.command_line.AppendSwitchASCII(kChromeMojoPipeTokenSwitch, |
| + mojo_pipe_token); |
| + |
| + mojo::edk::PlatformChannelPair channel; |
| + base::HandlesToInheritVector handles_to_inherit; |
| + channel.PrepareToPassClientHandleToChildProcess(&invocation_.command_line, |
| + &handles_to_inherit); |
| + |
| + base::LaunchOptions launch_options; |
| + launch_options.handles_to_inherit = &handles_to_inherit; |
| + base::Process reporter_process = |
| + base::LaunchProcess(invocation_.command_line, launch_options); |
| + |
| + if (!reporter_process.IsValid()) |
| + return reporter_process; |
| + |
| + pending_process_connection.Connect( |
| + reporter_process.Handle(), |
| + mojo::edk::ConnectionParams(channel.PassServerHandle())); |
| + |
| + // ChromePromptImpl tasks will need to run in the IO thread. There is no |
| + // need to synchronize its creation, since the client end will wait for this |
| + // initialization to be done before sending requests. |
| + BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) |
| + ->PostTask(FROM_HERE, |
| + base::Bind(&SwReporterProcess::CreateChromePromptImpl, this, |
| + base::Passed(&mojo_pipe))); |
| + |
| + return reporter_process; |
| +} |
| + |
| +void SwReporterProcess::CreateChromePromptImpl( |
| + mojo::ScopedMessagePipeHandle mojo_pipe) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request; |
| + chrome_prompt_request.Bind(std::move(mojo_pipe)); |
| + chrome_prompt_impl_ = |
| + base::MakeUnique<ChromePromptImpl>(std::move(chrome_prompt_request)); |
| +} |
| + |
| +void SwReporterProcess::ReleaseChromePromptImpl() { |
|
grt (UTC plus 2)
2017/03/31 11:32:33
nit: method definitions should be in the same orde
ftirelo
2017/03/31 16:37:37
It's private now, so keeping it here since it was
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)); |
| + |
| + chrome_prompt_impl_.release(); |
| +} |
| + |
| } // namespace |
| void DisplaySRTPromptForTesting(const base::FilePath& download_path) { |
| @@ -771,11 +897,17 @@ class ReporterRunner : public chrome::BrowserListObserver { |
| base::TaskRunner* task_runner = |
| g_testing_delegate_ ? g_testing_delegate_->BlockingTaskRunner() |
| : blocking_task_runner_.get(); |
| - base::PostTaskAndReplyWithResult( |
| - task_runner, FROM_HERE, |
| - base::Bind(&LaunchAndWaitForExit, next_invocation), |
| + scoped_refptr<SwReporterProcess> sw_reporter_process = |
|
grt (UTC plus 2)
2017/03/31 11:32:33
nit: this can become "auto sw_reporter_process" si
ftirelo
2017/03/31 16:37:37
Done.
|
| + make_scoped_refptr(new SwReporterProcess(next_invocation)); |
| + auto launch_and_wait = |
| + base::Bind(&SwReporterProcess::LaunchAndWaitForExitOnBackgroundThread, |
| + sw_reporter_process); |
| + auto reporter_done = |
| base::Bind(&ReporterRunner::ReporterDone, base::Unretained(this), Now(), |
| - version_, next_invocation)); |
| + version_, std::move(sw_reporter_process)); |
| + base::PostTaskAndReplyWithResult(task_runner, FROM_HERE, |
| + std::move(launch_and_wait), |
| + std::move(reporter_done)); |
| } |
| // This method is called on the UI thread when an invocation of the reporter |
| @@ -783,10 +915,17 @@ class ReporterRunner : public chrome::BrowserListObserver { |
| // thread so should be resilient to unexpected shutdown. |
| void ReporterDone(const base::Time& reporter_start_time, |
| const base::Version& version, |
| - const SwReporterInvocation& finished_invocation, |
| + scoped_refptr<SwReporterProcess> sw_reporter_process, |
| int exit_code) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)) { |
|
grt (UTC plus 2)
2017/03/31 11:32:33
nit: putting this conditional out here seems to be
ftirelo
2017/03/31 16:37:37
Done.
|
| + BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) |
| + ->PostTask(FROM_HERE, |
| + base::Bind(&SwReporterProcess::ReleaseChromePromptImpl, |
| + sw_reporter_process)); |
| + } |
| + |
| base::Time now = Now(); |
| base::TimeDelta reporter_running_time = now - reporter_start_time; |
| @@ -809,10 +948,12 @@ class ReporterRunner : public chrome::BrowserListObserver { |
| // If the reporter failed to launch, do not process the results. (The exit |
| // code itself doesn't need to be logged in this case because |
| - // SW_REPORTER_FAILED_TO_START is logged in |LaunchAndWaitForExit|.) |
| + // SW_REPORTER_FAILED_TO_START is logged in |
| + // |LaunchAndWaitForExitOnBackgroundThread|.) |
| if (exit_code == kReporterFailureExitCode) |
| return; |
| + auto finished_invocation = sw_reporter_process->invocation(); |
| UMAHistogramReporter uma(finished_invocation.suffix); |
| uma.ReportVersion(version); |
| uma.ReportExitCode(exit_code); |
| @@ -895,11 +1036,12 @@ class ReporterRunner : public chrome::BrowserListObserver { |
| const base::Time next_trigger( |
| last_time_triggered + |
| base::TimeDelta::FromDays(days_between_reporter_runs_)); |
| - if (!pending_invocations_.empty() && |
| - (next_trigger <= now || |
| - // Also make sure the kSwReporterLastTimeTriggered value is not set in |
| - // the future. |
| - last_time_triggered > now)) { |
| + if (kDebugOnlyForceReporterRun || |
| + (!pending_invocations_.empty() && |
| + (next_trigger <= now || |
| + // Also make sure the kSwReporterLastTimeTriggered value is not set in |
| + // the future. |
| + last_time_triggered > now))) { |
| const base::Time last_time_sent_logs = base::Time::FromInternalValue( |
| local_state->GetInt64(prefs::kSwReporterLastTimeSentReport)); |
| const base::Time next_time_send_logs = |
| @@ -993,8 +1135,8 @@ class ReporterRunner : public chrome::BrowserListObserver { |
| scoped_refptr<base::TaskRunner> blocking_task_runner_ = |
| base::CreateTaskRunnerWithTraits( |
| - // LaunchAndWaitForExit() creates (MayBlock()) and joins |
| - // (WithBaseSyncPrimitives()) a process. |
| + // LaunchAndWaitForExitOnBackgroundThread() creates (MayBlock()) and |
| + // joins (WithBaseSyncPrimitives()) a process. |
| base::TaskTraits() |
| .WithShutdownBehavior( |
| base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |