Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1461)

Side by Side Diff: chrome/browser/conflicts/module_database_win.h

Issue 2576843002: [win] Create ModuleDatabase and ModuleEventSinkImpl. (Closed)
Patch Set: Rework OnProcessStarted. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #ifndef CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_
6 #define CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_
7
8 #include <set>
9 #include <unordered_map>
grt (UTC plus 2) 2016/12/20 11:11:34 unused
chrisha 2016/12/20 19:46:24 Done.
10
11 #include "base/files/file_path.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequenced_task_runner.h"
14 #include "chrome/common/conflicts/module_watcher_win.h"
15 #include "content/public/common/process_type.h"
16
17 // A class that keeps track of all modules loaded across Chrome processes.
18 // Drives the chrome://conflicts UI.
19 class ModuleDatabase {
20 public:
21 // A ModuleDatabase is by default bound to a provided sequenced task runner.
22 // All calls must be made in the context of this task runner, unless
23 // otherwise noted. For calls from other contexts this task runner is used to
24 // bounce the call when appropriate.
25 explicit ModuleDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner);
26 ~ModuleDatabase();
27
28 // Indicates that process with the given type has started. This must be called
29 // before any calls to OnModuleEvent or OnModuleUnload. Must be called on the
30 // same thread as |task_runner_|.
31 void OnProcessStarted(uint32_t process_id, content::ProcessType process_type);
32
33 // Indicates that a module has been loaded or unloaded. This allows a
34 // ModuleWatcher to be directly connected to the ModuleDatabase. This can be
35 // called from any thread and will be bounced to the |task_runner_|.
36 void OnModuleEvent(uint32_t process_id,
37 const ModuleWatcher::ModuleEvent& event);
38
39 // Indicates that the module at the given |load_address| in the given
40 // process has been unloaded. This specialization is specifically for use
41 // with ModuleEventSinkImpl. This can be called from any thread and will be
42 // bounced to the |task_runner_|.
43 void OnModuleUnload(uint32_t process_id, uintptr_t load_address);
44
45 // Indicates that the given process has ended. This can be called from any
46 // thread and will be bounced to the |task_runner|. In practice it will be
47 // invoked from the UI thread as the Mojo channel is torn down.
48 void OnProcessEnded(uint32_t process_id);
49
50 // TODO(chrisha): Module analysis code, and various accessors for use by
51 // chrome://conflicts.
52
53 private:
54 friend class TestModuleDatabase;
55 friend class ModuleDatabaseTest;
56 friend class ModuleEventSinkImplTest;
57
58 static constexpr int kInvalidIndex = -1;
59
60 // Used as a unique identifier for a module in a ModuleSet.
61 using ModuleId = int;
62
63 // Structures for maintaining information about modules.
64 struct ModuleInfo;
65 using ModuleSet = std::set<ModuleInfo>;
66 using ModuleLoadAddresses = std::vector<std::pair<ModuleId, uintptr_t>>;
grt (UTC plus 2) 2016/12/20 11:11:34 #include <utility> #include <vector>
chrisha 2016/12/20 19:46:23 Done.
67
68 // Structures for maintaining information about running processes.
69 struct ProcessInfo;
70 using ProcessSet = std::set<ProcessInfo>;
71
72 // Converts a valid |process_type| to a bit for use in a bitmask of process
73 // values. Exposed in the header for testing.
74 static uint32_t ProcessTypeToBit(content::ProcessType process_type);
75
76 // Converts a |bit_index| (which maps to the bit 1 << bit_index) to the
77 // corresponding process type. Exposed in the header for testing.
78 static content::ProcessType BitIndexToProcessType(uint32_t bit_index);
79
80 // Performs a linear scan to find the index of a |module_id| or |load_address|
81 // in a collection of modules. Returns kInvalidIndex if the index is not
82 // found.
83 static int FindLoadAddressIndexById(
84 ModuleId module_id,
85 const ModuleLoadAddresses& load_addresses);
86 static int FindLoadAddressIndexByAddress(
87 uintptr_t load_address,
88 const ModuleLoadAddresses& load_addresses);
89
90 // Inserts a module into a ModuleLoadAddress object.
91 static void InsertLoadAddress(ModuleId module_id,
92 uintptr_t load_address,
93 ModuleLoadAddresses* load_addresses);
94
95 // Removes a module from a ModuleLoadAddress object, either by the
96 // |module_id| or the |index| in the collection.
97 static void RemoveLoadAddressById(ModuleId module_id,
98 ModuleLoadAddresses* load_addresses);
99 static void RemoveLoadAddressByIndex(int index,
100 ModuleLoadAddresses* load_addresses);
101
102 // Finds or creates a mutable ModuleInfo entry.
103 ModuleInfo* FindOrCreateModuleInfo(const base::FilePath& module_path);
104
105 // Finds a process info entry. Returns nullptr if none is found.
106 ProcessInfo* GetProcessInfo(uint32_t process_id);
107
108 // Creates a process info entry. Returns nullptr if one already existed.
109 const ProcessInfo* CreateProcessInfo(uint32_t process_id,
110 content::ProcessType process_type);
111
112 // Deletes a process info entry. Returns true on success, false otherwise.
113 bool DeleteProcessInfo(uint32_t process_id);
114
115 // The task runner to which this object is bound.
116 scoped_refptr<base::SequencedTaskRunner> task_runner_;
117
118 // A map of all known modules.
119 ModuleSet modules_;
120
121 // The set of all known running processes, and modules loaded/unloaded in
122 // them.
123 ProcessSet processes_;
124
125 // Weak pointer factory for this object. This is used when bouncing
126 // incoming events to |task_runner_|.
127 base::WeakPtrFactory<ModuleDatabase> weak_ptr_factory_;
128
129 DISALLOW_COPY_AND_ASSIGN(ModuleDatabase);
130 };
131
132 // Maintains information about a module. Modules are permanent once added to
133 // the ModuleSet, so this structure grows monotonically. In practice this is
134 // not an issue as the modules themselves are vastly bigger than the minor
135 // amount of metadata tracked here.
136 struct ModuleDatabase::ModuleInfo {
grt (UTC plus 2) 2016/12/20 11:11:34 move into .cc file unless this is needed by tests
chrisha 2016/12/20 19:46:24 Yeah, they're all exposed for tests. To ensure tha
grt (UTC plus 2) 2016/12/20 21:09:52 Acknowledged.
137 ModuleInfo(const base::FilePath& module_path, uint32_t module_id);
138
139 // Less-than operator allowing this object to be used in std::set.
140 bool operator<(const ModuleInfo& mi) const;
141
142 // Full path to the module on disk. This is the "key" of the object,
143 // and is how the ModuleInfo is indexed in std::set.
144 const base::FilePath module_path;
145
146 // The ID of this module. This is a strictly incrementing value, and is used
147 // to tie a module to the list of running processes in which it is found.
148 // It is not part of the key for the module, but it is immutable. This is
149 // simply the index of the module in the insertion order.
150 const ModuleId module_id;
151
152 // Everything below this point is implicitly mutable data. Const versions of
153 // this struct will have their constness casted away at runtime, allowing
154 // map-like behaviour for a set without duplicating the key externally.
155
156 // Set of all process types in which this module has been seen (may not be
157 // currently present in a process of that type). This is a conversion of
158 // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and
159 // "BitIndexToProcessType" for details.
160 uint32_t process_types;
161 };
162
163 // Information about a running process. This ties modules in a ModuleSet to
164 // processes in which they are (or have been) loaded.
165 struct ModuleDatabase::ProcessInfo {
grt (UTC plus 2) 2016/12/20 11:11:34 this, too
chrisha 2016/12/20 19:46:24 Ditto.
grt (UTC plus 2) 2016/12/20 21:09:52 Acknowledged.
166 ProcessInfo(uint32_t process_id, content::ProcessType process_type);
167
168 // Less-than operator allowing this object to be used in std::set.
169 bool operator<(const ProcessInfo& pi) const;
grt (UTC plus 2) 2016/12/20 11:11:34 q for me: see what Meyers says about this in Effec
chrisha 2016/12/20 19:46:24 Meyers specifically mentions keeping it out of the
grt (UTC plus 2) 2016/12/20 21:09:52 Thanks for checking!
170
171 const uint32_t process_id;
172 const content::ProcessType process_type;
173
174 // Everything below this point is implicitly mutable data. Const versions of
175 // this struct will have their constness casted away at runtime, allowing
176 // map-like behaviour for a set without duplicating the key externally.
177
178 // The set of modules that are loaded/unloaded in this process, by ID. This
179 // is typically a small list so a linear cost is okay to pay for
180 // lookup/deletion.
181 //
182 // These are modified by the various static *LoadAddress* helper functions in
183 // ModuleDatabase. The vector maintains the invariant the element with maximum
184 // module ID is always last. This ensures that the usual operation of loading
185 // a module is O(1).
186 ModuleLoadAddresses loaded_modules;
187 ModuleLoadAddresses unloaded_modules;
188 };
189
190 #endif // CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698