fragmented_load_transaction.cc revision abe6dd64a5ae46024eecaddc8f1dbdff9a4d4da5
1/* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "chre_host/fragmented_load_transaction.h" 18 19#include <algorithm> 20 21namespace android { 22namespace chre { 23 24namespace { 25 26/** 27 * Returns a vector containing a specific subarray of the original vector. 28 * 29 * If the ending index of the subarray exceeds that of the source vector, 30 * the size will be truncated to the last element of the source vector. 31 * 32 * @param source the source vector 33 * @param start the starting index of the subarray 34 * @param size the size of the subarray 35 * 36 * @return the vector containing the subarray 37 */ 38inline std::vector<uint8_t> getSubVector( 39 const std::vector<uint8_t>& source, size_t start, size_t size) { 40 size_t end = std::min(source.size(), start + size); 41 return (source.size() == 0) ? 42 std::vector<uint8_t>() : 43 std::vector<uint8_t>( 44 source.begin() + start, source.begin() + end); // [start, end) 45} 46 47} // anonymous namespace 48 49FragmentedLoadTransaction::FragmentedLoadTransaction( 50 uint32_t transactionId, uint64_t appId, uint32_t appVersion, 51 uint32_t targetApiVersion, const std::vector<uint8_t>& appBinary, 52 size_t fragmentSize) { 53 mTransactionId = transactionId; 54 55 // Start with fragmentId at 1 since 0 is used to indicate 56 // legacy behavior at CHRE 57 size_t fragmentId = 1; 58 size_t byteIndex = 0; 59 do { 60 if (fragmentId == 1) { 61 mFragmentRequests.emplace_back( 62 fragmentId++, transactionId, appId, appVersion, targetApiVersion, 63 appBinary.size(), getSubVector(appBinary, byteIndex, fragmentSize)); 64 } else { 65 mFragmentRequests.emplace_back( 66 fragmentId++, transactionId, 67 getSubVector(appBinary, byteIndex, fragmentSize)); 68 } 69 70 byteIndex += fragmentSize; 71 } while (byteIndex < appBinary.size()); 72} 73 74const FragmentedLoadRequest& FragmentedLoadTransaction::getNextRequest() { 75 return mFragmentRequests.at(mCurrentRequestIndex++); 76} 77 78bool FragmentedLoadTransaction::isComplete() const { 79 return (mCurrentRequestIndex >= mFragmentRequests.size()); 80} 81 82} // namespace chre 83} // namespace android 84