Index: webkit/browser/blob/blob_reader.h |
diff --git a/webkit/browser/blob/blob_reader.h b/webkit/browser/blob/blob_reader.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a5266749c87edd5fbe9c349c45423cf2613a9f11 |
--- /dev/null |
+++ b/webkit/browser/blob/blob_reader.h |
@@ -0,0 +1,114 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef WEBKIT_BROWSER_BLOB_BLOB_READER_H_ |
+#define WEBKIT_BROWSER_BLOB_BLOB_READER_H_ |
+ |
+#include <map> |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
+#include "webkit/browser/blob/file_stream_reader.h" |
+#include "webkit/browser/webkit_storage_browser_export.h" |
+#include "webkit/common/blob/blob_data.h" |
+ |
+namespace base { |
+class SequencedTaskRunner; |
+} |
+ |
+namespace fileapi { |
+class FileSystemContext; |
+} |
+ |
+namespace net { |
+class DrainableIOBuffer; |
+} |
+ |
+namespace webkit_blob { |
+ |
+// A reader implementation that will read the contents of a BlobData. |
+// Should only be used on the IO thread. Delete semantics so delete |
+// whenever you like. If deleted with a pending call, the callback |
+// will not be called (per the FileStreamReader interface design). |
+class WEBKIT_STORAGE_BROWSER_EXPORT BlobReader : public FileStreamReader { |
+ public: |
+ // Constructs a reader that will start reading from |blob_data|. |
+ // The filesystem context is needed to support reading file and |
+ // filesystem backed blobs. |
+ BlobReader(BlobData* blob_data, |
+ fileapi::FileSystemContext* file_system_context); |
+ virtual ~BlobReader(); |
+ |
+ // Must be called prior to making the first call to Read(). |
+ void SetInitialOffset(int64 position); |
+ |
+ // FileStreamReader impl, GetLength returns the full size of |
+ // the BlobData without subtracting the initial offset. |
+ virtual int Read(net::IOBuffer* buf, int buf_len, |
+ const net::CompletionCallback& callback) OVERRIDE; |
+ virtual int64 GetLength(const net::Int64CompletionCallback& callback) OVERRIDE; |
+ |
+ private: |
+ typedef std::map<size_t, FileStreamReader*> IndexToReaderMap; |
+ |
+ // For size computations. |
+ void CountSize(); |
+ void DidGetFileItemLength(size_t index, int64 result); |
+ bool AddItemLength(size_t index, int64 item_length); |
+ void OnSizeCounted(); |
+ |
+ // For reading the blob. |
+ int DoRead(); |
+ void PerformInitialSeek(); |
+ int ReadLoop(); |
+ bool ReadItem(); |
+ void AdvanceItem(); |
+ void AdvanceBytesRead(int result); |
+ bool ReadBytesItem(const BlobData::Item& item, int bytes_to_read); |
+ bool ReadFileItem(FileStreamReader* reader, int bytes_to_read); |
+ void DidReadFile(int result); |
+ int ComputeBytesToRead() const; |
+ int OnReadComplete(); |
+ |
+ // Called upon an error of any kind. |
+ void OnError(int error_code); |
+ |
+ FileStreamReader* GetFileStreamReader(size_t index); |
+ void CreateFileStreamReader(size_t index, int64 additional_offset); |
+ void DeleteCurrentFileReader(); |
+ |
+ base::WeakPtrFactory<BlobReader> weak_factory_; |
+ scoped_refptr<BlobData> blob_data_; |
+ net::CompletionCallback pending_read_callback_; |
+ scoped_refptr<net::IOBuffer> pending_read_buf_; |
+ int pending_read_length_; |
+ net::Int64CompletionCallback pending_size_callback_; |
+ int64 initial_offset_; |
+ bool is_counting_size_; |
+ bool has_total_size_; |
+ bool has_started_reading_; |
+ |
+ // Variables for controlling read from |blob_data_|. |
+ scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
+ scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
+ std::vector<int64> item_length_list_; |
+ int64 total_size_; |
+ int64 remaining_bytes_; |
+ int pending_get_file_info_count_; |
+ IndexToReaderMap index_to_reader_; |
+ size_t current_item_index_; |
+ int64 current_item_offset_; |
+ |
+ // Holds the buffer for read data with the IOBuffer interface. |
+ scoped_refptr<net::DrainableIOBuffer> read_buf_; |
+ |
+ // Is set when OnError() is called. |
+ int error_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BlobReader); |
+}; |
+ |
+} // namespace webkit_blob |
+ |
+#endif // WEBKIT_BROWSER_BLOB_BLOB_READER_H_ |