summaryrefslogtreecommitdiffstats
path: root/content/browser/android/url_request_content_job.h
blob: dad9fbfcf66d59641c750f908324b8aebee9e2b4 (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
100
101
102
103
104
105
// Copyright (c) 2014 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 CONTENT_BROWSER_ANDROID_URL_REQUEST_CONTENT_JOB_H_
#define CONTENT_BROWSER_ANDROID_URL_REQUEST_CONTENT_JOB_H_

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "content/common/content_export.h"
#include "net/base/net_errors.h"
#include "net/http/http_byte_range.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_job.h"

namespace base {
class TaskRunner;
}

namespace file_util {
struct FileInfo;
}

namespace net {
class FileStream;
}

namespace content {

// A request job that handles reading content URIs
class CONTENT_EXPORT URLRequestContentJob : public net::URLRequestJob {
 public:
  URLRequestContentJob(
      net::URLRequest* request,
      net::NetworkDelegate* network_delegate,
      const base::FilePath& content_path,
      const scoped_refptr<base::TaskRunner>& content_task_runner);

  // net::URLRequestJob:
  void Start() override;
  void Kill() override;
  int ReadRawData(net::IOBuffer* buf, int buf_size) override;
  bool IsRedirectResponse(GURL* location, int* http_status_code) override;
  bool GetMimeType(std::string* mime_type) const override;
  void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;

 protected:
  ~URLRequestContentJob() override;

 private:
  // Meta information about the content URI. It's used as a member in the
  // URLRequestContentJob and also passed between threads because disk access is
  // necessary to obtain it.
  struct ContentMetaInfo {
    ContentMetaInfo();
    // Flag showing whether the content URI exists.
    bool content_exists;
    // Size of the content URI.
    int64 content_size;
    // Mime type associated with the content URI.
    std::string mime_type;
  };

  // Fetches content URI info on a background thread.
  static void FetchMetaInfo(const base::FilePath& content_path,
                            ContentMetaInfo* meta_info);

  // Callback after fetching content URI info on a background thread.
  void DidFetchMetaInfo(const ContentMetaInfo* meta_info);

  // Callback after opening content URI on a background thread.
  void DidOpen(int result);

  // Callback after seeking to the beginning of |byte_range_| in the content URI
  // on a background thread.
  void DidSeek(int64 result);

  // Callback after data is asynchronously read from the content URI into |buf|.
  void DidRead(int result);

  // The full path of the content URI.
  base::FilePath content_path_;

  scoped_ptr<net::FileStream> stream_;
  ContentMetaInfo meta_info_;
  const scoped_refptr<base::TaskRunner> content_task_runner_;

  net::HttpByteRange byte_range_;
  net::Error range_parse_result_;
  int64 remaining_bytes_;

  bool io_pending_;

  base::WeakPtrFactory<URLRequestContentJob> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(URLRequestContentJob);
};

}  // namespace content

#endif  // CONTENT_BROWSER_ANDROID_URL_REQUEST_CONTENT_JOB_H_