OLD | NEW |
---|---|
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "content/browser/appcache/appcache_url_loader_job.h" | 5 #include "content/browser/appcache/appcache_url_loader_job.h" |
6 #include "content/browser/appcache/appcache_entry.h" | 6 #include "content/browser/appcache/appcache_histograms.h" |
7 #include "content/public/common/resource_type.h" | |
8 #include "net/base/io_buffer.h" | |
7 | 9 |
8 namespace content { | 10 namespace content { |
9 | 11 |
10 AppCacheURLLoaderJob::~AppCacheURLLoaderJob() {} | 12 AppCacheURLLoaderJob::~AppCacheURLLoaderJob() {} |
11 | 13 |
12 void AppCacheURLLoaderJob::Kill() {} | 14 void AppCacheURLLoaderJob::Kill() {} |
13 | 15 |
14 bool AppCacheURLLoaderJob::IsStarted() const { | 16 bool AppCacheURLLoaderJob::IsStarted() const { |
15 return false; | 17 return delivery_type_ != AWAITING_DELIVERY_ORDERS; |
16 } | |
17 | |
18 bool AppCacheURLLoaderJob::IsWaiting() const { | |
19 return false; | |
20 } | |
21 | |
22 bool AppCacheURLLoaderJob::IsDeliveringAppCacheResponse() const { | |
23 return false; | |
24 } | |
25 | |
26 bool AppCacheURLLoaderJob::IsDeliveringNetworkResponse() const { | |
27 return false; | |
28 } | |
29 | |
30 bool AppCacheURLLoaderJob::IsDeliveringErrorResponse() const { | |
31 return false; | |
32 } | |
33 | |
34 bool AppCacheURLLoaderJob::IsCacheEntryNotFound() const { | |
35 return false; | |
36 } | 18 } |
37 | 19 |
38 void AppCacheURLLoaderJob::DeliverAppCachedResponse(const GURL& manifest_url, | 20 void AppCacheURLLoaderJob::DeliverAppCachedResponse(const GURL& manifest_url, |
39 int64_t cache_id, | 21 int64_t cache_id, |
40 const AppCacheEntry& entry, | 22 const AppCacheEntry& entry, |
41 bool is_fallback) {} | 23 bool is_fallback) { |
24 delivery_type_ = APPCACHED_DELIVERY; | |
42 | 25 |
43 void AppCacheURLLoaderJob::DeliverNetworkResponse() {} | 26 AppCacheHistograms::AddAppCacheJobStartDelaySample(base::TimeTicks::Now() - |
27 start_time_tick_); | |
44 | 28 |
45 void AppCacheURLLoaderJob::DeliverErrorResponse() {} | 29 manifest_url_ = manifest_url; |
30 cache_id_ = cache_id; | |
31 entry_ = entry; | |
32 is_fallback_ = is_fallback; | |
33 | |
34 storage_->LoadResponseInfo(manifest_url_, entry_.response_id(), this); | |
35 } | |
36 | |
37 void AppCacheURLLoaderJob::DeliverNetworkResponse() { | |
38 delivery_type_ = NETWORK_DELIVERY; | |
39 | |
40 AppCacheHistograms::AddNetworkJobStartDelaySample(base::TimeTicks::Now() - | |
41 start_time_tick_); | |
42 | |
43 if (delegate_) | |
44 delegate_->SendNetworkResponse(); | |
45 } | |
46 | |
47 void AppCacheURLLoaderJob::DeliverErrorResponse() { | |
48 delivery_type_ = ERROR_DELIVERY; | |
49 | |
50 AppCacheHistograms::AddErrorJobStartDelaySample(base::TimeTicks::Now() - | |
51 start_time_tick_); | |
52 | |
53 if (delegate_) | |
54 delegate_->SendErrorResponse(); | |
55 } | |
46 | 56 |
47 const GURL& AppCacheURLLoaderJob::GetURL() const { | 57 const GURL& AppCacheURLLoaderJob::GetURL() const { |
48 return url_; | 58 return request_.url; |
49 } | 59 } |
50 | 60 |
51 AppCacheURLLoaderJob::AppCacheURLLoaderJob() {} | 61 AppCacheURLLoaderJob* AppCacheURLLoaderJob::AsURLLoaderJob() { |
62 return this; | |
63 } | |
64 | |
65 AppCacheURLLoaderJob::AppCacheURLLoaderJob(const ResourceRequest& request, | |
66 AppCacheStorage* storage) | |
67 : request_(request), | |
68 delegate_(nullptr), | |
69 storage_(storage), | |
70 start_time_tick_(base::TimeTicks::Now()), | |
71 cache_id_(kAppCacheNoCacheId), | |
72 is_fallback_(false), | |
73 buffer_(nullptr) {} | |
74 | |
75 void AppCacheURLLoaderJob::OnResponseInfoLoaded( | |
76 AppCacheResponseInfo* response_info, | |
77 int64_t response_id) { | |
78 DCHECK(IsDeliveringAppCacheResponse()); | |
79 | |
80 if (response_info) { | |
81 info_ = response_info; | |
82 reader_.reset( | |
83 storage_->CreateResponseReader(manifest_url_, entry_.response_id())); | |
84 | |
85 // TODO(ananta) | |
86 // Handle range requests. | |
87 | |
88 buffer_ = | |
89 new net::IOBuffer(static_cast<size_t>(info_->response_data_size())); | |
90 reader_->ReadData(buffer_.get(), info_->response_data_size(), | |
91 base::Bind(&AppCacheURLLoaderJob::OnReadComplete, | |
92 base::Unretained(this))); | |
93 if (delegate_) | |
94 delegate_->HandleAppCacheResponseStart(response_info); | |
95 } else { | |
96 // Error case here. We fallback to the network. | |
97 DeliverNetworkResponse(); | |
98 AppCacheHistograms::CountResponseRetrieval( | |
99 false, IsResourceTypeFrame(request_.resource_type), | |
100 manifest_url_.GetOrigin()); | |
101 | |
102 cache_entry_not_found_ = true; | |
103 } | |
104 } | |
105 | |
106 void AppCacheURLLoaderJob::OnCacheLoaded(AppCache* cache, int64_t cache_id) { | |
107 NOTREACHED() << "Unhandled at the moment."; | |
108 } | |
109 | |
110 void AppCacheURLLoaderJob::OnReadComplete(int result) { | |
111 DLOG(WARNING) << "AppCache read completed with result: " << result; | |
112 | |
113 bool is_main_resource = IsResourceTypeFrame(request_.resource_type); | |
114 | |
115 if (result == 0) { | |
116 AppCacheHistograms::CountResponseRetrieval(true, is_main_resource, | |
117 manifest_url_.GetOrigin()); | |
118 } else if (result < 0) { | |
119 // Error case here. We fallback to the network. | |
120 DeliverNetworkResponse(); | |
121 AppCacheHistograms::CountResponseRetrieval(false, is_main_resource, | |
122 manifest_url_.GetOrigin()); | |
123 return; | |
124 } | |
125 if (delegate_) | |
126 delegate_->HandleAppCacheData(buffer_.get(), result); | |
michaeln
2017/06/02 01:18:00
What causes the reader to read more? We need to ca
ananta
2017/06/02 03:19:57
Thanks for pointing that out. We now control this
ananta
2017/06/03 01:55:55
Please look at the updated patch. We continue read
| |
127 } | |
52 | 128 |
53 } // namespace content | 129 } // namespace content |
OLD | NEW |