summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/npapi/plugin_stream.cc
blob: 5fcbace14f38d59f3900ca95dfb4db28a2f4736c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// 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.

// TODO : Support NP_ASFILEONLY mode
// TODO : Support NP_SEEK mode
// TODO : Support SEEKABLE=true in NewStream

#include "webkit/plugins/npapi/plugin_stream.h"

#include <algorithm>

#include "base/bind.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
#include "net/base/mime_util.h"
#include "webkit/plugins/npapi/plugin_instance.h"

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);
  ResetTempFileHandle();
  ResetTempFileName();
}

PluginStream::~PluginStream() {
  // always close our temporary files.
  CloseTempFile();
  free(const_cast<char*>(stream_.url));
}

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) {
  headers_ = headers;
  NPP id = instance_->npp();
  stream_.end = length;
  stream_.lastmodified = last_modified;
  stream_.pdata = 0;
  stream_.ndata = id->ndata;
  stream_.notifyData = notify_data_;
  if (!headers_.empty())
    stream_.headers = headers_.c_str();

  bool seekable_stream = false;
  if (request_is_seekable) {
    std::string headers_lc = StringToLowerASCII(headers);
    if (headers_lc.find("accept-ranges: bytes") != std::string::npos) {
      seekable_stream = true;
    }
  }

  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();
  } else {
    GURL gurl(stream_.url);

    base::FilePath path = base::FilePath::FromUTF8Unsafe(gurl.path());
    if (net::GetMimeTypeFromFile(path, &temp_mime_type))
      char_mime_type = temp_mime_type.c_str();
  }

  // Silverlight expects a valid mime type
  DCHECK_NE(0U, strlen(char_mime_type));
  NPError err = instance_->NPP_NewStream((NPMIMEType)char_mime_type,
                                         &stream_, seekable_stream,
                                         &requested_plugin_mode_);
  if (err != NPERR_NO_ERROR) {
    Notify(err);
    return false;
  }

  opened_ = true;

  if (requested_plugin_mode_ == NP_SEEK) {
    seekable_stream_ = true;
  }
  // 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 (RequestedPluginModeIsAsFile()) {
    if (OpenTempFile() == false) {
      return false;
    }
  }

  mime_type_ = char_mime_type;
  return true;
}

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
  // this function are that the plugin must consume at least as many
  // bytes as returned by the WriteReady call.  So, we will attempt to
  // write that many to both streams.  If we can't write that many bytes
  // to each stream, we'll return failure.

  DCHECK(opened_);
  if (WriteToFile(buffer, length) &&
      WriteToPlugin(buffer, length, data_offset)) {
    return length;
  }

  return -1;
}

bool PluginStream::WriteToFile(const char* buf, size_t length) {
  // For ASFILEONLY, ASFILE, and SEEK modes, we need to write
  // to the disk
  if (TempFileIsValid() && RequestedPluginModeIsAsFile()) {
    size_t totalBytesWritten = 0, bytes;
    do {
      bytes = WriteBytes(buf, length);
      totalBytesWritten += bytes;
    } while (bytes > 0U && totalBytesWritten < length);

    if (totalBytesWritten != length) {
      return false;
    }
  }

  return true;
}

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) {
    return true;
  }

  int written = TryWriteToPlugin(buf, length, data_offset);
  if (written == -1)
    return false;

  if (written < length) {
    // Buffer the remaining data.
    size_t remaining = length - written;
    size_t previous_size = delivery_data_.size();
    delivery_data_.resize(previous_size + remaining);
    data_offset_ = data_offset;
    memcpy(&delivery_data_[previous_size], buf + written, remaining);
    base::MessageLoop::current()->PostTask(
        FROM_HERE, base::Bind(&PluginStream::OnDelayDelivery, this));
  }

  return true;
}

void PluginStream::OnDelayDelivery() {
  // It is possible that the plugin stream may have closed before the task
  // was hit.
  if (!opened_)
    return;

  int size = static_cast<int>(delivery_data_.size());
  int written = TryWriteToPlugin(&delivery_data_.front(), size, data_offset_);
  if (written > 0) {
    // Remove the data that we already wrote.
    delivery_data_.erase(delivery_data_.begin(),
                         delivery_data_.begin() + written);
  }
}

int PluginStream::TryWriteToPlugin(const char* buf, const int length,
                                   const int data_offset) {
  int byte_offset = 0;

  if (data_offset > 0)
    data_offset_ = data_offset;

  while (byte_offset < length) {
    int bytes_remaining = length - byte_offset;
    int bytes_to_write = instance_->NPP_WriteReady(&stream_);
    if (bytes_to_write > bytes_remaining)
      bytes_to_write = bytes_remaining;

    if (bytes_to_write == 0)
      return byte_offset;

    int bytes_consumed = instance_->NPP_Write(
        &stream_, data_offset_, bytes_to_write,
        const_cast<char*>(buf + byte_offset));
    if (bytes_consumed < 0) {
      // The plugin failed, which means that we need to close the stream.
      Close(NPRES_NETWORK_ERR);
      return -1;
    }
    if (bytes_consumed == 0) {
      // The plugin couldn't take all of the data now.
      return byte_offset;
    }

    // The plugin might report more that we gave it.
    bytes_consumed = std::min(bytes_consumed, bytes_to_write);

    data_offset_ += bytes_consumed;
    byte_offset += bytes_consumed;
  }

  if (close_on_write_data_)
    Close(NPRES_DONE);

  return length;
}

bool PluginStream::Close(NPReason reason) {
  if (opened_ == true) {
    opened_ = false;

    if (delivery_data_.size()) {
      if (reason == NPRES_DONE) {
        // There is more data to be streamed, don't destroy the stream now.
        close_on_write_data_ = true;
        return true;
      } else {
        // Stop any pending data from being streamed
        delivery_data_.resize(0);
      }
    }

    // If we have a temp file, be sure to close it.
    // Also, allow the plugin to access it now.
    if (TempFileIsValid()) {
      CloseTempFile();
      if (reason == NPRES_DONE)
        WriteAsFile();
    }

    if (stream_.ndata != NULL) {
      // Stream hasn't been closed yet.
      NPError err = instance_->NPP_DestroyStream(&stream_, reason);
      DCHECK(err == NPERR_NO_ERROR);
    }
  }

  Notify(reason);
  return true;
}

WebPluginResourceClient* PluginStream::AsResourceClient() {
  return NULL;
}

void PluginStream::Notify(NPReason reason) {
  if (notify_needed_) {
    instance_->NPP_URLNotify(stream_.url, reason, notify_data_);
    notify_needed_ = false;
  }
}

bool PluginStream::RequestedPluginModeIsAsFile() const {
  return (requested_plugin_mode_ == NP_ASFILE ||
          requested_plugin_mode_ == NP_ASFILEONLY);
}

}  // namespace npapi
}  // namespace webkit