Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / npapi / plugin_stream.h
blob644e16c681b3bcec40bca00a97a40309cfdf12da
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.
5 #ifndef CONTENT_CHILD_NPAPI_PLUGIN_STREAM_H_
6 #define CONTENT_CHILD_NPAPI_PLUGIN_STREAM_H_
8 #include <string>
9 #include <vector>
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"
16 namespace content {
18 class PluginInstance;
19 class WebPluginResourceClient;
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);
32 // Opens the stream to the Plugin.
33 // If the mime-type is not specified, we'll try to find one based on the
34 // mime-types table and the extension (if any) in the URL.
35 // If the size of the stream is known, use length to set the size. If
36 // not known, set length to 0.
37 // The request_is_seekable parameter indicates whether byte range requests
38 // can be issued on the stream.
39 bool Open(const std::string &mime_type,
40 const std::string &headers,
41 uint32 length,
42 uint32 last_modified,
43 bool request_is_seekable);
45 // Writes to the stream.
46 int Write(const char* buf, const int len, int data_offset);
48 // Write the result as a file.
49 void WriteAsFile();
51 // Notify the plugin that a stream is complete.
52 void Notify(NPReason reason);
54 // Close the stream.
55 virtual bool Close(NPReason reason);
57 virtual WebPluginResourceClient* AsResourceClient();
59 // Cancels any HTTP requests initiated by the stream.
60 virtual void CancelRequest() {}
62 NPStream* stream() { return &stream_; }
64 PluginInstance* instance() { return instance_.get(); }
66 // setter/getter for the seekable attribute on the stream.
67 bool seekable() const { return seekable_stream_; }
69 void set_seekable(bool seekable) { seekable_stream_ = seekable; }
71 // getters for reading the notification related attributes on the stream.
72 bool notify_needed() const { return notify_needed_; }
74 void* notify_data() const { return notify_data_; }
76 protected:
77 friend class base::RefCounted<PluginStream>;
79 virtual ~PluginStream();
81 // Check if the stream is open.
82 bool open() { return opened_; }
84 private:
85 // Per platform method to reset the temporary file handle.
86 void ResetTempFileHandle();
88 // Per platform method to reset the temporary file name.
89 void ResetTempFileName();
91 // Open a temporary file for this stream.
92 // If successful, will set temp_file_name_, temp_file_handle_, and
93 // return true.
94 bool OpenTempFile();
96 // Closes the temporary file if it is open.
97 void CloseTempFile();
99 // Sends the data to the file. Called From WriteToFile.
100 size_t WriteBytes(const char* buf, size_t length);
102 // Sends the data to the file if it's open.
103 bool WriteToFile(const char* buf, size_t length);
105 // Sends the data to the plugin. If it's not ready, handles buffering it
106 // and retrying later.
107 bool WriteToPlugin(const char* buf, const int length, const int data_offset);
109 // Send the data to the plugin, returning how many bytes it accepted, or -1
110 // if an error occurred.
111 int TryWriteToPlugin(const char* buf, const int length,
112 const int data_offset);
114 // The callback which calls TryWriteToPlugin.
115 void OnDelayDelivery();
117 // Returns true if the temp file is valid and open for writing.
118 bool TempFileIsValid() const;
120 // Returns true if |requested_plugin_mode_| is NP_ASFILE or NP_ASFILEONLY.
121 bool RequestedPluginModeIsAsFile() const;
123 private:
124 NPStream stream_;
125 std::string headers_;
126 scoped_refptr<PluginInstance> instance_;
127 bool notify_needed_;
128 void* notify_data_;
129 bool close_on_write_data_;
130 uint16 requested_plugin_mode_;
131 bool opened_;
132 #if defined(OS_WIN)
133 char temp_file_name_[MAX_PATH];
134 HANDLE temp_file_handle_;
135 #elif defined(OS_POSIX)
136 FILE* temp_file_;
137 base::FilePath temp_file_path_;
138 #endif
139 std::vector<char> delivery_data_;
140 int data_offset_;
141 bool seekable_stream_;
142 std::string mime_type_;
143 DISALLOW_COPY_AND_ASSIGN(PluginStream);
146 } // namespace content
148 #endif // CONTENT_CHILD_NPAPI_PLUGIN_STREAM_H_