diff options
-rw-r--r-- | base/file_util_proxy.cc | 31 | ||||
-rw-r--r-- | base/file_util_proxy.h | 10 | ||||
-rw-r--r-- | chrome/browser/safe_browsing/client_side_detection_service.cc | 2 | ||||
-rw-r--r-- | chrome/renderer/safe_browsing/scorer.cc | 5 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_file_io_impl.cc | 36 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_file_io_impl.h | 8 |
6 files changed, 60 insertions, 32 deletions
diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc index b4d0d54..d357e98 100644 --- a/base/file_util_proxy.cc +++ b/base/file_util_proxy.cc @@ -525,12 +525,11 @@ class RelayRead : public MessageLoopRelay { public: RelayRead(base::PlatformFile file, int64 offset, - char* buffer, int bytes_to_read, - base::FileUtilProxy::ReadWriteCallback* callback) + base::FileUtilProxy::ReadCallback* callback) : file_(file), offset_(offset), - buffer_(buffer), + buffer_(new char[bytes_to_read]), bytes_to_read_(bytes_to_read), callback_(callback), bytes_read_(0) { @@ -538,7 +537,7 @@ class RelayRead : public MessageLoopRelay { protected: virtual void RunWork() { - bytes_read_ = base::ReadPlatformFile(file_, offset_, buffer_, + bytes_read_ = base::ReadPlatformFile(file_, offset_, buffer_.get(), bytes_to_read_); if (bytes_read_ < 0) set_error_code(base::PLATFORM_FILE_ERROR_FAILED); @@ -546,7 +545,7 @@ class RelayRead : public MessageLoopRelay { virtual void RunCallback() { if (callback_) { - callback_->Run(error_code(), bytes_read_); + callback_->Run(error_code(), buffer_.get(), bytes_read_); delete callback_; } } @@ -554,9 +553,9 @@ class RelayRead : public MessageLoopRelay { private: base::PlatformFile file_; int64 offset_; - char* buffer_; + scoped_array<char> buffer_; int bytes_to_read_; - base::FileUtilProxy::ReadWriteCallback* callback_; + base::FileUtilProxy::ReadCallback* callback_; int bytes_read_; }; @@ -566,17 +565,18 @@ class RelayWrite : public MessageLoopRelay { int64 offset, const char* buffer, int bytes_to_write, - base::FileUtilProxy::ReadWriteCallback* callback) + base::FileUtilProxy::WriteCallback* callback) : file_(file), offset_(offset), - buffer_(buffer), + buffer_(new char[bytes_to_write]), bytes_to_write_(bytes_to_write), callback_(callback) { + memcpy(buffer_.get(), buffer, bytes_to_write); } protected: virtual void RunWork() { - bytes_written_ = base::WritePlatformFile(file_, offset_, buffer_, + bytes_written_ = base::WritePlatformFile(file_, offset_, buffer_.get(), bytes_to_write_); if (bytes_written_ < 0) set_error_code(base::PLATFORM_FILE_ERROR_FAILED); @@ -592,9 +592,9 @@ class RelayWrite : public MessageLoopRelay { private: base::PlatformFile file_; int64 offset_; - const char* buffer_; + scoped_array<char> buffer_; int bytes_to_write_; - base::FileUtilProxy::ReadWriteCallback* callback_; + base::FileUtilProxy::WriteCallback* callback_; int bytes_written_; }; @@ -843,11 +843,10 @@ bool FileUtilProxy::Read( scoped_refptr<MessageLoopProxy> message_loop_proxy, PlatformFile file, int64 offset, - char* buffer, int bytes_to_read, - ReadWriteCallback* callback) { + ReadCallback* callback) { return Start(FROM_HERE, message_loop_proxy, - new RelayRead(file, offset, buffer, bytes_to_read, callback)); + new RelayRead(file, offset, bytes_to_read, callback)); } // static @@ -857,7 +856,7 @@ bool FileUtilProxy::Write( int64 offset, const char* buffer, int bytes_to_write, - ReadWriteCallback* callback) { + WriteCallback* callback) { return Start(FROM_HERE, message_loop_proxy, new RelayWrite(file, offset, buffer, bytes_to_write, callback)); } diff --git a/base/file_util_proxy.h b/base/file_util_proxy.h index b5e28c0..f2368cc 100644 --- a/base/file_util_proxy.h +++ b/base/file_util_proxy.h @@ -46,8 +46,11 @@ class FileUtilProxy { >::Type GetFileInfoCallback; typedef Callback2<PlatformFileError /* error code */, const std::vector<Entry>&>::Type ReadDirectoryCallback; + typedef Callback3<PlatformFileError /* error code */, + const char* /* data */, + int /* bytes read/written */>::Type ReadCallback; typedef Callback2<PlatformFileError /* error code */, - int /* bytes read/written */>::Type ReadWriteCallback; + int /* bytes written */>::Type WriteCallback; // Creates or opens a file with the given flags. It is invalid to pass NULL // for the callback. @@ -149,9 +152,8 @@ class FileUtilProxy { scoped_refptr<MessageLoopProxy> message_loop_proxy, PlatformFile file, int64 offset, - char* buffer, int bytes_to_read, - ReadWriteCallback* callback); + ReadCallback* callback); // Writes to a file. If |offset| is greater than the length of the file, // |false| is returned. On success, the file pointer is moved to position @@ -162,7 +164,7 @@ class FileUtilProxy { int64 offset, const char* buffer, int bytes_to_write, - ReadWriteCallback* callback); + WriteCallback* callback); // Touches a file. The callback can be NULL. static bool Touch( diff --git a/chrome/browser/safe_browsing/client_side_detection_service.cc b/chrome/browser/safe_browsing/client_side_detection_service.cc index 008a2d2..3d55132 100644 --- a/chrome/browser/safe_browsing/client_side_detection_service.cc +++ b/chrome/browser/safe_browsing/client_side_detection_service.cc @@ -161,7 +161,7 @@ void ClientSideDetectionService::CreateModelFileDone( base::PassPlatformFile file, bool created) { model_file_ = file.ReleaseValue(); - base::FileUtilProxy::ReadWriteCallback* cb = callback_factory_.NewCallback( + base::FileUtilProxy::WriteCallback* cb = callback_factory_.NewCallback( &ClientSideDetectionService::WriteModelFileDone); if (!created || base::PLATFORM_FILE_OK != error_code || diff --git a/chrome/renderer/safe_browsing/scorer.cc b/chrome/renderer/safe_browsing/scorer.cc index e405bd3..22f7e59 100644 --- a/chrome/renderer/safe_browsing/scorer.cc +++ b/chrome/renderer/safe_browsing/scorer.cc @@ -55,7 +55,6 @@ class ScorerLoader { file_thread_proxy_, model_file_, 0, // offset - buffer_, Scorer::kMaxPhishingModelSizeBytes, NewCallback(this, &ScorerLoader::ModelReadDone)); DCHECK(success) << "Unable to post a task to read the phishing model file"; @@ -63,7 +62,8 @@ class ScorerLoader { private: // Callback that is run once the file data has been read. - void ModelReadDone(base::PlatformFileError error_code, int bytes_read) { + void ModelReadDone(base::PlatformFileError error_code, + const char* data, int bytes_read) { Scorer* scorer = NULL; if (error_code != base::PLATFORM_FILE_OK) { DLOG(ERROR) << "Error reading phishing model file: " << error_code; @@ -72,6 +72,7 @@ class ScorerLoader { } else if (bytes_read == Scorer::kMaxPhishingModelSizeBytes) { DLOG(ERROR) << "Phishing model is too large, ignoring"; } else { + memcpy(buffer_, data, bytes_read); scorer = Scorer::Create(base::StringPiece(buffer_, bytes_read)); } RunCallback(scorer); diff --git a/webkit/plugins/ppapi/ppb_file_io_impl.cc b/webkit/plugins/ppapi/ppb_file_io_impl.cc index ac8004b..fab8c7b 100644 --- a/webkit/plugins/ppapi/ppb_file_io_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_io_impl.cc @@ -205,7 +205,8 @@ PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance) ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), file_(base::kInvalidPlatformFileValue), callback_(), - info_(NULL) { + info_(NULL), + read_buffer_(NULL) { } PPB_FileIO_Impl::~PPB_FileIO_Impl() { @@ -311,10 +312,14 @@ int32_t PPB_FileIO_Impl::Read(int64_t offset, if (rv != PP_OK) return rv; + // If |read_buffer__|, a callback should be pending (caught above). + DCHECK(!read_buffer_); + read_buffer_ = buffer; + if (!base::FileUtilProxy::Read( instance()->delegate()->GetFileThreadMessageLoopProxy(), - file_, offset, buffer, bytes_to_read, - callback_factory_.NewCallback(&PPB_FileIO_Impl::ReadWriteCallback))) + file_, offset, bytes_to_read, + callback_factory_.NewCallback(&PPB_FileIO_Impl::ReadCallback))) return PP_ERROR_FAILED; RegisterCallback(callback); @@ -332,7 +337,7 @@ int32_t PPB_FileIO_Impl::Write(int64_t offset, if (!base::FileUtilProxy::Write( instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, offset, buffer, bytes_to_write, - callback_factory_.NewCallback(&PPB_FileIO_Impl::ReadWriteCallback))) + callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) return PP_ERROR_FAILED; RegisterCallback(callback); @@ -469,12 +474,29 @@ void PPB_FileIO_Impl::QueryInfoCallback( RunPendingCallback(PlatformFileErrorToPepperError(error_code)); } -void PPB_FileIO_Impl::ReadWriteCallback(base::PlatformFileError error_code, - int bytes_read_or_written) { +void PPB_FileIO_Impl::ReadCallback(base::PlatformFileError error_code, + const char* data, int bytes_read) { + DCHECK(data); + DCHECK(read_buffer_); + + int rv; + if (error_code == base::PLATFORM_FILE_OK) { + rv = bytes_read; + if (file_ != base::kInvalidPlatformFileValue) + memcpy(read_buffer_, data, bytes_read); + } else + rv = PlatformFileErrorToPepperError(error_code); + + read_buffer_ = NULL; + RunPendingCallback(rv); +} + +void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code, + int bytes_written) { if (error_code != base::PLATFORM_FILE_OK) RunPendingCallback(PlatformFileErrorToPepperError(error_code)); else - RunPendingCallback(bytes_read_or_written); + RunPendingCallback(bytes_written); } } // namespace ppapi diff --git a/webkit/plugins/ppapi/ppb_file_io_impl.h b/webkit/plugins/ppapi/ppb_file_io_impl.h index 3786540..3bc2742 100644 --- a/webkit/plugins/ppapi/ppb_file_io_impl.h +++ b/webkit/plugins/ppapi/ppb_file_io_impl.h @@ -95,8 +95,9 @@ class PPB_FileIO_Impl : public Resource { base::PlatformFile file); void QueryInfoCallback(base::PlatformFileError error_code, const base::PlatformFileInfo& file_info); - void ReadWriteCallback(base::PlatformFileError error_code, - int bytes_read_or_written); + void ReadCallback(base::PlatformFileError error_code, + const char* data, int bytes_read); + void WriteCallback(base::PlatformFileError error_code, int bytes_written); base::ScopedCallbackFactory<PPB_FileIO_Impl> callback_factory_; @@ -110,6 +111,9 @@ class PPB_FileIO_Impl : public Resource { // pending for it. PP_FileInfo_Dev* info_; + // Pointer back to the caller's read buffer; used by |Read()|. Not owned. + char* read_buffer_; + DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Impl); }; |