OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 #include "mojo/edk/test/test_utils.h" | |
6 | |
7 #include <windows.h> | |
8 #include <fcntl.h> | |
9 #include <io.h> | |
10 #include <string.h> | |
11 | |
12 #include "base/base_paths.h" | |
13 #include "base/path_service.h" | |
14 #include "base/strings/string_util.h" | |
15 | |
16 namespace mojo { | |
17 namespace test { | |
18 | |
19 bool BlockingWrite(const embedder::PlatformHandle& handle, | |
20 const void* buffer, | |
21 size_t bytes_to_write, | |
22 size_t* bytes_written) { | |
23 OVERLAPPED overlapped = {0}; | |
24 DWORD bytes_written_dword = 0; | |
25 | |
26 if (!WriteFile(handle.handle, buffer, static_cast<DWORD>(bytes_to_write), | |
27 &bytes_written_dword, &overlapped)) { | |
28 if (GetLastError() != ERROR_IO_PENDING || | |
29 !GetOverlappedResult(handle.handle, &overlapped, &bytes_written_dword, | |
30 TRUE)) { | |
31 return false; | |
32 } | |
33 } | |
34 | |
35 *bytes_written = bytes_written_dword; | |
36 return true; | |
37 } | |
38 | |
39 bool BlockingRead(const embedder::PlatformHandle& handle, | |
40 void* buffer, | |
41 size_t buffer_size, | |
42 size_t* bytes_read) { | |
43 OVERLAPPED overlapped = {0}; | |
44 DWORD bytes_read_dword = 0; | |
45 | |
46 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), | |
47 &bytes_read_dword, &overlapped)) { | |
48 if (GetLastError() != ERROR_IO_PENDING || | |
49 !GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, | |
50 TRUE)) { | |
51 return false; | |
52 } | |
53 } | |
54 | |
55 *bytes_read = bytes_read_dword; | |
56 return true; | |
57 } | |
58 | |
59 bool NonBlockingRead(const embedder::PlatformHandle& handle, | |
60 void* buffer, | |
61 size_t buffer_size, | |
62 size_t* bytes_read) { | |
63 OVERLAPPED overlapped = {0}; | |
64 DWORD bytes_read_dword = 0; | |
65 | |
66 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), | |
67 &bytes_read_dword, &overlapped)) { | |
68 if (GetLastError() != ERROR_IO_PENDING) | |
69 return false; | |
70 | |
71 CancelIo(handle.handle); | |
72 | |
73 if (!GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, | |
74 TRUE)) { | |
75 *bytes_read = 0; | |
76 return true; | |
77 } | |
78 } | |
79 | |
80 *bytes_read = bytes_read_dword; | |
81 return true; | |
82 } | |
83 | |
84 embedder::ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) { | |
85 CHECK(fp); | |
86 | |
87 HANDLE rv = INVALID_HANDLE_VALUE; | |
88 PCHECK(DuplicateHandle( | |
89 GetCurrentProcess(), | |
90 reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp.get()))), | |
91 GetCurrentProcess(), &rv, 0, TRUE, DUPLICATE_SAME_ACCESS)) | |
92 << "DuplicateHandle"; | |
93 return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv)); | |
94 } | |
95 | |
96 base::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, | |
97 const char* mode) { | |
98 CHECK(h.is_valid()); | |
99 // Microsoft's documentation for |_open_osfhandle()| only discusses these | |
100 // flags (and |_O_WTEXT|). Hmmm. | |
101 int flags = 0; | |
102 if (strchr(mode, 'a')) | |
103 flags |= _O_APPEND; | |
104 if (strchr(mode, 'r')) | |
105 flags |= _O_RDONLY; | |
106 if (strchr(mode, 't')) | |
107 flags |= _O_TEXT; | |
108 base::ScopedFILE rv(_fdopen( | |
109 _open_osfhandle(reinterpret_cast<intptr_t>(h.release().handle), flags), | |
110 mode)); | |
111 PCHECK(rv) << "_fdopen"; | |
112 return rv.Pass(); | |
113 } | |
114 | |
115 base::FilePath GetFilePathForJSResource(const std::string& path) { | |
116 std::string binding_path = "gen/" + path + ".js"; | |
117 base::ReplaceChars(binding_path, "//", "\\", &binding_path); | |
118 base::FilePath exe_dir; | |
119 PathService::Get(base::DIR_EXE, &exe_dir); | |
120 return exe_dir.AppendASCII(binding_path); | |
121 } | |
122 | |
123 } // namespace test | |
124 } // namespace mojo | |
OLD | NEW |