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
3 // found in the LICENSE file.
5 #ifndef STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_
6 #define STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "net/http/http_byte_range.h"
14 #include "net/http/http_status_code.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"
20 class SingleThreadTaskRunner
;
24 class DrainableIOBuffer
;
30 class FileStreamReader
;
31 class FileSystemContext
;
33 // A request job that handles reading blob URLs.
34 class STORAGE_EXPORT BlobURLRequestJob
35 : public net::URLRequestJob
{
37 BlobURLRequestJob(net::URLRequest
* request
,
38 net::NetworkDelegate
* network_delegate
,
39 scoped_ptr
<BlobDataSnapshot
> blob_data
,
40 storage::FileSystemContext
* file_system_context
,
41 base::SingleThreadTaskRunner
* resolving_thread_task_runner
);
43 // net::URLRequestJob methods.
44 void Start() override
;
46 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
47 bool GetMimeType(std::string
* mime_type
) const override
;
48 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
49 int GetResponseCode() const override
;
50 void SetExtraRequestHeaders(const net::HttpRequestHeaders
& headers
) override
;
53 ~BlobURLRequestJob() override
;
56 typedef std::map
<size_t, FileStreamReader
*> IndexToReaderMap
;
58 // For preparing for read: get the size, apply the range and perform seek.
60 bool AddItemLength(size_t index
, int64 item_length
);
62 void DidCountSize(int error
);
63 void DidGetFileItemLength(size_t index
, int64 result
);
64 void Seek(int64 offset
);
66 // For reading the blob.
67 bool ReadLoop(int* bytes_read
);
70 void AdvanceBytesRead(int result
);
71 bool ReadBytesItem(const BlobDataItem
& item
, int bytes_to_read
);
73 bool ReadFileItem(FileStreamReader
* reader
, int bytes_to_read
);
74 void DidReadFile(int chunk_number
, int result
);
75 void DeleteCurrentFileReader();
77 bool ReadDiskCacheEntryItem(const BlobDataItem
& item
, int bytes_to_read
);
78 void DidReadDiskCacheEntry(int result
);
80 int ComputeBytesToRead() const;
81 int BytesReadCompleted();
83 // These methods convert the result of blob data reading into response headers
84 // and pass it to URLRequestJob's NotifyDone() or NotifyHeadersComplete().
86 void NotifyFailure(int);
87 void HeadersCompleted(net::HttpStatusCode status_code
);
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
);
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
);
97 scoped_ptr
<BlobDataSnapshot
> blob_data_
;
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_
;
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_
;
110 // Holds the buffer for read data with the IOBuffer interface.
111 scoped_refptr
<net::DrainableIOBuffer
> read_buf_
;
113 // Is set when NotifyFailure() is called and reset when DidStart is called.
116 bool byte_range_set_
;
117 net::HttpByteRange byte_range_
;
119 // Used to create unique id's for tracing.
120 int current_file_chunk_number_
;
122 scoped_ptr
<net::HttpResponseInfo
> response_info_
;
124 base::WeakPtrFactory
<BlobURLRequestJob
> weak_factory_
;
126 DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob
);
129 } // namespace storage
131 #endif // STORAGE_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_