Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / sandbox / win / src / win_utils.h
blob3e4565f46f4e96d323999e1466987db633217757
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 the output parameter is not modified.
70 bool ConvertToLongPath(const base::string16& short_path,
71 base::string16* long_path);
73 // Sets result to true if the path contains a reparse point. The return value
74 // is ERROR_SUCCESS when the function succeeds or the appropriate error code
75 // when the function fails.
76 // This function is not smart. It looks for each element in the path and
77 // returns true if any of them is a reparse point.
78 DWORD IsReparsePoint(const base::string16& full_path, bool* result);
80 // Returns true if the handle corresponds to the object pointed by this path.
81 bool SameObject(HANDLE handle, const wchar_t* full_path);
83 // Resolves a handle to an nt path. Returns true if the handle can be resolved.
84 bool GetPathFromHandle(HANDLE handle, base::string16* path);
86 // Resolves a win32 path to an nt path using GetPathFromHandle. The path must
87 // exist. Returs true if the translation was succesful.
88 bool GetNtPathFromWin32Path(const base::string16& path,
89 base::string16* nt_path);
91 // Translates a reserved key name to its handle.
92 // For example "HKEY_LOCAL_MACHINE" returns HKEY_LOCAL_MACHINE.
93 // Returns NULL if the name does not represent any reserved key name.
94 HKEY GetReservedKeyFromName(const base::string16& name);
96 // Resolves a user-readable registry path to a system-readable registry path.
97 // For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to
98 // \\registry\\machine\\software\\microsoft. Returns false if the path
99 // cannot be resolved.
100 bool ResolveRegistryName(base::string16 name, base::string16* resolved_name);
102 // Writes |length| bytes from the provided |buffer| into the address space of
103 // |child_process|, at the specified |address|, preserving the original write
104 // protection attributes. Returns true on success.
105 bool WriteProtectedChildMemory(HANDLE child_process, void* address,
106 const void* buffer, size_t length);
108 // Returns true if the provided path points to a pipe.
109 bool IsPipe(const base::string16& path);
111 } // namespace sandbox
113 // Resolves a function name in NTDLL to a function pointer. The second parameter
114 // is a pointer to the function pointer.
115 void ResolveNTFunctionPtr(const char* name, void* ptr);
117 #endif // SANDBOX_SRC_WIN_UTILS_H_