atom_blob_reader.h 1.9 KB

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