atom_blob_reader.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "atom/browser/atom_blob_reader.h"
  5. #include "content/browser/blob_storage/chrome_blob_storage_context.h"
  6. #include "content/public/browser/browser_thread.h"
  7. #include "net/base/io_buffer.h"
  8. #include "net/base/net_errors.h"
  9. #include "storage/browser/blob/blob_data_handle.h"
  10. #include "storage/browser/blob/blob_reader.h"
  11. #include "storage/browser/blob/blob_storage_context.h"
  12. #include "storage/browser/fileapi/file_system_context.h"
  13. #include "atom/common/node_includes.h"
  14. using content::BrowserThread;
  15. namespace atom {
  16. namespace {
  17. void FreeNodeBufferData(char* data, void* hint) {
  18. delete[] data;
  19. }
  20. void RunCallbackInUI(const AtomBlobReader::CompletionCallback& callback,
  21. char* blob_data,
  22. int size) {
  23. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  24. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  25. v8::Locker locker(isolate);
  26. v8::HandleScope handle_scope(isolate);
  27. if (blob_data) {
  28. v8::Local<v8::Value> buffer =
  29. node::Buffer::New(isolate, blob_data, static_cast<size_t>(size),
  30. &FreeNodeBufferData, nullptr)
  31. .ToLocalChecked();
  32. callback.Run(buffer);
  33. } else {
  34. callback.Run(v8::Null(isolate));
  35. }
  36. }
  37. } // namespace
  38. AtomBlobReader::AtomBlobReader(content::ChromeBlobStorageContext* blob_context,
  39. storage::FileSystemContext* file_system_context)
  40. : blob_context_(blob_context), file_system_context_(file_system_context) {}
  41. AtomBlobReader::~AtomBlobReader() {}
  42. void AtomBlobReader::StartReading(
  43. const std::string& uuid,
  44. const AtomBlobReader::CompletionCallback& completion_callback) {
  45. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  46. auto blob_data_handle = blob_context_->context()->GetBlobDataFromUUID(uuid);
  47. auto callback = base::Bind(&RunCallbackInUI, completion_callback);
  48. if (!blob_data_handle) {
  49. BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
  50. base::BindOnce(callback, nullptr, 0));
  51. return;
  52. }
  53. auto blob_reader = blob_data_handle->CreateReader(file_system_context_.get());
  54. BlobReadHelper* blob_read_helper =
  55. new BlobReadHelper(std::move(blob_reader), callback);
  56. blob_read_helper->Read();
  57. }
  58. AtomBlobReader::BlobReadHelper::BlobReadHelper(
  59. std::unique_ptr<storage::BlobReader> blob_reader,
  60. const BlobReadHelper::CompletionCallback& callback)
  61. : blob_reader_(std::move(blob_reader)), completion_callback_(callback) {}
  62. AtomBlobReader::BlobReadHelper::~BlobReadHelper() {}
  63. void AtomBlobReader::BlobReadHelper::Read() {
  64. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  65. storage::BlobReader::Status size_status = blob_reader_->CalculateSize(
  66. base::Bind(&AtomBlobReader::BlobReadHelper::DidCalculateSize,
  67. base::Unretained(this)));
  68. if (size_status != storage::BlobReader::Status::IO_PENDING)
  69. DidCalculateSize(net::OK);
  70. }
  71. void AtomBlobReader::BlobReadHelper::DidCalculateSize(int result) {
  72. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  73. if (result != net::OK) {
  74. DidReadBlobData(nullptr, 0);
  75. return;
  76. }
  77. uint64_t total_size = blob_reader_->total_size();
  78. int bytes_read = 0;
  79. scoped_refptr<net::IOBuffer> blob_data =
  80. new net::IOBuffer(static_cast<size_t>(total_size));
  81. auto callback =
  82. base::Bind(&AtomBlobReader::BlobReadHelper::DidReadBlobData,
  83. base::Unretained(this), base::RetainedRef(blob_data));
  84. storage::BlobReader::Status read_status =
  85. blob_reader_->Read(blob_data.get(), total_size, &bytes_read, callback);
  86. if (read_status != storage::BlobReader::Status::IO_PENDING)
  87. callback.Run(bytes_read);
  88. }
  89. void AtomBlobReader::BlobReadHelper::DidReadBlobData(
  90. const scoped_refptr<net::IOBuffer>& blob_data,
  91. int size) {
  92. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  93. char* data = new char[size];
  94. memcpy(data, blob_data->data(), size);
  95. BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
  96. base::BindOnce(completion_callback_, data, size));
  97. delete this;
  98. }
  99. } // namespace atom