Chromium Code Reviews| 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..692bae055b2cfe465c8445c9e3a18355287f7506 |
| --- /dev/null |
| +++ b/chrome/browser/install_module_verifier_win.cc |
| @@ -0,0 +1,115 @@ |
| +// 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 <set> |
| +#include <string> |
| +#include "base/basictypes.h" |
|
sky
2013/09/24 20:16:16
nit: newline between 8/9
erikwright (departed)
2013/09/25 19:58:08
Done.
|
| +#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 { |
| + |
| +void OnModuleMatch(size_t module_id) { |
|
Alexei Svitkine (slow)
2013/09/24 18:52:25
Nit: I'd call this module_index for consistency wi
erikwright (departed)
2013/09/25 19:58:08
Done.
|
| + UMA_HISTOGRAM_SPARSE_SLOWLY("InstallVerifier.ModuleMatch", module_id); |
| +} |
| + |
| +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)); |
| +} |
| + |
| +class InstallModuleVerifier : public content::NotificationObserver { |
|
sky
2013/09/24 20:16:16
Description, in fact all your private functions sh
erikwright (departed)
2013/09/25 19:58:08
Done.
|
| + public: |
| + InstallModuleVerifier() { |
| + notification_registrar_.Add(this, |
| + chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, |
| + content::NotificationService::AllSources()); |
| + } |
| + 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; |
| + } |
| + |
| + private: |
| + content::NotificationRegistrar notification_registrar_; |
|
sky
2013/09/24 20:16:16
private destructor to make it class this object ow
erikwright (departed)
2013/09/25 19:58:08
Done.
|
| + |
| + 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 { |
| + // Will delete itself when scan results are available. |
| + new InstallModuleVerifier(); |
| + 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); |
| + } |
| + } |
| +} |