OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/install_module_verifier_win.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/bind.h" | |
11 #include "base/callback.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/metrics/sparse_histogram.h" | |
15 #include "base/strings/utf_string_conversions.h" | |
16 #include "base/values.h" | |
17 #include "chrome/browser/chrome_notification_types.h" | |
18 #include "chrome/browser/enumerate_modules_model_win.h" | |
19 #include "chrome/browser/expected_install_modules_win.h" | |
20 #include "content/public/browser/browser_thread.h" | |
21 #include "content/public/browser/notification_observer.h" | |
22 #include "content/public/browser/notification_registrar.h" | |
23 #include "content/public/browser/notification_service.h" | |
24 #include "content/public/browser/notification_source.h" | |
25 | |
26 namespace { | |
27 | |
28 // Callback for VerifyModules that reports matching module IDs via UMA. | |
29 void OnModuleMatch(size_t module_index) { | |
30 UMA_HISTOGRAM_SPARSE_SLOWLY("InstallVerifier.ModuleMatch", module_index); | |
31 } | |
32 | |
33 // Helper to extract canonical loaded module names from the EnumerateModulesWin | |
34 // output and then verify the results. | |
35 void VerifyEnumeratedModules(const base::ListValue& module_list) { | |
36 std::set<base::string16> canonical_module_names; | |
37 ExtractLoadedModuleNames(module_list, &canonical_module_names); | |
38 VerifyModules(canonical_module_names, base::Bind(&OnModuleMatch)); | |
39 } | |
40 | |
41 // Waits for NOTIFICATION_MODULE_LIST_ENUMERATED, which indicates that | |
42 // EnumerateModulesWin has completed its work. Retrieves the enumerated module | |
43 // list and processes it. | |
44 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
| |
45 public: | |
46 // Creates an instance that will wait for module enumeration to complete, | |
47 // process the results, and then delete itself. | |
48 static void WaitForModuleList() { | |
49 // Will delete itself when scan results are available. | |
50 new InstallModuleVerifier(); | |
51 } | |
52 | |
53 private: | |
54 // content::NotificationObserver implementation. | |
55 virtual void Observe(int type, | |
56 const content::NotificationSource& source, | |
57 const content::NotificationDetails& details) OVERRIDE { | |
58 DCHECK_EQ(type, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED); | |
59 EnumerateModulesModel* model = | |
60 content::Source<EnumerateModulesModel>(source).ptr(); | |
61 scoped_ptr<base::ListValue> module_list(model->GetModuleList()); | |
62 | |
63 if (module_list.get()) | |
64 VerifyEnumeratedModules(*module_list); | |
65 | |
66 delete this; | |
67 } | |
68 | |
69 InstallModuleVerifier() { | |
70 notification_registrar_.Add(this, | |
71 chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, | |
72 content::NotificationService::AllSources()); | |
73 } | |
74 | |
75 ~InstallModuleVerifier() {} | |
76 | |
77 content::NotificationRegistrar notification_registrar_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(InstallModuleVerifier); | |
80 }; | |
81 | |
82 } // namespace | |
83 | |
84 void BeginModuleVerification() { | |
85 scoped_ptr<base::ListValue> module_list( | |
86 EnumerateModulesModel::GetInstance()->GetModuleList()); | |
87 if (module_list.get()) { | |
88 VerifyEnumeratedModules(*module_list); | |
89 } else { | |
90 InstallModuleVerifier::WaitForModuleList(); | |
91 EnumerateModulesModel::GetInstance()->ScanNow(); | |
92 } | |
93 } | |
94 | |
95 void ExtractLoadedModuleNames(const base::ListValue& module_list, | |
96 std::set<base::string16>* module_names) { | |
97 DCHECK(module_names); | |
98 | |
99 // EnumerateModulesModel produces a list of dictionaries. | |
100 // Each dictionary corresponds to a module and exposes a number of properties. | |
101 // We care only about 'type' and 'name'. | |
102 for (size_t i = 0; i < module_list.GetSize(); ++i) { | |
103 const base::DictionaryValue* module_dictionary = NULL; | |
104 if (!module_list.GetDictionary(i, &module_dictionary)) | |
105 continue; | |
106 ModuleEnumerator::ModuleType module_type = | |
107 ModuleEnumerator::LOADED_MODULE; | |
108 if (!module_dictionary->GetInteger( | |
109 "type", reinterpret_cast<int*>(&module_type)) || | |
110 module_type != ModuleEnumerator::LOADED_MODULE) { | |
111 continue; | |
112 } | |
113 std::string utf8_module_name; | |
114 if (!module_dictionary->GetString("name", &utf8_module_name)) | |
115 continue; | |
116 base::string16 module_name = base::UTF8ToUTF16(utf8_module_name); | |
117 base::string16 canonical_module_name = CanonicalizeModuleName(module_name); | |
118 module_names->insert(canonical_module_name); | |
119 } | |
120 } | |
121 | |
122 void VerifyModules(const std::set<base::string16>& canonical_module_names, | |
123 const base::Callback<void(size_t)>& delegate) { | |
124 for (size_t module_index = 0; kExpectedInstallModules[module_index] != NULL; | |
125 ++module_index) { | |
126 if (canonical_module_names.find(kExpectedInstallModules[module_index]) != | |
127 canonical_module_names.end()) { | |
128 delegate.Run(module_index); | |
129 } | |
130 } | |
131 } | |
OLD | NEW |