PluginStream.cpp revision 635860845790a19bf50bbc51ba8fb66a96dde068
18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/*
28e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Copyright (C) 2008 Collabora, Ltd. All rights reserved.
48e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without
68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions
78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met:
88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    documentation and/or other materials provided with the distribution.
138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */
268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "config.h"
288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "PluginStream.h"
298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "CString.h"
318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "DocumentLoader.h"
328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "Frame.h"
338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "FrameLoader.h"
348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "PluginDebug.h"
358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "SharedBuffer.h"
368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "SubresourceLoader.h"
378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// We use -2 here because some plugins like to return -1 to indicate error
398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// and this way we won't clash with them.
408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectstatic const int WebReasonNone = -2;
418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectusing std::max;
438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectusing std::min;
448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectnamespace WebCore {
468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projecttypedef HashMap<NPStream*, NPP> StreamMap;
488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectstatic StreamMap& streams()
498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static StreamMap staticStreams;
518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return staticStreams;
528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectPluginStream::PluginStream(PluginStreamClient* client, Frame* frame, const ResourceRequest& resourceRequest, bool sendNotification, void* notifyData, const NPPluginFuncs* pluginFuncs, NPP instance, const PluginQuirkSet& quirks)
558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    : m_resourceRequest(resourceRequest)
568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_client(client)
578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_frame(frame)
588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_notifyData(notifyData)
598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_sendNotification(sendNotification)
608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_streamState(StreamBeforeStarted)
618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_loadManually(false)
628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_delayDeliveryTimer(this, &PluginStream::delayDeliveryTimerFired)
638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_deliveryData(0)
648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_tempFileHandle(invalidPlatformFileHandle)
658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_pluginFuncs(pluginFuncs)
668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_instance(instance)
678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    , m_quirks(quirks)
688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_instance);
708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.url = 0;
728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.ndata = 0;
738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.pdata = 0;
748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.end = 0;
758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.notifyData = 0;
768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.lastmodified = 0;
778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    streams().add(&m_stream, m_instance);
798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectPluginStream::~PluginStream()
828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_streamState != StreamStarted);
848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(!m_loader);
858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    free((char*)m_stream.url);
878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    streams().remove(&m_stream);
898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::start()
928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(!m_loadManually);
948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_loader = NetscapePlugInStreamLoader::create(m_frame, this);
968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_loader->setShouldBufferData(false);
988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_loader->documentLoader()->addPlugInStreamLoader(m_loader.get());
998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_loader->load(m_resourceRequest);
1008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
1018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::stop()
1038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
1048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_streamState = StreamStopped;
1058e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1068e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_loadManually) {
1078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        ASSERT(!m_loader);
1088e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1098e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        DocumentLoader* documentLoader = m_frame->loader()->activeDocumentLoader();
1108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        ASSERT(documentLoader);
1118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (documentLoader->isLoadingMainResource())
1138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            documentLoader->cancelMainResourceLoad(m_frame->loader()->cancelledError(m_resourceRequest));
1148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
1168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
1178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_loader) {
1198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_loader->cancel();
1208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_loader = 0;
1218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
1228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
1238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::startStream()
1258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
1268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_streamState == StreamBeforeStarted);
1278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    const KURL& responseURL = m_resourceResponse.url();
1298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // Some plugins (Flash) expect that javascript URLs are passed back decoded as this is the
1318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // format used when requesting the URL.
1328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (responseURL.protocolIs("javascript"))
1338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_stream.url = strdup(decodeURLEscapeSequences(responseURL.string()).utf8().data());
1348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    else
1358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_stream.url = strdup(responseURL.string().utf8().data());
1368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    CString mimeTypeStr = m_resourceResponse.mimeType().utf8();
1388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    long long expectedContentLength = m_resourceResponse.expectedContentLength();
1408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_resourceResponse.isHTTP()) {
1428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        Vector<UChar> stringBuilder;
1438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        String separator(": ");
1448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        String statusLine = String::format("HTTP %lu OK\n", m_resourceResponse.httpStatusCode());
1468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        stringBuilder.append(statusLine.characters(), statusLine.length());
1488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        HTTPHeaderMap::const_iterator end = m_resourceResponse.httpHeaderFields().end();
1508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        for (HTTPHeaderMap::const_iterator it = m_resourceResponse.httpHeaderFields().begin(); it != end; ++it) {
1518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            stringBuilder.append(it->first.characters(), it->first.length());
1528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            stringBuilder.append(separator.characters(), separator.length());
1538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            stringBuilder.append(it->second.characters(), it->second.length());
1548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            stringBuilder.append('\n');
1558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
1568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_headers = String::adopt(stringBuilder).utf8();
1588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // If the content is encoded (most likely compressed), then don't send its length to the plugin,
1608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // which is only interested in the decoded length, not yet known at the moment.
1618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // <rdar://problem/4470599> tracks a request for -[NSURLResponse expectedContentLength] to incorporate this logic.
1628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        String contentEncoding = m_resourceResponse.httpHeaderField("Content-Encoding");
1638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (!contentEncoding.isNull() && contentEncoding != "identity")
1648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            expectedContentLength = -1;
1658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
1668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.headers = m_headers.data();
1688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.pdata = 0;
1698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.ndata = this;
1708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.end = max(expectedContentLength, 0LL);
1718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.lastmodified = m_resourceResponse.lastModifiedDate();
1728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_stream.notifyData = m_notifyData;
1738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_transferMode = NP_NORMAL;
1758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_offset = 0;
1768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_reason = WebReasonNone;
1778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // Protect the stream if destroystream is called from within the newstream handler
1798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    RefPtr<PluginStream> protect(this);
1808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // calling into a plug-in could result in re-entrance if the plug-in yields
1828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // control to the system (rdar://5744899). prevent this by deferring further
1838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // loading while calling into the plug-in.
1848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_loader)
1858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_loader->setDefersLoading(true);
1868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    NPError npErr = m_pluginFuncs->newstream(m_instance, (NPMIMEType)mimeTypeStr.data(), &m_stream, false, &m_transferMode);
1878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_loader)
1888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_loader->setDefersLoading(false);
1898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // If the stream was destroyed in the call to newstream we return
1918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_reason != WebReasonNone)
1928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
1938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (npErr != NPERR_NO_ERROR) {
1958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        cancelAndDestroyStream(npErr);
1968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
1978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
1988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_streamState = StreamStarted;
2008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_transferMode == NP_NORMAL)
2028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
2038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_path = openTemporaryFile("WKP", m_tempFileHandle);
2058e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2068e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // Something went wrong, cancel loading the stream
2078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!isHandleValid(m_tempFileHandle))
2088e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        cancelAndDestroyStream(NPRES_NETWORK_ERR);
2098e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectNPP PluginStream::ownerForStream(NPStream* stream)
2128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return streams().get(stream);
2148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::cancelAndDestroyStream(NPReason reason)
2178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    RefPtr<PluginStream> protect(this);
2198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    destroyStream(reason);
2218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    stop();
2228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::destroyStream(NPReason reason)
2258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_reason = reason;
2278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_reason != NPRES_DONE) {
2288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // Stop any pending data from being streamed
2298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (m_deliveryData)
2308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_deliveryData->resize(0);
2318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    } else if (m_deliveryData && m_deliveryData->size() > 0) {
2328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // There is more data to be streamed, don't destroy the stream now.
2338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
2348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
2358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    destroyStream();
2368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::destroyStream()
2398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_streamState == StreamStopped)
2418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
2428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_reason != WebReasonNone);
2448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(!m_deliveryData || m_deliveryData->size() == 0);
2458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    closeFile(m_tempFileHandle);
2478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    bool newStreamCalled = m_stream.ndata;
2498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (newStreamCalled) {
2518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (m_reason == NPRES_DONE && (m_transferMode == NP_ASFILE || m_transferMode == NP_ASFILEONLY)) {
2528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            ASSERT(!m_path.isNull());
2538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            if (m_loader)
2558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                m_loader->setDefersLoading(true);
2568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_pluginFuncs->asfile(m_instance, &m_stream, m_path.data());
2578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            if (m_loader)
2588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                m_loader->setDefersLoading(false);
2598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
2608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (m_streamState != StreamBeforeStarted) {
2628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            if (m_loader)
2638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                m_loader->setDefersLoading(true);
2648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            NPError npErr = m_pluginFuncs->destroystream(m_instance, &m_stream, m_reason);
2668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            if (m_loader)
2688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                m_loader->setDefersLoading(false);
2698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            LOG_NPERROR(npErr);
2718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
2728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_stream.ndata = 0;
2748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
2758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_sendNotification) {
2778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // Flash 9 can dereference null if we call NPP_URLNotify without first calling NPP_NewStream
2788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // for requests made with NPN_PostURLNotify; see <rdar://5588807>
2798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (m_loader)
2808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_loader->setDefersLoading(true);
2818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (!newStreamCalled && m_quirks.contains(PluginQuirkFlashURLNotifyBug) &&
2828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            equalIgnoringCase(m_resourceRequest.httpMethod(), "POST")) {
2838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            // Protect the stream if NPN_DestroyStream is called from NPP_NewStream
2848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            RefPtr<PluginStream> protect(this);
2858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_transferMode = NP_NORMAL;
2878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_stream.url = "";
2888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_stream.notifyData = m_notifyData;
2898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
290635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project            static char emptyMimeType[] = "";
291635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project            m_pluginFuncs->newstream(m_instance, emptyMimeType, &m_stream, false, &m_transferMode);
2928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_pluginFuncs->destroystream(m_instance, &m_stream, m_reason);
2938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            // in successful requests, the URL is dynamically allocated and freed in our
2958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            // destructor, so reset it to 0
2968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_stream.url = 0;
2978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
2988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_pluginFuncs->urlnotify(m_instance, m_resourceRequest.url().string().utf8().data(), m_reason, m_notifyData);
2998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (m_loader)
3008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_loader->setDefersLoading(false);
3018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
3028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_streamState = StreamStopped;
3048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3058e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // streamDidFinishLoading can cause us to be deleted.
3068e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    RefPtr<PluginStream> protect(this);
3078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!m_loadManually)
3088e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_client->streamDidFinishLoading(this);
3098e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!m_path.isNull()) {
3118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        String tempFilePath = String::fromUTF8(m_path.data());
3128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        deleteFile(tempFilePath);
3138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
3148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
3158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::delayDeliveryTimerFired(Timer<PluginStream>* timer)
3178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
3188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(timer == &m_delayDeliveryTimer);
3198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    deliverData();
3218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
3228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::deliverData()
3248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
3258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_deliveryData);
3268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_streamState == StreamStopped)
3288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // FIXME: We should cancel our job in the SubresourceLoader on error so we don't reach this case
3298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
3308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_streamState != StreamBeforeStarted);
3328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!m_stream.ndata || m_deliveryData->size() == 0)
3348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
3358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    int32 totalBytes = m_deliveryData->size();
3378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    int32 totalBytesDelivered = 0;
3388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_loader)
3408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_loader->setDefersLoading(true);
3418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    while (totalBytesDelivered < totalBytes) {
3428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        int32 deliveryBytes = m_pluginFuncs->writeready(m_instance, &m_stream);
3438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (deliveryBytes <= 0) {
3458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_delayDeliveryTimer.startOneShot(0);
3468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            break;
3478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        } else {
3488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            deliveryBytes = min(deliveryBytes, totalBytes - totalBytesDelivered);
3498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            int32 dataLength = deliveryBytes;
3508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            char* data = m_deliveryData->data() + totalBytesDelivered;
3518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            // Write the data
3538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            deliveryBytes = m_pluginFuncs->write(m_instance, &m_stream, m_offset, dataLength, (void*)data);
3548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            if (deliveryBytes < 0) {
3558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                LOG_PLUGIN_NET_ERROR();
3568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                cancelAndDestroyStream(NPRES_NETWORK_ERR);
3578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                return;
3588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            }
3598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            deliveryBytes = min(deliveryBytes, dataLength);
3608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_offset += deliveryBytes;
3618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            totalBytesDelivered += deliveryBytes;
3628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
3638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
3648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_loader)
3658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_loader->setDefersLoading(false);
3668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (totalBytesDelivered > 0) {
3688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (totalBytesDelivered < totalBytes) {
3698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            int remainingBytes = totalBytes - totalBytesDelivered;
3708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            memmove(m_deliveryData->data(), m_deliveryData->data() + totalBytesDelivered, remainingBytes);
3718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_deliveryData->resize(remainingBytes);
3728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        } else {
3738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_deliveryData->resize(0);
3748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            if (m_reason != WebReasonNone)
3758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project                destroyStream();
3768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
3778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
3788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
3798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::sendJavaScriptStream(const KURL& requestURL, const CString& resultString)
3818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
3828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    didReceiveResponse(0, ResourceResponse(requestURL, "text/plain", resultString.length(), "", ""));
3838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_streamState == StreamStopped)
3858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return;
3868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!resultString.isNull()) {
3888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        didReceiveData(0, resultString.data(), resultString.length());
3898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (m_streamState == StreamStopped)
3908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            return;
3918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
3928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_loader = 0;
3948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    destroyStream(resultString.isNull() ? NPRES_NETWORK_ERR : NPRES_DONE);
3968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
3978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
3988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::didReceiveResponse(NetscapePlugInStreamLoader* loader, const ResourceResponse& response)
3998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
4008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(loader == m_loader);
4018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_streamState == StreamBeforeStarted);
4028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_resourceResponse = response;
4048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4058e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    startStream();
4068e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
4078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4088e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::didReceiveData(NetscapePlugInStreamLoader* loader, const char* data, int length)
4098e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
4108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(loader == m_loader);
4118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(length > 0);
4128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_streamState == StreamStarted);
4138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // If the plug-in cancels the stream in deliverData it could be deleted,
4158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // so protect it here.
4168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    RefPtr<PluginStream> protect(this);
4178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_transferMode != NP_ASFILEONLY) {
4198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (!m_deliveryData)
4208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            m_deliveryData.set(new Vector<char>);
4218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        int oldSize = m_deliveryData->size();
4238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        m_deliveryData->resize(oldSize + length);
4248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        memcpy(m_deliveryData->data() + oldSize, data, length);
4258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        deliverData();
4278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
4288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_streamState != StreamStopped && isHandleValid(m_tempFileHandle)) {
4308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        int bytesWritten = writeToFile(m_tempFileHandle, data, length);
4318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (bytesWritten != length)
4328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            cancelAndDestroyStream(NPRES_NETWORK_ERR);
4338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
4348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
4358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::didFail(NetscapePlugInStreamLoader* loader, const ResourceError&)
4378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
4388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(loader == m_loader);
4398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    LOG_PLUGIN_NET_ERROR();
4418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // destroyStream can result in our being deleted
4438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    RefPtr<PluginStream> protect(this);
4448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    destroyStream(NPRES_NETWORK_ERR);
4468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_loader = 0;
4488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
4498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectvoid PluginStream::didFinishLoading(NetscapePlugInStreamLoader* loader)
4518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
4528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(loader == m_loader);
4538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT(m_streamState == StreamStarted);
4548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // destroyStream can result in our being deleted
4568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    RefPtr<PluginStream> protect(this);
4578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    destroyStream(NPRES_DONE);
4598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    m_loader = 0;
4618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
4628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectbool PluginStream::wantsAllStreams() const
4648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
4658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!m_pluginFuncs->getvalue)
4668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return false;
4678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    void* result = 0;
4698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (m_pluginFuncs->getvalue(m_instance, NPPVpluginWantsAllNetworkStreams, &result) != NPERR_NO_ERROR)
4708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return false;
4718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return result != 0;
4738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
4748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
4758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
476