1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/win/scoped_process_information.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ 1.9 +#define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ 1.10 + 1.11 +#include <windows.h> 1.12 + 1.13 +#include "base/basictypes.h" 1.14 +#include "base/base_export.h" 1.15 +#include "base/win/scoped_handle.h" 1.16 + 1.17 +namespace base { 1.18 +namespace win { 1.19 + 1.20 +// Manages the closing of process and thread handles from PROCESS_INFORMATION 1.21 +// structures. Allows clients to take ownership of either handle independently. 1.22 +class BASE_EXPORT ScopedProcessInformation { 1.23 + public: 1.24 + // Helper object to contain the effect of Receive() to the funtion that needs 1.25 + // a pointer. 1.26 + class Receiver { 1.27 + public: 1.28 + explicit Receiver(ScopedProcessInformation* owner) 1.29 + : info_(), 1.30 + owner_(owner) {} 1.31 + ~Receiver() { owner_->Set(info_); } 1.32 + 1.33 + operator PROCESS_INFORMATION*() { return &info_; } 1.34 + 1.35 + private: 1.36 + PROCESS_INFORMATION info_; 1.37 + ScopedProcessInformation* owner_; 1.38 + }; 1.39 + 1.40 + ScopedProcessInformation(); 1.41 + ~ScopedProcessInformation(); 1.42 + 1.43 + // Returns an object that may be passed to API calls such as CreateProcess. 1.44 + // DCHECKs that the object is not currently holding any handles. 1.45 + // HANDLEs stored in the returned PROCESS_INFORMATION will be owned by this 1.46 + // instance. 1.47 + // The intended use case is something like this: 1.48 + // if (::CreateProcess(..., startup_info, scoped_proces_info.Receive())) 1.49 + Receiver Receive(); 1.50 + 1.51 + // Returns true iff this instance is holding a thread and/or process handle. 1.52 + bool IsValid() const; 1.53 + 1.54 + // Closes the held thread and process handles, if any. 1.55 + void Close(); 1.56 + 1.57 + // Populates this instance with the provided |process_info|. 1.58 + void Set(const PROCESS_INFORMATION& process_info); 1.59 + 1.60 + // Populates this instance with duplicate handles and the thread/process IDs 1.61 + // from |other|. Returns false in case of failure, in which case this instance 1.62 + // will be completely unpopulated. 1.63 + bool DuplicateFrom(const ScopedProcessInformation& other); 1.64 + 1.65 + // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this 1.66 + // instance. 1.67 + PROCESS_INFORMATION Take(); 1.68 + 1.69 + // Transfers ownership of the held process handle, if any, away from this 1.70 + // instance. Note that the related process_id will also be cleared. 1.71 + HANDLE TakeProcessHandle(); 1.72 + 1.73 + // Transfers ownership of the held thread handle, if any, away from this 1.74 + // instance. Note that the related thread_id will also be cleared. 1.75 + HANDLE TakeThreadHandle(); 1.76 + 1.77 + // Returns the held process handle, if any, while retaining ownership. 1.78 + HANDLE process_handle() const { 1.79 + return process_handle_.Get(); 1.80 + } 1.81 + 1.82 + // Returns the held thread handle, if any, while retaining ownership. 1.83 + HANDLE thread_handle() const { 1.84 + return thread_handle_.Get(); 1.85 + } 1.86 + 1.87 + // Returns the held process id, if any. 1.88 + DWORD process_id() const { 1.89 + return process_id_; 1.90 + } 1.91 + 1.92 + // Returns the held thread id, if any. 1.93 + DWORD thread_id() const { 1.94 + return thread_id_; 1.95 + } 1.96 + 1.97 + private: 1.98 + ScopedHandle process_handle_; 1.99 + ScopedHandle thread_handle_; 1.100 + DWORD process_id_; 1.101 + DWORD thread_id_; 1.102 + 1.103 + DISALLOW_COPY_AND_ASSIGN(ScopedProcessInformation); 1.104 +}; 1.105 + 1.106 +} // namespace win 1.107 +} // namespace base 1.108 + 1.109 +#endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_