Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_set> | |
| 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_event_win.mojom.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 // Alls 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 // ModuleDatabaseImpl notifications are eventually routed here. These should | |
| 29 // only be invoked on the |task_runner| provided at time of construction. They | |
| 30 // are exposed so that the browser process can call these directly. These can | |
| 31 // be called from any thread, but will bounce to the provided |task_runner| | |
| 32 // if not called there. | |
| 33 void OnProcessStarted(uint32_t process_id, content::ProcessType process_type); | |
| 34 void OnModuleEvent(uint32_t process_id, const mojom::ModuleEvent& event); | |
| 35 void OnProcessEnded(uint32_t process_id); | |
| 36 | |
| 37 // TODO(chrisha): Module analysis code, and various accessors for use by | |
| 38 // chrome://conflicts. | |
| 39 | |
| 40 private: | |
| 41 friend class ModuleDatabaseTest; | |
| 42 friend class ModuleDatabaseImplTest; | |
| 43 | |
| 44 // Converts |process_type| to the corresponding bit. Exposed in the header for | |
|
sky
2016/12/14 21:58:08
Without looking at the implementation it isn't cle
chrisha
2016/12/19 20:15:36
Done.
| |
| 45 // testing. | |
| 46 static uint32_t ProcessTypeToBit(content::ProcessType process_type); | |
|
grt (UTC plus 2)
2016/12/15 09:07:12
nit: move these function decls down below the type
chrisha
2016/12/19 20:15:37
Done.
| |
| 47 | |
| 48 // Converts a |bit_index| to the corresponding process type. Exposed in the | |
| 49 // header for testing. | |
| 50 static content::ProcessType BitIndexToProcessType(uint32_t bit_index); | |
|
sky
2016/12/14 21:58:08
Same comment about bit index here.
chrisha
2016/12/19 20:15:37
Done.
| |
| 51 | |
| 52 // Maintains information about a module. Modules are permanent once added to | |
| 53 // the ModuleSet, so this structure grows monotonically. In practice this is | |
| 54 // not an issue as the modules themselves are vastly bigger than the minor | |
| 55 // amount of metadata tracked here. | |
| 56 struct ModuleInfo { | |
| 57 ModuleInfo(const base::FilePath& module_path, uint32_t module_id); | |
| 58 | |
| 59 // Full path to the module on disk. This is the "key" of the object, | |
| 60 // and is how the ModuleInfo is indexed in std::set. | |
| 61 const base::FilePath module_path; | |
| 62 | |
| 63 // The ID of this module. This is a strictly incrementing value, and is used | |
| 64 // to tie a module to the list of running processes in which it is found. | |
| 65 // It is not part of the key for the module, but it is immutable. This is | |
| 66 // simply the index of the module in the insertion order. | |
| 67 const uint32_t module_id; | |
|
grt (UTC plus 2)
2016/12/15 09:07:12
int is the type of choice for things that don't ex
chrisha
2016/12/19 20:15:37
Done.
| |
| 68 | |
| 69 // Everything below this point is implicitly mutable data. Const versions of | |
| 70 // this struct will have their constness casted away at runtime, allowing | |
|
grt (UTC plus 2)
2016/12/15 09:07:12
use "mutable" keyword rather than casting?
chrisha
2016/12/19 20:15:37
I get the ability to have const-correct accessors
| |
| 71 // map-like behaviour for a set without duplicating the key externally. | |
| 72 | |
| 73 // Set of all process types in which this module has been seen (may not be | |
| 74 // currently present in a process of that type). This is a conversion of | |
| 75 // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and | |
| 76 // "BitIndexToProcessType". | |
| 77 uint32_t process_types; | |
| 78 }; | |
| 79 | |
| 80 // Key comparator for ModuleInfo, allowing it to be used in a set. | |
|
grt (UTC plus 2)
2016/12/15 09:07:12
is it relevant to document that it compares by mod
chrisha
2016/12/19 20:15:36
Done.
| |
| 81 struct ModuleInfoKeyComparator { | |
| 82 // Implements the less-than operator. | |
| 83 bool operator()(const ModuleInfo& mi1, const ModuleInfo& mi2) const; | |
| 84 }; | |
| 85 | |
| 86 using ModuleSet = std::set<ModuleInfo, ModuleInfoKeyComparator>; | |
| 87 | |
| 88 // Information about a running process. This ties modules in a ModuleSet to | |
| 89 // processes in which they are (or have been) loaded. | |
| 90 struct ProcessInfo { | |
| 91 ProcessInfo(uint32_t process_id, content::ProcessType process_type); | |
| 92 | |
| 93 const uint32_t process_id; | |
| 94 const content::ProcessType process_type; | |
| 95 | |
| 96 // Everything below this point is implicitly mutable data. Const versions of | |
| 97 // this struct will have their constness casted away at runtime, allowing | |
| 98 // map-like behaviour for a set without duplicating the key externally. | |
| 99 | |
| 100 // The set of modules that are loaded/unloaded in this process, by ID. This | |
| 101 // is an unordered_set in order to maintain density of memory use as well as | |
| 102 // O(1) updates/accesses. | |
| 103 std::unordered_set<uint32_t> loaded_module_ids; | |
|
grt (UTC plus 2)
2016/12/15 09:07:12
will these be large enough that std::vector (sorte
chrisha
2016/12/19 20:15:37
These won't be enormous... the long tail sees some
| |
| 104 std::unordered_set<uint32_t> unloaded_module_ids; | |
| 105 }; | |
| 106 | |
| 107 // Key comparator for ProcessInfo, allowing it to be used in a set. | |
| 108 struct ProcessInfoKeyComparator { | |
| 109 // Implements the less-than operator. | |
| 110 bool operator()(const ProcessInfo& pi1, const ProcessInfo& pi2) const; | |
| 111 }; | |
| 112 | |
| 113 using ProcessSet = std::set<ProcessInfo, ProcessInfoKeyComparator>; | |
| 114 | |
| 115 // Finds or creates a mutable ModuleInfo entry. | |
| 116 ModuleInfo* FindOrCreateModuleInfo(const base::FilePath& module_path); | |
| 117 | |
| 118 // Finds a process info entry. Returns nullptr if none is found. | |
| 119 ProcessInfo* GetProcessInfo(uint32_t process_id); | |
| 120 | |
| 121 // Creates a process info entry. Returns nullptr if one already existed. | |
| 122 const ProcessInfo* CreateProcessInfo(uint32_t process_id, | |
| 123 content::ProcessType process_type); | |
| 124 | |
| 125 // Deletes a process info entry. Returns true on success, false otherwise. | |
| 126 bool DeleteProcessInfo(uint32_t process_id); | |
| 127 | |
| 128 // The task runner to which this object is bound. | |
| 129 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 130 | |
| 131 // Weak pointer factory for this object. This is used when bouncing | |
| 132 // incoming events to |task_runner_|. | |
| 133 base::WeakPtrFactory<ModuleDatabase> weak_ptr_factory_; | |
|
sky
2016/12/14 21:58:08
Make this the last members (I'm surprised you didn
chrisha
2016/12/19 20:15:37
Done.
| |
| 134 | |
| 135 // A map of all known modules. | |
| 136 ModuleSet modules_; | |
| 137 | |
| 138 // The set of all known running processes, and modules loaded/unloaded in | |
| 139 // them. | |
| 140 ProcessSet processes_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(ModuleDatabase); | |
| 143 }; | |
| 144 | |
| 145 #endif // CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ | |
| OLD | NEW |