- add sources.
[platform/framework/web/crosswalk.git] / src / webkit / browser / blob / blob_url_request_job.h
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.
4
5 #ifndef WEBKIT_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_
6 #define WEBKIT_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_
7
8 #include <map>
9
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"
18
19 namespace base {
20 class MessageLoopProxy;
21 struct PlatformFileInfo;
22 }
23
24 namespace fileapi {
25 class FileSystemContext;
26 }
27
28 namespace net {
29 class DrainableIOBuffer;
30 class IOBuffer;
31 }
32
33 namespace webkit_blob {
34
35 class FileStreamReader;
36
37 // A request job that handles reading blob URLs.
38 class WEBKIT_STORAGE_BROWSER_EXPORT BlobURLRequestJob
39     : public net::URLRequestJob {
40  public:
41   BlobURLRequestJob(net::URLRequest* request,
42                     net::NetworkDelegate* network_delegate,
43                     BlobData* blob_data,
44                     fileapi::FileSystemContext* file_system_context,
45                     base::MessageLoopProxy* resolving_message_loop_proxy);
46
47   // net::URLRequestJob methods.
48   virtual void Start() OVERRIDE;
49   virtual void Kill() OVERRIDE;
50   virtual bool ReadRawData(net::IOBuffer* buf,
51                            int buf_size,
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;
58
59  protected:
60   virtual ~BlobURLRequestJob();
61
62  private:
63   typedef std::map<size_t, FileStreamReader*> IndexToReaderMap;
64
65   // For preparing for read: get the size, apply the range and perform seek.
66   void DidStart();
67   bool AddItemLength(size_t index, int64 item_length);
68   void CountSize();
69   void DidCountSize(int error);
70   void DidGetFileItemLength(size_t index, int64 result);
71   void Seek(int64 offset);
72
73   // For reading the blob.
74   bool ReadLoop(int* bytes_read);
75   bool ReadItem();
76   void AdvanceItem();
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);
80
81   void DidReadFile(int result);
82   void DeleteCurrentFileReader();
83
84   int ComputeBytesToRead() const;
85   int BytesReadCompleted();
86
87   // These methods convert the result of blob data reading into response headers
88   // and pass it to URLRequestJob's NotifyDone() or NotifyHeadersComplete().
89   void NotifySuccess();
90   void NotifyFailure(int);
91   void HeadersCompleted(net::HttpStatusCode status_code);
92
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);
96
97   // Creates a FileStreamReader for the item at |index| with additional_offset.
98   void CreateFileStreamReader(size_t index, int64 additional_offset);
99
100   scoped_refptr<BlobData> blob_data_;
101
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_;
106   int64 total_size_;
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_;
112
113   // Holds the buffer for read data with the IOBuffer interface.
114   scoped_refptr<net::DrainableIOBuffer> read_buf_;
115
116   // Is set when NotifyFailure() is called and reset when DidStart is called.
117   bool error_;
118
119   bool byte_range_set_;
120   net::HttpByteRange byte_range_;
121
122   scoped_ptr<net::HttpResponseInfo> response_info_;
123
124   base::WeakPtrFactory<BlobURLRequestJob> weak_factory_;
125
126   DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob);
127 };
128
129 }  // namespace webkit_blob
130
131 #endif  // WEBKIT_BROWSER_BLOB_BLOB_URL_REQUEST_JOB_H_