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_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ | |
| 6 #define CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "chrome/common/conflicts/module_event_win.mojom.h" | |
| 10 | |
| 11 class ModuleWatcherTest; | |
| 12 | |
| 13 union LDR_DLL_NOTIFICATION_DATA; | |
| 14 | |
| 15 // This class observes modules as they are loaded and unloaded into a process's | |
| 16 // address space. | |
| 17 // | |
| 18 // This class is safe to be created on any thread. Similarly, it is safe to be | |
| 19 // destroyed on any thread, independent of the thread on which the instance was | |
| 20 // created. | |
| 21 class ModuleWatcher { | |
| 22 public: | |
| 23 // The type of callback that will be invoked for each module event. This is | |
| 24 // invoked by the loader and potentially on any thread. The loader lock is not | |
| 25 // held but the execution of this callback blocks the module from being bound. | |
| 26 // Keep the amount of work performed here to an absolute minimum. | |
|
grt (UTC plus 2)
2016/11/14 20:30:31
maybe also mention that the callback may be invoke
chrisha
2016/11/14 21:38:40
Done.
| |
| 27 using OnModuleEventCallback = | |
| 28 base::Callback<void(const mojom::ModuleEvent& event)>; | |
| 29 | |
| 30 // Creates and starts a watcher. This enumerates all loaded modules | |
| 31 // synchronously on the current thread during construction, and provides | |
| 32 // synchronous notifications as modules are loaded and unloaded. The callback | |
| 33 // is invoked in the context of the thread that is loading a module, and as | |
| 34 // such may be invoked on any thread in the process. Note that it is possible | |
| 35 // to receive two notifications for some modules as the initial loaded module | |
| 36 // enumeration races briefly with the callback mechanism. In this case both a | |
| 37 // MODULE_LOADED and a MODULE_ALREADY_LOADED event will be received for the | |
| 38 // same module. Since the callback is installed first no modules can be | |
| 39 // missed, however. This factory function may be called on any thread. | |
| 40 // | |
| 41 // Only a single instance of a watcher may exist at any moment. This will | |
| 42 // return nullptr when trying to create a second watcher. | |
| 43 static std::unique_ptr<ModuleWatcher> Create( | |
|
grt (UTC plus 2)
2016/11/14 20:30:31
#include <memory>
chrisha
2016/11/14 21:38:40
Done.
| |
| 44 const OnModuleEventCallback& callback); | |
| 45 | |
| 46 // This can be called on any thread. After destruction the |callback| | |
| 47 // provided to the constructor will no longer be invoked with module events. | |
| 48 ~ModuleWatcher(); | |
| 49 | |
| 50 protected: | |
| 51 // For unittesting. | |
| 52 friend class ModuleWatcherTest; | |
| 53 | |
| 54 // Registers a DllNotification callback with the OS. Modifies | |
| 55 // |dll_notification_cookie_|. Can be called on any thread. | |
| 56 void RegisterDllNotificationCallback(); | |
| 57 | |
| 58 // Removes the installed DllNotification callback. Modifies | |
| 59 // |dll_notification_cookie_|. Can be called on any thread. | |
| 60 void UnregisterDllNotificationCallback(); | |
| 61 | |
| 62 // Enumerates all currently loaded modules, synchronously invoking callbacks | |
| 63 // on the current thread. Can be called on any thread. | |
| 64 void EnumerateAlreadyLoadedModules(); | |
| 65 | |
| 66 // Helper function for retrieving the callback associated with a given | |
| 67 // LdrNotification context. | |
| 68 static OnModuleEventCallback GetCallbackForContext(void* context); | |
| 69 | |
| 70 // The loader notification callback. This is actually | |
| 71 // void CALLBACK LoaderNotificationCallback( | |
| 72 // DWORD, const LDR_DLL_NOTIFICATION_DATA*, PVOID) | |
| 73 // Not using CALLBACK/DWORD/PVOID allows skipping the windows.h header from | |
| 74 // this file. | |
| 75 static void __stdcall LoaderNotificationCallback( | |
| 76 unsigned long notification_reason, | |
| 77 const LDR_DLL_NOTIFICATION_DATA* notification_data, | |
| 78 void* context); | |
| 79 | |
| 80 private: | |
| 81 // Private to enforce Singleton semantics. See Create above. | |
| 82 explicit ModuleWatcher(const OnModuleEventCallback& callback); | |
| 83 | |
| 84 // The current callback. Can end up being invoked on any thread. | |
| 85 OnModuleEventCallback callback_; | |
| 86 // Used by the DllNotification mechanism. | |
| 87 void* dll_notification_cookie_ = nullptr; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(ModuleWatcher); | |
| 90 }; | |
| 91 | |
| 92 #endif // CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ | |
| OLD | NEW |