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.
5 #include "mojo/common/test/test_utils.h"
12 #include "base/base_paths.h"
13 #include "base/path_service.h"
14 #include "base/strings/string_util.h"
19 bool BlockingWrite(const embedder::PlatformHandle
& handle
,
21 size_t bytes_to_write
,
22 size_t* bytes_written
) {
23 OVERLAPPED overlapped
= { 0 };
24 DWORD bytes_written_dword
= 0;
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
,
35 *bytes_written
= bytes_written_dword
;
39 bool BlockingRead(const embedder::PlatformHandle
& handle
,
43 OVERLAPPED overlapped
= { 0 };
44 DWORD bytes_read_dword
= 0;
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
,
55 *bytes_read
= bytes_read_dword
;
59 bool NonBlockingRead(const embedder::PlatformHandle
& handle
,
63 OVERLAPPED overlapped
= { 0 };
64 DWORD bytes_read_dword
= 0;
66 if (!ReadFile(handle
.handle
, buffer
, static_cast<DWORD
>(buffer_size
),
67 &bytes_read_dword
, &overlapped
)) {
68 if (GetLastError() != ERROR_IO_PENDING
)
71 CancelIo(handle
.handle
);
73 if (!GetOverlappedResult(handle
.handle
, &overlapped
, &bytes_read_dword
,
80 *bytes_read
= bytes_read_dword
;
84 embedder::ScopedPlatformHandle
PlatformHandleFromFILE(base::ScopedFILE fp
) {
87 HANDLE rv
= INVALID_HANDLE_VALUE
;
88 PCHECK(DuplicateHandle(
90 reinterpret_cast<HANDLE
>(_get_osfhandle(_fileno(fp
.get()))),
95 DUPLICATE_SAME_ACCESS
)) << "DuplicateHandle";
96 return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv
));
99 base::ScopedFILE
FILEFromPlatformHandle(embedder::ScopedPlatformHandle h
,
102 // Microsoft's documentation for |_open_osfhandle()| only discusses these
103 // flags (and |_O_WTEXT|). Hmmm.
105 if (strchr(mode
, 'a'))
107 if (strchr(mode
, 'r'))
109 if (strchr(mode
, 't'))
112 _fdopen(_open_osfhandle(reinterpret_cast<intptr_t>(h
.release().handle
),
115 PCHECK(rv
) << "_fdopen";
119 base::FilePath
GetFilePathForJSResource(const std::string
& path
) {
120 std::string binding_path
= "gen/" + path
+ ".js";
121 base::ReplaceChars(binding_path
, "//", "\\", &binding_path
);
122 base::FilePath exe_dir
;
123 PathService::Get(base::DIR_EXE
, &exe_dir
);
124 return exe_dir
.AppendASCII(binding_path
);