1 // Copyright (c) 2011 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.
5 #ifndef SANDBOX_SRC_HANDLE_TABLE_H_
6 #define SANDBOX_SRC_HANDLE_TABLE_H_
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
13 #include "sandbox/win/src/nt_internals.h"
17 // HandleTable retrieves the global handle table and provides helper classes
18 // for iterating through the table and retrieving handle info.
21 static const char16
* HandleTable::kTypeProcess
;
22 static const char16
* HandleTable::kTypeThread
;
23 static const char16
* HandleTable::kTypeFile
;
24 static const char16
* HandleTable::kTypeDirectory
;
25 static const char16
* HandleTable::kTypeKey
;
26 static const char16
* HandleTable::kTypeWindowStation
;
27 static const char16
* HandleTable::kTypeDesktop
;
28 static const char16
* HandleTable::kTypeService
;
29 static const char16
* HandleTable::kTypeMutex
;
30 static const char16
* HandleTable::kTypeSemaphore
;
31 static const char16
* HandleTable::kTypeEvent
;
32 static const char16
* HandleTable::kTypeTimer
;
33 static const char16
* HandleTable::kTypeNamedPipe
;
34 static const char16
* HandleTable::kTypeJobObject
;
35 static const char16
* HandleTable::kTypeFileMap
;
36 static const char16
* HandleTable::kTypeAlpcPort
;
40 // Used by the iterator to provide simple caching accessors to handle data.
43 bool operator==(const HandleEntry
& rhs
) const {
44 return handle_entry_
== rhs
.handle_entry_
;
47 bool operator!=(const HandleEntry
& rhs
) const {
48 return handle_entry_
!= rhs
.handle_entry_
;
51 const SYSTEM_HANDLE_INFORMATION
* handle_entry() const {
55 const OBJECT_TYPE_INFORMATION
* TypeInfo();
57 const string16
& Name();
59 const string16
& Type();
61 bool IsType(const string16
& type_string
);
64 friend class Iterator
;
65 friend class HandleTable
;
70 UPDATE_INFO_AND_TYPE_NAME
,
73 explicit HandleEntry(const SYSTEM_HANDLE_INFORMATION
* handle_info_entry
);
75 bool needs_info_update() { return handle_entry_
!= last_entry_
; }
77 void UpdateInfo(UpdateType flag
);
79 OBJECT_TYPE_INFORMATION
* type_info_internal() {
80 return reinterpret_cast<OBJECT_TYPE_INFORMATION
*>(
81 &(type_info_buffer_
[0]));
84 const SYSTEM_HANDLE_INFORMATION
* handle_entry_
;
85 const SYSTEM_HANDLE_INFORMATION
* last_entry_
;
86 std::vector
<BYTE
> type_info_buffer_
;
87 string16 handle_name_
;
90 DISALLOW_COPY_AND_ASSIGN(HandleEntry
);
95 Iterator(const HandleTable
& table
, const SYSTEM_HANDLE_INFORMATION
* start
,
96 const SYSTEM_HANDLE_INFORMATION
* stop
);
98 Iterator(const Iterator
& it
);
100 Iterator
& operator++() {
101 if (++(current_
.handle_entry_
) == end_
)
102 current_
.handle_entry_
= table_
.end();
106 bool operator==(const Iterator
& rhs
) const {
107 return current_
== rhs
.current_
;
110 bool operator!=(const Iterator
& rhs
) const {
111 return current_
!= rhs
.current_
;
114 HandleEntry
& operator*() { return current_
; }
116 operator const SYSTEM_HANDLE_INFORMATION
*() {
117 return current_
.handle_entry_
;
120 HandleEntry
* operator->() { return ¤t_
; }
123 const HandleTable
& table_
;
124 HandleEntry current_
;
125 const SYSTEM_HANDLE_INFORMATION
* end_
;
130 Iterator
begin() const {
131 return Iterator(*this, handle_info()->Information
,
132 &handle_info()->Information
[handle_info()->NumberOfHandles
]);
135 const SYSTEM_HANDLE_INFORMATION_EX
* handle_info() const {
136 return reinterpret_cast<const SYSTEM_HANDLE_INFORMATION_EX
*>(
137 &(handle_info_buffer_
[0]));
140 // Returns an iterator to the handles for only the supplied process ID.
141 Iterator
HandlesForProcess(ULONG process_id
) const;
142 const SYSTEM_HANDLE_INFORMATION
* end() const {
143 return &handle_info()->Information
[handle_info()->NumberOfHandles
];
147 SYSTEM_HANDLE_INFORMATION_EX
* handle_info_internal() {
148 return reinterpret_cast<SYSTEM_HANDLE_INFORMATION_EX
*>(
149 &(handle_info_buffer_
[0]));
152 std::vector
<BYTE
> handle_info_buffer_
;
154 DISALLOW_COPY_AND_ASSIGN(HandleTable
);
157 } // namespace sandbox
159 #endif // SANDBOX_SRC_HANDLE_TABLE_H_