| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "net/http/http_byte_range.h" | 13 #include "net/http/http_byte_range.h" |
| 14 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
| 15 #include "net/url_request/url_request_job.h" | 15 #include "net/url_request/url_request_job.h" |
| 16 #include "storage/browser/blob/blob_data_snapshot.h" | |
| 17 #include "storage/browser/storage_browser_export.h" | 16 #include "storage/browser/storage_browser_export.h" |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
| 21 } | 20 } |
| 22 | 21 |
| 23 namespace net { | 22 namespace net { |
| 24 class DrainableIOBuffer; | 23 class DrainableIOBuffer; |
| 25 class IOBuffer; | 24 class IOBuffer; |
| 26 } | 25 } |
| 27 | 26 |
| 28 namespace storage { | 27 namespace storage { |
| 29 | 28 |
| 29 class BlobDataHandle; |
| 30 class BlobReader; |
| 30 class FileStreamReader; | 31 class FileStreamReader; |
| 31 class FileSystemContext; | 32 class FileSystemContext; |
| 32 | 33 |
| 33 // A request job that handles reading blob URLs. | 34 // A request job that handles reading blob URLs. |
| 34 class STORAGE_EXPORT BlobURLRequestJob | 35 class STORAGE_EXPORT BlobURLRequestJob |
| 35 : public net::URLRequestJob { | 36 : public net::URLRequestJob { |
| 36 public: | 37 public: |
| 37 BlobURLRequestJob(net::URLRequest* request, | 38 BlobURLRequestJob(net::URLRequest* request, |
| 38 net::NetworkDelegate* network_delegate, | 39 net::NetworkDelegate* network_delegate, |
| 39 scoped_ptr<BlobDataSnapshot> blob_data, | 40 BlobDataHandle* blob_handle, |
| 40 storage::FileSystemContext* file_system_context, | 41 storage::FileSystemContext* file_system_context, |
| 41 base::SingleThreadTaskRunner* resolving_thread_task_runner); | 42 base::SingleThreadTaskRunner* resolving_thread_task_runner); |
| 42 | 43 |
| 43 // net::URLRequestJob methods. | 44 // net::URLRequestJob methods. |
| 44 void Start() override; | 45 void Start() override; |
| 45 void Kill() override; | 46 void Kill() override; |
| 46 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; | 47 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; |
| 47 bool GetMimeType(std::string* mime_type) const override; | 48 bool GetMimeType(std::string* mime_type) const override; |
| 48 void GetResponseInfo(net::HttpResponseInfo* info) override; | 49 void GetResponseInfo(net::HttpResponseInfo* info) override; |
| 49 int GetResponseCode() const override; | 50 int GetResponseCode() const override; |
| 50 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override; | 51 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override; |
| 51 | 52 |
| 52 protected: | 53 protected: |
| 53 ~BlobURLRequestJob() override; | 54 ~BlobURLRequestJob() override; |
| 54 | 55 |
| 55 private: | 56 private: |
| 56 typedef std::map<size_t, FileStreamReader*> IndexToReaderMap; | 57 typedef std::map<size_t, FileStreamReader*> IndexToReaderMap; |
| 57 | 58 |
| 58 // For preparing for read: get the size, apply the range and perform seek. | 59 // For preparing for read: get the size, apply the range and perform seek. |
| 59 void DidStart(); | 60 void DidStart(); |
| 60 bool AddItemLength(size_t index, int64 item_length); | 61 void DidCalculateSize(int result); |
| 61 bool CountSize(); | 62 void DidReadRawData(int result); |
| 62 void DidCountSize(int error); | |
| 63 void DidGetFileItemLength(size_t index, int64 result); | |
| 64 void Seek(int64 offset); | |
| 65 | 63 |
| 66 // For reading the blob. | |
| 67 bool ReadLoop(int* bytes_read); | |
| 68 bool ReadItem(); | |
| 69 void AdvanceItem(); | |
| 70 void AdvanceBytesRead(int result); | |
| 71 bool ReadBytesItem(const BlobDataItem& item, int bytes_to_read); | |
| 72 | |
| 73 bool ReadFileItem(FileStreamReader* reader, int bytes_to_read); | |
| 74 void DidReadFile(int chunk_number, int result); | |
| 75 void DeleteCurrentFileReader(); | |
| 76 | |
| 77 bool ReadDiskCacheEntryItem(const BlobDataItem& item, int bytes_to_read); | |
| 78 void DidReadDiskCacheEntry(int result); | |
| 79 | |
| 80 int ComputeBytesToRead() const; | |
| 81 int BytesReadCompleted(); | |
| 82 | |
| 83 // These methods convert the result of blob data reading into response headers | |
| 84 // and pass it to URLRequestJob's NotifyDone() or NotifyHeadersComplete(). | |
| 85 void NotifySuccess(); | |
| 86 void NotifyFailure(int); | 64 void NotifyFailure(int); |
| 87 void HeadersCompleted(net::HttpStatusCode status_code); | 65 void HeadersCompleted(net::HttpStatusCode status_code); |
| 88 | 66 |
| 89 // Returns a FileStreamReader for a blob item at |index|. | |
| 90 // If the item at |index| is not of file this returns NULL. | |
| 91 FileStreamReader* GetFileStreamReader(size_t index); | |
| 92 | |
| 93 // Creates a FileStreamReader for the item at |index| with additional_offset. | |
| 94 // If failed, then returns false. | |
| 95 bool CreateFileStreamReader(size_t index, int64 additional_offset); | |
| 96 | |
| 97 scoped_ptr<BlobDataSnapshot> blob_data_; | |
| 98 | |
| 99 // Variables for controlling read from |blob_data_|. | |
| 100 scoped_refptr<storage::FileSystemContext> file_system_context_; | |
| 101 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
| 102 std::vector<int64> item_length_list_; | |
| 103 int64 total_size_; | |
| 104 int64 remaining_bytes_; | |
| 105 int pending_get_file_info_count_; | |
| 106 IndexToReaderMap index_to_reader_; | |
| 107 size_t current_item_index_; | |
| 108 int64 current_item_offset_; | |
| 109 | |
| 110 // Holds the buffer for read data with the IOBuffer interface. | |
| 111 scoped_refptr<net::DrainableIOBuffer> read_buf_; | |
| 112 | |
| 113 // Is set when NotifyFailure() is called and reset when DidStart is called. | 67 // Is set when NotifyFailure() is called and reset when DidStart is called. |
| 114 bool error_; | 68 bool error_; |
| 115 | 69 |
| 116 bool byte_range_set_; | 70 bool byte_range_set_; |
| 117 net::HttpByteRange byte_range_; | 71 net::HttpByteRange byte_range_; |
| 118 | 72 |
| 119 // Used to create unique id's for tracing. | 73 scoped_ptr<BlobDataHandle> blob_handle_; |
| 120 int current_file_chunk_number_; | 74 scoped_ptr<BlobReader> blob_reader_; |
| 121 | |
| 122 scoped_ptr<net::HttpResponseInfo> response_info_; | 75 scoped_ptr<net::HttpResponseInfo> response_info_; |
| 123 | 76 |
| 124 base::WeakPtrFactory<BlobURLRequestJob> weak_factory_; | 77 base::WeakPtrFactory<BlobURLRequestJob> weak_factory_; |
| 125 | 78 |
| 126 DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob); | 79 DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob); |
| 127 }; | 80 }; |
| 128 | 81 |
| 129 } // namespace storage | 82 } // namespace storage |
| 130 | 83 |
| 131 #endif // STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_ | 84 #endif // STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_ |
| OLD | NEW |