Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: chrome/browser/safe_browsing/srt_fetcher_win.cc

Issue 2834613003: Adds error handling support for the SwReporter launcher. (Closed)
Patch Set: Code review Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/safe_browsing/srt_fetcher_win.h" 5 #include "chrome/browser/safe_browsing/srt_fetcher_win.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
(...skipping 17 matching lines...) Expand all
28 #include "base/task_runner_util.h" 28 #include "base/task_runner_util.h"
29 #include "base/task_scheduler/post_task.h" 29 #include "base/task_scheduler/post_task.h"
30 #include "base/task_scheduler/task_traits.h" 30 #include "base/task_scheduler/task_traits.h"
31 #include "base/time/time.h" 31 #include "base/time/time.h"
32 #include "base/version.h" 32 #include "base/version.h"
33 #include "base/win/registry.h" 33 #include "base/win/registry.h"
34 #include "chrome/browser/browser_process.h" 34 #include "chrome/browser/browser_process.h"
35 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" 35 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
36 #include "chrome/browser/profiles/profile.h" 36 #include "chrome/browser/profiles/profile.h"
37 #include "chrome/browser/profiles/profile_io_data.h" 37 #include "chrome/browser/profiles/profile_io_data.h"
38 #include "chrome/browser/safe_browsing/srt_chrome_prompt_impl.h"
39 #include "chrome/browser/safe_browsing/srt_client_info_win.h" 38 #include "chrome/browser/safe_browsing/srt_client_info_win.h"
40 #include "chrome/browser/safe_browsing/srt_global_error_win.h" 39 #include "chrome/browser/safe_browsing/srt_global_error_win.h"
41 #include "chrome/browser/ui/browser_finder.h" 40 #include "chrome/browser/ui/browser_finder.h"
42 #include "chrome/browser/ui/browser_list.h" 41 #include "chrome/browser/ui/browser_list.h"
43 #include "chrome/browser/ui/browser_list_observer.h" 42 #include "chrome/browser/ui/browser_list_observer.h"
44 #include "chrome/browser/ui/global_error/global_error_service.h" 43 #include "chrome/browser/ui/global_error/global_error_service.h"
45 #include "chrome/browser/ui/global_error/global_error_service_factory.h" 44 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
46 #include "chrome/common/pref_names.h" 45 #include "chrome/common/pref_names.h"
47 #include "components/chrome_cleaner/public/constants/constants.h" 46 #include "components/chrome_cleaner/public/constants/constants.h"
48 #include "components/chrome_cleaner/public/interfaces/chrome_prompt.mojom.h" 47 #include "components/chrome_cleaner/public/interfaces/chrome_prompt.mojom.h"
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 583
585 private: 584 private:
586 friend class base::RefCountedThreadSafe<SwReporterProcess>; 585 friend class base::RefCountedThreadSafe<SwReporterProcess>;
587 ~SwReporterProcess() = default; 586 ~SwReporterProcess() = default;
588 587
589 // Starts a new IPC service implementing the ChromePrompt interface and 588 // Starts a new IPC service implementing the ChromePrompt interface and
590 // launches a new reporter process that can connect to the IPC. 589 // launches a new reporter process that can connect to the IPC.
591 base::Process LaunchConnectedReporterProcess(); 590 base::Process LaunchConnectedReporterProcess();
592 591
593 // Starts a new instance of ChromePromptImpl to receive requests from the 592 // Starts a new instance of ChromePromptImpl to receive requests from the
594 // reporter. Must be run on the IO thread. 593 // reporter and establishes the mojo connection to it.
594 // Must be run on the IO thread.
595 void CreateChromePromptImpl( 595 void CreateChromePromptImpl(
596 base::ProcessHandle process,
597 mojo::edk::ConnectionParams connection_params,
598 std::unique_ptr<mojo::edk::PendingProcessConnection>
599 pending_process_connection,
596 chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request); 600 chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request);
597 601
598 // Releases the instance of ChromePromptImpl. Must be run on the IO thread. 602 // Releases the instance of ChromePromptImpl. Must be run on the IO thread.
599 void ReleaseChromePromptImpl(); 603 void ReleaseChromePromptImpl();
600 604
601 // Launches a new process with the command line in the invocation and 605 // Launches a new process with the command line in the invocation and
602 // provided launch options. Uses g_testing_delegate_ if not null. 606 // provided launch options. Uses g_testing_delegate_ if not null.
603 base::Process LaunchReporterProcess( 607 base::Process LaunchReporterProcess(
604 const SwReporterInvocation& invocation, 608 const SwReporterInvocation& invocation,
605 const base::LaunchOptions& launch_options); 609 const base::LaunchOptions& launch_options);
606 610
607 // The invocation for the current reporter process. 611 // The invocation for the current reporter process.
608 SwReporterInvocation invocation_; 612 SwReporterInvocation invocation_;
609 613
610 // Implementation of the ChromePrompt service to be used by the current 614 // Implementation of the ChromePrompt service to be used by the current
611 // reporter process. Can only be accessed on the IO thread. 615 // reporter process. Can only be accessed on the IO thread.
612 std::unique_ptr<ChromePromptImpl> chrome_prompt_impl_; 616 std::unique_ptr<ChromePromptImpl> chrome_prompt_impl_;
613 }; 617 };
614 618
615 int SwReporterProcess::LaunchAndWaitForExitOnBackgroundThread() { 619 int SwReporterProcess::LaunchAndWaitForExitOnBackgroundThread() {
616 base::Process reporter_process = 620 base::Process reporter_process =
617 base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature) 621 base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)
618 ? LaunchConnectedReporterProcess() 622 ? LaunchConnectedReporterProcess()
619 : LaunchReporterProcess(invocation_, base::LaunchOptions()); 623 : LaunchReporterProcess(invocation_, base::LaunchOptions());
620 624
621 // This exit code is used to identify that a reporter run didn't happen, so 625 // This exit code is used to identify that a reporter run didn't happen, so
622 // the result should be ignored and a rerun scheduled for the usual delay. 626 // the result should be ignored and a rerun scheduled for the usual delay.
623 int exit_code = kReporterFailureExitCode; 627 int exit_code = kReporterNotLaunchedExitCode;
624 UMAHistogramReporter uma(invocation_.suffix); 628 UMAHistogramReporter uma(invocation_.suffix);
625 if (reporter_process.IsValid()) { 629 if (reporter_process.IsValid()) {
626 uma.RecordReporterStep(SW_REPORTER_START_EXECUTION); 630 uma.RecordReporterStep(SW_REPORTER_START_EXECUTION);
627 bool success = reporter_process.WaitForExit(&exit_code); 631 bool success = reporter_process.WaitForExit(&exit_code);
628 DCHECK(success); 632 DCHECK(success);
629 } else { 633 } else {
630 uma.RecordReporterStep(SW_REPORTER_FAILED_TO_START); 634 uma.RecordReporterStep(SW_REPORTER_FAILED_TO_START);
631 } 635 }
632 return exit_code; 636 return exit_code;
633 } 637 }
634 638
635 void SwReporterProcess::OnReporterDone() { 639 void SwReporterProcess::OnReporterDone() {
636 if (base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)) { 640 if (base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)) {
637 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) 641 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)
638 ->PostTask(FROM_HERE, 642 ->PostTask(FROM_HERE,
639 base::Bind(&SwReporterProcess::ReleaseChromePromptImpl, 643 base::Bind(&SwReporterProcess::ReleaseChromePromptImpl,
640 base::RetainedRef(this))); 644 base::RetainedRef(this)));
641 } 645 }
642 } 646 }
643 647
644 base::Process SwReporterProcess::LaunchConnectedReporterProcess() { 648 base::Process SwReporterProcess::LaunchConnectedReporterProcess() {
645 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)); 649 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature));
646 650
647 mojo::edk::PendingProcessConnection pending_process_connection; 651 std::unique_ptr<mojo::edk::PendingProcessConnection>
652 pending_process_connection =
653 base::MakeUnique<mojo::edk::PendingProcessConnection>();
648 std::string mojo_pipe_token; 654 std::string mojo_pipe_token;
649 mojo::ScopedMessagePipeHandle mojo_pipe = 655 mojo::ScopedMessagePipeHandle mojo_pipe =
650 pending_process_connection.CreateMessagePipe(&mojo_pipe_token); 656 pending_process_connection->CreateMessagePipe(&mojo_pipe_token);
651 invocation_.command_line.AppendSwitchASCII( 657 invocation_.command_line.AppendSwitchASCII(
652 chrome_cleaner::kChromeMojoPipeTokenSwitch, mojo_pipe_token); 658 chrome_cleaner::kChromeMojoPipeTokenSwitch, mojo_pipe_token);
653 invocation_.command_line.AppendSwitchASCII( 659 invocation_.command_line.AppendSwitchASCII(
654 chrome_cleaner::kExecutionModeSwitch, 660 chrome_cleaner::kExecutionModeSwitch,
655 base::IntToString( 661 base::IntToString(
656 static_cast<int>(chrome_cleaner::ExecutionMode::kScanning))); 662 static_cast<int>(chrome_cleaner::ExecutionMode::kScanning)));
657 663
658 mojo::edk::PlatformChannelPair channel; 664 mojo::edk::PlatformChannelPair channel;
659 base::HandlesToInheritVector handles_to_inherit; 665 base::HandlesToInheritVector handles_to_inherit;
660 channel.PrepareToPassClientHandleToChildProcess(&invocation_.command_line, 666 channel.PrepareToPassClientHandleToChildProcess(&invocation_.command_line,
661 &handles_to_inherit); 667 &handles_to_inherit);
662 668
663 base::LaunchOptions launch_options; 669 base::LaunchOptions launch_options;
664 launch_options.handles_to_inherit = &handles_to_inherit; 670 launch_options.handles_to_inherit = &handles_to_inherit;
665 base::Process reporter_process = 671 base::Process reporter_process =
666 LaunchReporterProcess(invocation_, launch_options); 672 LaunchReporterProcess(invocation_, launch_options);
667 673
668 if (!reporter_process.IsValid()) 674 if (!reporter_process.IsValid())
669 return reporter_process; 675 return reporter_process;
670 676
671 pending_process_connection.Connect(
672 reporter_process.Handle(),
673 mojo::edk::ConnectionParams(channel.PassServerHandle()));
674
675 chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request; 677 chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request;
676 chrome_prompt_request.Bind(std::move(mojo_pipe)); 678 chrome_prompt_request.Bind(std::move(mojo_pipe));
677 679
678 // ChromePromptImpl tasks will need to run on the IO thread. There is no 680 // ChromePromptImpl tasks will need to run on the IO thread. There is no
679 // need to synchronize its creation, since the client end will wait for this 681 // need to synchronize its creation, since the client end will wait for this
680 // initialization to be done before sending requests. 682 // initialization to be done before sending requests.
683 // It's safe to pass the process handle here
Joe Mason 2017/04/24 14:53:48 Nit: End this line with a period, and either put i
ftirelo 2017/04/24 15:47:46 This was still wip, not needed anymore.
681 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) 684 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)
682 ->PostTask(FROM_HERE, 685 ->PostTask(FROM_HERE,
683 base::BindOnce(&SwReporterProcess::CreateChromePromptImpl, 686 base::BindOnce(
684 base::RetainedRef(this), 687 &SwReporterProcess::CreateChromePromptImpl,
685 std::move(chrome_prompt_request))); 688 base::RetainedRef(this), reporter_process.Handle(),
689 mojo::edk::ConnectionParams(channel.PassServerHandle()),
690 std::move(pending_process_connection),
691 std::move(chrome_prompt_request)));
686 692
687 return reporter_process; 693 return reporter_process;
688 } 694 }
689 695
690 base::Process SwReporterProcess::LaunchReporterProcess( 696 base::Process SwReporterProcess::LaunchReporterProcess(
691 const SwReporterInvocation& invocation, 697 const SwReporterInvocation& invocation,
692 const base::LaunchOptions& launch_options) { 698 const base::LaunchOptions& launch_options) {
693 return g_testing_delegate_ 699 return g_testing_delegate_
694 ? g_testing_delegate_->LaunchReporter(invocation, launch_options) 700 ? g_testing_delegate_->LaunchReporter(invocation, launch_options)
695 : base::LaunchProcess(invocation.command_line, launch_options); 701 : base::LaunchProcess(invocation.command_line, launch_options);
696 } 702 }
697 703
698 void SwReporterProcess::CreateChromePromptImpl( 704 void SwReporterProcess::CreateChromePromptImpl(
705 base::ProcessHandle process,
706 mojo::edk::ConnectionParams connection_params,
707 std::unique_ptr<mojo::edk::PendingProcessConnection>
708 pending_process_connection,
699 chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request) { 709 chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request) {
700 DCHECK_CURRENTLY_ON(BrowserThread::IO); 710 DCHECK_CURRENTLY_ON(BrowserThread::IO);
701 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)); 711 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature));
702 712
703 chrome_prompt_impl_ = 713 chrome_prompt_impl_ = g_testing_delegate_
704 base::MakeUnique<ChromePromptImpl>(std::move(chrome_prompt_request)); 714 ? g_testing_delegate_->CreateChromePromptImpl(
715 std::move(chrome_prompt_request))
716 : base::MakeUnique<ChromePromptImpl>(
717 std::move(chrome_prompt_request));
718
719 pending_process_connection->Connect(
720 process, std::move(connection_params),
721 base::Bind(&ChromePromptImpl::OnConnectionError,
722 base::Unretained(chrome_prompt_impl_.get())));
Joe Mason 2017/04/24 14:53:48 Because I know grt will ask: please add a comment
ftirelo 2017/04/24 15:47:46 Obsolete.
grt (UTC plus 2) 2017/04/25 07:06:39 Impact! Influence! ;-)
705 } 723 }
706 724
707 void SwReporterProcess::ReleaseChromePromptImpl() { 725 void SwReporterProcess::ReleaseChromePromptImpl() {
708 DCHECK_CURRENTLY_ON(BrowserThread::IO); 726 DCHECK_CURRENTLY_ON(BrowserThread::IO);
709 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)); 727 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature));
710 728
711 chrome_prompt_impl_.release(); 729 chrome_prompt_impl_.release();
712 } 730 }
713 731
714 } // namespace 732 } // namespace
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 scoped_refptr<SwReporterProcess> sw_reporter_process, 945 scoped_refptr<SwReporterProcess> sw_reporter_process,
928 int exit_code) { 946 int exit_code) {
929 DCHECK_CURRENTLY_ON(BrowserThread::UI); 947 DCHECK_CURRENTLY_ON(BrowserThread::UI);
930 948
931 sw_reporter_process->OnReporterDone(); 949 sw_reporter_process->OnReporterDone();
932 950
933 base::Time now = Now(); 951 base::Time now = Now();
934 base::TimeDelta reporter_running_time = now - reporter_start_time; 952 base::TimeDelta reporter_running_time = now - reporter_start_time;
935 953
936 // Don't continue the current queue of reporters if one failed to launch. 954 // Don't continue the current queue of reporters if one failed to launch.
937 if (exit_code == kReporterFailureExitCode) 955 if (exit_code == kReporterNotLaunchedExitCode)
938 current_invocations_ = SwReporterQueue(); 956 current_invocations_ = SwReporterQueue();
939 957
940 // As soon as we're not running this queue, schedule the next overall queue 958 // As soon as we're not running this queue, schedule the next overall queue
941 // run after the regular delay. (If there was a failure it's not worth 959 // run after the regular delay. (If there was a failure it's not worth
942 // retrying earlier, risking running too often if it always fails, since 960 // retrying earlier, risking running too often if it always fails, since
943 // not many users fail here.) 961 // not many users fail here.)
944 if (current_invocations_.empty()) { 962 if (current_invocations_.empty()) {
945 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 963 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
946 FROM_HERE, 964 FROM_HERE,
947 base::Bind(&ReporterRunner::TryToRun, base::Unretained(this)), 965 base::Bind(&ReporterRunner::TryToRun, base::Unretained(this)),
948 base::TimeDelta::FromDays(days_between_reporter_runs_)); 966 base::TimeDelta::FromDays(days_between_reporter_runs_));
949 } else { 967 } else {
950 ScheduleNextInvocation(); 968 ScheduleNextInvocation();
951 } 969 }
952 970
953 // If the reporter failed to launch, do not process the results. (The exit 971 // If the reporter failed to launch, do not process the results. (The exit
954 // code itself doesn't need to be logged in this case because 972 // code itself doesn't need to be logged in this case because
955 // SW_REPORTER_FAILED_TO_START is logged in 973 // SW_REPORTER_FAILED_TO_START is logged in
956 // |LaunchAndWaitForExitOnBackgroundThread|.) 974 // |LaunchAndWaitForExitOnBackgroundThread|.)
957 if (exit_code == kReporterFailureExitCode) 975 if (exit_code == kReporterNotLaunchedExitCode)
958 return; 976 return;
959 977
960 const auto& finished_invocation = sw_reporter_process->invocation(); 978 const auto& finished_invocation = sw_reporter_process->invocation();
961 UMAHistogramReporter uma(finished_invocation.suffix); 979 UMAHistogramReporter uma(finished_invocation.suffix);
962 uma.ReportVersion(version); 980 uma.ReportVersion(version);
963 uma.ReportExitCode(exit_code); 981 uma.ReportExitCode(exit_code);
964 uma.ReportEngineErrorCode(); 982 uma.ReportEngineErrorCode();
965 uma.ReportFoundUwS(); 983 uma.ReportFoundUwS();
966 984
967 PrefService* local_state = g_browser_process->local_state(); 985 PrefService* local_state = g_browser_process->local_state();
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 return srt_cleaner_key.Open(HKEY_CURRENT_USER, cleaner_key_path.c_str(), 1239 return srt_cleaner_key.Open(HKEY_CURRENT_USER, cleaner_key_path.c_str(),
1222 KEY_QUERY_VALUE) == ERROR_SUCCESS && 1240 KEY_QUERY_VALUE) == ERROR_SUCCESS &&
1223 srt_cleaner_key.GetValueCount() > 0; 1241 srt_cleaner_key.GetValueCount() > 0;
1224 } 1242 }
1225 1243
1226 void SetSwReporterTestingDelegate(SwReporterTestingDelegate* delegate) { 1244 void SetSwReporterTestingDelegate(SwReporterTestingDelegate* delegate) {
1227 g_testing_delegate_ = delegate; 1245 g_testing_delegate_ = delegate;
1228 } 1246 }
1229 1247
1230 } // namespace safe_browsing 1248 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698