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