1// Copyright 2014 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 CHROME_BROWSER_CHROMEOS_FILEAPI_EXTERNAL_FILE_URL_REQUEST_JOB_H_
6#define CHROME_BROWSER_CHROMEOS_FILEAPI_EXTERNAL_FILE_URL_REQUEST_JOB_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/memory/weak_ptr.h"
13#include "chrome/browser/chromeos/drive/file_errors.h"
14#include "net/base/net_errors.h"
15#include "net/http/http_byte_range.h"
16#include "net/url_request/url_request_job.h"
17#include "storage/browser/blob/file_stream_reader.h"
18#include "storage/browser/fileapi/file_system_url.h"
19
20namespace net {
21class IOBuffer;
22class NetworkDelegate;
23class URLRequest;
24}  // namespace net
25
26namespace chromeos {
27
28// ExternalFileURLRequestJob is the gateway between network-level drive:...
29// requests for drive resources and FileSystem.  It exposes content URLs
30// formatted as drive:<drive-file-path>.
31// The methods should be run on IO thread.
32class ExternalFileURLRequestJob : public net::URLRequestJob {
33 public:
34  // Callback to take results from an internal helper defined in
35  // drive_url_request_job.cc.
36  typedef base::Callback<
37      void(net::Error,
38           const scoped_refptr<storage::FileSystemContext>& file_system_context,
39           const storage::FileSystemURL& file_system_url,
40           const std::string& mime_type)> HelperCallback;
41
42  ExternalFileURLRequestJob(void* profile_id,
43                            net::URLRequest* request,
44                            net::NetworkDelegate* network_delegate);
45
46  // net::URLRequestJob overrides:
47  virtual void SetExtraRequestHeaders(
48      const net::HttpRequestHeaders& headers) OVERRIDE;
49  virtual void Start() OVERRIDE;
50  virtual void Kill() OVERRIDE;
51  virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
52  virtual bool IsRedirectResponse(GURL* location,
53                                  int* http_status_code) OVERRIDE;
54  virtual bool ReadRawData(net::IOBuffer* buf,
55                           int buf_size,
56                           int* bytes_read) OVERRIDE;
57
58 protected:
59  virtual ~ExternalFileURLRequestJob();
60
61 private:
62  // Called from an internal helper class defined in drive_url_request_job.cc,
63  // which is running on the UI thread.
64  void OnHelperResultObtained(
65      net::Error error,
66      const scoped_refptr<storage::FileSystemContext>& file_system_context,
67      const storage::FileSystemURL& file_system_url,
68      const std::string& mime_type);
69
70  // Called from FileSystemBackend::GetRedirectURLForContents.
71  void OnRedirectURLObtained(const GURL& redirect_url);
72
73  // Called from DriveURLRequestJob::OnFileInfoObtained.
74  void OnFileInfoObtained(base::File::Error result,
75                          const base::File::Info& file_info);
76
77  // Called when DriveFileStreamReader::Read is completed.
78  void OnReadCompleted(int read_result);
79
80  void* const profile_id_;
81
82  // The range of the file to be returned.
83  net::HttpByteRange byte_range_;
84  int64 remaining_bytes_;
85
86  scoped_refptr<storage::FileSystemContext> file_system_context_;
87  storage::FileSystemURL file_system_url_;
88  std::string mime_type_;
89  scoped_ptr<storage::FileStreamReader> stream_reader_;
90  GURL redirect_url_;
91
92  // This should remain the last member so it'll be destroyed first and
93  // invalidate its weak pointers before other members are destroyed.
94  base::WeakPtrFactory<ExternalFileURLRequestJob> weak_ptr_factory_;
95  DISALLOW_COPY_AND_ASSIGN(ExternalFileURLRequestJob);
96};
97
98}  // namespace chromeos
99
100#endif  // CHROME_BROWSER_CHROMEOS_FILEAPI_EXTERNAL_FILE_URL_REQUEST_JOB_H_
101