Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: ppapi/native_client/src/trusted/plugin/temporary_file.cc

Issue 299143004: Pepper: TempFile cleanup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: build fix Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 existing_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
29 // written.
32 PP_FileHandle file_handle; 30 PP_FileHandle file_handle;
33 if (existing_handle_ == PP_kInvalidFileHandle) { 31 if (existing_handle_ == PP_kInvalidFileHandle) {
34 file_handle = 32 file_handle =
35 plugin_->nacl_interface()->CreateTemporaryFile(plugin_->pp_instance()); 33 plugin_->nacl_interface()->CreateTemporaryFile(plugin_->pp_instance());
jvoung (off chromium) 2014/05/23 17:53:04 Might have to set "existing_handle_ = file_handle"
36 } else { 34 } else {
37 file_handle = existing_handle_; 35 file_handle = existing_handle_;
38 } 36 }
39 37
40 pp::Core* core = pp::Module::Get()->core();
41 if (file_handle == PP_kInvalidFileHandle) { 38 if (file_handle == PP_kInvalidFileHandle) {
42 PLUGIN_PRINTF(("TempFile::Open failed w/ PP_kInvalidFileHandle\n")); 39 PLUGIN_PRINTF(("TempFile::Open failed w/ PP_kInvalidFileHandle\n"));
43 core->CallOnMainThread(0, cb, PP_ERROR_FAILED); 40 return PP_ERROR_FAILED;
44 } 41 }
45 42
46 #if NACL_WINDOWS 43 #if NACL_WINDOWS
47 HANDLE handle = file_handle; 44 HANDLE handle = file_handle;
48 45
49 //////// Now try the posix view. 46 //////// Now try the posix view.
50 int rdwr_flag = writeable ? _O_RDWR : _O_RDONLY; 47 int rdwr_flag = writeable ? _O_RDWR : _O_RDONLY;
51 int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(handle), 48 int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(handle),
52 rdwr_flag | _O_BINARY 49 rdwr_flag | _O_BINARY
53 | _O_TEMPORARY | _O_SHORT_LIVED ); 50 | _O_TEMPORARY | _O_SHORT_LIVED );
54 if (posix_desc == -1) { 51 if (posix_desc == -1) {
55 PLUGIN_PRINTF(("TempFile::Open failed to convert HANDLE to posix\n")); 52 PLUGIN_PRINTF(("TempFile::Open failed to convert HANDLE to posix\n"));
56 // Close the Windows HANDLE if it can't be converted. 53 // Close the Windows HANDLE if it can't be converted.
57 CloseHandle(handle); 54 CloseHandle(handle);
58 } 55 }
59 int32_t fd = posix_desc; 56 int32_t fd = posix_desc;
60 #else 57 #else
61 int32_t fd = file_handle; 58 int32_t fd = file_handle;
62 #endif 59 #endif
63 60
64 if (fd < 0) { 61 if (fd < 0) {
65 PLUGIN_PRINTF(("TempFile::Open failed\n")); 62 PLUGIN_PRINTF(("TempFile::Open failed\n"));
66 core->CallOnMainThread(0, cb, PP_ERROR_FAILED); 63 return PP_ERROR_FAILED;
67 return;
68 } 64 }
69 65
70 // dup the fd to make allow making separate read and write wrappers. 66 // dup the fd to make allow making separate read and write wrappers.
71 int32_t read_fd = DUP(fd); 67 int32_t read_fd = DUP(fd);
72 if (read_fd == NACL_NO_FILE_DESC) { 68 if (read_fd == NACL_NO_FILE_DESC) {
73 PLUGIN_PRINTF(("TempFile::Open DUP failed\n")); 69 PLUGIN_PRINTF(("TempFile::Open DUP failed\n"));
74 core->CallOnMainThread(0, cb, PP_ERROR_FAILED); 70 return PP_ERROR_FAILED;
75 return;
76 } 71 }
77 72
78 if (writeable) { 73 if (writeable) {
79 write_wrapper_.reset( 74 write_wrapper_.reset(
80 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDWR)); 75 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDWR));
81 } 76 }
82 77
83 read_wrapper_.reset( 78 read_wrapper_.reset(
84 plugin_->wrapper_factory()->MakeFileDesc(read_fd, O_RDONLY)); 79 plugin_->wrapper_factory()->MakeFileDesc(read_fd, O_RDONLY));
85 core->CallOnMainThread(0, cb, PP_OK); 80 return PP_OK;
86 } 81 }
87 82
88 bool TempFile::Reset() { 83 bool TempFile::Reset() {
89 PLUGIN_PRINTF(("TempFile::Reset\n")); 84 PLUGIN_PRINTF(("TempFile::Reset\n"));
90 // Use the read_wrapper_ to reset the file pos. The write_wrapper_ is also 85 // 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. 86 // backed by the same file, so it should also reset.
92 CHECK(read_wrapper_.get() != NULL); 87 CHECK(read_wrapper_.get() != NULL);
93 nacl_off64_t newpos = read_wrapper_->Seek(0, SEEK_SET); 88 nacl_off64_t newpos = read_wrapper_->Seek(0, SEEK_SET);
94 return newpos == 0; 89 return newpos == 0;
95 } 90 }
96 91
92 PP_FileHandle TempFile::TakeFileHandle() {
93 PP_FileHandle to_return = existing_handle_;
94 existing_handle_ = PP_kInvalidFileHandle;
95 // TODO(teravest): Figure out why we can't reset read_wrapper_ and
jvoung (off chromium) 2014/05/23 17:53:04 =/ not sure why either...
96 // write_wrapper_ here; it seems like when this is called, we shouldn't need
97 // those DescWrappers to be valid anymore.
98 return to_return;
99 }
100
97 } // namespace plugin 101 } // namespace plugin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698