OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ppapi/native_client/src/trusted/plugin/temporary_file.h" | 5 #include "ppapi/native_client/src/trusted/plugin/temporary_file.h" |
6 | 6 |
7 #include "native_client/src/include/portability_io.h" | 7 #include "native_client/src/include/portability_io.h" |
8 #include "native_client/src/shared/platform/nacl_check.h" | 8 #include "native_client/src/shared/platform/nacl_check.h" |
9 #include "native_client/src/trusted/service_runtime/include/sys/stat.h" | 9 #include "native_client/src/trusted/service_runtime/include/sys/stat.h" |
10 | 10 |
11 #include "ppapi/cpp/core.h" | 11 #include "ppapi/cpp/core.h" |
12 #include "ppapi/cpp/instance.h" | 12 #include "ppapi/cpp/instance.h" |
13 #include "ppapi/cpp/module.h" | 13 #include "ppapi/cpp/module.h" |
14 #include "ppapi/c/private/pp_file_handle.h" | 14 #include "ppapi/c/private/pp_file_handle.h" |
15 | 15 |
16 #include "ppapi/native_client/src/trusted/plugin/plugin.h" | 16 #include "ppapi/native_client/src/trusted/plugin/plugin.h" |
17 #include "ppapi/native_client/src/trusted/plugin/utility.h" | 17 #include "ppapi/native_client/src/trusted/plugin/utility.h" |
18 | 18 |
19 namespace plugin { | 19 namespace plugin { |
20 | 20 |
21 TempFile::TempFile(Plugin* plugin) : plugin_(plugin), | 21 TempFile::TempFile(Plugin* plugin) : plugin_(plugin), |
22 existing_handle_(PP_kInvalidFileHandle) { | 22 internal_handle_(PP_kInvalidFileHandle) { |
23 PLUGIN_PRINTF(("TempFile::TempFile\n")); | |
24 } | 23 } |
25 | 24 |
26 TempFile::~TempFile() { | 25 TempFile::~TempFile() { } |
27 PLUGIN_PRINTF(("TempFile::~TempFile\n")); | |
28 } | |
29 | 26 |
30 void TempFile::Open(const pp::CompletionCallback& cb, bool writeable) { | 27 int32_t TempFile::Open(bool writeable) { |
31 PLUGIN_PRINTF(("TempFile::Open\n")); | 28 // TODO(teravest): Clean up this Open() behavior; this is really confusing as |
32 PP_FileHandle file_handle; | 29 // written. |
33 if (existing_handle_ == PP_kInvalidFileHandle) { | 30 if (internal_handle_ == PP_kInvalidFileHandle) { |
34 file_handle = | 31 internal_handle_ = |
35 plugin_->nacl_interface()->CreateTemporaryFile(plugin_->pp_instance()); | 32 plugin_->nacl_interface()->CreateTemporaryFile(plugin_->pp_instance()); |
36 } else { | |
37 file_handle = existing_handle_; | |
38 } | 33 } |
39 | 34 |
40 pp::Core* core = pp::Module::Get()->core(); | 35 if (internal_handle_ == PP_kInvalidFileHandle) { |
41 if (file_handle == PP_kInvalidFileHandle) { | |
42 PLUGIN_PRINTF(("TempFile::Open failed w/ PP_kInvalidFileHandle\n")); | 36 PLUGIN_PRINTF(("TempFile::Open failed w/ PP_kInvalidFileHandle\n")); |
43 core->CallOnMainThread(0, cb, PP_ERROR_FAILED); | 37 return PP_ERROR_FAILED; |
44 } | 38 } |
45 | 39 |
46 #if NACL_WINDOWS | 40 #if NACL_WINDOWS |
47 HANDLE handle = file_handle; | 41 HANDLE handle = internal_handle_; |
48 | 42 |
49 //////// Now try the posix view. | 43 //////// Now try the posix view. |
50 int rdwr_flag = writeable ? _O_RDWR : _O_RDONLY; | 44 int rdwr_flag = writeable ? _O_RDWR : _O_RDONLY; |
51 int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(handle), | 45 int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(handle), |
52 rdwr_flag | _O_BINARY | 46 rdwr_flag | _O_BINARY |
53 | _O_TEMPORARY | _O_SHORT_LIVED ); | 47 | _O_TEMPORARY | _O_SHORT_LIVED ); |
54 if (posix_desc == -1) { | 48 if (posix_desc == -1) { |
55 PLUGIN_PRINTF(("TempFile::Open failed to convert HANDLE to posix\n")); | 49 PLUGIN_PRINTF(("TempFile::Open failed to convert HANDLE to posix\n")); |
56 // Close the Windows HANDLE if it can't be converted. | 50 // Close the Windows HANDLE if it can't be converted. |
57 CloseHandle(handle); | 51 CloseHandle(handle); |
58 } | 52 } |
59 int32_t fd = posix_desc; | 53 int32_t fd = posix_desc; |
60 #else | 54 #else |
61 int32_t fd = file_handle; | 55 int32_t fd = internal_handle_; |
62 #endif | 56 #endif |
63 | 57 |
64 if (fd < 0) { | 58 if (fd < 0) { |
65 PLUGIN_PRINTF(("TempFile::Open failed\n")); | 59 PLUGIN_PRINTF(("TempFile::Open failed\n")); |
66 core->CallOnMainThread(0, cb, PP_ERROR_FAILED); | 60 return PP_ERROR_FAILED; |
67 return; | |
68 } | 61 } |
69 | 62 |
70 // dup the fd to make allow making separate read and write wrappers. | 63 // dup the fd to make allow making separate read and write wrappers. |
71 int32_t read_fd = DUP(fd); | 64 int32_t read_fd = DUP(fd); |
72 if (read_fd == NACL_NO_FILE_DESC) { | 65 if (read_fd == NACL_NO_FILE_DESC) { |
73 PLUGIN_PRINTF(("TempFile::Open DUP failed\n")); | 66 PLUGIN_PRINTF(("TempFile::Open DUP failed\n")); |
74 core->CallOnMainThread(0, cb, PP_ERROR_FAILED); | 67 return PP_ERROR_FAILED; |
75 return; | |
76 } | 68 } |
77 | 69 |
78 if (writeable) { | 70 if (writeable) { |
79 write_wrapper_.reset( | 71 write_wrapper_.reset( |
80 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDWR)); | 72 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDWR)); |
81 } | 73 } |
82 | 74 |
83 read_wrapper_.reset( | 75 read_wrapper_.reset( |
84 plugin_->wrapper_factory()->MakeFileDesc(read_fd, O_RDONLY)); | 76 plugin_->wrapper_factory()->MakeFileDesc(read_fd, O_RDONLY)); |
85 core->CallOnMainThread(0, cb, PP_OK); | 77 return PP_OK; |
86 } | 78 } |
87 | 79 |
88 bool TempFile::Reset() { | 80 bool TempFile::Reset() { |
89 PLUGIN_PRINTF(("TempFile::Reset\n")); | 81 PLUGIN_PRINTF(("TempFile::Reset\n")); |
90 // Use the read_wrapper_ to reset the file pos. The write_wrapper_ is also | 82 // Use the read_wrapper_ to reset the file pos. The write_wrapper_ is also |
91 // backed by the same file, so it should also reset. | 83 // backed by the same file, so it should also reset. |
92 CHECK(read_wrapper_.get() != NULL); | 84 CHECK(read_wrapper_.get() != NULL); |
93 nacl_off64_t newpos = read_wrapper_->Seek(0, SEEK_SET); | 85 nacl_off64_t newpos = read_wrapper_->Seek(0, SEEK_SET); |
94 return newpos == 0; | 86 return newpos == 0; |
95 } | 87 } |
96 | 88 |
| 89 PP_FileHandle TempFile::TakeFileHandle() { |
| 90 PP_FileHandle to_return = internal_handle_; |
| 91 internal_handle_ = PP_kInvalidFileHandle; |
| 92 read_wrapper_.release(); |
| 93 write_wrapper_.release(); |
| 94 return to_return; |
| 95 } |
| 96 |
97 } // namespace plugin | 97 } // namespace plugin |
OLD | NEW |