| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ | 5 #ifndef CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ |
| 6 #define CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ | 6 #define CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "chrome/common/conflicts/module_event_win.mojom.h" | 11 #include "base/files/file_path.h" |
| 12 #include "chrome/common/conflicts/module_event_sink_win.mojom.h" |
| 12 | 13 |
| 13 class ModuleWatcherTest; | 14 class ModuleWatcherTest; |
| 14 | 15 |
| 15 union LDR_DLL_NOTIFICATION_DATA; | 16 union LDR_DLL_NOTIFICATION_DATA; |
| 16 | 17 |
| 17 // This class observes modules as they are loaded and unloaded into a process's | 18 // This class observes modules as they are loaded and unloaded into a process's |
| 18 // address space. | 19 // address space. |
| 19 // | 20 // |
| 20 // This class is safe to be created on any thread. Similarly, it is safe to be | 21 // This class is safe to be created on any thread. Similarly, it is safe to be |
| 21 // destroyed on any thread, independent of the thread on which the instance was | 22 // destroyed on any thread, independent of the thread on which the instance was |
| 22 // created. | 23 // created. |
| 23 class ModuleWatcher { | 24 class ModuleWatcher { |
| 24 public: | 25 public: |
| 26 // Houses information about a module load/unload event, and some module |
| 27 // metadata. |
| 28 struct ModuleEvent { |
| 29 ModuleEvent() = default; |
| 30 ModuleEvent(const ModuleEvent& other) = default; |
| 31 ModuleEvent(mojom::ModuleEventType event_type, |
| 32 const base::FilePath& module_path, |
| 33 void* module_load_address, |
| 34 size_t module_size) |
| 35 : event_type(event_type), |
| 36 module_path(module_path), |
| 37 module_load_address(module_load_address), |
| 38 module_size(module_size) {} |
| 39 |
| 40 // The type of module event. |
| 41 mojom::ModuleEventType event_type; |
| 42 // The full path to the module on disk. |
| 43 base::FilePath module_path; |
| 44 // The load address of the module. |
| 45 void* module_load_address; |
| 46 // The size of the module in memory. |
| 47 size_t module_size; |
| 48 }; |
| 49 |
| 25 // The type of callback that will be invoked for each module event. This is | 50 // The type of callback that will be invoked for each module event. This is |
| 26 // invoked by the loader and potentially on any thread. The loader lock is not | 51 // invoked by the loader and potentially on any thread. The loader lock is not |
| 27 // held but the execution of this callback blocks the module from being bound. | 52 // held but the execution of this callback blocks the module from being bound. |
| 28 // Keep the amount of work performed here to an absolute minimum. Note that | 53 // Keep the amount of work performed here to an absolute minimum. Note that |
| 29 // it is possible for this callback to be invoked after the destruction of the | 54 // it is possible for this callback to be invoked after the destruction of the |
| 30 // watcher, but very unlikely. | 55 // watcher, but very unlikely. |
| 31 using OnModuleEventCallback = | 56 using OnModuleEventCallback = base::Callback<void(const ModuleEvent& event)>; |
| 32 base::Callback<void(const mojom::ModuleEvent& event)>; | |
| 33 | 57 |
| 34 // Creates and starts a watcher. This enumerates all loaded modules | 58 // Creates and starts a watcher. This enumerates all loaded modules |
| 35 // synchronously on the current thread during construction, and provides | 59 // synchronously on the current thread during construction, and provides |
| 36 // synchronous notifications as modules are loaded and unloaded. The callback | 60 // synchronous notifications as modules are loaded and unloaded. The callback |
| 37 // is invoked in the context of the thread that is loading a module, and as | 61 // is invoked in the context of the thread that is loading a module, and as |
| 38 // such may be invoked on any thread in the process. Note that it is possible | 62 // such may be invoked on any thread in the process. Note that it is possible |
| 39 // to receive two notifications for some modules as the initial loaded module | 63 // to receive two notifications for some modules as the initial loaded module |
| 40 // enumeration races briefly with the callback mechanism. In this case both a | 64 // enumeration races briefly with the callback mechanism. In this case both a |
| 41 // MODULE_LOADED and a MODULE_ALREADY_LOADED event will be received for the | 65 // MODULE_LOADED and a MODULE_ALREADY_LOADED event will be received for the |
| 42 // same module. Since the callback is installed first no modules can be | 66 // same module. Since the callback is installed first no modules can be |
| 43 // missed, however. This factory function may be called on any thread. | 67 // missed, however. This factory function may be called on any thread. |
| 44 // | 68 // |
| 45 // Only a single instance of a watcher may exist at any moment. This will | 69 // Only a single instance of a watcher may exist at any moment. This will |
| 46 // return nullptr when trying to create a second watcher. | 70 // return nullptr when trying to create a second watcher. |
| 47 static std::unique_ptr<ModuleWatcher> Create( | 71 static std::unique_ptr<ModuleWatcher> Create(OnModuleEventCallback callback); |
| 48 const OnModuleEventCallback& callback); | |
| 49 | 72 |
| 50 // This can be called on any thread. After destruction the |callback| | 73 // This can be called on any thread. After destruction the |callback| |
| 51 // provided to the constructor will no longer be invoked with module events. | 74 // provided to the constructor will no longer be invoked with module events. |
| 52 ~ModuleWatcher(); | 75 ~ModuleWatcher(); |
| 53 | 76 |
| 54 protected: | 77 protected: |
| 55 // For unittesting. | 78 // For unittesting. |
| 56 friend class ModuleWatcherTest; | 79 friend class ModuleWatcherTest; |
| 57 | 80 |
| 58 // Registers a DllNotification callback with the OS. Modifies | 81 // Registers a DllNotification callback with the OS. Modifies |
| (...skipping 17 matching lines...) Expand all Loading... |
| 76 // DWORD, const LDR_DLL_NOTIFICATION_DATA*, PVOID) | 99 // DWORD, const LDR_DLL_NOTIFICATION_DATA*, PVOID) |
| 77 // Not using CALLBACK/DWORD/PVOID allows skipping the windows.h header from | 100 // Not using CALLBACK/DWORD/PVOID allows skipping the windows.h header from |
| 78 // this file. | 101 // this file. |
| 79 static void __stdcall LoaderNotificationCallback( | 102 static void __stdcall LoaderNotificationCallback( |
| 80 unsigned long notification_reason, | 103 unsigned long notification_reason, |
| 81 const LDR_DLL_NOTIFICATION_DATA* notification_data, | 104 const LDR_DLL_NOTIFICATION_DATA* notification_data, |
| 82 void* context); | 105 void* context); |
| 83 | 106 |
| 84 private: | 107 private: |
| 85 // Private to enforce Singleton semantics. See Create above. | 108 // Private to enforce Singleton semantics. See Create above. |
| 86 explicit ModuleWatcher(const OnModuleEventCallback& callback); | 109 explicit ModuleWatcher(OnModuleEventCallback callback); |
| 87 | 110 |
| 88 // The current callback. Can end up being invoked on any thread. | 111 // The current callback. Can end up being invoked on any thread. |
| 89 OnModuleEventCallback callback_; | 112 OnModuleEventCallback callback_; |
| 90 // Used by the DllNotification mechanism. | 113 // Used by the DllNotification mechanism. |
| 91 void* dll_notification_cookie_ = nullptr; | 114 void* dll_notification_cookie_ = nullptr; |
| 92 | 115 |
| 93 DISALLOW_COPY_AND_ASSIGN(ModuleWatcher); | 116 DISALLOW_COPY_AND_ASSIGN(ModuleWatcher); |
| 94 }; | 117 }; |
| 95 | 118 |
| 96 #endif // CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ | 119 #endif // CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ |
| OLD | NEW |