| 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 WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_ | 5 #ifndef WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_ |
| 6 #define WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_ | 6 #define WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_ |
| 7 | 7 |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| 11 #include "net/http/http_byte_range.h" | 11 #include "net/http/http_byte_range.h" |
| 12 #include "net/url_request/url_request_job.h" | 12 #include "net/url_request/url_request_job.h" |
| 13 #include "webkit/blob/blob_data.h" | 13 #include "webkit/blob/blob_data.h" |
| 14 #include "webkit/blob/blob_export.h" | 14 #include "webkit/blob/blob_export.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class MessageLoopProxy; | 17 class MessageLoopProxy; |
| 18 struct PlatformFileInfo; | 18 struct PlatformFileInfo; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 class DrainableIOBuffer; | 22 class DrainableIOBuffer; |
| 23 class FileStream; | |
| 24 class IOBuffer; | 23 class IOBuffer; |
| 25 } | 24 } |
| 26 | 25 |
| 27 namespace webkit_blob { | 26 namespace webkit_blob { |
| 28 | 27 |
| 29 class LocalFileReader; | 28 class LocalFileStreamReader; |
| 30 | 29 |
| 31 // A request job that handles reading blob URLs. | 30 // A request job that handles reading blob URLs. |
| 32 class BLOB_EXPORT BlobURLRequestJob : public net::URLRequestJob { | 31 class BLOB_EXPORT BlobURLRequestJob : public net::URLRequestJob { |
| 33 public: | 32 public: |
| 34 BlobURLRequestJob(net::URLRequest* request, | 33 BlobURLRequestJob(net::URLRequest* request, |
| 35 BlobData* blob_data, | 34 BlobData* blob_data, |
| 36 base::MessageLoopProxy* resolving_message_loop_proxy); | 35 base::MessageLoopProxy* resolving_message_loop_proxy); |
| 37 | 36 |
| 38 // net::URLRequestJob methods. | 37 // net::URLRequestJob methods. |
| 39 virtual void Start() OVERRIDE; | 38 virtual void Start() OVERRIDE; |
| 40 virtual void Kill() OVERRIDE; | 39 virtual void Kill() OVERRIDE; |
| 41 virtual bool ReadRawData(net::IOBuffer* buf, | 40 virtual bool ReadRawData(net::IOBuffer* buf, |
| 42 int buf_size, | 41 int buf_size, |
| 43 int* bytes_read) OVERRIDE; | 42 int* bytes_read) OVERRIDE; |
| 44 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; | 43 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
| 45 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE; | 44 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE; |
| 46 virtual int GetResponseCode() const OVERRIDE; | 45 virtual int GetResponseCode() const OVERRIDE; |
| 47 virtual void SetExtraRequestHeaders( | 46 virtual void SetExtraRequestHeaders( |
| 48 const net::HttpRequestHeaders& headers) OVERRIDE; | 47 const net::HttpRequestHeaders& headers) OVERRIDE; |
| 49 | 48 |
| 50 protected: | 49 protected: |
| 51 virtual ~BlobURLRequestJob(); | 50 virtual ~BlobURLRequestJob(); |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 typedef std::map<size_t, LocalFileReader*> IndexToReaderMap; | 53 typedef std::map<size_t, LocalFileStreamReader*> IndexToReaderMap; |
| 55 | 54 |
| 56 // For preparing for read: get the size, apply the range and perform seek. | 55 // For preparing for read: get the size, apply the range and perform seek. |
| 57 void DidStart(); | 56 void DidStart(); |
| 58 void CountSize(); | 57 void CountSize(); |
| 59 void DidCountSize(int error); | 58 void DidCountSize(int error); |
| 60 void DidGetFileItemLength(size_t index, int64 result); | 59 void DidGetFileItemLength(size_t index, int64 result); |
| 61 void Seek(int64 offset); | 60 void Seek(int64 offset); |
| 62 | 61 |
| 63 // For reading the blob. | 62 // For reading the blob. |
| 64 bool ReadLoop(int* bytes_read); | 63 bool ReadLoop(int* bytes_read); |
| 65 bool ReadItem(); | 64 bool ReadItem(); |
| 66 void AdvanceItem(); | 65 void AdvanceItem(); |
| 67 void AdvanceBytesRead(int result); | 66 void AdvanceBytesRead(int result); |
| 68 bool ReadBytesItem(const BlobData::Item& item, int bytes_to_read); | 67 bool ReadBytesItem(const BlobData::Item& item, int bytes_to_read); |
| 69 bool ReadFileItem(LocalFileReader* reader, int bytes_to_read); | 68 bool ReadFileItem(LocalFileStreamReader* reader, int bytes_to_read); |
| 70 | 69 |
| 71 void DidReadFile(int result); | 70 void DidReadFile(int result); |
| 72 void DeleteCurrentFileReader(); | 71 void DeleteCurrentFileReader(); |
| 73 | 72 |
| 74 int ComputeBytesToRead() const; | 73 int ComputeBytesToRead() const; |
| 75 int BytesReadCompleted(); | 74 int BytesReadCompleted(); |
| 76 | 75 |
| 77 void NotifySuccess(); | 76 void NotifySuccess(); |
| 78 void NotifyFailure(int); | 77 void NotifyFailure(int); |
| 79 void HeadersCompleted(int status_code, const std::string& status_txt); | 78 void HeadersCompleted(int status_code, const std::string& status_txt); |
| 80 | 79 |
| 81 // Returns a LocalFileReader for a blob item at |index|. | 80 // Returns a LocalFileStreamReader for a blob item at |index|. |
| 82 // If the item at |index| is not of TYPE_FILE this returns NULL. | 81 // If the item at |index| is not of TYPE_FILE this returns NULL. |
| 83 LocalFileReader* GetFileReader(size_t index); | 82 LocalFileStreamReader* GetFileStreamReader(size_t index); |
| 84 | 83 |
| 85 base::WeakPtrFactory<BlobURLRequestJob> weak_factory_; | 84 base::WeakPtrFactory<BlobURLRequestJob> weak_factory_; |
| 86 scoped_refptr<BlobData> blob_data_; | 85 scoped_refptr<BlobData> blob_data_; |
| 87 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; | 86 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; |
| 88 std::vector<int64> item_length_list_; | 87 std::vector<int64> item_length_list_; |
| 89 int64 total_size_; | 88 int64 total_size_; |
| 90 int64 remaining_bytes_; | 89 int64 remaining_bytes_; |
| 91 int pending_get_file_info_count_; | 90 int pending_get_file_info_count_; |
| 92 IndexToReaderMap index_to_reader_; | 91 IndexToReaderMap index_to_reader_; |
| 93 size_t current_item_index_; | 92 size_t current_item_index_; |
| 94 int64 current_item_offset_; | 93 int64 current_item_offset_; |
| 95 scoped_refptr<net::DrainableIOBuffer> read_buf_; | 94 scoped_refptr<net::DrainableIOBuffer> read_buf_; |
| 96 bool error_; | 95 bool error_; |
| 97 bool headers_set_; | 96 bool headers_set_; |
| 98 bool byte_range_set_; | 97 bool byte_range_set_; |
| 99 net::HttpByteRange byte_range_; | 98 net::HttpByteRange byte_range_; |
| 100 scoped_ptr<net::HttpResponseInfo> response_info_; | 99 scoped_ptr<net::HttpResponseInfo> response_info_; |
| 101 | 100 |
| 102 DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob); | 101 DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob); |
| 103 }; | 102 }; |
| 104 | 103 |
| 105 } // namespace webkit_blob | 104 } // namespace webkit_blob |
| 106 | 105 |
| 107 #endif // WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_ | 106 #endif // WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_ |
| OLD | NEW |