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/common/test/test_utils.h" | 5 #include "mojo/common/test/test_utils.h" |
6 | 6 |
| 7 #include <fcntl.h> |
| 8 #include <io.h> |
| 9 #include <string.h> |
7 #include <windows.h> | 10 #include <windows.h> |
8 | 11 |
9 #include "base/base_paths.h" | 12 #include "base/base_paths.h" |
10 #include "base/path_service.h" | 13 #include "base/path_service.h" |
11 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
12 #include "mojo/embedder/platform_handle.h" | |
13 | 15 |
14 namespace mojo { | 16 namespace mojo { |
15 namespace test { | 17 namespace test { |
16 | 18 |
17 bool BlockingWrite(const embedder::PlatformHandle& handle, | 19 bool BlockingWrite(const embedder::PlatformHandle& handle, |
18 const void* buffer, | 20 const void* buffer, |
19 size_t bytes_to_write, | 21 size_t bytes_to_write, |
20 size_t* bytes_written) { | 22 size_t* bytes_written) { |
21 OVERLAPPED overlapped = { 0 }; | 23 OVERLAPPED overlapped = { 0 }; |
22 DWORD bytes_written_dword = 0; | 24 DWORD bytes_written_dword = 0; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 TRUE)) { | 74 TRUE)) { |
73 *bytes_read = 0; | 75 *bytes_read = 0; |
74 return true; | 76 return true; |
75 } | 77 } |
76 } | 78 } |
77 | 79 |
78 *bytes_read = bytes_read_dword; | 80 *bytes_read = bytes_read_dword; |
79 return true; | 81 return true; |
80 } | 82 } |
81 | 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(), |
| 92 &rv, |
| 93 0, |
| 94 TRUE, |
| 95 DUPLICATE_SAME_ACCESS)) << "DuplicateHandle"; |
| 96 return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv)); |
| 97 } |
| 98 |
| 99 base::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, |
| 100 const char* mode) { |
| 101 CHECK(h.is_valid()); |
| 102 // Microsoft's documentation for |_open_osfhandle()| only discusses these |
| 103 // flags (and |_O_WTEXT|). Hmmm. |
| 104 int flags = 0; |
| 105 if (strchr(mode, 'a')) |
| 106 flags |= _O_APPEND; |
| 107 if (strchr(mode, 'r')) |
| 108 flags |= _O_RDONLY; |
| 109 if (strchr(mode, 't')) |
| 110 flags |= _O_TEXT; |
| 111 base::ScopedFILE rv( |
| 112 _fdopen(_open_osfhandle(reinterpret_cast<intptr_t>(h.release().handle), |
| 113 flags), |
| 114 mode)); |
| 115 PCHECK(rv) << "_fdopen"; |
| 116 return rv.Pass(); |
| 117 } |
| 118 |
82 base::FilePath GetFilePathForJSResource(const std::string& path) { | 119 base::FilePath GetFilePathForJSResource(const std::string& path) { |
83 std::string binding_path = "gen/" + path + ".js"; | 120 std::string binding_path = "gen/" + path + ".js"; |
84 base::ReplaceChars(binding_path, "//", "\\", &binding_path); | 121 base::ReplaceChars(binding_path, "//", "\\", &binding_path); |
85 base::FilePath exe_dir; | 122 base::FilePath exe_dir; |
86 PathService::Get(base::DIR_EXE, &exe_dir); | 123 PathService::Get(base::DIR_EXE, &exe_dir); |
87 return exe_dir.AppendASCII(binding_path); | 124 return exe_dir.AppendASCII(binding_path); |
88 } | 125 } |
89 | 126 |
90 } // namespace test | 127 } // namespace test |
91 } // namespace mojo | 128 } // namespace mojo |
OLD | NEW |