Chromium Code Reviews| Index: chrome/browser/conflicts/module_database_win.h |
| diff --git a/chrome/browser/conflicts/module_database_win.h b/chrome/browser/conflicts/module_database_win.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..16842b0827b32053d7325db76ddd40cfc8ab07e0 |
| --- /dev/null |
| +++ b/chrome/browser/conflicts/module_database_win.h |
| @@ -0,0 +1,191 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ |
| +#define CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ |
| + |
| +#include <set> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "chrome/common/conflicts/module_watcher_win.h" |
| +#include "content/public/common/process_type.h" |
| + |
| +// A class that keeps track of all modules loaded across Chrome processes. |
| +// Drives the chrome://conflicts UI. |
| +class ModuleDatabase { |
| + public: |
| + // A ModuleDatabase is by default bound to a provided sequenced task runner. |
| + // All calls must be made in the context of this task runner, unless |
| + // otherwise noted. For calls from other contexts this task runner is used to |
| + // bounce the call when appropriate. |
| + explicit ModuleDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner); |
| + ~ModuleDatabase(); |
| + |
| + // Indicates that process with the given type has started. This must be called |
| + // before any calls to OnModuleEvent or OnModuleUnload. Must be called on the |
| + // same thread as |task_runner_|. |
| + void OnProcessStarted(uint32_t process_id, content::ProcessType process_type); |
| + |
| + // Indicates that a module has been loaded or unloaded. This allows a |
| + // ModuleWatcher to be directly connected to the ModuleDatabase. This can be |
| + // called from any thread and will be bounced to the |task_runner_|. |
| + void OnModuleEvent(uint32_t process_id, |
| + const ModuleWatcher::ModuleEvent& event); |
| + |
| + // Indicates that the module at the given |load_address| in the given |
| + // process has been unloaded. This specialization is specifically for use |
| + // with ModuleEventSinkImpl. This can be called from any thread and will be |
| + // bounced to the |task_runner_|. |
| + void OnModuleUnload(uint32_t process_id, uintptr_t load_address); |
| + |
| + // Indicates that the given process has ended. This can be called from any |
| + // thread and will be bounced to the |task_runner|. In practice it will be |
| + // invoked from the UI thread as the Mojo channel is torn down. |
| + void OnProcessEnded(uint32_t process_id); |
| + |
| + // TODO(chrisha): Module analysis code, and various accessors for use by |
| + // chrome://conflicts. |
| + |
| + private: |
| + friend class TestModuleDatabase; |
| + friend class ModuleDatabaseTest; |
| + friend class ModuleEventSinkImplTest; |
| + |
| + 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.
|
| + |
| + // Used as a unique identifier for a module in a ModuleSet. |
| + using ModuleId = int; |
| + |
| + // Structures for maintaining information about modules. |
| + struct ModuleInfo; |
| + using ModuleSet = std::set<ModuleInfo>; |
| + using ModuleLoadAddresses = std::vector<std::pair<ModuleId, uintptr_t>>; |
| + |
| + // Structures for maintaining information about running processes. |
| + struct ProcessInfo; |
| + using ProcessSet = std::set<ProcessInfo>; |
| + |
| + // Converts a valid |process_type| to a bit for use in a bitmask of process |
| + // values. Exposed in the header for testing. |
| + static uint32_t ProcessTypeToBit(content::ProcessType process_type); |
| + |
| + // Converts a |bit_index| (which maps to the bit 1 << bit_index) to the |
| + // corresponding process type. Exposed in the header for testing. |
| + static content::ProcessType BitIndexToProcessType(uint32_t bit_index); |
| + |
| + // Performs a linear scan to find the index of a |module_id| or |load_address| |
| + // in a collection of modules. Returns kInvalidIndex if the index is not |
| + // found. |
| + static size_t FindLoadAddressIndexById( |
| + ModuleId module_id, |
| + const ModuleLoadAddresses& load_addresses); |
| + static size_t FindLoadAddressIndexByAddress( |
| + uintptr_t load_address, |
| + const ModuleLoadAddresses& load_addresses); |
| + |
| + // Inserts a module into a ModuleLoadAddress object. |
| + static void InsertLoadAddress(ModuleId module_id, |
| + uintptr_t load_address, |
| + ModuleLoadAddresses* load_addresses); |
| + |
| + // Removes a module from a ModuleLoadAddress object, either by the |
| + // |module_id| or the |index| in the collection. |
| + static void RemoveLoadAddressById(ModuleId module_id, |
| + ModuleLoadAddresses* load_addresses); |
| + static void RemoveLoadAddressByIndex(size_t index, |
| + ModuleLoadAddresses* load_addresses); |
| + |
| + // Finds or creates a mutable ModuleInfo entry. |
| + ModuleInfo* FindOrCreateModuleInfo(const base::FilePath& module_path); |
| + |
| + // Finds a process info entry. Returns nullptr if none is found. |
| + ProcessInfo* GetProcessInfo(uint32_t process_id); |
| + |
| + // Creates a process info entry. |
| + void CreateProcessInfo(uint32_t process_id, |
| + content::ProcessType process_type); |
| + |
| + // Deletes a process info entry. |
| + void DeleteProcessInfo(uint32_t process_id); |
| + |
| + // The task runner to which this object is bound. |
| + scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| + |
| + // A map of all known modules. |
| + ModuleSet modules_; |
| + |
| + // The set of all known running processes, and modules loaded/unloaded in |
| + // them. |
| + ProcessSet processes_; |
| + |
| + // Weak pointer factory for this object. This is used when bouncing |
| + // incoming events to |task_runner_|. |
| + base::WeakPtrFactory<ModuleDatabase> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ModuleDatabase); |
| +}; |
| + |
| +// Maintains information about a module. Modules are permanent once added to |
| +// the ModuleSet, so this structure grows monotonically. In practice this is |
| +// not an issue as the modules themselves are vastly bigger than the minor |
| +// amount of metadata tracked here. |
| +struct ModuleDatabase::ModuleInfo { |
| + ModuleInfo(const base::FilePath& module_path, uint32_t module_id); |
| + |
| + // Less-than operator allowing this object to be used in std::set. |
| + bool operator<(const ModuleInfo& mi) const; |
| + |
| + // Full path to the module on disk. This is the "key" of the object, |
| + // and is how the ModuleInfo is indexed in std::set. |
| + const base::FilePath module_path; |
| + |
| + // The ID of this module. This is a strictly incrementing value, and is used |
| + // to tie a module to the list of running processes in which it is found. |
| + // It is not part of the key for the module, but it is immutable. This is |
| + // simply the index of the module in the insertion order. |
| + const ModuleId module_id; |
| + |
| + // Everything below this point is implicitly mutable data. Const versions of |
| + // this struct will have their constness casted away at runtime, allowing |
| + // map-like behaviour for a set without duplicating the key externally. |
| + |
| + // Set of all process types in which this module has been seen (may not be |
| + // currently present in a process of that type). This is a conversion of |
| + // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and |
| + // "BitIndexToProcessType" for details. |
| + uint32_t process_types; |
| +}; |
| + |
| +// Information about a running process. This ties modules in a ModuleSet to |
| +// processes in which they are (or have been) loaded. |
| +struct ModuleDatabase::ProcessInfo { |
| + ProcessInfo(uint32_t process_id, content::ProcessType process_type); |
| + |
| + // Less-than operator allowing this object to be used in std::set. |
| + bool operator<(const ProcessInfo& pi) const; |
| + |
| + const uint32_t process_id; |
| + const content::ProcessType process_type; |
| + |
| + // Everything below this point is implicitly mutable data. Const versions of |
| + // this struct will have their constness casted away at runtime, allowing |
| + // map-like behaviour for a set without duplicating the key externally. |
| + |
| + // The set of modules that are loaded/unloaded in this process, by ID. This |
| + // is typically a small list so a linear cost is okay to pay for |
| + // lookup/deletion. |
| + // |
| + // These are modified by the various static *LoadAddress* helper functions in |
| + // ModuleDatabase. The vector maintains the invariant the element with maximum |
| + // module ID is always last. This ensures that the usual operation of loading |
| + // a module is O(1). |
| + ModuleLoadAddresses loaded_modules; |
| + ModuleLoadAddresses unloaded_modules; |
| +}; |
| + |
| +#endif // CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ |