OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/conflicts_ui.h" | 5 #include "chrome/browser/ui/webui/conflicts_ui.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "chrome/browser/chrome_notification_types.h" | |
20 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/win/enumerate_modules_model.h" | 20 #include "chrome/browser/win/enumerate_modules_model.h" |
22 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
23 #include "chrome/grit/chromium_strings.h" | 22 #include "chrome/grit/chromium_strings.h" |
24 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
25 #include "content/public/browser/notification_observer.h" | |
26 #include "content/public/browser/notification_registrar.h" | |
27 #include "content/public/browser/notification_service.h" | |
28 #include "content/public/browser/user_metrics.h" | 24 #include "content/public/browser/user_metrics.h" |
29 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
30 #include "content/public/browser/web_ui.h" | 26 #include "content/public/browser/web_ui.h" |
31 #include "content/public/browser/web_ui_data_source.h" | 27 #include "content/public/browser/web_ui_data_source.h" |
32 #include "content/public/browser/web_ui_message_handler.h" | 28 #include "content/public/browser/web_ui_message_handler.h" |
33 #include "grit/browser_resources.h" | 29 #include "grit/browser_resources.h" |
34 #include "grit/components_strings.h" | 30 #include "grit/components_strings.h" |
35 #include "grit/theme_resources.h" | 31 #include "grit/theme_resources.h" |
36 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
37 #include "ui/base/layout.h" | 33 #include "ui/base/layout.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 source->SetDefaultResource(IDR_ABOUT_CONFLICTS_HTML); | 66 source->SetDefaultResource(IDR_ABOUT_CONFLICTS_HTML); |
71 return source; | 67 return source; |
72 } | 68 } |
73 | 69 |
74 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
75 // | 71 // |
76 // ConflictsDOMHandler | 72 // ConflictsDOMHandler |
77 // | 73 // |
78 //////////////////////////////////////////////////////////////////////////////// | 74 //////////////////////////////////////////////////////////////////////////////// |
79 | 75 |
80 // The handler for JavaScript messages for the about:flags page. | 76 // The handler for JavaScript messages for the about:conflicts page. |
81 class ConflictsDOMHandler : public WebUIMessageHandler, | 77 class ConflictsDOMHandler : public WebUIMessageHandler, |
82 public content::NotificationObserver { | 78 public EnumerateModulesModel::Observer { |
83 public: | 79 public: |
84 ConflictsDOMHandler() {} | 80 ConflictsDOMHandler() {} |
85 ~ConflictsDOMHandler() override {} | 81 ~ConflictsDOMHandler() override {} |
86 | 82 |
87 // WebUIMessageHandler implementation. | 83 // WebUIMessageHandler implementation. |
88 void RegisterMessages() override; | 84 void RegisterMessages() override; |
89 | 85 |
90 // Callback for the "requestModuleList" message. | 86 // Callback for the "requestModuleList" message. |
91 void HandleRequestModuleList(const base::ListValue* args); | 87 void HandleRequestModuleList(const base::ListValue* args); |
92 | 88 |
93 private: | 89 private: |
94 void SendModuleList(); | 90 void SendModuleList(); |
95 | 91 |
96 void Observe(int type, | 92 // EnumerateModulesModel::Observer implementation. |
97 const content::NotificationSource& source, | 93 void OnScanCompleted() override; |
98 const content::NotificationDetails& details) override; | |
99 | |
100 content::NotificationRegistrar registrar_; | |
101 | 94 |
102 DISALLOW_COPY_AND_ASSIGN(ConflictsDOMHandler); | 95 DISALLOW_COPY_AND_ASSIGN(ConflictsDOMHandler); |
103 }; | 96 }; |
104 | 97 |
105 void ConflictsDOMHandler::RegisterMessages() { | 98 void ConflictsDOMHandler::RegisterMessages() { |
106 web_ui()->RegisterMessageCallback("requestModuleList", | 99 web_ui()->RegisterMessageCallback("requestModuleList", |
107 base::Bind(&ConflictsDOMHandler::HandleRequestModuleList, | 100 base::Bind(&ConflictsDOMHandler::HandleRequestModuleList, |
108 base::Unretained(this))); | 101 base::Unretained(this))); |
109 } | 102 } |
110 | 103 |
111 void ConflictsDOMHandler::HandleRequestModuleList(const base::ListValue* args) { | 104 void ConflictsDOMHandler::HandleRequestModuleList(const base::ListValue* args) { |
112 // This request is handled asynchronously. See Observe for when we reply back. | 105 // The request is handled asynchronously, and will callback via |
113 registrar_.Add(this, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, | 106 // OnScanCompleted on completion. |
114 content::NotificationService::AllSources()); | 107 auto model = EnumerateModulesModel::GetInstance(); |
Lei Zhang
2016/08/04 14:33:32
auto*
chrisha
2016/08/12 19:04:40
Done.
| |
115 EnumerateModulesModel::GetInstance()->ScanNow(); | 108 model->AddObserver(this); |
Lei Zhang
2016/08/04 14:33:32
Can this end up adding the same observer twice? Th
Peter Kasting
2016/08/05 00:10:08
If this is a real concern, using a ScopedObserver
chrisha
2016/08/12 19:04:40
The ScopedObserver is cleaner IMO, and also allows
chrisha
2016/08/12 19:04:40
Shouldn't be possible from how the JS code is writ
| |
109 model->ScanNow(); | |
116 } | 110 } |
117 | 111 |
118 void ConflictsDOMHandler::SendModuleList() { | 112 void ConflictsDOMHandler::SendModuleList() { |
119 EnumerateModulesModel* loaded_modules = EnumerateModulesModel::GetInstance(); | 113 EnumerateModulesModel* loaded_modules = EnumerateModulesModel::GetInstance(); |
120 base::ListValue* list = loaded_modules->GetModuleList(); | 114 base::ListValue* list = loaded_modules->GetModuleList(); |
121 base::DictionaryValue results; | 115 base::DictionaryValue results; |
122 results.Set("moduleList", list); | 116 results.Set("moduleList", list); |
123 | 117 |
124 // Add the section title and the total count for bad modules found. | 118 // Add the section title and the total count for bad modules found. |
125 int confirmed_bad = loaded_modules->confirmed_bad_modules_detected(); | 119 int confirmed_bad = loaded_modules->confirmed_bad_modules_detected(); |
126 int suspected_bad = loaded_modules->suspected_bad_modules_detected(); | 120 int suspected_bad = loaded_modules->suspected_bad_modules_detected(); |
127 base::string16 table_title; | 121 base::string16 table_title; |
128 if (!confirmed_bad && !suspected_bad) { | 122 if (!confirmed_bad && !suspected_bad) { |
129 table_title += l10n_util::GetStringFUTF16( | 123 table_title += l10n_util::GetStringFUTF16( |
130 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_ONE, | 124 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_ONE, |
131 base::IntToString16(list->GetSize())); | 125 base::IntToString16(list->GetSize())); |
132 } else { | 126 } else { |
133 table_title += l10n_util::GetStringFUTF16( | 127 table_title += l10n_util::GetStringFUTF16( |
134 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_TWO, | 128 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_TWO, |
135 base::IntToString16(list->GetSize()), | 129 base::IntToString16(list->GetSize()), |
136 base::IntToString16(confirmed_bad), | 130 base::IntToString16(confirmed_bad), |
137 base::IntToString16(suspected_bad)); | 131 base::IntToString16(suspected_bad)); |
138 } | 132 } |
139 results.SetString("modulesTableTitle", table_title); | 133 results.SetString("modulesTableTitle", table_title); |
140 | 134 |
141 web_ui()->CallJavascriptFunctionUnsafe("returnModuleList", results); | 135 web_ui()->CallJavascriptFunctionUnsafe("returnModuleList", results); |
142 } | 136 } |
143 | 137 |
144 void ConflictsDOMHandler::Observe(int type, | 138 void ConflictsDOMHandler::OnScanCompleted() { |
145 const content::NotificationSource& source, | |
146 const content::NotificationDetails& details) { | |
147 DCHECK_EQ(chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, type); | |
148 | |
149 SendModuleList(); | 139 SendModuleList(); |
150 registrar_.RemoveAll(); | 140 EnumerateModulesModel::GetInstance()->RemoveObserver(this); |
151 } | 141 } |
152 | 142 |
153 } // namespace | 143 } // namespace |
154 | 144 |
155 /////////////////////////////////////////////////////////////////////////////// | 145 /////////////////////////////////////////////////////////////////////////////// |
156 // | 146 // |
157 // ConflictsUI | 147 // ConflictsUI |
158 // | 148 // |
159 /////////////////////////////////////////////////////////////////////////////// | 149 /////////////////////////////////////////////////////////////////////////////// |
160 | 150 |
161 ConflictsUI::ConflictsUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 151 ConflictsUI::ConflictsUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
162 content::RecordAction(UserMetricsAction("ViewAboutConflicts")); | 152 content::RecordAction(UserMetricsAction("ViewAboutConflicts")); |
163 web_ui->AddMessageHandler(new ConflictsDOMHandler()); | 153 web_ui->AddMessageHandler(new ConflictsDOMHandler()); |
164 | 154 |
165 // Set up the about:conflicts source. | 155 // Set up the about:conflicts source. |
166 Profile* profile = Profile::FromWebUI(web_ui); | 156 Profile* profile = Profile::FromWebUI(web_ui); |
167 content::WebUIDataSource::Add(profile, CreateConflictsUIHTMLSource()); | 157 content::WebUIDataSource::Add(profile, CreateConflictsUIHTMLSource()); |
168 } | 158 } |
169 | 159 |
170 // static | 160 // static |
171 base::RefCountedMemory* ConflictsUI::GetFaviconResourceBytes( | 161 base::RefCountedMemory* ConflictsUI::GetFaviconResourceBytes( |
172 ui::ScaleFactor scale_factor) { | 162 ui::ScaleFactor scale_factor) { |
173 return static_cast<base::RefCountedMemory*>( | 163 return static_cast<base::RefCountedMemory*>( |
174 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 164 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
175 IDR_CONFLICT_FAVICON, scale_factor)); | 165 IDR_CONFLICT_FAVICON, scale_factor)); |
176 } | 166 } |
177 | 167 |
178 #endif | 168 #endif |
OLD | NEW |