Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / sandbox / win / src / win_utils.h
blobbf3ed840de1156eb4e53ffa9a3fe86ac2d02145e
1 // Copyright (c) 2006-2010 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_WIN_UTILS_H_
6 #define SANDBOX_SRC_WIN_UTILS_H_
8 #include <windows.h>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
14 namespace sandbox {
16 // Prefix for path used by NT calls.
17 const wchar_t kNTPrefix[] = L"\\??\\";
18 const size_t kNTPrefixLen = arraysize(kNTPrefix) - 1;
20 const wchar_t kNTDevicePrefix[] = L"\\Device\\";
21 const size_t kNTDevicePrefixLen = arraysize(kNTDevicePrefix) - 1;
23 // Automatically acquires and releases a lock when the object is
24 // is destroyed.
25 class AutoLock {
26 public:
27 // Acquires the lock.
28 explicit AutoLock(CRITICAL_SECTION *lock) : lock_(lock) {
29 ::EnterCriticalSection(lock);
32 // Releases the lock;
33 ~AutoLock() {
34 ::LeaveCriticalSection(lock_);
37 private:
38 CRITICAL_SECTION *lock_;
39 DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock);
42 // Basic implementation of a singleton which calls the destructor
43 // when the exe is shutting down or the DLL is being unloaded.
44 template <typename Derived>
45 class SingletonBase {
46 public:
47 static Derived* GetInstance() {
48 static Derived* instance = NULL;
49 if (NULL == instance) {
50 instance = new Derived();
51 // Microsoft CRT extension. In an exe this this called after
52 // winmain returns, in a dll is called in DLL_PROCESS_DETACH
53 _onexit(OnExit);
55 return instance;
58 private:
59 // this is the function that gets called by the CRT when the
60 // process is shutting down.
61 static int __cdecl OnExit() {
62 delete GetInstance();
63 return 0;
67 // Convert a short path (C:\path~1 or \\??\\c:\path~1) to the long version of
68 // the path. If the path is not a valid filesystem path, the function returns
69 // false and argument is not modified.
70 bool ConvertToLongPath(base::string16* path);
72 // Returns ERROR_SUCCESS if the path contains a reparse point,
73 // ERROR_NOT_A_REPARSE_POINT if there's no reparse point in this path, or an
74 // error code when the function fails.
75 // This function is not smart. It looks for each element in the path and
76 // returns true if any of them is a reparse point.
77 DWORD IsReparsePoint(const base::string16& full_path);
79 // Returns true if the handle corresponds to the object pointed by this path.
80 bool SameObject(HANDLE handle, const wchar_t* full_path);
82 // Resolves a handle to an nt path. Returns true if the handle can be resolved.
83 bool GetPathFromHandle(HANDLE handle, base::string16* path);
85 // Resolves a win32 path to an nt path using GetPathFromHandle. The path must
86 // exist. Returs true if the translation was succesful.
87 bool GetNtPathFromWin32Path(const base::string16& path,
88 base::string16* nt_path);
90 // Translates a reserved key name to its handle.
91 // For example "HKEY_LOCAL_MACHINE" returns HKEY_LOCAL_MACHINE.
92 // Returns NULL if the name does not represent any reserved key name.
93 HKEY GetReservedKeyFromName(const base::string16& name);
95 // Resolves a user-readable registry path to a system-readable registry path.
96 // For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to
97 // \\registry\\machine\\software\\microsoft. Returns false if the path
98 // cannot be resolved.
99 bool ResolveRegistryName(base::string16 name, base::string16* resolved_name);
101 // Writes |length| bytes from the provided |buffer| into the address space of
102 // |child_process|, at the specified |address|, preserving the original write
103 // protection attributes. Returns true on success.
104 bool WriteProtectedChildMemory(HANDLE child_process, void* address,
105 const void* buffer, size_t length);
107 // Returns true if the provided path points to a pipe.
108 bool IsPipe(const base::string16& path);
110 } // namespace sandbox
112 // Resolves a function name in NTDLL to a function pointer. The second parameter
113 // is a pointer to the function pointer.
114 void ResolveNTFunctionPtr(const char* name, void* ptr);
116 #endif // SANDBOX_SRC_WIN_UTILS_H_