16d528ed9Sopenharmony_ci// Copyright (c) 2012 The Chromium Authors. All rights reserved.
26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
36d528ed9Sopenharmony_ci// found in the LICENSE file.
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci#ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
66d528ed9Sopenharmony_ci#define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
76d528ed9Sopenharmony_ci
86d528ed9Sopenharmony_ci#include <windows.h>
96d528ed9Sopenharmony_ci
106d528ed9Sopenharmony_ci#include "base/win/scoped_handle.h"
116d528ed9Sopenharmony_ci
126d528ed9Sopenharmony_cinamespace base {
136d528ed9Sopenharmony_cinamespace win {
146d528ed9Sopenharmony_ci
156d528ed9Sopenharmony_ci// Manages the closing of process and thread handles from PROCESS_INFORMATION
166d528ed9Sopenharmony_ci// structures. Allows clients to take ownership of either handle independently.
176d528ed9Sopenharmony_ciclass ScopedProcessInformation {
186d528ed9Sopenharmony_ci public:
196d528ed9Sopenharmony_ci  ScopedProcessInformation();
206d528ed9Sopenharmony_ci  explicit ScopedProcessInformation(const PROCESS_INFORMATION& process_info);
216d528ed9Sopenharmony_ci  ~ScopedProcessInformation();
226d528ed9Sopenharmony_ci
236d528ed9Sopenharmony_ci  // Returns true iff this instance is holding a thread and/or process handle.
246d528ed9Sopenharmony_ci  bool IsValid() const;
256d528ed9Sopenharmony_ci
266d528ed9Sopenharmony_ci  // Closes the held thread and process handles, if any.
276d528ed9Sopenharmony_ci  void Close();
286d528ed9Sopenharmony_ci
296d528ed9Sopenharmony_ci  // Populates this instance with the provided |process_info|.
306d528ed9Sopenharmony_ci  void Set(const PROCESS_INFORMATION& process_info);
316d528ed9Sopenharmony_ci
326d528ed9Sopenharmony_ci  // Populates this instance with duplicate handles and the thread/process IDs
336d528ed9Sopenharmony_ci  // from |other|. Returns false in case of failure, in which case this instance
346d528ed9Sopenharmony_ci  // will be completely unpopulated.
356d528ed9Sopenharmony_ci  bool DuplicateFrom(const ScopedProcessInformation& other);
366d528ed9Sopenharmony_ci
376d528ed9Sopenharmony_ci  // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this
386d528ed9Sopenharmony_ci  // instance.
396d528ed9Sopenharmony_ci  PROCESS_INFORMATION Take();
406d528ed9Sopenharmony_ci
416d528ed9Sopenharmony_ci  // Transfers ownership of the held process handle, if any, away from this
426d528ed9Sopenharmony_ci  // instance. Note that the related process_id will also be cleared.
436d528ed9Sopenharmony_ci  HANDLE TakeProcessHandle();
446d528ed9Sopenharmony_ci
456d528ed9Sopenharmony_ci  // Transfers ownership of the held thread handle, if any, away from this
466d528ed9Sopenharmony_ci  // instance. Note that the related thread_id will also be cleared.
476d528ed9Sopenharmony_ci  HANDLE TakeThreadHandle();
486d528ed9Sopenharmony_ci
496d528ed9Sopenharmony_ci  // Returns the held process handle, if any, while retaining ownership.
506d528ed9Sopenharmony_ci  HANDLE process_handle() const { return process_handle_.Get(); }
516d528ed9Sopenharmony_ci
526d528ed9Sopenharmony_ci  // Returns the held thread handle, if any, while retaining ownership.
536d528ed9Sopenharmony_ci  HANDLE thread_handle() const { return thread_handle_.Get(); }
546d528ed9Sopenharmony_ci
556d528ed9Sopenharmony_ci  // Returns the held process id, if any.
566d528ed9Sopenharmony_ci  DWORD process_id() const { return process_id_; }
576d528ed9Sopenharmony_ci
586d528ed9Sopenharmony_ci  // Returns the held thread id, if any.
596d528ed9Sopenharmony_ci  DWORD thread_id() const { return thread_id_; }
606d528ed9Sopenharmony_ci
616d528ed9Sopenharmony_ci private:
626d528ed9Sopenharmony_ci  ScopedHandle process_handle_;
636d528ed9Sopenharmony_ci  ScopedHandle thread_handle_;
646d528ed9Sopenharmony_ci  DWORD process_id_;
656d528ed9Sopenharmony_ci  DWORD thread_id_;
666d528ed9Sopenharmony_ci
676d528ed9Sopenharmony_ci  ScopedProcessInformation(const ScopedProcessInformation&) = delete;
686d528ed9Sopenharmony_ci  ScopedProcessInformation& operator=(const ScopedProcessInformation&) = delete;
696d528ed9Sopenharmony_ci};
706d528ed9Sopenharmony_ci
716d528ed9Sopenharmony_ci}  // namespace win
726d528ed9Sopenharmony_ci}  // namespace base
736d528ed9Sopenharmony_ci
746d528ed9Sopenharmony_ci#endif  // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
75