OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/ui/webui/cleanup_tool/cleanup_action_handler.h" | 5 #include "chrome/browser/ui/webui/cleanup_tool/cleanup_action_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/timer/timer.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/component_updater/sw_reporter_installer_win.h" |
9 | 12 |
10 CleanupActionHandler::CleanupActionHandler() {} | 13 namespace { |
| 14 |
| 15 bool IsCleanupComponentRegistered() { |
| 16 component_updater::ComponentUpdateService* cus = |
| 17 g_browser_process->component_updater(); |
| 18 std::vector<std::string> component_ids = cus->GetComponentIDs(); |
| 19 |
| 20 return std::find(component_ids.begin(), component_ids.end(), |
| 21 component_updater::kSwReporterComponentId) != |
| 22 component_ids.end(); |
| 23 } |
| 24 |
| 25 // TODO(proberge): Localize strings once they are finalized. |
| 26 constexpr char kDetectionOkText[] = "No problems detected"; |
| 27 constexpr char kDetectionUwSText[] = "2 potentially harmful programs detected"; |
| 28 constexpr char kDetectionTimeText[] = "Last scanned today"; |
| 29 constexpr char kCleanupUnsupportedText[] = "Chrome Cleanup is not supported."; |
| 30 constexpr char kUnsupportedHelpText[] = "Please try reinstalling Chrome"; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 CleanupActionHandler::CleanupActionHandler() |
| 35 : callback_weak_ptr_factory_(this) {} |
11 | 36 |
12 CleanupActionHandler::~CleanupActionHandler() {} | 37 CleanupActionHandler::~CleanupActionHandler() {} |
13 | 38 |
14 void CleanupActionHandler::RegisterMessages() { | 39 void CleanupActionHandler::RegisterMessages() { |
15 web_ui()->RegisterMessageCallback( | 40 web_ui()->RegisterMessageCallback( |
16 "requestLastScanResult", | 41 "requestLastScanResult", |
17 base::Bind(&CleanupActionHandler::HandleRequestLastScanResult, | 42 base::Bind(&CleanupActionHandler::HandleRequestLastScanResult, |
18 base::Unretained(this))); | 43 base::Unretained(this))); |
| 44 web_ui()->RegisterMessageCallback( |
| 45 "startScan", base::Bind(&CleanupActionHandler::HandleStartScan, |
| 46 base::Unretained(this))); |
| 47 } |
| 48 |
| 49 void CleanupActionHandler::OnJavascriptDisallowed() { |
| 50 callback_weak_ptr_factory_.InvalidateWeakPtrs(); |
19 } | 51 } |
20 | 52 |
21 void CleanupActionHandler::HandleRequestLastScanResult( | 53 void CleanupActionHandler::HandleRequestLastScanResult( |
22 const base::ListValue* args) { | 54 const base::ListValue* args) { |
23 std::string webui_callback_id; | 55 std::string webui_callback_id; |
24 CHECK_EQ(1U, args->GetSize()); | 56 CHECK_EQ(1U, args->GetSize()); |
25 bool success = args->GetString(0, &webui_callback_id); | 57 bool success = args->GetString(0, &webui_callback_id); |
26 DCHECK(success); | 58 DCHECK(success); |
27 | 59 |
28 base::DictionaryValue last_scan_results; | 60 base::DictionaryValue last_scan_results; |
29 // TODO(proberge): Return real information about the last run. | 61 // TODO(proberge): Return real information about the last run. |
30 // TODO(proberge): Localize strings once they are finalized. | |
31 last_scan_results.SetBoolean("hasScanResults", false); | 62 last_scan_results.SetBoolean("hasScanResults", false); |
32 last_scan_results.SetBoolean("isInfected", false); | 63 last_scan_results.SetBoolean("isInfected", false); |
33 last_scan_results.SetString("detectionStatusText", "No problems detected"); | 64 last_scan_results.SetString("detectionStatusText", kDetectionOkText); |
34 last_scan_results.SetString("detectionTimeText", "Last scanned today"); | 65 last_scan_results.SetString("detectionTimeText", kDetectionTimeText); |
35 | 66 |
36 AllowJavascript(); | 67 AllowJavascript(); |
37 ResolveJavascriptCallback(base::Value(webui_callback_id), last_scan_results); | 68 ResolveJavascriptCallback(base::Value(webui_callback_id), last_scan_results); |
38 } | 69 } |
| 70 |
| 71 void CleanupActionHandler::HandleStartScan(const base::ListValue* args) { |
| 72 std::string webui_callback_id; |
| 73 CHECK_EQ(1U, args->GetSize()); |
| 74 bool success = args->GetString(0, &webui_callback_id); |
| 75 DCHECK(success); |
| 76 |
| 77 if (!IsCleanupComponentRegistered()) { |
| 78 base::DictionaryValue scan_results; |
| 79 scan_results.SetBoolean("hasScanResults", false); |
| 80 scan_results.SetBoolean("isInfected", false); |
| 81 scan_results.SetString("detectionStatusText", kCleanupUnsupportedText); |
| 82 scan_results.SetString("detectionTimeText", kUnsupportedHelpText); |
| 83 |
| 84 AllowJavascript(); |
| 85 ResolveJavascriptCallback(base::Value(webui_callback_id), scan_results); |
| 86 return; |
| 87 } |
| 88 |
| 89 component_updater::RegisterUserInitiatedSwReporterScan( |
| 90 base::Bind(&CleanupActionHandler::ReportScanResults, |
| 91 callback_weak_ptr_factory_.GetWeakPtr(), webui_callback_id)); |
| 92 } |
| 93 |
| 94 void CleanupActionHandler::ReportScanResults(const std::string& callback_id) { |
| 95 base::DictionaryValue scan_results; |
| 96 // TODO(proberge): Return real information about the scan. |
| 97 scan_results.SetBoolean("hasScanResults", true); |
| 98 scan_results.SetBoolean("isInfected", true); |
| 99 scan_results.SetString("detectionStatusText", kDetectionUwSText); |
| 100 scan_results.SetString("detectionTimeText", kDetectionTimeText); |
| 101 |
| 102 AllowJavascript(); |
| 103 ResolveJavascriptCallback(base::Value(callback_id), scan_results); |
| 104 } |
OLD | NEW |