| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/test/test_utils.h" | 5 #include "mojo/edk/test/test_utils.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <io.h> | 9 #include <io.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| 11 | 11 |
| 12 namespace mojo { | 12 namespace mojo { |
| 13 namespace edk { |
| 13 namespace test { | 14 namespace test { |
| 14 | 15 |
| 15 bool BlockingWrite(const embedder::PlatformHandle& handle, | 16 bool BlockingWrite(const PlatformHandle& handle, |
| 16 const void* buffer, | 17 const void* buffer, |
| 17 size_t bytes_to_write, | 18 size_t bytes_to_write, |
| 18 size_t* bytes_written) { | 19 size_t* bytes_written) { |
| 19 OVERLAPPED overlapped = {0}; | 20 OVERLAPPED overlapped = {0}; |
| 20 DWORD bytes_written_dword = 0; | 21 DWORD bytes_written_dword = 0; |
| 21 | 22 |
| 22 if (!WriteFile(handle.handle, buffer, static_cast<DWORD>(bytes_to_write), | 23 if (!WriteFile(handle.handle, buffer, static_cast<DWORD>(bytes_to_write), |
| 23 &bytes_written_dword, &overlapped)) { | 24 &bytes_written_dword, &overlapped)) { |
| 24 if (GetLastError() != ERROR_IO_PENDING || | 25 if (GetLastError() != ERROR_IO_PENDING || |
| 25 !GetOverlappedResult(handle.handle, &overlapped, &bytes_written_dword, | 26 !GetOverlappedResult(handle.handle, &overlapped, &bytes_written_dword, |
| 26 TRUE)) { | 27 TRUE)) { |
| 27 return false; | 28 return false; |
| 28 } | 29 } |
| 29 } | 30 } |
| 30 | 31 |
| 31 *bytes_written = bytes_written_dword; | 32 *bytes_written = bytes_written_dword; |
| 32 return true; | 33 return true; |
| 33 } | 34 } |
| 34 | 35 |
| 35 bool BlockingRead(const embedder::PlatformHandle& handle, | 36 bool BlockingRead(const PlatformHandle& handle, |
| 36 void* buffer, | 37 void* buffer, |
| 37 size_t buffer_size, | 38 size_t buffer_size, |
| 38 size_t* bytes_read) { | 39 size_t* bytes_read) { |
| 39 OVERLAPPED overlapped = {0}; | 40 OVERLAPPED overlapped = {0}; |
| 40 DWORD bytes_read_dword = 0; | 41 DWORD bytes_read_dword = 0; |
| 41 | 42 |
| 42 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), | 43 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), |
| 43 &bytes_read_dword, &overlapped)) { | 44 &bytes_read_dword, &overlapped)) { |
| 44 if (GetLastError() != ERROR_IO_PENDING || | 45 if (GetLastError() != ERROR_IO_PENDING || |
| 45 !GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, | 46 !GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, |
| 46 TRUE)) { | 47 TRUE)) { |
| 47 return false; | 48 return false; |
| 48 } | 49 } |
| 49 } | 50 } |
| 50 | 51 |
| 51 *bytes_read = bytes_read_dword; | 52 *bytes_read = bytes_read_dword; |
| 52 return true; | 53 return true; |
| 53 } | 54 } |
| 54 | 55 |
| 55 bool NonBlockingRead(const embedder::PlatformHandle& handle, | 56 bool NonBlockingRead(const PlatformHandle& handle, |
| 56 void* buffer, | 57 void* buffer, |
| 57 size_t buffer_size, | 58 size_t buffer_size, |
| 58 size_t* bytes_read) { | 59 size_t* bytes_read) { |
| 59 OVERLAPPED overlapped = {0}; | 60 OVERLAPPED overlapped = {0}; |
| 60 DWORD bytes_read_dword = 0; | 61 DWORD bytes_read_dword = 0; |
| 61 | 62 |
| 62 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), | 63 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), |
| 63 &bytes_read_dword, &overlapped)) { | 64 &bytes_read_dword, &overlapped)) { |
| 64 if (GetLastError() != ERROR_IO_PENDING) | 65 if (GetLastError() != ERROR_IO_PENDING) |
| 65 return false; | 66 return false; |
| 66 | 67 |
| 67 CancelIo(handle.handle); | 68 CancelIo(handle.handle); |
| 68 | 69 |
| 69 if (!GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, | 70 if (!GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, |
| 70 TRUE)) { | 71 TRUE)) { |
| 71 *bytes_read = 0; | 72 *bytes_read = 0; |
| 72 return true; | 73 return true; |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 | 76 |
| 76 *bytes_read = bytes_read_dword; | 77 *bytes_read = bytes_read_dword; |
| 77 return true; | 78 return true; |
| 78 } | 79 } |
| 79 | 80 |
| 80 embedder::ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) { | 81 ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) { |
| 81 CHECK(fp); | 82 CHECK(fp); |
| 82 | 83 |
| 83 HANDLE rv = INVALID_HANDLE_VALUE; | 84 HANDLE rv = INVALID_HANDLE_VALUE; |
| 84 PCHECK(DuplicateHandle( | 85 PCHECK(DuplicateHandle( |
| 85 GetCurrentProcess(), | 86 GetCurrentProcess(), |
| 86 reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp.get()))), | 87 reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp.get()))), |
| 87 GetCurrentProcess(), &rv, 0, TRUE, DUPLICATE_SAME_ACCESS)) | 88 GetCurrentProcess(), &rv, 0, TRUE, DUPLICATE_SAME_ACCESS)) |
| 88 << "DuplicateHandle"; | 89 << "DuplicateHandle"; |
| 89 return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv)); | 90 return ScopedPlatformHandle(PlatformHandle(rv)); |
| 90 } | 91 } |
| 91 | 92 |
| 92 base::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, | 93 base::ScopedFILE FILEFromPlatformHandle(ScopedPlatformHandle h, |
| 93 const char* mode) { | 94 const char* mode) { |
| 94 CHECK(h.is_valid()); | 95 CHECK(h.is_valid()); |
| 95 // Microsoft's documentation for |_open_osfhandle()| only discusses these | 96 // Microsoft's documentation for |_open_osfhandle()| only discusses these |
| 96 // flags (and |_O_WTEXT|). Hmmm. | 97 // flags (and |_O_WTEXT|). Hmmm. |
| 97 int flags = 0; | 98 int flags = 0; |
| 98 if (strchr(mode, 'a')) | 99 if (strchr(mode, 'a')) |
| 99 flags |= _O_APPEND; | 100 flags |= _O_APPEND; |
| 100 if (strchr(mode, 'r')) | 101 if (strchr(mode, 'r')) |
| 101 flags |= _O_RDONLY; | 102 flags |= _O_RDONLY; |
| 102 if (strchr(mode, 't')) | 103 if (strchr(mode, 't')) |
| 103 flags |= _O_TEXT; | 104 flags |= _O_TEXT; |
| 104 base::ScopedFILE rv(_fdopen( | 105 base::ScopedFILE rv(_fdopen( |
| 105 _open_osfhandle(reinterpret_cast<intptr_t>(h.release().handle), flags), | 106 _open_osfhandle(reinterpret_cast<intptr_t>(h.release().handle), flags), |
| 106 mode)); | 107 mode)); |
| 107 PCHECK(rv) << "_fdopen"; | 108 PCHECK(rv) << "_fdopen"; |
| 108 return rv.Pass(); | 109 return rv.Pass(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 } // namespace test | 112 } // namespace test |
| 113 } // namespace edk |
| 112 } // namespace mojo | 114 } // namespace mojo |
| OLD | NEW |