atom_blob_reader.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_BROWSER_ATOM_BLOB_READER_H_
  5. #define ATOM_BROWSER_ATOM_BLOB_READER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. namespace content {
  9. class ChromeBlobStorageContext;
  10. }
  11. namespace net {
  12. class IOBuffer;
  13. }
  14. namespace storage {
  15. class BlobDataHandle;
  16. class BlobReader;
  17. } // namespace storage
  18. namespace v8 {
  19. template <class T>
  20. class Local;
  21. class Value;
  22. } // namespace v8
  23. namespace atom {
  24. // A class to keep track of the blob context. All methods,
  25. // except Ctor are expected to be called on IO thread.
  26. class AtomBlobReader {
  27. public:
  28. using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
  29. explicit AtomBlobReader(content::ChromeBlobStorageContext* blob_context);
  30. ~AtomBlobReader();
  31. void StartReading(const std::string& uuid,
  32. const AtomBlobReader::CompletionCallback& callback);
  33. private:
  34. // A self-destroyed helper class to read the blob data.
  35. // Must be accessed on IO thread.
  36. class BlobReadHelper {
  37. public:
  38. using CompletionCallback = base::Callback<void(char*, int)>;
  39. BlobReadHelper(std::unique_ptr<storage::BlobReader> blob_reader,
  40. const BlobReadHelper::CompletionCallback& callback);
  41. ~BlobReadHelper();
  42. void Read();
  43. private:
  44. void DidCalculateSize(int result);
  45. void DidReadBlobData(const scoped_refptr<net::IOBuffer>& blob_data,
  46. int bytes_read);
  47. std::unique_ptr<storage::BlobReader> blob_reader_;
  48. BlobReadHelper::CompletionCallback completion_callback_;
  49. DISALLOW_COPY_AND_ASSIGN(BlobReadHelper);
  50. };
  51. scoped_refptr<content::ChromeBlobStorageContext> blob_context_;
  52. DISALLOW_COPY_AND_ASSIGN(AtomBlobReader);
  53. };
  54. } // namespace atom
  55. #endif // ATOM_BROWSER_ATOM_BLOB_READER_H_