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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_CHILD_NPAPI_PLUGIN_STREAM_H_ 6 #define CONTENT_CHILD_NPAPI_PLUGIN_STREAM_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/files/file_path.h" 12 #include "base/memory/ref_counted.h" 13 #include "build/build_config.h" 14 #include "third_party/npapi/bindings/npapi.h" 15 16 namespace content { 17 18 class PluginInstance; 19 class WebPluginResourceClient; 20 21 // Base class for a NPAPI stream. Tracks basic elements 22 // of a stream for NPAPI notifications and stream position. 23 class PluginStream : public base::RefCounted<PluginStream> { 24 public: 25 // Create a new PluginStream object. If needNotify is true, then the 26 // plugin will be notified when the stream has been fully sent. 27 PluginStream(PluginInstance* instance, 28 const char* url, 29 bool need_notify, 30 void* notify_data); 31 32 // In case of a redirect, this can be called to update the url. But it must 33 // be called before Open(). 34 void UpdateUrl(const char* url); 35 36 // Opens the stream to the Plugin. 37 // If the mime-type is not specified, we'll try to find one based on the 38 // mime-types table and the extension (if any) in the URL. 39 // If the size of the stream is known, use length to set the size. If 40 // not known, set length to 0. 41 // The request_is_seekable parameter indicates whether byte range requests 42 // can be issued on the stream. 43 bool Open(const std::string &mime_type, 44 const std::string &headers, 45 uint32 length, 46 uint32 last_modified, 47 bool request_is_seekable); 48 49 // Writes to the stream. 50 int Write(const char* buf, const int len, int data_offset); 51 52 // Write the result as a file. 53 void WriteAsFile(); 54 55 // Notify the plugin that a stream is complete. 56 void Notify(NPReason reason); 57 58 // Close the stream. 59 virtual bool Close(NPReason reason); 60 61 virtual WebPluginResourceClient* AsResourceClient(); 62 63 // Cancels any HTTP requests initiated by the stream. 64 virtual void CancelRequest() {} 65 66 const NPStream* stream() const { return &stream_; } 67 68 // setter/getter for the seekable attribute on the stream. 69 bool seekable() const { return seekable_stream_; } 70 71 void set_seekable(bool seekable) { seekable_stream_ = seekable; } 72 73 // getters for reading the notification related attributes on the stream. 74 bool notify_needed() const { return notify_needed_; } 75 76 void* notify_data() const { return notify_data_; } 77 78 std::string pending_redirect_url() const { return pending_redirect_url_; } 79 80 protected: 81 friend class base::RefCounted<PluginStream>; 82 83 virtual ~PluginStream(); 84 85 PluginInstance* instance() { return instance_.get(); } 86 87 // Check if the stream is open. 88 bool open() { return opened_; } 89 90 // If the plugin participates in HTTP URL redirect handling then this member 91 // holds the url being redirected to while we wait for the plugin to make a 92 // decision on whether to allow or deny the redirect. 93 std::string pending_redirect_url_; 94 95 private: 96 // Per platform method to reset the temporary file handle. 97 void ResetTempFileHandle(); 98 99 // Per platform method to reset the temporary file name. 100 void ResetTempFileName(); 101 102 // Open a temporary file for this stream. 103 // If successful, will set temp_file_name_, temp_file_handle_, and 104 // return true. 105 bool OpenTempFile(); 106 107 // Closes the temporary file if it is open. 108 void CloseTempFile(); 109 110 // Sends the data to the file. Called From WriteToFile. 111 size_t WriteBytes(const char* buf, size_t length); 112 113 // Sends the data to the file if it's open. 114 bool WriteToFile(const char* buf, size_t length); 115 116 // Sends the data to the plugin. If it's not ready, handles buffering it 117 // and retrying later. 118 bool WriteToPlugin(const char* buf, const int length, const int data_offset); 119 120 // Send the data to the plugin, returning how many bytes it accepted, or -1 121 // if an error occurred. 122 int TryWriteToPlugin(const char* buf, const int length, 123 const int data_offset); 124 125 // The callback which calls TryWriteToPlugin. 126 void OnDelayDelivery(); 127 128 // Returns true if the temp file is valid and open for writing. 129 bool TempFileIsValid() const; 130 131 // Returns true if |requested_plugin_mode_| is NP_ASFILE or NP_ASFILEONLY. 132 bool RequestedPluginModeIsAsFile() const; 133 134 private: 135 NPStream stream_; 136 std::string headers_; 137 scoped_refptr<PluginInstance> instance_; 138 bool notify_needed_; 139 void* notify_data_; 140 bool close_on_write_data_; 141 uint16 requested_plugin_mode_; 142 bool opened_; 143 #if defined(OS_WIN) 144 char temp_file_name_[MAX_PATH]; 145 HANDLE temp_file_handle_; 146 #elif defined(OS_POSIX) 147 FILE* temp_file_; 148 base::FilePath temp_file_path_; 149 #endif 150 std::vector<char> delivery_data_; 151 int data_offset_; 152 bool seekable_stream_; 153 std::string mime_type_; 154 DISALLOW_COPY_AND_ASSIGN(PluginStream); 155 }; 156 157 } // namespace content 158 159 #endif // CONTENT_CHILD_NPAPI_PLUGIN_STREAM_H_ 160