| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 WEBKIT_GLUE_PLUGINS_PLUGIN_STREAM_H_ | |
| 6 #define WEBKIT_GLUE_PLUGINS_PLUGIN_STREAM_H_ | |
| 7 | |
| 8 #include "build/build_config.h" | |
| 9 | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #if defined(OS_POSIX) | |
| 14 #include "base/file_path.h" | |
| 15 #endif | |
| 16 #include "base/ref_counted.h" | |
| 17 #include "third_party/npapi/bindings/npapi.h" | |
| 18 | |
| 19 namespace webkit_glue { | |
| 20 class WebPluginResourceClient; | |
| 21 } | |
| 22 | |
| 23 namespace NPAPI { | |
| 24 | |
| 25 class PluginInstance; | |
| 26 | |
| 27 // Base class for a NPAPI stream. Tracks basic elements | |
| 28 // of a stream for NPAPI notifications and stream position. | |
| 29 class PluginStream : public base::RefCounted<PluginStream> { | |
| 30 public: | |
| 31 // Create a new PluginStream object. If needNotify is true, then the | |
| 32 // plugin will be notified when the stream has been fully sent. | |
| 33 PluginStream(PluginInstance *instance, | |
| 34 const char *url, | |
| 35 bool need_notify, | |
| 36 void *notify_data); | |
| 37 | |
| 38 // In case of a redirect, this can be called to update the url. But it must | |
| 39 // be called before Open(). | |
| 40 void UpdateUrl(const char* url); | |
| 41 | |
| 42 // Opens the stream to the Plugin. | |
| 43 // If the mime-type is not specified, we'll try to find one based on the | |
| 44 // mime-types table and the extension (if any) in the URL. | |
| 45 // If the size of the stream is known, use length to set the size. If | |
| 46 // not known, set length to 0. | |
| 47 // The request_is_seekable parameter indicates whether byte range requests | |
| 48 // can be issued on the stream. | |
| 49 bool Open(const std::string &mime_type, | |
| 50 const std::string &headers, | |
| 51 uint32 length, | |
| 52 uint32 last_modified, | |
| 53 bool request_is_seekable); | |
| 54 | |
| 55 // Writes to the stream. | |
| 56 int Write(const char *buf, const int len, int data_offset); | |
| 57 | |
| 58 // Write the result as a file. | |
| 59 void WriteAsFile(); | |
| 60 | |
| 61 // Notify the plugin that a stream is complete. | |
| 62 void Notify(NPReason reason); | |
| 63 | |
| 64 // Close the stream. | |
| 65 virtual bool Close(NPReason reason); | |
| 66 | |
| 67 virtual webkit_glue::WebPluginResourceClient* AsResourceClient(); | |
| 68 | |
| 69 // Cancels any HTTP requests initiated by the stream. | |
| 70 virtual void CancelRequest() {} | |
| 71 | |
| 72 const NPStream* stream() const { return &stream_; } | |
| 73 | |
| 74 // setter/getter for the seekable attribute on the stream. | |
| 75 bool seekable() const { return seekable_stream_; } | |
| 76 | |
| 77 void set_seekable(bool seekable) { seekable_stream_ = seekable; } | |
| 78 | |
| 79 // getters for reading the notification related attributes on the stream. | |
| 80 bool notify_needed() const { return notify_needed_; } | |
| 81 | |
| 82 void* notify_data() const { return notify_data_; } | |
| 83 | |
| 84 std::string pending_redirect_url() const { return pending_redirect_url_; } | |
| 85 | |
| 86 protected: | |
| 87 friend class base::RefCounted<PluginStream>; | |
| 88 | |
| 89 virtual ~PluginStream(); | |
| 90 | |
| 91 PluginInstance* instance() { return instance_.get(); } | |
| 92 // Check if the stream is open. | |
| 93 bool open() { return opened_; } | |
| 94 | |
| 95 // If the plugin participates in HTTP URL redirect handling then this member | |
| 96 // holds the url being redirected to while we wait for the plugin to make a | |
| 97 // decision on whether to allow or deny the redirect. | |
| 98 std::string pending_redirect_url_; | |
| 99 | |
| 100 private: | |
| 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(); | |
| 130 | |
| 131 private: | |
| 132 NPStream stream_; | |
| 133 std::string headers_; | |
| 134 scoped_refptr<PluginInstance> instance_; | |
| 135 bool notify_needed_; | |
| 136 void * notify_data_; | |
| 137 bool close_on_write_data_; | |
| 138 uint16 requested_plugin_mode_; | |
| 139 bool opened_; | |
| 140 #if defined(OS_WIN) | |
| 141 char temp_file_name_[MAX_PATH]; | |
| 142 HANDLE temp_file_handle_; | |
| 143 #elif defined(OS_POSIX) | |
| 144 FILE* temp_file_; | |
| 145 FilePath temp_file_path_; | |
| 146 #endif | |
| 147 std::vector<char> delivery_data_; | |
| 148 int data_offset_; | |
| 149 bool seekable_stream_; | |
| 150 std::string mime_type_; | |
| 151 DISALLOW_COPY_AND_ASSIGN(PluginStream); | |
| 152 }; | |
| 153 | |
| 154 } // namespace NPAPI | |
| 155 | |
| 156 #endif // WEBKIT_GLUE_PLUGINS_PLUGIN_STREAM_H_ | |
| OLD | NEW |