qinmin | 120a155 | 2014-11-26 03:02:16 | [diff] [blame] | 1 | // Copyright (c) 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 CONTENT_BROWSER_ANDROID_URL_REQUEST_CONTENT_JOB_H_ |
| 6 | #define CONTENT_BROWSER_ANDROID_URL_REQUEST_CONTENT_JOB_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/files/file_path.h" |
| 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "content/common/content_export.h" |
| 15 | #include "net/http/http_byte_range.h" |
| 16 | #include "net/url_request/url_request.h" |
| 17 | #include "net/url_request/url_request_job.h" |
| 18 | |
| 19 | namespace base { |
| 20 | class TaskRunner; |
| 21 | } |
| 22 | |
| 23 | namespace file_util { |
| 24 | struct FileInfo; |
| 25 | } |
| 26 | |
| 27 | namespace net { |
| 28 | class FileStream; |
| 29 | } |
| 30 | |
| 31 | namespace content { |
| 32 | |
| 33 | // A request job that handles reading content URIs |
| 34 | class CONTENT_EXPORT URLRequestContentJob : public net::URLRequestJob { |
| 35 | public: |
| 36 | URLRequestContentJob( |
| 37 | net::URLRequest* request, |
| 38 | net::NetworkDelegate* network_delegate, |
| 39 | const base::FilePath& content_path, |
| 40 | const scoped_refptr<base::TaskRunner>& content_task_runner); |
| 41 | |
| 42 | // net::URLRequestJob: |
| 43 | void Start() override; |
| 44 | void Kill() override; |
| 45 | bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; |
| 46 | bool IsRedirectResponse(GURL* location, int* http_status_code) override; |
| 47 | bool GetMimeType(std::string* mime_type) const override; |
| 48 | void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override; |
| 49 | |
| 50 | protected: |
| 51 | ~URLRequestContentJob() override; |
| 52 | |
| 53 | private: |
| 54 | // Meta information about the content URI. It's used as a member in the |
| 55 | // URLRequestContentJob and also passed between threads because disk access is |
| 56 | // necessary to obtain it. |
| 57 | struct ContentMetaInfo { |
| 58 | ContentMetaInfo(); |
| 59 | // Flag showing whether the content URI exists. |
| 60 | bool content_exists; |
| 61 | // Size of the content URI. |
| 62 | int64 content_size; |
| 63 | // Mime type associated with the content URI. |
| 64 | std::string mime_type; |
| 65 | }; |
| 66 | |
| 67 | // Fetches content URI info on a background thread. |
| 68 | static void FetchMetaInfo(const base::FilePath& content_path, |
| 69 | ContentMetaInfo* meta_info); |
| 70 | |
| 71 | // Callback after fetching content URI info on a background thread. |
| 72 | void DidFetchMetaInfo(const ContentMetaInfo* meta_info); |
| 73 | |
| 74 | // Callback after opening content URI on a background thread. |
| 75 | void DidOpen(int result); |
| 76 | |
| 77 | // Callback after seeking to the beginning of |byte_range_| in the content URI |
| 78 | // on a background thread. |
| 79 | void DidSeek(int64 result); |
| 80 | |
| 81 | // Callback after data is asynchronously read from the content URI into |buf|. |
| 82 | void DidRead(scoped_refptr<net::IOBuffer> buf, int result); |
| 83 | |
| 84 | // The full path of the content URI. |
| 85 | base::FilePath content_path_; |
| 86 | |
| 87 | scoped_ptr<net::FileStream> stream_; |
| 88 | ContentMetaInfo meta_info_; |
| 89 | const scoped_refptr<base::TaskRunner> content_task_runner_; |
| 90 | |
| 91 | net::HttpByteRange byte_range_; |
| 92 | int64 remaining_bytes_; |
| 93 | |
| 94 | bool io_pending_; |
| 95 | |
| 96 | base::WeakPtrFactory<URLRequestContentJob> weak_ptr_factory_; |
| 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(URLRequestContentJob); |
| 99 | }; |
| 100 | |
| 101 | } // namespace content |
| 102 | |
| 103 | #endif // CONTENT_BROWSER_ANDROID_URL_REQUEST_CONTENT_JOB_H_ |