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/scoped_observer.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/win/enumerate_modules_model.h" | 22 #include "chrome/browser/win/enumerate_modules_model.h" |
22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
23 #include "chrome/grit/browser_resources.h" | 24 #include "chrome/grit/browser_resources.h" |
24 #include "chrome/grit/chromium_strings.h" | 25 #include "chrome/grit/chromium_strings.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 source->SetDefaultResource(IDR_ABOUT_CONFLICTS_HTML); | 71 source->SetDefaultResource(IDR_ABOUT_CONFLICTS_HTML); |
71 return source; | 72 return source; |
72 } | 73 } |
73 | 74 |
74 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
75 // | 76 // |
76 // ConflictsDOMHandler | 77 // ConflictsDOMHandler |
77 // | 78 // |
78 //////////////////////////////////////////////////////////////////////////////// | 79 //////////////////////////////////////////////////////////////////////////////// |
79 | 80 |
80 // The handler for JavaScript messages for the about:flags page. | 81 // The handler for JavaScript messages for the about:conflicts page. |
81 class ConflictsDOMHandler : public WebUIMessageHandler, | 82 class ConflictsDOMHandler : public WebUIMessageHandler, |
82 public content::NotificationObserver { | 83 public EnumerateModulesModel::Observer { |
83 public: | 84 public: |
84 ConflictsDOMHandler() {} | 85 ConflictsDOMHandler(); |
85 ~ConflictsDOMHandler() override {} | 86 ~ConflictsDOMHandler() override {} |
86 | 87 |
87 // WebUIMessageHandler implementation. | 88 // WebUIMessageHandler implementation. |
88 void RegisterMessages() override; | 89 void RegisterMessages() override; |
89 | 90 |
90 // Callback for the "requestModuleList" message. | 91 // Callback for the "requestModuleList" message. |
91 void HandleRequestModuleList(const base::ListValue* args); | 92 void HandleRequestModuleList(const base::ListValue* args); |
92 | 93 |
93 private: | 94 private: |
94 void SendModuleList(); | 95 void SendModuleList(); |
95 | 96 |
96 void Observe(int type, | 97 // EnumerateModulesModel::Observer implementation. |
97 const content::NotificationSource& source, | 98 void OnScanCompleted() override; |
98 const content::NotificationDetails& details) override; | |
99 | 99 |
100 content::NotificationRegistrar registrar_; | 100 ScopedObserver<EnumerateModulesModel, |
| 101 EnumerateModulesModel::Observer> observer_; |
101 | 102 |
102 DISALLOW_COPY_AND_ASSIGN(ConflictsDOMHandler); | 103 DISALLOW_COPY_AND_ASSIGN(ConflictsDOMHandler); |
103 }; | 104 }; |
104 | 105 |
| 106 ConflictsDOMHandler::ConflictsDOMHandler() |
| 107 : observer_(this) { |
| 108 } |
| 109 |
105 void ConflictsDOMHandler::RegisterMessages() { | 110 void ConflictsDOMHandler::RegisterMessages() { |
106 web_ui()->RegisterMessageCallback("requestModuleList", | 111 web_ui()->RegisterMessageCallback("requestModuleList", |
107 base::Bind(&ConflictsDOMHandler::HandleRequestModuleList, | 112 base::Bind(&ConflictsDOMHandler::HandleRequestModuleList, |
108 base::Unretained(this))); | 113 base::Unretained(this))); |
109 } | 114 } |
110 | 115 |
111 void ConflictsDOMHandler::HandleRequestModuleList(const base::ListValue* args) { | 116 void ConflictsDOMHandler::HandleRequestModuleList(const base::ListValue* args) { |
112 // This request is handled asynchronously. See Observe for when we reply back. | 117 // The request is handled asynchronously, and will callback via |
113 registrar_.Add(this, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, | 118 // OnScanCompleted on completion. |
114 content::NotificationService::AllSources()); | 119 auto* model = EnumerateModulesModel::GetInstance(); |
115 EnumerateModulesModel::GetInstance()->ScanNow(); | 120 |
| 121 // The JS shouldn't be abusive and call 'requestModuleList' twice, but it's |
| 122 // easy enough to defend against this. |
| 123 if (!observer_.IsObserving(model)) { |
| 124 observer_.Add(model); |
| 125 model->ScanNow(); |
| 126 } |
116 } | 127 } |
117 | 128 |
118 void ConflictsDOMHandler::SendModuleList() { | 129 void ConflictsDOMHandler::SendModuleList() { |
119 EnumerateModulesModel* loaded_modules = EnumerateModulesModel::GetInstance(); | 130 auto* loaded_modules = EnumerateModulesModel::GetInstance(); |
120 base::ListValue* list = loaded_modules->GetModuleList(); | 131 base::ListValue* list = loaded_modules->GetModuleList(); |
121 base::DictionaryValue results; | 132 base::DictionaryValue results; |
122 results.Set("moduleList", list); | 133 results.Set("moduleList", list); |
123 | 134 |
124 // Add the section title and the total count for bad modules found. | 135 // Add the section title and the total count for bad modules found. |
125 int confirmed_bad = loaded_modules->confirmed_bad_modules_detected(); | 136 int confirmed_bad = loaded_modules->confirmed_bad_modules_detected(); |
126 int suspected_bad = loaded_modules->suspected_bad_modules_detected(); | 137 int suspected_bad = loaded_modules->suspected_bad_modules_detected(); |
127 base::string16 table_title; | 138 base::string16 table_title; |
128 if (!confirmed_bad && !suspected_bad) { | 139 if (!confirmed_bad && !suspected_bad) { |
129 table_title += l10n_util::GetStringFUTF16( | 140 table_title += l10n_util::GetStringFUTF16( |
130 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_ONE, | 141 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_ONE, |
131 base::IntToString16(list->GetSize())); | 142 base::IntToString16(list->GetSize())); |
132 } else { | 143 } else { |
133 table_title += l10n_util::GetStringFUTF16( | 144 table_title += l10n_util::GetStringFUTF16( |
134 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_TWO, | 145 IDS_CONFLICTS_CHECK_PAGE_TABLE_TITLE_SUFFIX_TWO, |
135 base::IntToString16(list->GetSize()), | 146 base::IntToString16(list->GetSize()), |
136 base::IntToString16(confirmed_bad), | 147 base::IntToString16(confirmed_bad), |
137 base::IntToString16(suspected_bad)); | 148 base::IntToString16(suspected_bad)); |
138 } | 149 } |
139 results.SetString("modulesTableTitle", table_title); | 150 results.SetString("modulesTableTitle", table_title); |
140 | 151 |
141 web_ui()->CallJavascriptFunctionUnsafe("returnModuleList", results); | 152 web_ui()->CallJavascriptFunctionUnsafe("returnModuleList", results); |
142 } | 153 } |
143 | 154 |
144 void ConflictsDOMHandler::Observe(int type, | 155 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(); | 156 SendModuleList(); |
150 registrar_.RemoveAll(); | 157 observer_.Remove(EnumerateModulesModel::GetInstance()); |
151 } | 158 } |
152 | 159 |
153 } // namespace | 160 } // namespace |
154 | 161 |
155 /////////////////////////////////////////////////////////////////////////////// | 162 /////////////////////////////////////////////////////////////////////////////// |
156 // | 163 // |
157 // ConflictsUI | 164 // ConflictsUI |
158 // | 165 // |
159 /////////////////////////////////////////////////////////////////////////////// | 166 /////////////////////////////////////////////////////////////////////////////// |
160 | 167 |
161 ConflictsUI::ConflictsUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 168 ConflictsUI::ConflictsUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
162 content::RecordAction(UserMetricsAction("ViewAboutConflicts")); | 169 content::RecordAction(UserMetricsAction("ViewAboutConflicts")); |
163 web_ui->AddMessageHandler(new ConflictsDOMHandler()); | 170 web_ui->AddMessageHandler(new ConflictsDOMHandler()); |
164 | 171 |
165 // Set up the about:conflicts source. | 172 // Set up the about:conflicts source. |
166 Profile* profile = Profile::FromWebUI(web_ui); | 173 Profile* profile = Profile::FromWebUI(web_ui); |
167 content::WebUIDataSource::Add(profile, CreateConflictsUIHTMLSource()); | 174 content::WebUIDataSource::Add(profile, CreateConflictsUIHTMLSource()); |
168 } | 175 } |
169 | 176 |
170 // static | 177 // static |
171 base::RefCountedMemory* ConflictsUI::GetFaviconResourceBytes( | 178 base::RefCountedMemory* ConflictsUI::GetFaviconResourceBytes( |
172 ui::ScaleFactor scale_factor) { | 179 ui::ScaleFactor scale_factor) { |
173 return static_cast<base::RefCountedMemory*>( | 180 return static_cast<base::RefCountedMemory*>( |
174 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 181 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
175 IDR_CONFLICT_FAVICON, scale_factor)); | 182 IDR_CONFLICT_FAVICON, scale_factor)); |
176 } | 183 } |
177 | 184 |
178 #endif | 185 #endif |
OLD | NEW |