1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_URL_REQUEST_JOB_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_URL_REQUEST_JOB_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "chrome/browser/chromeos/drive/file_errors.h"
16 #include "net/http/http_byte_range.h"
17 #include "net/url_request/url_request_job.h"
20 class SequencedTaskRunner;
25 class NetworkDelegate;
31 class DriveFileStreamReader;
32 class FileSystemInterface;
35 // DriveURLRequestJob is the gateway between network-level drive:...
36 // requests for drive resources and FileSystem. It exposes content URLs
37 // formatted as drive:<drive-file-path>.
38 // The methods should be run on IO thread, and the operations to communicate
39 // with a locally cached file will run on |file_task_runner|.
40 class DriveURLRequestJob : public net::URLRequestJob {
43 // Callback to return the FileSystemInterface instance. This is an
44 // injecting point for testing.
45 // Note that the callback will be copied between threads (IO and UI), and
46 // will be called on UI thread.
47 typedef base::Callback<FileSystemInterface*()> FileSystemGetter;
49 DriveURLRequestJob(const FileSystemGetter& file_system_getter,
50 base::SequencedTaskRunner* file_task_runner,
51 net::URLRequest* request,
52 net::NetworkDelegate* network_delegate);
54 // net::URLRequestJob overrides:
55 virtual void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers)
57 virtual void Start() OVERRIDE;
58 virtual void Kill() OVERRIDE;
59 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
60 virtual bool IsRedirectResponse(
61 GURL* location, int* http_status_code) OVERRIDE;
62 virtual bool ReadRawData(
63 net::IOBuffer* buf, int buf_size, int* bytes_read) OVERRIDE;
66 virtual ~DriveURLRequestJob();
69 // Called when the initialization of DriveFileStreamReader is completed.
70 void OnDriveFileStreamReaderInitialized(
71 int error, scoped_ptr<ResourceEntry> entry);
73 // Called when DriveFileStreamReader::Read is completed.
74 void OnReadCompleted(int read_result);
76 const FileSystemGetter file_system_getter_;
77 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
79 // The range of the file to be returned.
80 net::HttpByteRange byte_range_;
82 scoped_ptr<DriveFileStreamReader> stream_reader_;
83 scoped_ptr<ResourceEntry> entry_;
85 // This should remain the last member so it'll be destroyed first and
86 // invalidate its weak pointers before other members are destroyed.
87 base::WeakPtrFactory<DriveURLRequestJob> weak_ptr_factory_;
88 DISALLOW_COPY_AND_ASSIGN(DriveURLRequestJob);
93 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_URL_REQUEST_JOB_H_