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