DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "cdm-test-storage.h"
#include <vector>

#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"

using namespace cdm;

class WriteRecordClient : public FileIOClient {
 public:
  WriteRecordClient(std::function<void()>&& aOnSuccess,
                    std::function<void()>&& aOnFailure, const uint8_t* aData,
                    uint32_t aDataSize)
      : mOnSuccess(move(aOnSuccess)), mOnFailure(move(aOnFailure)) {
    mData.insert(mData.end(), aData, aData + aDataSize);
  }

  void OnOpenComplete(Status aStatus) override {
    // If we hit an error, fail.
    if (aStatus != Status::kSuccess) {
      Done(aStatus);
    } else if (mFileIO) {  // Otherwise, write our data to the file.
      mFileIO->Write(mData.empty() ? nullptr : &mData.front(), mData.size());
    }
  }

  void OnReadComplete(Status aStatus, const uint8_t* aData,
                      uint32_t aDataSize) override {}

  void OnWriteComplete(Status aStatus) override { Done(aStatus); }

  void Do(const std::string& aName, Host_10* aHost) {
    // Initialize the FileIO.
    mFileIO = aHost->CreateFileIO(this);
    mFileIO->Open(aName.c_str(), aName.size());
  }

 private:
  void Done(cdm::FileIOClient::Status aStatus) {
    // Note: Call Close() before running continuation, in case the
    // continuation tries to open the same record; if we call Close()
    // after running the continuation, the Close() call will arrive
    // just after the Open() call succeeds, immediately closing the
    // record we just opened.
    if (mFileIO) {
      // will delete mFileIO inside Close.
      mFileIO->Close();
    }

    if (IO_SUCCEEDED(aStatus)) {
      mOnSuccess();
    } else {
      mOnFailure();
    }

    delete this;
  }

  FileIO* mFileIO = nullptr;
  std::function<void()> mOnSuccess;
  std::function<void()> mOnFailure;
  std::vector<uint8_t> mData;
};

void WriteRecord(Host_10* aHost, const std::string& aRecordName,
                 const uint8_t* aData, uint32_t aNumBytes,
                 std::function<void()>&& aOnSuccess,
                 std::function<void()>&& aOnFailure) {
  // client will be delete in WriteRecordClient::Done
  WriteRecordClient* client = new WriteRecordClient(
      move(aOnSuccess), move(aOnFailure), aData, aNumBytes);
  client->Do(aRecordName, aHost);
}

void WriteRecord(Host_10* aHost, const std::string& aRecordName,
                 const std::string& aData, std::function<void()>&& aOnSuccess,
                 std::function<void()>&& aOnFailure) {
  return WriteRecord(aHost, aRecordName, (const uint8_t*)aData.c_str(),
                     aData.size(), move(aOnSuccess), move(aOnFailure));
}

class ReadRecordClient : public FileIOClient {
 public:
  explicit ReadRecordClient(
      std::function<void(bool, const uint8_t*, uint32_t)>&& aOnReadComplete)
      : mOnReadComplete(move(aOnReadComplete)) {}

  void OnOpenComplete(Status aStatus) override {
    auto err = aStatus;
    if (aStatus != Status::kSuccess) {
      Done(err, reinterpret_cast<const uint8_t*>(""), 0);
    } else {
      mFileIO->Read();
    }
  }

  void OnReadComplete(Status aStatus, const uint8_t* aData,
                      uint32_t aDataSize) override {
    Done(aStatus, aData, aDataSize);
  }

  void OnWriteComplete(Status aStatus) override {}

  void Do(const std::string& aName, Host_10* aHost) {
    mFileIO = aHost->CreateFileIO(this);
    mFileIO->Open(aName.c_str(), aName.size());
  }

 private:
  void Done(cdm::FileIOClient::Status aStatus, const uint8_t* aData,
            uint32_t aDataSize) {
    // Note: Call Close() before running continuation, in case the
    // continuation tries to open the same record; if we call Close()
    // after running the continuation, the Close() call will arrive
    // just after the Open() call succeeds, immediately closing the
    // record we just opened.
    if (mFileIO) {
      // will delete mFileIO inside Close.
      mFileIO->Close();
    }

    if (IO_SUCCEEDED(aStatus)) {
      mOnReadComplete(true, aData, aDataSize);
    } else {
      mOnReadComplete(false, reinterpret_cast<const uint8_t*>(""), 0);
    }

    delete this;
  }

  FileIO* mFileIO = nullptr;
  std::function<void(bool, const uint8_t*, uint32_t)> mOnReadComplete;
};

void ReadRecord(
    Host_10* aHost, const std::string& aRecordName,
    std::function<void(bool, const uint8_t*, uint32_t)>&& aOnReadComplete) {
  // client will be delete in ReadRecordClient::Done
  ReadRecordClient* client = new ReadRecordClient(move(aOnReadComplete));
  client->Do(aRecordName, aHost);
}

class OpenRecordClient : public FileIOClient {
 public:
  explicit OpenRecordClient(std::function<void(bool)>&& aOpenComplete)
      : mOpenComplete(move(aOpenComplete)) {}

  void OnOpenComplete(Status aStatus) override { Done(aStatus); }

  void OnReadComplete(Status aStatus, const uint8_t* aData,
                      uint32_t aDataSize) override {}

  void OnWriteComplete(Status aStatus) override {}

  void Do(const std::string& aName, Host_10* aHost) {
    // Initialize the FileIO.
    mFileIO = aHost->CreateFileIO(this);
    mFileIO->Open(aName.c_str(), aName.size());
  }

 private:
  void Done(cdm::FileIOClient::Status aStatus) {
    // Note: Call Close() before running continuation, in case the
    // continuation tries to open the same record; if we call Close()
    // after running the continuation, the Close() call will arrive
    // just after the Open() call succeeds, immediately closing the
    // record we just opened.
    if (mFileIO) {
      // will delete mFileIO inside Close.
      mFileIO->Close();
    }

    if (IO_SUCCEEDED(aStatus)) {
      mOpenComplete(true);
    } else {
      mOpenComplete(false);
    }

    delete this;
  }

  FileIO* mFileIO = nullptr;
  std::function<void(bool)> mOpenComplete;
  ;
};

void OpenRecord(Host_10* aHost, const std::string& aRecordName,
                std::function<void(bool)>&& aOpenComplete) {
  // client will be delete in OpenRecordClient::Done
  OpenRecordClient* client = new OpenRecordClient(move(aOpenComplete));
  client->Do(aRecordName, aHost);
}