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

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

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

Powered by Google App Engine
This is Rietveld 408576698