Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / sandbox / win / src / win_utils.h
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.
4
5 #ifndef SANDBOX_SRC_WIN_UTILS_H_
6 #define SANDBOX_SRC_WIN_UTILS_H_
7
8 #include <windows.h>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
13
14 namespace sandbox {
15
16 // Prefix for path used by NT calls.
17 const wchar_t kNTPrefix[] = L"\\??\\";
18 const size_t kNTPrefixLen = arraysize(kNTPrefix) - 1;
19
20 const wchar_t kNTObjManPrefix[] = L"\\Device\\";
21 const size_t kNTObjManPrefixLen = arraysize(kNTObjManPrefix) - 1;
22
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);
30   };
31
32   // Releases the lock;
33   ~AutoLock() {
34     ::LeaveCriticalSection(lock_);
35   };
36
37  private:
38   CRITICAL_SECTION *lock_;
39   DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock);
40 };
41
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);
54     }
55     return instance;
56   }
57
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;
64   }
65 };
66
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);
72
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);
79
80 // Returns true if the handle corresponds to the object pointed by this path.
81 bool SameObject(HANDLE handle, const wchar_t* full_path);
82
83 // Resolves a handle to an nt path. Returns true if the handle can be resolved.
84 bool GetPathFromHandle(HANDLE handle, base::string16* path);
85
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);
90
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);
95
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);
101
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);
107
108 }  // namespace sandbox
109
110 // Resolves a function name in NTDLL to a function pointer. The second parameter
111 // is a pointer to the function pointer.
112 void ResolveNTFunctionPtr(const char* name, void* ptr);
113
114 #endif  // SANDBOX_SRC_WIN_UTILS_H_