Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / components / nacl / renderer / plugin / temporary_file.cc
blob95e8a5288bc8e3dfd5139a9089969f11bfb3ec9b
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.
5 #include "components/nacl/renderer/plugin/temporary_file.h"
7 #include "components/nacl/renderer/plugin/plugin.h"
8 #include "components/nacl/renderer/plugin/utility.h"
9 #include "native_client/src/include/portability_io.h"
10 #include "native_client/src/shared/platform/nacl_check.h"
11 #include "native_client/src/trusted/service_runtime/include/sys/stat.h"
12 #include "ppapi/c/private/pp_file_handle.h"
13 #include "ppapi/cpp/core.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/module.h"
17 namespace plugin {
19 TempFile::TempFile(Plugin* plugin, PP_FileHandle handle)
20 : plugin_(plugin),
21 internal_handle_(handle) { }
23 TempFile::~TempFile() { }
25 int32_t TempFile::Open(bool writeable) {
26 if (internal_handle_ == PP_kInvalidFileHandle)
27 return PP_ERROR_FAILED;
29 #if NACL_WINDOWS
30 HANDLE handle = internal_handle_;
32 //////// Now try the posix view.
33 int rdwr_flag = writeable ? _O_RDWR : _O_RDONLY;
34 int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(handle),
35 rdwr_flag | _O_BINARY
36 | _O_TEMPORARY | _O_SHORT_LIVED );
38 // Close the Windows HANDLE if it can't be converted.
39 if (posix_desc == -1) {
40 PLUGIN_PRINTF(("TempFile::Open failed to convert HANDLE to posix\n"));
41 CloseHandle(handle);
43 int32_t fd = posix_desc;
44 #else
45 int32_t fd = internal_handle_;
46 #endif
48 if (fd < 0)
49 return PP_ERROR_FAILED;
51 // dup the fd to make allow making separate read and write wrappers.
52 int32_t read_fd = DUP(fd);
53 if (read_fd == NACL_NO_FILE_DESC)
54 return PP_ERROR_FAILED;
56 if (writeable) {
57 write_wrapper_.reset(
58 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDWR));
61 read_wrapper_.reset(
62 plugin_->wrapper_factory()->MakeFileDesc(read_fd, O_RDONLY));
63 return PP_OK;
66 bool TempFile::Reset() {
67 // Use the read_wrapper_ to reset the file pos. The write_wrapper_ is also
68 // backed by the same file, so it should also reset.
69 CHECK(read_wrapper_.get() != NULL);
70 nacl_off64_t newpos = read_wrapper_->Seek(0, SEEK_SET);
71 return newpos == 0;
74 PP_FileHandle TempFile::TakeFileHandle() {
75 PP_FileHandle to_return = internal_handle_;
76 internal_handle_ = PP_kInvalidFileHandle;
77 read_wrapper_.release();
78 write_wrapper_.release();
79 return to_return;
82 } // namespace plugin