OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_REQUEST_INFO_H_ | 5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_REQUEST_INFO_H_ |
6 #define CONTENT_BROWSER_BACKGROUND_FETCH_REQUEST_INFO_H_ | 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_REQUEST_INFO_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/time/time.h" |
14 #include "content/browser/background_fetch/background_fetch_constants.h" | 15 #include "content/browser/background_fetch/background_fetch_constants.h" |
15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
16 #include "content/common/service_worker/service_worker_types.h" | 17 #include "content/common/service_worker/service_worker_types.h" |
17 #include "content/public/browser/download_interrupt_reasons.h" | 18 #include "content/public/browser/download_interrupt_reasons.h" |
18 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 namespace content { | 22 namespace content { |
22 | 23 |
| 24 class DownloadItem; |
| 25 |
23 // Simple class to encapsulate the components of a fetch request. | 26 // Simple class to encapsulate the components of a fetch request. |
24 // TODO(peter): This can likely change to have a single owner, and thus become | 27 // TODO(peter): This can likely change to have a single owner, and thus become |
25 // an std::unique_ptr<>, when persistent storage has been implemented. | 28 // an std::unique_ptr<>, when persistent storage has been implemented. |
26 class CONTENT_EXPORT BackgroundFetchRequestInfo | 29 class CONTENT_EXPORT BackgroundFetchRequestInfo |
27 : public base::RefCountedThreadSafe<BackgroundFetchRequestInfo> { | 30 : public base::RefCountedThreadSafe<BackgroundFetchRequestInfo> { |
28 public: | 31 public: |
29 BackgroundFetchRequestInfo(int request_index, | 32 BackgroundFetchRequestInfo(int request_index, |
30 const ServiceWorkerFetchRequest& fetch_request); | 33 const ServiceWorkerFetchRequest& fetch_request); |
31 | 34 |
32 // Returns the index of this request in the larger Background Fetch fetch. | 35 // Populates the cached state for the in-progress |download_item|. |
33 // Should only be consumed by the BackgroundFetchDataManager. | 36 void PopulateDownloadState(DownloadItem* download_item, |
| 37 DownloadInterruptReason download_interrupt_reason); |
| 38 |
| 39 // Populates the response portion of this object from the information made |
| 40 // available in the |download_item|. |
| 41 void PopulateResponseFromDownloadItem(DownloadItem* download_item); |
| 42 |
| 43 // Returns the index of this request within a Background Fetch registration. |
34 int request_index() const { return request_index_; } | 44 int request_index() const { return request_index_; } |
35 | 45 |
36 // Returns the Fetch API request object this object encapsulates. | 46 // Returns the Fetch API Request object that details the developer's request. |
37 const ServiceWorkerFetchRequest& fetch_request() const { | 47 const ServiceWorkerFetchRequest& fetch_request() const { |
38 return fetch_request_; | 48 return fetch_request_; |
39 } | 49 } |
40 | 50 |
41 const GURL& GetURL() const; | 51 // Returns the URL chain for the response, including redirects. |
| 52 const std::vector<GURL>& GetURLChain() const; |
42 | 53 |
43 DownloadItem::DownloadState state() const { return state_; } | 54 // Returns the absolute path to the file in which the response is stored. |
44 void set_state(DownloadItem::DownloadState state) { state_ = state; } | 55 const base::FilePath& GetFilePath() const; |
45 | 56 |
46 const std::string& download_guid() const { return download_guid_; } | 57 // Returns the size of the file containing the response, in bytes. |
47 void set_download_guid(const std::string& download_guid) { | 58 int64_t GetFileSize() const; |
48 download_guid_ = download_guid; | |
49 } | |
50 | 59 |
51 DownloadInterruptReason interrupt_reason() const { return interrupt_reason_; } | 60 // Returns the time at which the response was completed. |
52 void set_interrupt_reason(DownloadInterruptReason reason) { | 61 const base::Time& GetResponseTime() const; |
53 interrupt_reason_ = reason; | |
54 } | |
55 | |
56 const base::FilePath& file_path() const { return file_path_; } | |
57 void set_file_path(const base::FilePath& file_path) { | |
58 file_path_ = file_path; | |
59 } | |
60 | |
61 int64_t received_bytes() const { return received_bytes_; } | |
62 void set_received_bytes(int64_t received_bytes) { | |
63 received_bytes_ = received_bytes; | |
64 } | |
65 | |
66 bool IsComplete() const; | |
67 | 62 |
68 private: | 63 private: |
69 friend class base::RefCountedThreadSafe<BackgroundFetchRequestInfo>; | 64 friend class base::RefCountedThreadSafe<BackgroundFetchRequestInfo>; |
70 | 65 |
71 ~BackgroundFetchRequestInfo(); | 66 ~BackgroundFetchRequestInfo(); |
72 | 67 |
73 // Index of this request within a Background Fetch registration. | 68 // ---- Data associated with the request ------------------------------------- |
| 69 |
74 int request_index_ = kInvalidBackgroundFetchRequestIndex; | 70 int request_index_ = kInvalidBackgroundFetchRequestIndex; |
75 | |
76 // The request information provided by the developer. | |
77 ServiceWorkerFetchRequest fetch_request_; | 71 ServiceWorkerFetchRequest fetch_request_; |
78 | 72 |
| 73 // ---- Data associated with the in-progress download ------------------------ |
| 74 |
| 75 // Indicates whether download progress data has been populated. |
| 76 bool download_state_populated_ = false; |
| 77 |
79 std::string download_guid_; | 78 std::string download_guid_; |
| 79 DownloadItem::DownloadState download_state_ = DownloadItem::IN_PROGRESS; |
80 | 80 |
81 // The following members do not need to be persisted, they can be reset after | 81 // ---- Data associated with the response ------------------------------------ |
82 // a chrome restart. | 82 |
83 DownloadItem::DownloadState state_ = | 83 // Indicates whether response data has been populated. |
84 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE; | 84 bool response_data_populated_ = false; |
85 DownloadInterruptReason interrupt_reason_ = | 85 |
86 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE; | 86 std::vector<GURL> url_chain_; |
87 base::FilePath file_path_; | 87 base::FilePath file_path_; |
88 int64_t received_bytes_ = 0; | 88 int64_t file_size_ = 0; |
| 89 base::Time response_time_; |
89 | 90 |
90 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchRequestInfo); | 91 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchRequestInfo); |
91 }; | 92 }; |
92 | 93 |
93 } // namespace content | 94 } // namespace content |
94 | 95 |
95 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_REQUEST_INFO_H_ | 96 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_REQUEST_INFO_H_ |
OLD | NEW |