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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef mozilla_system_volumecommand_h__ |
michael@0 | 6 | #define mozilla_system_volumecommand_h__ |
michael@0 | 7 | |
michael@0 | 8 | #include "nsString.h" |
michael@0 | 9 | #include "nsISupportsImpl.h" |
michael@0 | 10 | #include "mozilla/RefPtr.h" |
michael@0 | 11 | #include <algorithm> |
michael@0 | 12 | #include <vold/ResponseCode.h> |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace system { |
michael@0 | 16 | |
michael@0 | 17 | class Volume; |
michael@0 | 18 | class VolumeCommand; |
michael@0 | 19 | |
michael@0 | 20 | /*************************************************************************** |
michael@0 | 21 | * |
michael@0 | 22 | * The VolumeResponseCallback class is an abstract base class. The ResponseReceived |
michael@0 | 23 | * method will be called for each response received. |
michael@0 | 24 | * |
michael@0 | 25 | * Depending on the command, there may be multiple responses for the |
michael@0 | 26 | * command. Done() will return true if this is the last response. |
michael@0 | 27 | * |
michael@0 | 28 | * The responses from vold are all of the form: |
michael@0 | 29 | * |
michael@0 | 30 | * <ResponseCode> <String> |
michael@0 | 31 | * |
michael@0 | 32 | * Valid Response codes can be found in the vold/ResponseCode.h header. |
michael@0 | 33 | * |
michael@0 | 34 | ***************************************************************************/ |
michael@0 | 35 | |
michael@0 | 36 | class VolumeResponseCallback |
michael@0 | 37 | { |
michael@0 | 38 | protected: |
michael@0 | 39 | virtual ~VolumeResponseCallback() {} |
michael@0 | 40 | |
michael@0 | 41 | public: |
michael@0 | 42 | NS_INLINE_DECL_REFCOUNTING(VolumeResponseCallback) |
michael@0 | 43 | VolumeResponseCallback() |
michael@0 | 44 | : mResponseCode(0), mPending(false) {} |
michael@0 | 45 | |
michael@0 | 46 | bool Done() const |
michael@0 | 47 | { |
michael@0 | 48 | // Response codes from the 200, 400, and 500 series all indicated that |
michael@0 | 49 | // the command has completed. |
michael@0 | 50 | |
michael@0 | 51 | return (mResponseCode >= ResponseCode::CommandOkay) |
michael@0 | 52 | && (mResponseCode < ResponseCode::UnsolicitedInformational); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | bool WasSuccessful() const |
michael@0 | 56 | { |
michael@0 | 57 | return mResponseCode == ResponseCode::CommandOkay; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | bool IsPending() const { return mPending; } |
michael@0 | 61 | int ResponseCode() const { return mResponseCode; } |
michael@0 | 62 | const nsCString &ResponseStr() const { return mResponseStr; } |
michael@0 | 63 | |
michael@0 | 64 | protected: |
michael@0 | 65 | virtual void ResponseReceived(const VolumeCommand* aCommand) = 0; |
michael@0 | 66 | |
michael@0 | 67 | private: |
michael@0 | 68 | friend class VolumeCommand; // Calls HandleResponse and SetPending |
michael@0 | 69 | |
michael@0 | 70 | void HandleResponse(const VolumeCommand* aCommand, |
michael@0 | 71 | int aResponseCode, |
michael@0 | 72 | nsACString& aResponseStr) |
michael@0 | 73 | { |
michael@0 | 74 | mResponseCode = aResponseCode; |
michael@0 | 75 | #if ANDROID_VERSION >= 17 |
michael@0 | 76 | // There's a sequence number here that we don't care about |
michael@0 | 77 | // We expect it to be 0. See VolumeCommand::SetCmd |
michael@0 | 78 | mResponseStr = Substring(aResponseStr, 2); |
michael@0 | 79 | #else |
michael@0 | 80 | mResponseStr = aResponseStr; |
michael@0 | 81 | #endif |
michael@0 | 82 | if (mResponseCode >= ResponseCode::CommandOkay) { |
michael@0 | 83 | // This is a final response. |
michael@0 | 84 | mPending = false; |
michael@0 | 85 | } |
michael@0 | 86 | ResponseReceived(aCommand); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | void SetPending(bool aPending) { mPending = aPending; } |
michael@0 | 90 | |
michael@0 | 91 | int mResponseCode; // The response code parsed from vold |
michael@0 | 92 | nsCString mResponseStr; // The rest of the line. |
michael@0 | 93 | bool mPending; // Waiting for response? |
michael@0 | 94 | }; |
michael@0 | 95 | |
michael@0 | 96 | /*************************************************************************** |
michael@0 | 97 | * |
michael@0 | 98 | * The VolumeCommand class is an abstract base class used to encapsulate |
michael@0 | 99 | * volume commands send to vold. |
michael@0 | 100 | * |
michael@0 | 101 | * See VolumeManager.h for a list of the volume commands. |
michael@0 | 102 | * |
michael@0 | 103 | * Commands sent to vold need an explicit null character so we add one |
michael@0 | 104 | * to the command to ensure that it's included in the length. |
michael@0 | 105 | * |
michael@0 | 106 | * All of these commands are asynchronous in nature, and the |
michael@0 | 107 | * ResponseReceived callback will be called when a response is available. |
michael@0 | 108 | * |
michael@0 | 109 | ***************************************************************************/ |
michael@0 | 110 | |
michael@0 | 111 | class VolumeCommand |
michael@0 | 112 | { |
michael@0 | 113 | protected: |
michael@0 | 114 | virtual ~VolumeCommand() {} |
michael@0 | 115 | |
michael@0 | 116 | public: |
michael@0 | 117 | NS_INLINE_DECL_REFCOUNTING(VolumeCommand) |
michael@0 | 118 | |
michael@0 | 119 | VolumeCommand(VolumeResponseCallback* aCallback) |
michael@0 | 120 | : mBytesConsumed(0), |
michael@0 | 121 | mCallback(aCallback) |
michael@0 | 122 | { |
michael@0 | 123 | SetCmd(NS_LITERAL_CSTRING("")); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | VolumeCommand(const nsACString& aCommand, VolumeResponseCallback* aCallback) |
michael@0 | 127 | : mBytesConsumed(0), |
michael@0 | 128 | mCallback(aCallback) |
michael@0 | 129 | { |
michael@0 | 130 | SetCmd(aCommand); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | void SetCmd(const nsACString& aCommand) |
michael@0 | 134 | { |
michael@0 | 135 | mCmd.Truncate(); |
michael@0 | 136 | #if ANDROID_VERSION >= 17 |
michael@0 | 137 | // JB requires a sequence number at the beginning of messages. |
michael@0 | 138 | // It doesn't matter what we use, so we use 0. |
michael@0 | 139 | mCmd = "0 "; |
michael@0 | 140 | #endif |
michael@0 | 141 | mCmd.Append(aCommand); |
michael@0 | 142 | // Add a null character. We want this to be included in the length since |
michael@0 | 143 | // vold uses it to determine the end of the command. |
michael@0 | 144 | mCmd.Append('\0'); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | const char* CmdStr() const { return mCmd.get(); } |
michael@0 | 148 | const char* Data() const { return mCmd.Data() + mBytesConsumed; } |
michael@0 | 149 | size_t BytesConsumed() const { return mBytesConsumed; } |
michael@0 | 150 | |
michael@0 | 151 | size_t BytesRemaining() const |
michael@0 | 152 | { |
michael@0 | 153 | return mCmd.Length() - std::min(mCmd.Length(), mBytesConsumed); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | void ConsumeBytes(size_t aNumBytes) |
michael@0 | 157 | { |
michael@0 | 158 | mBytesConsumed += std::min(BytesRemaining(), aNumBytes); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | private: |
michael@0 | 162 | friend class VolumeManager; // Calls SetPending & HandleResponse |
michael@0 | 163 | |
michael@0 | 164 | void SetPending(bool aPending) |
michael@0 | 165 | { |
michael@0 | 166 | if (mCallback) { |
michael@0 | 167 | mCallback->SetPending(aPending); |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | void HandleResponse(int aResponseCode, nsACString& aResponseStr) |
michael@0 | 172 | { |
michael@0 | 173 | if (mCallback) { |
michael@0 | 174 | mCallback->HandleResponse(this, aResponseCode, aResponseStr); |
michael@0 | 175 | } |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | nsCString mCmd; // Command being sent |
michael@0 | 179 | size_t mBytesConsumed; // How many bytes have been sent |
michael@0 | 180 | |
michael@0 | 181 | // Called when a response to the command is received. |
michael@0 | 182 | RefPtr<VolumeResponseCallback> mCallback; |
michael@0 | 183 | }; |
michael@0 | 184 | |
michael@0 | 185 | class VolumeActionCommand : public VolumeCommand |
michael@0 | 186 | { |
michael@0 | 187 | public: |
michael@0 | 188 | VolumeActionCommand(Volume* aVolume, const char* aAction, |
michael@0 | 189 | const char* aExtraArgs, VolumeResponseCallback* aCallback); |
michael@0 | 190 | |
michael@0 | 191 | private: |
michael@0 | 192 | RefPtr<Volume> mVolume; |
michael@0 | 193 | }; |
michael@0 | 194 | |
michael@0 | 195 | class VolumeListCommand : public VolumeCommand |
michael@0 | 196 | { |
michael@0 | 197 | public: |
michael@0 | 198 | VolumeListCommand(VolumeResponseCallback* aCallback); |
michael@0 | 199 | }; |
michael@0 | 200 | |
michael@0 | 201 | } // system |
michael@0 | 202 | } // mozilla |
michael@0 | 203 | |
michael@0 | 204 | #endif // mozilla_system_volumecommand_h__ |