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