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/edk/test/test_utils.h"
10 #include "base/base_paths.h"
11 #include "base/path_service.h"
12 #include "base/posix/eintr_wrapper.h"
17 bool BlockingWrite(const embedder::PlatformHandle& handle,
19 size_t bytes_to_write,
20 size_t* bytes_written) {
21 int original_flags = fcntl(handle.fd, F_GETFL);
22 if (original_flags == -1 ||
23 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
27 ssize_t result = HANDLE_EINTR(write(handle.fd, buffer, bytes_to_write));
29 fcntl(handle.fd, F_SETFL, original_flags);
34 *bytes_written = result;
38 bool BlockingRead(const embedder::PlatformHandle& handle,
42 int original_flags = fcntl(handle.fd, F_GETFL);
43 if (original_flags == -1 ||
44 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
48 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size));
50 fcntl(handle.fd, F_SETFL, original_flags);
59 bool NonBlockingRead(const embedder::PlatformHandle& handle,
63 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size));
66 if (errno != EAGAIN && errno != EWOULDBLOCK)
77 embedder::ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) {
79 int rv = dup(fileno(fp.get()));
80 PCHECK(rv != -1) << "dup";
81 return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv));
84 base::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h,
87 base::ScopedFILE rv(fdopen(h.release().fd, mode));
88 PCHECK(rv) << "fdopen";
92 base::FilePath GetFilePathForJSResource(const std::string& path) {
93 std::string binding_path = "gen/" + path + ".js";
94 base::FilePath exe_dir;
95 PathService::Get(base::DIR_EXE, &exe_dir);
96 return exe_dir.AppendASCII(binding_path);