1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/safe_browsing/srt_global_error_win.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/files/file_util.h"
12 #include "base/metrics/histogram.h"
13 #include "base/process/launch.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/global_error/global_error_service.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "grit/chromium_strings.h"
22 #include "grit/generated_resources.h"
23 #include "grit/google_chrome_strings.h"
24 #include "ui/base/l10n/l10n_util.h"
26 using base::SingleThreadTaskRunner
;
27 using base::ThreadTaskRunnerHandle
;
28 using content::BrowserThread
;
32 // Used as a backup plan in case the SRT executable was not successfully
34 const char kSRTDownloadURL
[] =
35 "https://www.google.com/chrome/srt/?chrome-prompt=1";
37 // The extension to use to replace the temporary one created when the SRT was
39 const base::FilePath::CharType kExecutableExtension
[] = L
"exe";
41 // A switch to add to the command line when executing the SRT.
42 const char kChromePromptSwitch
[] = "chrome-prompt";
44 // Enum values for the SRTPrompt histogram. Don't change order, always add
45 // to the end, before SRT_PROMPT_MAX, of course.
46 enum SRTPromptHistogramValue
{
48 SRT_PROMPT_ACCEPTED
= 1,
49 SRT_PROMPT_DENIED
= 2,
50 SRT_PROMPT_FALLBACK
= 3,
55 void RecordSRTPromptHistogram(SRTPromptHistogramValue value
) {
56 UMA_HISTOGRAM_ENUMERATION(
57 "SoftwareReporter.PromptUsage", value
, SRT_PROMPT_MAX
);
60 void MaybeExecuteSRTFromBlockingPool(
61 const base::FilePath
& downloaded_path
,
62 const scoped_refptr
<SingleThreadTaskRunner
>& task_runner
,
63 const base::Closure
& success_callback
,
64 const base::Closure
& failure_callback
) {
65 DCHECK(!downloaded_path
.empty());
67 if (base::PathExists(downloaded_path
)) {
68 base::FilePath
executable_path(
69 downloaded_path
.ReplaceExtension(kExecutableExtension
));
70 if (base::ReplaceFile(downloaded_path
, executable_path
, NULL
)) {
71 base::CommandLine
srt_command_line(executable_path
);
72 srt_command_line
.AppendSwitch(kChromePromptSwitch
);
73 base::Process
srt_process(
74 base::LaunchProcess(srt_command_line
, base::LaunchOptions()));
75 if (srt_process
.IsValid()) {
76 task_runner
->PostTask(FROM_HERE
, success_callback
);
82 task_runner
->PostTask(FROM_HERE
, failure_callback
);
85 void DeleteFilesFromBlockingPool(const base::FilePath
& downloaded_path
) {
86 base::DeleteFile(downloaded_path
, false);
87 base::DeleteFile(downloaded_path
.ReplaceExtension(kExecutableExtension
),
93 // SRTGlobalError ------------------------------------------------------------
95 SRTGlobalError::SRTGlobalError(GlobalErrorService
* global_error_service
,
96 const base::FilePath
& downloaded_path
)
97 : global_error_service_(global_error_service
),
98 downloaded_path_(downloaded_path
) {
99 DCHECK(global_error_service_
);
102 SRTGlobalError::~SRTGlobalError() {
105 bool SRTGlobalError::HasMenuItem() {
109 int SRTGlobalError::MenuItemCommandID() {
110 return IDC_SHOW_SRT_BUBBLE
;
113 base::string16
SRTGlobalError::MenuItemLabel() {
114 return l10n_util::GetStringUTF16(IDS_SRT_MENU_ITEM
);
117 void SRTGlobalError::ExecuteMenuItem(Browser
* browser
) {
118 ShowBubbleView(browser
);
121 void SRTGlobalError::ShowBubbleView(Browser
* browser
) {
122 RecordSRTPromptHistogram(SRT_PROMPT_SHOWN
);
123 GlobalErrorWithStandardBubble::ShowBubbleView(browser
);
126 base::string16
SRTGlobalError::GetBubbleViewTitle() {
127 return l10n_util::GetStringUTF16(IDS_SRT_BUBBLE_TITLE
);
130 std::vector
<base::string16
> SRTGlobalError::GetBubbleViewMessages() {
131 std::vector
<base::string16
> messages
;
132 messages
.push_back(l10n_util::GetStringUTF16(IDS_SRT_BUBBLE_TEXT
));
136 base::string16
SRTGlobalError::GetBubbleViewAcceptButtonLabel() {
137 return downloaded_path_
.empty()
138 ? l10n_util::GetStringUTF16(IDS_SRT_BUBBLE_DOWNLOAD_BUTTON_TEXT
)
139 : l10n_util::GetStringUTF16(IDS_SRT_BUBBLE_RUN_BUTTON_TEXT
);
142 bool SRTGlobalError::ShouldAddElevationIconToAcceptButton() {
143 return !downloaded_path_
.empty();
146 base::string16
SRTGlobalError::GetBubbleViewCancelButtonLabel() {
147 return l10n_util::GetStringUTF16(IDS_NO_THANKS
);
150 void SRTGlobalError::OnBubbleViewDidClose(Browser
* browser
) {
153 void SRTGlobalError::BubbleViewAcceptButtonPressed(Browser
* browser
) {
154 RecordSRTPromptHistogram(SRT_PROMPT_ACCEPTED
);
155 global_error_service_
->RemoveGlobalError(this);
159 void SRTGlobalError::BubbleViewCancelButtonPressed(Browser
* browser
) {
160 RecordSRTPromptHistogram(SRT_PROMPT_DENIED
);
161 global_error_service_
->RemoveGlobalError(this);
163 BrowserThread::PostBlockingPoolTask(
164 FROM_HERE
, base::Bind(&DeleteFilesFromBlockingPool
, downloaded_path_
));
168 bool SRTGlobalError::ShouldCloseOnDeactivate() const {
172 void SRTGlobalError::MaybeExecuteSRT() {
173 if (downloaded_path_
.empty()) {
174 FallbackToDownloadPage();
177 // At this point, this object owns itself, since ownership has been taken back
178 // from the global_error_service_ in the call to RemoveGlobalError. This means
179 // that it is safe to use base::Unretained here.
180 BrowserThread::PostBlockingPoolTask(
181 FROM_HERE
, base::Bind(&MaybeExecuteSRTFromBlockingPool
, downloaded_path_
,
182 base::ThreadTaskRunnerHandle::Get(),
183 base::Bind(&SRTGlobalError::DestroySelf
,
184 base::Unretained(this)),
185 base::Bind(&SRTGlobalError::FallbackToDownloadPage
,
186 base::Unretained(this))));
189 void SRTGlobalError::FallbackToDownloadPage() {
190 RecordSRTPromptHistogram(SRT_PROMPT_FALLBACK
);
192 chrome::HostDesktopType desktop_type
= chrome::GetActiveDesktop();
193 Browser
* browser
= chrome::FindLastActiveWithHostDesktopType(desktop_type
);
195 browser
->OpenURL(content::OpenURLParams(
196 GURL(kSRTDownloadURL
), content::Referrer(), NEW_FOREGROUND_TAB
,
197 ui::PAGE_TRANSITION_LINK
, false));
200 BrowserThread::PostBlockingPoolTask(
201 FROM_HERE
, base::Bind(&DeleteFilesFromBlockingPool
, downloaded_path_
));
205 void SRTGlobalError::DestroySelf() {