diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-18 05:51:06 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-18 05:51:06 +0000 |
commit | 3fac86133acd6821307bb2165b7222b807b44682 (patch) | |
tree | 268e74259751ff7b22fcc3b4decc58d45051185e | |
parent | 3e3715e8cd62029ac8e3f17dcb235e47ed0a9a11 (diff) | |
download | chromium_src-3fac86133acd6821307bb2165b7222b807b44682.zip chromium_src-3fac86133acd6821307bb2165b7222b807b44682.tar.gz chromium_src-3fac86133acd6821307bb2165b7222b807b44682.tar.bz2 |
Cleanup webkit::npapi::PluginStream a bit.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/10105021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132747 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | webkit/plugins/npapi/plugin_stream.cc | 61 | ||||
-rw-r--r-- | webkit/plugins/npapi/plugin_stream.h | 25 | ||||
-rw-r--r-- | webkit/plugins/npapi/plugin_stream_posix.cc | 47 | ||||
-rw-r--r-- | webkit/plugins/npapi/plugin_stream_win.cc | 41 |
4 files changed, 86 insertions, 88 deletions
diff --git a/webkit/plugins/npapi/plugin_stream.cc b/webkit/plugins/npapi/plugin_stream.cc index c45c8a2..2cfde67 100644 --- a/webkit/plugins/npapi/plugin_stream.cc +++ b/webkit/plugins/npapi/plugin_stream.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,6 +8,8 @@ #include "webkit/plugins/npapi/plugin_stream.h" +#include <algorithm> + #include "base/bind.h" #include "base/message_loop.h" #include "base/string_util.h" @@ -19,14 +21,39 @@ namespace webkit { namespace npapi { +PluginStream::PluginStream( + PluginInstance* instance, + const char* url, + bool need_notify, + void* notify_data) + : instance_(instance), + notify_needed_(need_notify), + notify_data_(notify_data), + close_on_write_data_(false), + requested_plugin_mode_(NP_NORMAL), + opened_(false), + data_offset_(0), + seekable_stream_(false) { + memset(&stream_, 0, sizeof(stream_)); + stream_.url = base::strdup(url); + ResetTempFilenameAndHandle(); +} + PluginStream::~PluginStream() { // always close our temporary files. CloseTempFile(); free(const_cast<char*>(stream_.url)); } -bool PluginStream::Open(const std::string &mime_type, - const std::string &headers, +void PluginStream::UpdateUrl(const char* url) { + DCHECK(!opened_); + free(const_cast<char*>(stream_.url)); + stream_.url = base::strdup(url); + pending_redirect_url_.clear(); +} + +bool PluginStream::Open(const std::string& mime_type, + const std::string& headers, uint32 length, uint32 last_modified, bool request_is_seekable) { @@ -48,7 +75,7 @@ bool PluginStream::Open(const std::string &mime_type, } } - const char *char_mime_type = "application/x-unknown-content-type"; + const char* char_mime_type = "application/x-unknown-content-type"; std::string temp_mime_type; if (!mime_type.empty()) { char_mime_type = mime_type.c_str(); @@ -65,7 +92,7 @@ bool PluginStream::Open(const std::string &mime_type, } // Silverlight expects a valid mime type - DCHECK(strlen(char_mime_type) != 0); + DCHECK_NE(0U, strlen(char_mime_type)); NPError err = instance_->NPP_NewStream((NPMIMEType)char_mime_type, &stream_, seekable_stream, &requested_plugin_mode_); @@ -82,16 +109,17 @@ bool PluginStream::Open(const std::string &mime_type, // If the plugin has requested certain modes, then we need a copy // of this file on disk. Open it and save it as we go. if (requested_plugin_mode_ == NP_ASFILEONLY || - requested_plugin_mode_ == NP_ASFILE) { - if (OpenTempFile() == false) + requested_plugin_mode_ == NP_ASFILE) { + if (OpenTempFile() == false) { return false; + } } mime_type_ = char_mime_type; return true; } -int PluginStream::Write(const char *buffer, const int length, +int PluginStream::Write(const char* buffer, const int length, int data_offset) { // There may be two streams to write to - the plugin and the file. // It is unclear what to do if we cannot write to both. The rules of @@ -102,13 +130,14 @@ int PluginStream::Write(const char *buffer, const int length, DCHECK(opened_); if (WriteToFile(buffer, length) && - WriteToPlugin(buffer, length, data_offset)) + WriteToPlugin(buffer, length, data_offset)) { return length; + } return -1; } -bool PluginStream::WriteToFile(const char *buf, size_t length) { +bool PluginStream::WriteToFile(const char* buf, size_t length) { // For ASFILEONLY, ASFILE, and SEEK modes, we need to write // to the disk if (TempFileIsValid() && @@ -127,17 +156,18 @@ bool PluginStream::WriteToFile(const char *buf, size_t length) { return true; } -bool PluginStream::WriteToPlugin(const char *buf, const int length, +bool PluginStream::WriteToPlugin(const char* buf, const int length, const int data_offset) { // For NORMAL and ASFILE modes, we send the data to the plugin now if (requested_plugin_mode_ != NP_NORMAL && requested_plugin_mode_ != NP_ASFILE && - requested_plugin_mode_ != NP_SEEK) + requested_plugin_mode_ != NP_SEEK) { return true; + } int written = TryWriteToPlugin(buf, length, data_offset); if (written == -1) - return false; + return false; if (written < length) { // Buffer the remaining data. @@ -156,9 +186,8 @@ bool PluginStream::WriteToPlugin(const char *buf, const int length, void PluginStream::OnDelayDelivery() { // It is possible that the plugin stream may have closed before the task // was hit. - if (!opened_) { + if (!opened_) return; - } int size = static_cast<int>(delivery_data_.size()); int written = TryWriteToPlugin(&delivery_data_.front(), size, @@ -170,7 +199,7 @@ void PluginStream::OnDelayDelivery() { } } -int PluginStream::TryWriteToPlugin(const char *buf, const int length, +int PluginStream::TryWriteToPlugin(const char* buf, const int length, const int data_offset) { int byte_offset = 0; diff --git a/webkit/plugins/npapi/plugin_stream.h b/webkit/plugins/npapi/plugin_stream.h index 139b7a7..2c18a1e 100644 --- a/webkit/plugins/npapi/plugin_stream.h +++ b/webkit/plugins/npapi/plugin_stream.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -25,10 +25,10 @@ class PluginStream : public base::RefCounted<PluginStream> { public: // Create a new PluginStream object. If needNotify is true, then the // plugin will be notified when the stream has been fully sent. - PluginStream(PluginInstance *instance, - const char *url, + PluginStream(PluginInstance* instance, + const char* url, bool need_notify, - void *notify_data); + void* notify_data); // In case of a redirect, this can be called to update the url. But it must // be called before Open(). @@ -48,7 +48,7 @@ class PluginStream : public base::RefCounted<PluginStream> { bool request_is_seekable); // Writes to the stream. - int Write(const char *buf, const int len, int data_offset); + int Write(const char* buf, const int len, int data_offset); // Write the result as a file. void WriteAsFile(); @@ -84,6 +84,7 @@ class PluginStream : public base::RefCounted<PluginStream> { virtual ~PluginStream(); PluginInstance* instance() { return instance_.get(); } + // Check if the stream is open. bool open() { return opened_; } @@ -93,6 +94,8 @@ class PluginStream : public base::RefCounted<PluginStream> { std::string pending_redirect_url_; private: + // Per platform method to reset the temporary file name and handle. + void ResetTempFilenameAndHandle(); // Open a temporary file for this stream. // If successful, will set temp_file_name_, temp_file_handle_, and @@ -103,32 +106,32 @@ class PluginStream : public base::RefCounted<PluginStream> { void CloseTempFile(); // Sends the data to the file. Called From WriteToFile. - size_t WriteBytes(const char *buf, size_t length); + size_t WriteBytes(const char* buf, size_t length); // Sends the data to the file if it's open. - bool WriteToFile(const char *buf, size_t length); + bool WriteToFile(const char* buf, size_t length); // Sends the data to the plugin. If it's not ready, handles buffering it // and retrying later. - bool WriteToPlugin(const char *buf, const int length, const int data_offset); + bool WriteToPlugin(const char* buf, const int length, const int data_offset); // Send the data to the plugin, returning how many bytes it accepted, or -1 // if an error occurred. - int TryWriteToPlugin(const char *buf, const int length, + int TryWriteToPlugin(const char* buf, const int length, const int data_offset); // The callback which calls TryWriteToPlugin. void OnDelayDelivery(); // Returns true if the temp file is valid and open for writing. - bool TempFileIsValid(); + bool TempFileIsValid() const; private: NPStream stream_; std::string headers_; scoped_refptr<PluginInstance> instance_; bool notify_needed_; - void * notify_data_; + void* notify_data_; bool close_on_write_data_; uint16 requested_plugin_mode_; bool opened_; diff --git a/webkit/plugins/npapi/plugin_stream_posix.cc b/webkit/plugins/npapi/plugin_stream_posix.cc index 7a89ff1..fd275a2 100644 --- a/webkit/plugins/npapi/plugin_stream_posix.cc +++ b/webkit/plugins/npapi/plugin_stream_posix.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -14,61 +14,46 @@ namespace webkit { namespace npapi { -PluginStream::PluginStream( - PluginInstance *instance, - const char *url, - bool need_notify, - void *notify_data) - : instance_(instance), - notify_needed_(need_notify), - notify_data_(notify_data), - close_on_write_data_(false), - requested_plugin_mode_(NP_NORMAL), - opened_(false), - temp_file_(NULL), - temp_file_path_(), - data_offset_(0), - seekable_stream_(false) { - memset(&stream_, 0, sizeof(stream_)); - stream_.url = strdup(url); -} - -void PluginStream::UpdateUrl(const char* url) { - DCHECK(!opened_); - free(const_cast<char*>(stream_.url)); - stream_.url = strdup(url); +void PluginStream::ResetTempFilenameAndHandle() { + temp_file_path_ = FilePath(); + temp_file_ = NULL; } void PluginStream::WriteAsFile() { if (requested_plugin_mode_ == NP_ASFILE || - requested_plugin_mode_ == NP_ASFILEONLY) + requested_plugin_mode_ == NP_ASFILEONLY) { instance_->NPP_StreamAsFile(&stream_, temp_file_path_.value().c_str()); + } } -size_t PluginStream::WriteBytes(const char *buf, size_t length) { +size_t PluginStream::WriteBytes(const char* buf, size_t length) { return fwrite(buf, sizeof(char), length, temp_file_); } bool PluginStream::OpenTempFile() { - DCHECK(temp_file_ == NULL); + DCHECK_EQ(static_cast<FILE*>(NULL), temp_file_); if (file_util::CreateTemporaryFile(&temp_file_path_)) temp_file_ = file_util::OpenFile(temp_file_path_, "a"); if (!temp_file_) { - temp_file_path_ = FilePath(""); + file_util::Delete(temp_file_path_, false); + temp_file_path_ = FilePath(); return false; } - return true; } void PluginStream::CloseTempFile() { + if (!TempFileIsValid()) + return; + file_util::CloseFile(temp_file_); - temp_file_ = NULL; + file_util::Delete(temp_file_path_, false); + ResetTempFilenameAndHandle(); } -bool PluginStream::TempFileIsValid() { +bool PluginStream::TempFileIsValid() const { return temp_file_ != NULL; } diff --git a/webkit/plugins/npapi/plugin_stream_win.cc b/webkit/plugins/npapi/plugin_stream_win.cc index edc9770..62ecc11d9 100644 --- a/webkit/plugins/npapi/plugin_stream_win.cc +++ b/webkit/plugins/npapi/plugin_stream_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -10,32 +10,11 @@ namespace webkit { namespace npapi { -PluginStream::PluginStream( - PluginInstance *instance, - const char *url, - bool need_notify, - void *notify_data) - : instance_(instance), - notify_needed_(need_notify), - notify_data_(notify_data), - close_on_write_data_(false), - opened_(false), - requested_plugin_mode_(NP_NORMAL), - temp_file_handle_(INVALID_HANDLE_VALUE), - seekable_stream_(false), - data_offset_(0) { - memset(&stream_, 0, sizeof(stream_)); - stream_.url = _strdup(url); +void PluginStream::ResetTempFilenameAndHandle() { + temp_file_handle_ = INVALID_HANDLE_VALUE; temp_file_name_[0] = '\0'; } -void PluginStream::UpdateUrl(const char* url) { - DCHECK(!opened_); - free(const_cast<char*>(stream_.url)); - stream_.url = _strdup(url); - pending_redirect_url_.clear(); -} - void PluginStream::WriteAsFile() { if (requested_plugin_mode_ == NP_ASFILE || requested_plugin_mode_ == NP_ASFILEONLY) @@ -52,7 +31,7 @@ size_t PluginStream::WriteBytes(const char *buf, size_t length) { } bool PluginStream::OpenTempFile() { - DCHECK(temp_file_handle_ == INVALID_HANDLE_VALUE); + DCHECK_EQ(INVALID_HANDLE_VALUE, temp_file_handle_); // The reason for using all the Ascii versions of these filesystem // calls is that the filename which we pass back to the plugin @@ -85,13 +64,15 @@ bool PluginStream::OpenTempFile() { } void PluginStream::CloseTempFile() { - if (temp_file_handle_ != INVALID_HANDLE_VALUE) { - CloseHandle(temp_file_handle_); - temp_file_handle_ = INVALID_HANDLE_VALUE; - } + if (!TempFileIsValid()) + return; + + CloseHandle(temp_file_handle_); + DeleteFileA(temp_file_name_); + ResetTempFilenameAndHandle(); } -bool PluginStream::TempFileIsValid() { +bool PluginStream::TempFileIsValid() const { return temp_file_handle_ != INVALID_HANDLE_VALUE; } |