Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | #include "BrowserStreamParent.h" |
michael@0 | 8 | #include "PluginInstanceParent.h" |
michael@0 | 9 | #include "nsNPAPIPlugin.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/unused.h" |
michael@0 | 12 | |
michael@0 | 13 | // How much data are we willing to send across the wire |
michael@0 | 14 | // in one chunk? |
michael@0 | 15 | static const int32_t kSendDataChunk = 0x4000; |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | namespace plugins { |
michael@0 | 19 | |
michael@0 | 20 | BrowserStreamParent::BrowserStreamParent(PluginInstanceParent* npp, |
michael@0 | 21 | NPStream* stream) |
michael@0 | 22 | : mNPP(npp) |
michael@0 | 23 | , mStream(stream) |
michael@0 | 24 | , mState(ALIVE) |
michael@0 | 25 | { |
michael@0 | 26 | mStream->pdata = static_cast<AStream*>(this); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | BrowserStreamParent::~BrowserStreamParent() |
michael@0 | 30 | { |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | bool |
michael@0 | 34 | BrowserStreamParent::AnswerNPN_RequestRead(const IPCByteRanges& ranges, |
michael@0 | 35 | NPError* result) |
michael@0 | 36 | { |
michael@0 | 37 | PLUGIN_LOG_DEBUG_FUNCTION; |
michael@0 | 38 | |
michael@0 | 39 | switch (mState) { |
michael@0 | 40 | case ALIVE: |
michael@0 | 41 | break; |
michael@0 | 42 | |
michael@0 | 43 | case DYING: |
michael@0 | 44 | *result = NPERR_GENERIC_ERROR; |
michael@0 | 45 | return true; |
michael@0 | 46 | |
michael@0 | 47 | default: |
michael@0 | 48 | NS_ERROR("Unexpected state"); |
michael@0 | 49 | return false; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | if (!mStream) |
michael@0 | 53 | return false; |
michael@0 | 54 | |
michael@0 | 55 | if (ranges.size() > INT32_MAX) |
michael@0 | 56 | return false; |
michael@0 | 57 | |
michael@0 | 58 | nsAutoArrayPtr<NPByteRange> rp(new NPByteRange[ranges.size()]); |
michael@0 | 59 | for (uint32_t i = 0; i < ranges.size(); ++i) { |
michael@0 | 60 | rp[i].offset = ranges[i].offset; |
michael@0 | 61 | rp[i].length = ranges[i].length; |
michael@0 | 62 | rp[i].next = &rp[i + 1]; |
michael@0 | 63 | } |
michael@0 | 64 | rp[ranges.size() - 1].next = nullptr; |
michael@0 | 65 | |
michael@0 | 66 | *result = mNPP->mNPNIface->requestread(mStream, rp); |
michael@0 | 67 | return true; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | bool |
michael@0 | 71 | BrowserStreamParent::RecvNPN_DestroyStream(const NPReason& reason) |
michael@0 | 72 | { |
michael@0 | 73 | switch (mState) { |
michael@0 | 74 | case ALIVE: |
michael@0 | 75 | break; |
michael@0 | 76 | |
michael@0 | 77 | case DYING: |
michael@0 | 78 | return true; |
michael@0 | 79 | |
michael@0 | 80 | default: |
michael@0 | 81 | NS_ERROR("Unexpected state"); |
michael@0 | 82 | return false; |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | mNPP->mNPNIface->destroystream(mNPP->mNPP, mStream, reason); |
michael@0 | 86 | return true; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | void |
michael@0 | 90 | BrowserStreamParent::NPP_DestroyStream(NPReason reason) |
michael@0 | 91 | { |
michael@0 | 92 | NS_ASSERTION(ALIVE == mState, "NPP_DestroyStream called twice?"); |
michael@0 | 93 | mState = DYING; |
michael@0 | 94 | unused << SendNPP_DestroyStream(reason); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | bool |
michael@0 | 98 | BrowserStreamParent::RecvStreamDestroyed() |
michael@0 | 99 | { |
michael@0 | 100 | if (DYING != mState) { |
michael@0 | 101 | NS_ERROR("Unexpected state"); |
michael@0 | 102 | return false; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | mStreamPeer = nullptr; |
michael@0 | 106 | |
michael@0 | 107 | mState = DELETING; |
michael@0 | 108 | return Send__delete__(this); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | int32_t |
michael@0 | 112 | BrowserStreamParent::WriteReady() |
michael@0 | 113 | { |
michael@0 | 114 | return kSendDataChunk; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | int32_t |
michael@0 | 118 | BrowserStreamParent::Write(int32_t offset, |
michael@0 | 119 | int32_t len, |
michael@0 | 120 | void* buffer) |
michael@0 | 121 | { |
michael@0 | 122 | PLUGIN_LOG_DEBUG_FUNCTION; |
michael@0 | 123 | |
michael@0 | 124 | NS_ASSERTION(ALIVE == mState, "Sending data after NPP_DestroyStream?"); |
michael@0 | 125 | NS_ASSERTION(len > 0, "Non-positive length to NPP_Write"); |
michael@0 | 126 | |
michael@0 | 127 | if (len > kSendDataChunk) |
michael@0 | 128 | len = kSendDataChunk; |
michael@0 | 129 | |
michael@0 | 130 | return SendWrite(offset, |
michael@0 | 131 | nsCString(static_cast<char*>(buffer), len), |
michael@0 | 132 | mStream->end) ? |
michael@0 | 133 | len : -1; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | void |
michael@0 | 137 | BrowserStreamParent::StreamAsFile(const char* fname) |
michael@0 | 138 | { |
michael@0 | 139 | PLUGIN_LOG_DEBUG_FUNCTION; |
michael@0 | 140 | |
michael@0 | 141 | NS_ASSERTION(ALIVE == mState, |
michael@0 | 142 | "Calling streamasfile after NPP_DestroyStream?"); |
michael@0 | 143 | |
michael@0 | 144 | // Make sure our stream survives until the plugin process tells us we've |
michael@0 | 145 | // been destroyed (until RecvStreamDestroyed() is called). Since we retain |
michael@0 | 146 | // mStreamPeer at most once, we won't get in trouble if StreamAsFile() is |
michael@0 | 147 | // called more than once. |
michael@0 | 148 | if (!mStreamPeer) { |
michael@0 | 149 | nsNPAPIPlugin::RetainStream(mStream, getter_AddRefs(mStreamPeer)); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | unused << SendNPP_StreamAsFile(nsCString(fname)); |
michael@0 | 153 | return; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | } // namespace plugins |
michael@0 | 157 | } // namespace mozilla |