• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // 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);
44 
45   // Writes to the stream.
46   int Write(const char* buf, const int len, int data_offset);
47 
48   // Write the result as a file.
49   void WriteAsFile();
50 
51   // Notify the plugin that a stream is complete.
52   void Notify(NPReason reason);
53 
54   // Close the stream.
55   virtual bool Close(NPReason reason);
56 
57   virtual WebPluginResourceClient* AsResourceClient();
58 
59   // Cancels any HTTP requests initiated by the stream.
CancelRequest()60   virtual void CancelRequest() {}
61 
stream()62   NPStream* stream() { return &stream_; }
63 
instance()64   PluginInstance* instance() { return instance_.get(); }
65 
66   // setter/getter for the seekable attribute on the stream.
seekable()67   bool seekable() const { return seekable_stream_; }
68 
set_seekable(bool seekable)69   void set_seekable(bool seekable) { seekable_stream_ = seekable; }
70 
71   // getters for reading the notification related attributes on the stream.
notify_needed()72   bool notify_needed() const { return notify_needed_; }
73 
notify_data()74   void* notify_data() const { return notify_data_; }
75 
76  protected:
77   friend class base::RefCounted<PluginStream>;
78 
79   virtual ~PluginStream();
80 
81   // Check if the stream is open.
open()82   bool open() { return opened_; }
83 
84  private:
85   // Per platform method to reset the temporary file handle.
86   void ResetTempFileHandle();
87 
88   // Per platform method to reset the temporary file name.
89   void ResetTempFileName();
90 
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();
95 
96   // Closes the temporary file if it is open.
97   void CloseTempFile();
98 
99   // Sends the data to the file. Called From WriteToFile.
100   size_t WriteBytes(const char* buf, size_t length);
101 
102   // Sends the data to the file if it's open.
103   bool WriteToFile(const char* buf, size_t length);
104 
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);
108 
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);
113 
114   // The callback which calls TryWriteToPlugin.
115   void OnDelayDelivery();
116 
117   // Returns true if the temp file is valid and open for writing.
118   bool TempFileIsValid() const;
119 
120   // Returns true if |requested_plugin_mode_| is NP_ASFILE or NP_ASFILEONLY.
121   bool RequestedPluginModeIsAsFile() const;
122 
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);
144 };
145 
146 }  // namespace content
147 
148 #endif  // CONTENT_CHILD_NPAPI_PLUGIN_STREAM_H_
149