summaryrefslogtreecommitdiffstats
path: root/webkit/blob/blob_url_request_job.h
blob: 1153f135b71e49afd475a593fb26a740694ca5fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_
#define WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_callback_factory.h"
#include "base/memory/scoped_ptr.h"
#include "base/platform_file.h"
#include "base/task.h"
#include "net/base/completion_callback.h"
#include "net/http/http_byte_range.h"
#include "net/url_request/url_request_job.h"
#include "webkit/blob/blob_data.h"

namespace base {
class MessageLoopProxy;
struct PlatformFileInfo;
}

namespace net {
class FileStream;
}

namespace webkit_blob {

// A request job that handles reading blob URLs.
class BlobURLRequestJob : public net::URLRequestJob {
 public:
  BlobURLRequestJob(net::URLRequest* request,
                    BlobData* blob_data,
                    base::MessageLoopProxy* resolving_message_loop_proxy);
  virtual ~BlobURLRequestJob();

  // net::URLRequestJob methods.
  virtual void Start();
  virtual void Kill();
  virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read);
  virtual bool GetMimeType(std::string* mime_type) const;
  virtual void GetResponseInfo(net::HttpResponseInfo* info);
  virtual int GetResponseCode() const;
  virtual void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers);

 private:
  void CloseStream();
  void ResolveFile(const FilePath& file_path);
  void CountSize();
  void Seek(int64 offset);
  void AdvanceItem();
  void AdvanceBytesRead(int result);
  int ComputeBytesToRead() const;
  bool ReadLoop(int* bytes_read);
  bool ReadItem();
  bool ReadBytes(const BlobData::Item& item);
  bool DispatchReadFile(const BlobData::Item& item);
  bool ReadFile(const BlobData::Item& item);
  void HeadersCompleted(int status_code, const std::string& status_txt);
  int ReadCompleted();
  void NotifySuccess();
  void NotifyFailure(int);

  void DidStart();
  void DidResolve(base::PlatformFileError rv,
                  const base::PlatformFileInfo& file_info);
  void DidOpen(base::PlatformFileError rv,
               base::PassPlatformFile file,
               bool created);
  void DidRead(int result);

  base::ScopedCallbackFactory<BlobURLRequestJob> callback_factory_;
  scoped_refptr<BlobData> blob_data_;
  scoped_refptr<base::MessageLoopProxy> file_thread_proxy_;
  net::CompletionCallbackImpl<BlobURLRequestJob> io_callback_;
  std::vector<int64> item_length_list_;
  scoped_ptr<net::FileStream> stream_;
  size_t item_index_;
  int64 total_size_;
  int64 current_item_offset_;
  int64 remaining_bytes_;
  scoped_refptr<net::IOBuffer> read_buf_;
  int read_buf_offset_;
  int read_buf_size_;
  int read_buf_remaining_bytes_;
  int bytes_to_read_;
  bool error_;
  bool headers_set_;
  bool byte_range_set_;
  net::HttpByteRange byte_range_;
  scoped_ptr<net::HttpResponseInfo> response_info_;
  ScopedRunnableMethodFactory<BlobURLRequestJob> method_factory_;

  DISALLOW_COPY_AND_ASSIGN(BlobURLRequestJob);
};

}  // namespace webkit_blob

#endif  // WEBKIT_BLOB_BLOB_URL_REQUEST_JOB_H_