Index: chrome/browser/install_module_verifier_win.cc |
diff --git a/chrome/browser/install_module_verifier_win.cc b/chrome/browser/install_module_verifier_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6b549ed49991cd554131806837f36fff841591d2 |
--- /dev/null |
+++ b/chrome/browser/install_module_verifier_win.cc |
@@ -0,0 +1,131 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/install_module_verifier_win.h" |
+ |
+#include <string> |
+ |
+#include "base/basictypes.h" |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/metrics/sparse_histogram.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "base/values.h" |
+#include "chrome/browser/chrome_notification_types.h" |
+#include "chrome/browser/enumerate_modules_model_win.h" |
+#include "chrome/browser/expected_install_modules_win.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/notification_observer.h" |
+#include "content/public/browser/notification_registrar.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/browser/notification_source.h" |
+ |
+namespace { |
+ |
+// Callback for VerifyModules that reports matching module IDs via UMA. |
+void OnModuleMatch(size_t module_index) { |
+ UMA_HISTOGRAM_SPARSE_SLOWLY("InstallVerifier.ModuleMatch", module_index); |
+} |
+ |
+// Helper to extract canonical loaded module names from the EnumerateModulesWin |
+// output and then verify the results. |
+void VerifyEnumeratedModules(const base::ListValue& module_list) { |
+ std::set<base::string16> canonical_module_names; |
+ ExtractLoadedModuleNames(module_list, &canonical_module_names); |
+ VerifyModules(canonical_module_names, base::Bind(&OnModuleMatch)); |
+} |
+ |
+// Waits for NOTIFICATION_MODULE_LIST_ENUMERATED, which indicates that |
+// EnumerateModulesWin has completed its work. Retrieves the enumerated module |
+// list and processes it. |
+class InstallModuleVerifier : private content::NotificationObserver { |
Finnur
2013/09/25 12:54:22
Private inheritance is against the style guide:
ht
erikwright (departed)
2013/09/25 19:58:08
Done. Yes you can make Observe private but it is m
|
+ public: |
+ // Creates an instance that will wait for module enumeration to complete, |
+ // process the results, and then delete itself. |
+ static void WaitForModuleList() { |
+ // Will delete itself when scan results are available. |
+ new InstallModuleVerifier(); |
+ } |
+ |
+ private: |
+ // content::NotificationObserver implementation. |
+ virtual void Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) OVERRIDE { |
+ DCHECK_EQ(type, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED); |
+ EnumerateModulesModel* model = |
+ content::Source<EnumerateModulesModel>(source).ptr(); |
+ scoped_ptr<base::ListValue> module_list(model->GetModuleList()); |
+ |
+ if (module_list.get()) |
+ VerifyEnumeratedModules(*module_list); |
+ |
+ delete this; |
+ } |
+ |
+ InstallModuleVerifier() { |
+ notification_registrar_.Add(this, |
+ chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, |
+ content::NotificationService::AllSources()); |
+ } |
+ |
+ ~InstallModuleVerifier() {} |
+ |
+ content::NotificationRegistrar notification_registrar_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(InstallModuleVerifier); |
+}; |
+ |
+} // namespace |
+ |
+void BeginModuleVerification() { |
+ scoped_ptr<base::ListValue> module_list( |
+ EnumerateModulesModel::GetInstance()->GetModuleList()); |
+ if (module_list.get()) { |
+ VerifyEnumeratedModules(*module_list); |
+ } else { |
+ InstallModuleVerifier::WaitForModuleList(); |
+ EnumerateModulesModel::GetInstance()->ScanNow(); |
+ } |
+} |
+ |
+void ExtractLoadedModuleNames(const base::ListValue& module_list, |
+ std::set<base::string16>* module_names) { |
+ DCHECK(module_names); |
+ |
+ // EnumerateModulesModel produces a list of dictionaries. |
+ // Each dictionary corresponds to a module and exposes a number of properties. |
+ // We care only about 'type' and 'name'. |
+ for (size_t i = 0; i < module_list.GetSize(); ++i) { |
+ const base::DictionaryValue* module_dictionary = NULL; |
+ if (!module_list.GetDictionary(i, &module_dictionary)) |
+ continue; |
+ ModuleEnumerator::ModuleType module_type = |
+ ModuleEnumerator::LOADED_MODULE; |
+ if (!module_dictionary->GetInteger( |
+ "type", reinterpret_cast<int*>(&module_type)) || |
+ module_type != ModuleEnumerator::LOADED_MODULE) { |
+ continue; |
+ } |
+ std::string utf8_module_name; |
+ if (!module_dictionary->GetString("name", &utf8_module_name)) |
+ continue; |
+ base::string16 module_name = base::UTF8ToUTF16(utf8_module_name); |
+ base::string16 canonical_module_name = CanonicalizeModuleName(module_name); |
+ module_names->insert(canonical_module_name); |
+ } |
+} |
+ |
+void VerifyModules(const std::set<base::string16>& canonical_module_names, |
+ const base::Callback<void(size_t)>& delegate) { |
+ for (size_t module_index = 0; kExpectedInstallModules[module_index] != NULL; |
+ ++module_index) { |
+ if (canonical_module_names.find(kExpectedInstallModules[module_index]) != |
+ canonical_module_names.end()) { |
+ delegate.Run(module_index); |
+ } |
+ } |
+} |