Home | History | Annotate | Download | only in plugin
      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 
     21 TempFile::TempFile(Plugin* plugin, PP_FileHandle handle)
     22     : plugin_(plugin),
     23       internal_handle_(handle) { }
     24 
     25 TempFile::~TempFile() { }
     26 
     27 int32_t TempFile::Open(bool writeable) {
     28   if (internal_handle_ == PP_kInvalidFileHandle)
     29     return PP_ERROR_FAILED;
     30 
     31 #if NACL_WINDOWS
     32   HANDLE handle = internal_handle_;
     33 
     34   //////// Now try the posix view.
     35   int rdwr_flag = writeable ? _O_RDWR : _O_RDONLY;
     36   int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(handle),
     37                                        rdwr_flag | _O_BINARY
     38                                        | _O_TEMPORARY | _O_SHORT_LIVED );
     39 
     40   // Close the Windows HANDLE if it can't be converted.
     41   if (posix_desc == -1) {
     42     PLUGIN_PRINTF(("TempFile::Open failed to convert HANDLE to posix\n"));
     43     CloseHandle(handle);
     44   }
     45   int32_t fd = posix_desc;
     46 #else
     47   int32_t fd = internal_handle_;
     48 #endif
     49 
     50   if (fd < 0)
     51     return PP_ERROR_FAILED;
     52 
     53   // dup the fd to make allow making separate read and write wrappers.
     54   int32_t read_fd = DUP(fd);
     55   if (read_fd == NACL_NO_FILE_DESC)
     56     return PP_ERROR_FAILED;
     57 
     58   if (writeable) {
     59     write_wrapper_.reset(
     60         plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDWR));
     61   }
     62 
     63   read_wrapper_.reset(
     64       plugin_->wrapper_factory()->MakeFileDesc(read_fd, O_RDONLY));
     65   return PP_OK;
     66 }
     67 
     68 bool TempFile::Reset() {
     69   // Use the read_wrapper_ to reset the file pos.  The write_wrapper_ is also
     70   // backed by the same file, so it should also reset.
     71   CHECK(read_wrapper_.get() != NULL);
     72   nacl_off64_t newpos = read_wrapper_->Seek(0, SEEK_SET);
     73   return newpos == 0;
     74 }
     75 
     76 PP_FileHandle TempFile::TakeFileHandle() {
     77   PP_FileHandle to_return = internal_handle_;
     78   internal_handle_ = PP_kInvalidFileHandle;
     79   read_wrapper_.release();
     80   write_wrapper_.release();
     81   return to_return;
     82 }
     83 
     84 }  // namespace plugin
     85