Skip to content

Commit

Permalink
[Pepper] Wire up append mode writing support of FileIO
Browse files Browse the repository at this point in the history
BUG=334171

Review URL: https://codereview.chromium.org/130053003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247504 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
tzik@chromium.org committed Jan 28, 2014
1 parent 8ca54e1 commit 540d6af
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 81 deletions.
6 changes: 2 additions & 4 deletions content/browser/renderer_host/pepper/pepper_file_io_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,9 @@ int32_t PepperFileIOHost::OnHostMsgFlush(

int32_t PepperFileIOHost::OnHostMsgClose(
ppapi::host::HostMessageContext* context,
int64_t max_written_offset) {
const ppapi::FileGrowth& file_growth) {
if (check_quota_) {
file_system_host_->CloseQuotaFile(
this,
ppapi::FileGrowth(max_written_offset, 0));
file_system_host_->CloseQuotaFile(this, file_growth);
check_quota_ = false;
}

Expand Down
6 changes: 5 additions & 1 deletion content/browser/renderer_host/pepper/pepper_file_io_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "url/gurl.h"
#include "webkit/browser/fileapi/file_system_context.h"

namespace ppapi {
struct FileGrowth;
}

namespace content {
class PepperFileSystemBrowserHost;

Expand Down Expand Up @@ -58,7 +62,7 @@ class PepperFileIOHost : public ppapi::host::ResourceHost,
int32_t OnHostMsgSetLength(ppapi::host::HostMessageContext* context,
int64_t length);
int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context,
int64_t max_written_offset);
const ppapi::FileGrowth& file_growth);
int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context);
int32_t OnHostMsgRequestOSFileHandle(
ppapi::host::HostMessageContext* context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,14 @@ int32_t PepperFileSystemBrowserHost::OnHostMsgInitIsolatedFileSystem(
int32_t PepperFileSystemBrowserHost::OnHostMsgReserveQuota(
ppapi::host::HostMessageContext* context,
int64_t amount,
const ppapi::FileSizeMap& file_sizes) {
const ppapi::FileGrowthMap& file_growths) {
DCHECK(ChecksQuota());
DCHECK(amount > 0);

if (reserving_quota_)
return PP_ERROR_INPROGRESS;
reserving_quota_ = true;

ppapi::FileGrowthMap file_growths;
for (ppapi::FileSizeMap::const_iterator it = file_sizes.begin();
it != file_sizes.end(); ++it)
file_growths[it->first] = ppapi::FileGrowth(it->second, 0);

int64_t reservation_amount = std::max<int64_t>(kMinimumQuotaReservationSize,
amount);
file_system_context_->default_file_task_runner()->PostTask(
Expand Down Expand Up @@ -463,15 +458,15 @@ void PepperFileSystemBrowserHost::GotQuotaReservation(
void PepperFileSystemBrowserHost::GotReservedQuota(
ppapi::host::ReplyMessageContext reply_context,
int64_t amount,
const ppapi::FileSizeMap& max_written_offsets) {
const ppapi::FileSizeMap& file_sizes) {
DCHECK(reserving_quota_);
reserving_quota_ = false;
reserved_quota_ = amount;

reply_context.params.set_result(PP_OK);
host()->SendReply(
reply_context,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(amount, max_written_offsets));
PpapiPluginMsg_FileSystem_ReserveQuotaReply(amount, file_sizes));
}

std::string PepperFileSystemBrowserHost::GetPluginMimeType() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CONTENT_EXPORT PepperFileSystemBrowserHost
int32_t OnHostMsgReserveQuota(
ppapi::host::HostMessageContext* context,
int64_t amount,
const ppapi::FileSizeMap& max_written_offsets);
const ppapi::FileGrowthMap& file_growths);

void SendReplyForFileSystem(
ppapi::host::ReplyMessageContext reply_context,
Expand All @@ -137,7 +137,7 @@ class CONTENT_EXPORT PepperFileSystemBrowserHost
void GotReservedQuota(
ppapi::host::ReplyMessageContext reply_context,
int64_t amount,
const ppapi::FileSizeMap& max_written_offsets);
const ppapi::FileSizeMap& file_sizes);
void DidOpenQuotaFile(
PP_Resource file_io_resource,
const OpenQuotaFileCallback& callback,
Expand Down
12 changes: 5 additions & 7 deletions content/browser/renderer_host/pepper/quota_reservation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,20 @@ void QuotaReservation::OnClientCrash() {
void QuotaReservation::GotReservedQuota(
const ReserveQuotaCallback& callback,
base::File::Error error) {
ppapi::FileSizeMap max_written_offsets;
for (FileMap::iterator it = files_.begin(); it != files_.end(); ++ it) {
max_written_offsets.insert(
std::make_pair(it->first, it->second->GetMaxWrittenOffset()));
}
ppapi::FileSizeMap file_sizes;
for (FileMap::iterator it = files_.begin(); it != files_.end(); ++ it)
file_sizes[it->first] = it->second->GetMaxWrittenOffset();

if (file_system_context_) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(callback,
quota_reservation_->remaining_quota(),
max_written_offsets));
file_sizes));
} else {
// Unit testing code path.
callback.Run(quota_reservation_->remaining_quota(), max_written_offsets);
callback.Run(quota_reservation_->remaining_quota(), file_sizes);
}
}

Expand Down
4 changes: 1 addition & 3 deletions ppapi/proxy/file_io_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,8 @@ void FileIOResource::Close() {
if (file_handle_)
file_handle_ = NULL;

// TODO(tzik): Post |max_written_offset_| and |append_mode_write_amount_|
// separately by using FileGrowth after the IPC signature changed.
Post(BROWSER, PpapiHostMsg_FileIO_Close(
max_written_offset_ + append_mode_write_amount_));
FileGrowth(max_written_offset_, append_mode_write_amount_)));
}

int32_t FileIOResource::RequestOSFileHandle(
Expand Down
17 changes: 8 additions & 9 deletions ppapi/proxy/file_system_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ void FileSystemResource::ReserveQuota(int64_t amount) {
DCHECK(!reserving_quota_);
reserving_quota_ = true;

// TODO(tzik): Use FileGrowthMap here after the IPC signature changed.
FileSizeMap file_sizes;
FileGrowthMap file_growths;
for (std::set<PP_Resource>::iterator it = files_.begin();
it != files_.end(); ++it) {
EnterResourceNoLock<PPB_FileIO_API> enter(*it, true);
Expand All @@ -185,26 +184,26 @@ void FileSystemResource::ReserveQuota(int64_t amount) {
continue;
}
PPB_FileIO_API* file_io_api = enter.object();
file_sizes[*it] =
file_io_api->GetMaxWrittenOffset() +
file_io_api->GetAppendModeWriteAmount();
file_growths[*it] = FileGrowth(
file_io_api->GetMaxWrittenOffset(),
file_io_api->GetAppendModeWriteAmount());
}
Call<PpapiPluginMsg_FileSystem_ReserveQuotaReply>(BROWSER,
PpapiHostMsg_FileSystem_ReserveQuota(amount, file_sizes),
PpapiHostMsg_FileSystem_ReserveQuota(amount, file_growths),
base::Bind(&FileSystemResource::ReserveQuotaComplete,
this));
}

void FileSystemResource::ReserveQuotaComplete(
const ResourceMessageReplyParams& params,
int64_t amount,
const FileSizeMap& max_written_offsets) {
const FileSizeMap& file_sizes) {
DCHECK(reserving_quota_);
reserving_quota_ = false;
reserved_quota_ = amount;

for (FileSizeMap::const_iterator it = max_written_offsets.begin();
it != max_written_offsets.end(); ++it) {
for (FileSizeMap::const_iterator it = file_sizes.begin();
it != file_sizes.end(); ++it) {
EnterResourceNoLock<PPB_FileIO_API> enter(it->first, true);

// It is possible that the host has sent an offset for a file that has been
Expand Down
18 changes: 9 additions & 9 deletions ppapi/proxy/file_system_resource_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ TEST_F(FileSystemResourceTest, RequestQuota) {
sink().ClearMessages();

int64_t amount = 0;
FileOffsetMap max_written_offsets;
FileGrowthMap file_growths;
ASSERT_TRUE(UnpackMessage<PpapiHostMsg_FileSystem_ReserveQuota>(
msg, &amount, &max_written_offsets));
msg, &amount, &file_growths));
ASSERT_EQ(kQuotaRequestAmount1, amount);
ASSERT_EQ(2U, max_written_offsets.size());
ASSERT_EQ(0, max_written_offsets[file_io1.get()]);
ASSERT_EQ(0, max_written_offsets[file_io2.get()]);
ASSERT_EQ(2U, file_growths.size());
ASSERT_EQ(0, file_growths[file_io1.get()].max_written_offset);
ASSERT_EQ(0, file_growths[file_io2.get()].max_written_offset);

// Make another request while the "reserve quota" message is pending.
MockRequestQuotaCallback cb2;
Expand All @@ -263,7 +263,7 @@ TEST_F(FileSystemResourceTest, RequestQuota) {
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1 + kQuotaRequestAmount2,
max_written_offsets));
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}
ASSERT_TRUE(cb1.called());
ASSERT_EQ(kQuotaRequestAmount1, cb1.result());
Expand Down Expand Up @@ -294,7 +294,7 @@ TEST_F(FileSystemResourceTest, RequestQuota) {
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1 - 1,
max_written_offsets));
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}
ASSERT_TRUE(cb1.called());
ASSERT_EQ(0, cb1.result());
Expand Down Expand Up @@ -325,7 +325,7 @@ TEST_F(FileSystemResourceTest, RequestQuota) {
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1,
max_written_offsets));
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}
ASSERT_TRUE(cb1.called());
ASSERT_EQ(kQuotaRequestAmount1, cb1.result());
Expand All @@ -343,7 +343,7 @@ TEST_F(FileSystemResourceTest, RequestQuota) {
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1 + kQuotaRequestAmount2,
max_written_offsets));
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}

ASSERT_TRUE(cb2.called());
Expand Down
35 changes: 20 additions & 15 deletions ppapi/proxy/nacl_message_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,21 +381,22 @@ void NaClMessageScanner::ScanUntrustedMessage(
return;
// Audit FileIO Close messages to make sure the plugin reports an
// accurate file size.
int64_t max_written_offset = 0;
FileGrowth file_growth;
if (!UnpackMessage<PpapiHostMsg_FileIO_Close>(
nested_msg, &max_written_offset))
nested_msg, &file_growth))
return;

int64_t trusted_max_written_offset = it->second->max_written_offset();
delete it->second;
files_.erase(it);
// If the plugin is under-reporting, rewrite the message with the
// trusted value.
if (trusted_max_written_offset > max_written_offset) {
if (trusted_max_written_offset > file_growth.max_written_offset) {
new_msg_ptr->reset(
new PpapiHostMsg_ResourceCall(
params,
PpapiHostMsg_FileIO_Close(trusted_max_written_offset)));
PpapiHostMsg_FileIO_Close(
FileGrowth(trusted_max_written_offset, 0))));
}
}
case PpapiHostMsg_FileIO_SetLength::ID: {
Expand Down Expand Up @@ -429,30 +430,34 @@ void NaClMessageScanner::ScanUntrustedMessage(
// Audit FileSystem ReserveQuota messages to make sure the plugin
// reports accurate file sizes.
int64_t amount = 0;
FileOffsetMap max_written_offsets;
FileGrowthMap file_growths;
if (!UnpackMessage<PpapiHostMsg_FileSystem_ReserveQuota>(
nested_msg, &amount, &max_written_offsets))
nested_msg, &amount, &file_growths))
return;

bool audit_failed = false;
for (FileOffsetMap::iterator it = max_written_offsets.begin();
it != max_written_offsets.end(); ++it) {
for (FileGrowthMap::iterator it = file_growths.begin();
it != file_growths.end(); ++it) {
FileIOMap::iterator file_it = files_.find(it->first);
if (file_it == files_.end())
continue;
int64_t trusted_max_written_offset =
file_it->second->max_written_offset();
if (trusted_max_written_offset > it->second) {
if (trusted_max_written_offset > it->second.max_written_offset) {
audit_failed = true;
it->second = trusted_max_written_offset;
it->second.max_written_offset = trusted_max_written_offset;
}
if (it->second.append_mode_write_amount < 0) {
audit_failed = true;
it->second.append_mode_write_amount = 0;
}
}
if (audit_failed) {
new_msg_ptr->reset(
new PpapiHostMsg_ResourceCall(
params,
PpapiHostMsg_FileSystem_ReserveQuota(
amount, max_written_offsets)));
amount, file_growths)));
}
break;
}
Expand Down Expand Up @@ -518,15 +523,15 @@ void NaClMessageScanner::AuditNestedMessage(PP_Resource resource,
case PpapiPluginMsg_FileSystem_ReserveQuotaReply::ID: {
// The amount of reserved quota for a FileSystem was refreshed.
int64_t amount = 0;
FileOffsetMap max_written_offsets;
FileSizeMap file_sizes;
if (ppapi::UnpackMessage<PpapiPluginMsg_FileSystem_ReserveQuotaReply>(
msg, &amount, &max_written_offsets)) {
msg, &amount, &file_sizes)) {
FileSystemMap::iterator it = file_systems_.find(resource);
DCHECK(it != file_systems_.end());
it->second->UpdateReservedQuota(amount);

FileOffsetMap::const_iterator offset_it = max_written_offsets.begin();
for (; offset_it != max_written_offsets.end(); ++offset_it) {
FileSizeMap::const_iterator offset_it = file_sizes.begin();
for (; offset_it != file_sizes.end(); ++offset_it) {
FileIOMap::iterator fio_it = files_.find(offset_it->first);
DCHECK(fio_it != files_.end());
if (fio_it != files_.end())
Expand Down
Loading

0 comments on commit 540d6af

Please sign in to comment.