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 <memory> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "chrome/common/conflicts/module_event_win.mojom.h" | |
| 12 | |
| 13 class ModuleWatcherTest; | |
| 14 | |
| 15 union LDR_DLL_NOTIFICATION_DATA; | |
| 16 | |
| 17 // This class observes modules as they are loaded and unloaded into a process's | |
| 18 // address space. | |
| 19 // | |
| 20 // 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 // created. | |
| 23 class ModuleWatcher { | |
| 24 public: | |
| 25 // 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 | |
| 27 // 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 | |
| 29 // it is possible for this callback to be invoked after the destruction of the | |
| 30 // watcher, but very unlikely. | |
|
grt (UTC plus 2)
2016/11/15 10:35:34
what makes it unlikely? is it any less likely than
| |
| 31 using OnModuleEventCallback = | |
| 32 base::Callback<void(const mojom::ModuleEvent& event)>; | |
| 33 | |
| 34 // Creates and starts a watcher. This enumerates all loaded modules | |
| 35 // synchronously on the current thread during construction, and provides | |
| 36 // 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 | |
| 38 // 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 | |
| 40 // 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 | |
| 42 // same module. Since the callback is installed first no modules can be | |
| 43 // missed, however. This factory function may be called on any thread. | |
| 44 // | |
| 45 // Only a single instance of a watcher may exist at any moment. This will | |
| 46 // return nullptr when trying to create a second watcher. | |
| 47 static std::unique_ptr<ModuleWatcher> Create( | |
| 48 const OnModuleEventCallback& callback); | |
| 49 | |
| 50 // This can be called on any thread. After destruction the |callback| | |
| 51 // provided to the constructor will no longer be invoked with module events. | |
| 52 ~ModuleWatcher(); | |
| 53 | |
| 54 protected: | |
| 55 // For unittesting. | |
| 56 friend class ModuleWatcherTest; | |
| 57 | |
| 58 // Registers a DllNotification callback with the OS. Modifies | |
| 59 // |dll_notification_cookie_|. Can be called on any thread. | |
| 60 void RegisterDllNotificationCallback(); | |
| 61 | |
| 62 // Removes the installed DllNotification callback. Modifies | |
| 63 // |dll_notification_cookie_|. Can be called on any thread. | |
| 64 void UnregisterDllNotificationCallback(); | |
| 65 | |
| 66 // Enumerates all currently loaded modules, synchronously invoking callbacks | |
| 67 // on the current thread. Can be called on any thread. | |
| 68 void EnumerateAlreadyLoadedModules(); | |
| 69 | |
| 70 // Helper function for retrieving the callback associated with a given | |
| 71 // LdrNotification context. | |
| 72 static OnModuleEventCallback GetCallbackForContext(void* context); | |
| 73 | |
| 74 // The loader notification callback. This is actually | |
| 75 // void CALLBACK LoaderNotificationCallback( | |
| 76 // DWORD, const LDR_DLL_NOTIFICATION_DATA*, PVOID) | |
| 77 // Not using CALLBACK/DWORD/PVOID allows skipping the windows.h header from | |
| 78 // this file. | |
| 79 static void __stdcall LoaderNotificationCallback( | |
| 80 unsigned long notification_reason, | |
| 81 const LDR_DLL_NOTIFICATION_DATA* notification_data, | |
| 82 void* context); | |
| 83 | |
| 84 private: | |
| 85 // Private to enforce Singleton semantics. See Create above. | |
| 86 explicit ModuleWatcher(const OnModuleEventCallback& callback); | |
| 87 | |
| 88 // The current callback. Can end up being invoked on any thread. | |
| 89 OnModuleEventCallback callback_; | |
| 90 // Used by the DllNotification mechanism. | |
| 91 void* dll_notification_cookie_ = nullptr; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(ModuleWatcher); | |
| 94 }; | |
| 95 | |
| 96 #endif // CHROME_COMMON_CONFLICTS_MODULE_WATCHER_WIN_H_ | |
| OLD | NEW |