ea5ab93e675c4360612de18009073381ea196c34
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / fileapi / external_file_url_request_job.h
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
20 namespace net {
21 class IOBuffer;
22 class NetworkDelegate;
23 class URLRequest;
24 }  // namespace net
25
26 namespace 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.
32 // TODO(hirono): After removing MHTML support, stop to use the special
33 // externalfile: scheme and use filesystem: URL directly.  crbug.com/415455
34 class ExternalFileURLRequestJob : public net::URLRequestJob {
35  public:
36   // Scope of isolated file system.
37   class IsolatedFileSystemScope {
38    public:
39     explicit IsolatedFileSystemScope(const std::string& file_system_id);
40     ~IsolatedFileSystemScope();
41
42    private:
43     std::string file_system_id_;
44     DISALLOW_COPY_AND_ASSIGN(IsolatedFileSystemScope);
45   };
46
47   // Callback to take results from an internal helper defined in
48   // drive_url_request_job.cc.
49   typedef base::Callback<void(
50       net::Error,
51       const scoped_refptr<storage::FileSystemContext>& file_system_context,
52       scoped_ptr<IsolatedFileSystemScope> isolated_file_system_scope,
53       const storage::FileSystemURL& file_system_url,
54       const std::string& mime_type)> HelperCallback;
55
56   ExternalFileURLRequestJob(void* profile_id,
57                             net::URLRequest* request,
58                             net::NetworkDelegate* network_delegate);
59
60   // net::URLRequestJob overrides:
61   virtual void SetExtraRequestHeaders(
62       const net::HttpRequestHeaders& headers) override;
63   virtual void Start() override;
64   virtual void Kill() override;
65   virtual bool GetMimeType(std::string* mime_type) const override;
66   virtual bool IsRedirectResponse(GURL* location,
67                                   int* http_status_code) override;
68   virtual bool ReadRawData(net::IOBuffer* buf,
69                            int buf_size,
70                            int* bytes_read) override;
71
72  protected:
73   virtual ~ExternalFileURLRequestJob();
74
75  private:
76   // Called from an internal helper class defined in drive_url_request_job.cc,
77   // which is running on the UI thread.
78   void OnHelperResultObtained(
79       net::Error error,
80       const scoped_refptr<storage::FileSystemContext>& file_system_context,
81       scoped_ptr<IsolatedFileSystemScope> isolated_file_system_scope,
82       const storage::FileSystemURL& file_system_url,
83       const std::string& mime_type);
84
85   // Called from FileSystemBackend::GetRedirectURLForContents.
86   void OnRedirectURLObtained(const GURL& redirect_url);
87
88   // Called from DriveURLRequestJob::OnFileInfoObtained.
89   void OnFileInfoObtained(base::File::Error result,
90                           const base::File::Info& file_info);
91
92   // Called when DriveFileStreamReader::Read is completed.
93   void OnReadCompleted(int read_result);
94
95   void* const profile_id_;
96
97   // The range of the file to be returned.
98   net::HttpByteRange byte_range_;
99   int64 remaining_bytes_;
100
101   scoped_refptr<storage::FileSystemContext> file_system_context_;
102   scoped_ptr<IsolatedFileSystemScope> isolated_file_system_scope_;
103   storage::FileSystemURL file_system_url_;
104   std::string mime_type_;
105   scoped_ptr<storage::FileStreamReader> stream_reader_;
106   GURL redirect_url_;
107
108   // This should remain the last member so it'll be destroyed first and
109   // invalidate its weak pointers before other members are destroyed.
110   base::WeakPtrFactory<ExternalFileURLRequestJob> weak_ptr_factory_;
111   DISALLOW_COPY_AND_ASSIGN(ExternalFileURLRequestJob);
112 };
113
114 }  // namespace chromeos
115
116 #endif  // CHROME_BROWSER_CHROMEOS_FILEAPI_EXTERNAL_FILE_URL_REQUEST_JOB_H_