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 <set> | |
8 #include <string> | |
9 #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.
| |
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 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.
| |
29 UMA_HISTOGRAM_SPARSE_SLOWLY("InstallVerifier.ModuleMatch", module_id); | |
30 } | |
31 | |
32 void VerifyEnumeratedModules(const base::ListValue& module_list) { | |
33 std::set<base::string16> canonical_module_names; | |
34 ExtractLoadedModuleNames(module_list, &canonical_module_names); | |
35 VerifyModules(canonical_module_names, base::Bind(&OnModuleMatch)); | |
36 } | |
37 | |
38 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.
| |
39 public: | |
40 InstallModuleVerifier() { | |
41 notification_registrar_.Add(this, | |
42 chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, | |
43 content::NotificationService::AllSources()); | |
44 } | |
45 virtual void Observe(int type, | |
46 const content::NotificationSource& source, | |
47 const content::NotificationDetails& details) OVERRIDE { | |
48 DCHECK_EQ(type, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED); | |
49 EnumerateModulesModel* model = | |
50 content::Source<EnumerateModulesModel>(source).ptr(); | |
51 scoped_ptr<base::ListValue> module_list(model->GetModuleList()); | |
52 | |
53 if (module_list.get()) | |
54 VerifyEnumeratedModules(*module_list); | |
55 | |
56 delete this; | |
57 } | |
58 | |
59 private: | |
60 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.
| |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(InstallModuleVerifier); | |
63 }; | |
64 | |
65 } // namespace | |
66 | |
67 void BeginModuleVerification() { | |
68 scoped_ptr<base::ListValue> module_list( | |
69 EnumerateModulesModel::GetInstance()->GetModuleList()); | |
70 if (module_list.get()) { | |
71 VerifyEnumeratedModules(*module_list); | |
72 } else { | |
73 // Will delete itself when scan results are available. | |
74 new InstallModuleVerifier(); | |
75 EnumerateModulesModel::GetInstance()->ScanNow(); | |
76 } | |
77 } | |
78 | |
79 void ExtractLoadedModuleNames(const base::ListValue& module_list, | |
80 std::set<base::string16>* module_names) { | |
81 DCHECK(module_names); | |
82 | |
83 // EnumerateModulesModel produces a list of dictionaries. | |
84 // Each dictionary corresponds to a module and exposes a number of properties. | |
85 // We care only about 'type' and 'name'. | |
86 for (size_t i = 0; i < module_list.GetSize(); ++i) { | |
87 const base::DictionaryValue* module_dictionary = NULL; | |
88 if (!module_list.GetDictionary(i, &module_dictionary)) | |
89 continue; | |
90 ModuleEnumerator::ModuleType module_type = | |
91 ModuleEnumerator::LOADED_MODULE; | |
92 if (!module_dictionary->GetInteger( | |
93 "type", reinterpret_cast<int*>(&module_type)) || | |
94 module_type != ModuleEnumerator::LOADED_MODULE) { | |
95 continue; | |
96 } | |
97 std::string utf8_module_name; | |
98 if (!module_dictionary->GetString("name", &utf8_module_name)) | |
99 continue; | |
100 base::string16 module_name = base::UTF8ToUTF16(utf8_module_name); | |
101 base::string16 canonical_module_name = CanonicalizeModuleName(module_name); | |
102 module_names->insert(canonical_module_name); | |
103 } | |
104 } | |
105 | |
106 void VerifyModules(const std::set<base::string16>& canonical_module_names, | |
107 const base::Callback<void(size_t)>& delegate) { | |
108 for (size_t module_index = 0; kExpectedInstallModules[module_index] != NULL; | |
109 ++module_index) { | |
110 if (canonical_module_names.find(kExpectedInstallModules[module_index]) != | |
111 canonical_module_names.end()) { | |
112 delegate.Run(module_index); | |
113 } | |
114 } | |
115 } | |
OLD | NEW |