Skip to content

Commit 0ccbf04

Browse files
author
rvargas@chromium.org
committed
Replace FileUtilProxy with FileProxy in renderer_host/pepper
BUG=322664 Review URL: https://codereview.chromium.org/252583007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270781 0039d316-1c4b-4281-b951-d872f2087c98
1 parent e2504c8 commit 0ccbf04

File tree

5 files changed

+67
-47
lines changed

5 files changed

+67
-47
lines changed

base/files/file_proxy.cc

+9-5
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,19 @@ bool FileProxy::IsValid() const {
265265
return file_.IsValid();
266266
}
267267

268+
void FileProxy::SetFile(File file) {
269+
DCHECK(!file_.IsValid());
270+
file_ = file.Pass();
271+
}
272+
268273
File FileProxy::TakeFile() {
269274
return file_.Pass();
270275
}
271276

277+
PlatformFile FileProxy::GetPlatformFile() const {
278+
return file_.GetPlatformFile();
279+
}
280+
272281
bool FileProxy::Close(const StatusCallback& callback) {
273282
DCHECK(file_.IsValid());
274283
GenericFileHelper* helper = new GenericFileHelper(this, file_.Pass());
@@ -347,9 +356,4 @@ bool FileProxy::Flush(const StatusCallback& callback) {
347356
Bind(&GenericFileHelper::Reply, Owned(helper), callback));
348357
}
349358

350-
void FileProxy::SetFile(File file) {
351-
DCHECK(!file_.IsValid());
352-
file_ = file.Pass();
353-
}
354-
355359
} // namespace base

base/files/file_proxy.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ class Time;
3333
// In other words, having a sequence like
3434
//
3535
// proxy.Write(...);
36-
// delete proxy;
36+
// proxy.Write(...);
3737
//
38-
// will keep the file valid during the Write operation but will cause the file
39-
// to be closed in the current thread, when the operation finishes. If Close is
40-
// called right away after Write, the second call will fail because there is an
41-
// operation in progress.
38+
// means the second Write will always fail.
4239
class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
4340
public:
4441
// This callback is used by methods that report only an error code. It is
@@ -88,8 +85,14 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
8885
// length to simulate a new file), and false otherwise.
8986
bool created() const { return file_.created(); }
9087

88+
// Claims ownership of |file|. It is an error to call this method when
89+
// IsValid() returns true.
90+
void SetFile(File file);
91+
9192
File TakeFile();
9293

94+
PlatformFile GetPlatformFile() const;
95+
9396
// Proxies File::Close. The callback can be null.
9497
// This returns false if task posting to |task_runner| has failed.
9598
bool Close(const StatusCallback& callback);
@@ -127,7 +130,6 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
127130

128131
private:
129132
friend class FileHelper;
130-
void SetFile(File file);
131133
TaskRunner* task_runner() { return task_runner_.get(); }
132134

133135
scoped_refptr<TaskRunner> task_runner_;

base/files/file_proxy_unittest.cc

+14
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ TEST_F(FileProxyTest, CreateTemporary) {
207207
EXPECT_TRUE(base::DeleteFile(path_, false));
208208
}
209209

210+
TEST_F(FileProxyTest, SetAndTake) {
211+
File file(test_path(), File::FLAG_CREATE | File::FLAG_READ);
212+
ASSERT_TRUE(file.IsValid());
213+
FileProxy proxy(file_task_runner());
214+
EXPECT_FALSE(proxy.IsValid());
215+
proxy.SetFile(file.Pass());
216+
EXPECT_TRUE(proxy.IsValid());
217+
EXPECT_FALSE(file.IsValid());
218+
219+
file = proxy.TakeFile();
220+
EXPECT_FALSE(proxy.IsValid());
221+
EXPECT_TRUE(file.IsValid());
222+
}
223+
210224
TEST_F(FileProxyTest, GetInfo) {
211225
// Setup.
212226
ASSERT_EQ(4, base::WriteFile(test_path(), "test", 4));

content/browser/renderer_host/pepper/pepper_file_io_host.cc

+30-32
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ PepperFileIOHost::PepperFileIOHost(BrowserPpapiHostImpl* host,
8686
: ResourceHost(host->GetPpapiHost(), instance, resource),
8787
browser_ppapi_host_(host),
8888
render_process_host_(NULL),
89-
file_(base::kInvalidPlatformFileValue),
89+
file_(BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)),
9090
open_flags_(0),
9191
file_system_type_(PP_FILESYSTEMTYPE_INVALID),
9292
max_written_offset_(0),
@@ -97,8 +97,6 @@ PepperFileIOHost::PepperFileIOHost(BrowserPpapiHostImpl* host,
9797
instance, &render_process_id_, &unused)) {
9898
render_process_id_ = -1;
9999
}
100-
file_message_loop_ =
101-
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
102100
}
103101

104102
PepperFileIOHost::~PepperFileIOHost() {}
@@ -265,15 +263,14 @@ void PepperFileIOHost::DidOpenInternalFile(
265263
void PepperFileIOHost::GotResolvedRenderProcessId(
266264
ppapi::host::ReplyMessageContext reply_context,
267265
base::FilePath path,
268-
int platform_file_flags,
266+
int file_flags,
269267
base::ProcessId resolved_render_process_id) {
270268
DCHECK_CURRENTLY_ON(BrowserThread::IO);
271269
resolved_render_process_id_ = resolved_render_process_id;
272-
base::FileUtilProxy::CreateOrOpen(
273-
file_message_loop_,
270+
file_.CreateOrOpen(
274271
path,
275-
platform_file_flags,
276-
base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback,
272+
file_flags,
273+
base::Bind(&PepperFileIOHost::OnOpenProxyCallback,
277274
weak_factory_.GetWeakPtr(),
278275
reply_context));
279276
}
@@ -287,15 +284,14 @@ int32_t PepperFileIOHost::OnHostMsgTouch(
287284
if (rv != PP_OK)
288285
return rv;
289286

290-
if (!base::FileUtilProxy::Touch(
291-
file_message_loop_,
292-
file_,
287+
if (!file_.SetTimes(
293288
PPTimeToTime(last_access_time),
294289
PPTimeToTime(last_modified_time),
295290
base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
296291
weak_factory_.GetWeakPtr(),
297-
context->MakeReplyMessageContext())))
292+
context->MakeReplyMessageContext()))) {
298293
return PP_ERROR_FAILED;
294+
}
299295

300296
state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
301297
return PP_OK_COMPLETIONPENDING;
@@ -314,14 +310,13 @@ int32_t PepperFileIOHost::OnHostMsgSetLength(
314310
// Quota checks are performed on the plugin side, in order to use the same
315311
// quota reservation and request system as Write.
316312

317-
if (!base::FileUtilProxy::Truncate(
318-
file_message_loop_,
319-
file_,
313+
if (!file_.SetLength(
320314
length,
321315
base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
322316
weak_factory_.GetWeakPtr(),
323-
context->MakeReplyMessageContext())))
317+
context->MakeReplyMessageContext()))) {
324318
return PP_ERROR_FAILED;
319+
}
325320

326321
state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
327322
return PP_OK_COMPLETIONPENDING;
@@ -334,13 +329,12 @@ int32_t PepperFileIOHost::OnHostMsgFlush(
334329
if (rv != PP_OK)
335330
return rv;
336331

337-
if (!base::FileUtilProxy::Flush(
338-
file_message_loop_,
339-
file_,
332+
if (!file_.Flush(
340333
base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
341334
weak_factory_.GetWeakPtr(),
342-
context->MakeReplyMessageContext())))
335+
context->MakeReplyMessageContext()))) {
343336
return PP_ERROR_FAILED;
337+
}
344338

345339
state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
346340
return PP_OK_COMPLETIONPENDING;
@@ -354,12 +348,9 @@ int32_t PepperFileIOHost::OnHostMsgClose(
354348
check_quota_ = false;
355349
}
356350

357-
if (file_ != base::kInvalidPlatformFileValue) {
358-
base::FileUtilProxy::Close(file_message_loop_,
359-
file_,
360-
base::Bind(&PepperFileIOHost::DidCloseFile,
361-
weak_factory_.GetWeakPtr()));
362-
file_ = base::kInvalidPlatformFileValue;
351+
if (file_.IsValid()) {
352+
file_.Close(base::Bind(&PepperFileIOHost::DidCloseFile,
353+
weak_factory_.GetWeakPtr()));
363354
}
364355
return PP_OK;
365356
}
@@ -425,17 +416,23 @@ void PepperFileIOHost::ExecutePlatformGeneralCallback(
425416
state_manager_.SetOperationFinished();
426417
}
427418

419+
// TODO(rvargas): this method should go away when FileApi moves to use File.
428420
void PepperFileIOHost::ExecutePlatformOpenFileCallback(
429421
ppapi::host::ReplyMessageContext reply_context,
430422
base::File::Error error_code,
431423
base::PassPlatformFile file,
432424
bool unused_created) {
433-
int32_t pp_error = ppapi::FileErrorToPepperError(error_code);
434-
DCHECK(file_ == base::kInvalidPlatformFileValue);
435-
file_ = file.ReleaseValue();
425+
DCHECK(!file_.IsValid());
426+
file_.SetFile(base::File(file.ReleaseValue()));
436427

437-
if (file_ != base::kInvalidPlatformFileValue &&
438-
!AddFileToReplyContext(open_flags_, &reply_context))
428+
OnOpenProxyCallback(reply_context, error_code);
429+
}
430+
431+
void PepperFileIOHost::OnOpenProxyCallback(
432+
ppapi::host::ReplyMessageContext reply_context,
433+
base::File::Error error_code) {
434+
int32_t pp_error = ppapi::FileErrorToPepperError(error_code);
435+
if (file_.IsValid() && !AddFileToReplyContext(open_flags_, &reply_context))
439436
pp_error = PP_ERROR_FAILED;
440437

441438
PP_Resource quota_file_system = 0;
@@ -467,7 +464,8 @@ bool PepperFileIOHost::AddFileToReplyContext(
467464
plugin_process_id = resolved_render_process_id_;
468465

469466
IPC::PlatformFileForTransit transit_file =
470-
BrokerGetFileHandleForProcess(file_, plugin_process_id, false);
467+
BrokerGetFileHandleForProcess(file_.GetPlatformFile(), plugin_process_id,
468+
false);
471469
if (transit_file == IPC::InvalidPlatformFileForTransit())
472470
return false;
473471

content/browser/renderer_host/pepper/pepper_file_io_host.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "base/basictypes.h"
1111
#include "base/callback_forward.h"
1212
#include "base/files/file.h"
13+
#include "base/files/file_proxy.h"
1314
#include "base/memory/weak_ptr.h"
1415
#include "base/platform_file.h"
1516
#include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
@@ -84,6 +85,9 @@ class PepperFileIOHost : public ppapi::host::ResourceHost,
8485
base::PassPlatformFile file,
8586
bool unused_created);
8687

88+
void OnOpenProxyCallback(ppapi::host::ReplyMessageContext reply_context,
89+
base::File::Error error_code);
90+
8791
void GotUIThreadStuffForInternalFileSystems(
8892
ppapi::host::ReplyMessageContext reply_context,
8993
int platform_file_flags,
@@ -95,7 +99,7 @@ class PepperFileIOHost : public ppapi::host::ResourceHost,
9599
void GotResolvedRenderProcessId(
96100
ppapi::host::ReplyMessageContext reply_context,
97101
base::FilePath path,
98-
int platform_file_flags,
102+
int file_flags,
99103
base::ProcessId resolved_render_process_id);
100104

101105
void DidOpenQuotaFile(ppapi::host::ReplyMessageContext reply_context,
@@ -119,7 +123,7 @@ class PepperFileIOHost : public ppapi::host::ResourceHost,
119123
int render_process_id_;
120124
base::ProcessId resolved_render_process_id_;
121125

122-
base::PlatformFile file_;
126+
base::FileProxy file_;
123127
int32_t open_flags_;
124128

125129
// The file system type specified in the Open() call. This will be
@@ -137,8 +141,6 @@ class PepperFileIOHost : public ppapi::host::ResourceHost,
137141

138142
ppapi::FileIOStateManager state_manager_;
139143

140-
scoped_refptr<base::MessageLoopProxy> file_message_loop_;
141-
142144
base::WeakPtrFactory<PepperFileIOHost> weak_factory_;
143145

144146
DISALLOW_COPY_AND_ASSIGN(PepperFileIOHost);

0 commit comments

Comments
 (0)