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 WEBKIT_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_
6 #define WEBKIT_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/platform_file.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 "webkit/browser/webkit_storage_browser_export.h"
17 #include "webkit/common/blob/blob_data.h"
20 class MessageLoopProxy;
21 struct PlatformFileInfo;
25 class FileSystemContext;
29 class DrainableIOBuffer;
33 namespace webkit_blob {
35 class FileStreamReader;
37 // A request job that handles reading blob URLs.
38 class WEBKIT_STORAGE_BROWSER_EXPORT BlobURLRequestJob
39 : public net::URLRequestJob {
41 BlobURLRequestJob(net::URLRequest* request,
42 net::NetworkDelegate* network_delegate,
44 fileapi::FileSystemContext* file_system_context,
45 base::MessageLoopProxy* resolving_message_loop_proxy);
47 // net::URLRequestJob methods.
48 virtual void Start() OVERRIDE;
49 virtual void Kill() OVERRIDE;
50 virtual bool ReadRawData(net::IOBuffer* buf,
52 int* bytes_read) OVERRIDE;
53 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
54 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
55 virtual int GetResponseCode() const OVERRIDE;
56 virtual void SetExtraRequestHeaders(
57 const net::HttpRequestHeaders& headers) OVERRIDE;
60 virtual ~BlobURLRequestJob();
63 typedef std::map<size_t, FileStreamReader*> IndexToReaderMap;
65 // For preparing for read: get the size, apply the range and perform seek.
67 bool AddItemLength(size_t index, int64 item_length);
69 void DidCountSize(int error);
70 void DidGetFileItemLength(size_t index, int64 result);
71 void Seek(int64 offset);
73 // For reading the blob.
74 bool ReadLoop(int* bytes_read);
77 void AdvanceBytesRead(int result);
78 bool ReadBytesItem(const BlobData::Item& item, int bytes_to_read);
79 bool ReadFileItem(FileStreamReader* reader, int bytes_to_read);
81 void DidReadFile(int result);
82 void DeleteCurrentFileReader();
84 int ComputeBytesToRead() const;
85 int BytesReadCompleted();
87 // These methods convert the result of blob data reading into response headers
88 // and pass it to URLRequestJob's NotifyDone() or NotifyHeadersComplete().
90 void NotifyFailure(int);
91 void HeadersCompleted(net::HttpStatusCode status_code);
93 // Returns a FileStreamReader for a blob item at |index|.
94 // If the item at |index| is not of file this returns NULL.
95 FileStreamReader* GetFileStreamReader(size_t index);
97 // Creates a FileStreamReader for the item at |index| with additional_offset.
98 void CreateFileStreamReader(size_t index, int64 additional_offset);
100 scoped_refptr<BlobData> blob_data_;
102 // Variables for controlling read from |blob_data_|.
103 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
104 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_;
105 std::vector<int64> item_length_list_;
107 int64 remaining_bytes_;
108 int pending_get_file_info_count_;
109 IndexToReaderMap index_to_reader_;
110 size_t current_item_index_;
111 int64 current_item_offset_;
113 // Holds the buffer for read data with the IOBuffer interface.
114 scoped_refptr<net::DrainableIOBuffer> read_buf_;
116 // Is set when NotifyFailure() is called and reset when DidStart is called.
119 bool byte_range_set_;
120 net::HttpByteRange byte_range_;
122 scoped_ptr<net::HttpResponseInfo> response_info_;
124 base::WeakPtrFactory<BlobURLRequestJob> weak_factory_;
126 DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob);
129 } // namespace webkit_blob
131 #endif // WEBKIT_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_