OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 #include <set> | |
6 #include <utility> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/time/time.h" | |
11 #include "media/blink/resource_multibuffer_data_provider.h" | |
12 #include "media/blink/url_index.h" | |
13 | |
14 namespace media { | |
15 | |
16 const int kBlockSizeShift = 15; // 1<<15 == 32kb | |
17 const int kUrlMappingTimeoutSeconds = 300; | |
18 | |
19 ResourceMultiBuffer::ResourceMultiBuffer(UrlData* url_data) | |
20 : MultiBuffer(kBlockSizeShift, url_data->url_index_->lru_), | |
21 url_data_(url_data), | |
22 frame_(url_data->url_index_->frame()) {} | |
xhwang
2015/11/19 23:34:18
So the DataProvider gets the frame from url_data_-
hubbe
2015/11/20 23:24:24
moved to UrlData, not sure if you think that's bet
| |
23 | |
24 ResourceMultiBuffer::~ResourceMultiBuffer() {} | |
25 | |
26 MultiBuffer::DataProvider* ResourceMultiBuffer::CreateWriter( | |
27 const MultiBufferBlockId& pos) { | |
28 ResourceMultiBufferDataProvider* ret = | |
29 new ResourceMultiBufferDataProvider(url_data_, pos); | |
30 ret->Start(); | |
31 return ret; | |
xhwang
2015/11/19 23:34:18
See comments above. If the caller owns the writer,
hubbe
2015/11/20 23:24:24
Done.
| |
32 } | |
33 | |
34 bool ResourceMultiBuffer::RangeSupported() const { | |
35 return url_data_->range_supported_; | |
36 } | |
37 | |
38 void ResourceMultiBuffer::OnEmpty() { | |
39 url_data_->OnEmpty(); | |
40 } | |
41 | |
42 UrlData::UrlData(const GURL& url, | |
43 CORSMode cors_mode, | |
44 const base::WeakPtr<UrlIndex>& url_index) | |
45 : url_(url), | |
46 cors_mode_(cors_mode), | |
47 url_index_(url_index), | |
48 length_(kPositionNotSpecified), | |
49 range_supported_(false), | |
50 cacheable_(false), | |
51 last_used_(), | |
52 multibuffer_(this) {} | |
53 | |
54 UrlData::~UrlData() { | |
55 if (url_index_) | |
56 url_index_->RemoveUrlData(this); | |
57 } | |
58 | |
59 std::pair<GURL, UrlData::CORSMode> UrlData::key() const { | |
60 DCHECK(thread_checker_.CalledOnValidThread()); | |
61 return std::make_pair(url(), cors_mode()); | |
62 } | |
63 | |
64 void UrlData::set_valid_until(base::TimeTicks t) { | |
xhwang
2015/11/19 23:34:18
nit: s/t/valid_until?
following the "no abbreviat
hubbe
2015/11/20 23:24:23
Done.
| |
65 DCHECK(thread_checker_.CalledOnValidThread()); | |
66 valid_until_ = t; | |
67 } | |
68 | |
69 void UrlData::MergeFrom(const scoped_refptr<UrlData>& other) { | |
70 DCHECK(thread_checker_.CalledOnValidThread()); | |
71 valid_until_ = std::max(valid_until_, other->valid_until_); | |
xhwang
2015/11/19 23:34:18
Sorry for the lack of context... Could you please
hubbe
2015/11/20 23:24:24
Comment added.
| |
72 set_length(other->length_); | |
xhwang
2015/11/19 23:34:18
Could you please explain why?
hubbe
2015/11/20 23:24:24
Done.
| |
73 cacheable_ &= other->cacheable_; | |
74 range_supported_ |= other->range_supported_; | |
xhwang
2015/11/19 23:34:18
why it's "or"?
hubbe
2015/11/20 23:24:23
Or is right, the & above it is probably not though
| |
75 if (last_modified_.is_null()) { | |
76 last_modified_ = other->last_modified_; | |
77 } | |
78 multibuffer()->MergeFrom(other->multibuffer()); | |
79 } | |
80 | |
81 void UrlData::set_cacheable(bool cacheable) { | |
82 DCHECK(thread_checker_.CalledOnValidThread()); | |
83 cacheable_ = cacheable; | |
84 } | |
85 | |
86 void UrlData::set_length(int64 length) { | |
87 DCHECK(thread_checker_.CalledOnValidThread()); | |
88 if (length != kPositionNotSpecified) { | |
89 length_ = length; | |
90 } | |
91 } | |
92 | |
93 void UrlData::RedirectTo(const scoped_refptr<UrlData>& url_data) { | |
94 DCHECK(thread_checker_.CalledOnValidThread()); | |
95 // Copy any cached data over to the new location. | |
96 url_data->multibuffer()->MergeFrom(multibuffer()); | |
97 | |
98 std::vector<RedirectCB> redirect_callbacks; | |
99 redirect_callbacks.swap(redirect_callbacks_); | |
100 for (const RedirectCB& cb : redirect_callbacks) { | |
101 cb.Run(url_data); | |
102 } | |
103 } | |
104 | |
105 void UrlData::Fail() { | |
106 DCHECK(thread_checker_.CalledOnValidThread()); | |
107 // Handled similar to a redirect. | |
108 std::vector<RedirectCB> redirect_callbacks; | |
109 redirect_callbacks.swap(redirect_callbacks_); | |
110 for (const RedirectCB& cb : redirect_callbacks) { | |
111 cb.Run(nullptr); | |
112 } | |
113 } | |
114 | |
115 void UrlData::OnRedirect(const RedirectCB& cb) { | |
116 DCHECK(thread_checker_.CalledOnValidThread()); | |
117 redirect_callbacks_.push_back(cb); | |
118 } | |
119 | |
120 void UrlData::Use() { | |
121 DCHECK(thread_checker_.CalledOnValidThread()); | |
122 last_used_ = base::TimeTicks::Now(); | |
123 } | |
124 | |
125 void UrlData::OnEmpty() { | |
126 DCHECK(thread_checker_.CalledOnValidThread()); | |
127 if (url_index_) | |
128 url_index_->RemoveUrlData(this); | |
129 } | |
130 | |
131 bool UrlData::Valid() const { | |
132 DCHECK(thread_checker_.CalledOnValidThread()); | |
133 base::TimeTicks now = base::TimeTicks::Now(); | |
134 if (!range_supported_) | |
135 return false; | |
136 // When ranges are not supported, we cannot re-use cached data. | |
137 if (valid_until_ > now) | |
138 return true; | |
139 if (now - last_used_ < | |
140 base::TimeDelta::FromSeconds(kUrlMappingTimeoutSeconds)) | |
141 return true; | |
142 return false; | |
143 } | |
144 | |
145 void UrlData::set_last_modified(base::Time last_modified) { | |
146 DCHECK(thread_checker_.CalledOnValidThread()); | |
147 last_modified_ = last_modified; | |
148 } | |
149 | |
150 void UrlData::set_range_supported() { | |
151 DCHECK(thread_checker_.CalledOnValidThread()); | |
152 range_supported_ = true; | |
153 } | |
154 | |
155 ResourceMultiBuffer* UrlData::multibuffer() { | |
156 DCHECK(thread_checker_.CalledOnValidThread()); | |
157 return &multibuffer_; | |
158 } | |
159 | |
160 size_t UrlData::CachedSize() { | |
161 DCHECK(thread_checker_.CalledOnValidThread()); | |
162 return multibuffer()->map().size(); | |
163 } | |
164 | |
165 UrlIndex::UrlIndex(blink::WebFrame* frame) | |
166 : frame_(frame), lru_(new MultiBuffer::GlobalLRU()), weak_factory_(this) {} | |
167 | |
168 UrlIndex::~UrlIndex() {} | |
169 | |
170 void UrlIndex::RemoveUrlData(const UrlData* url_data) { | |
171 auto i = by_url_.find(url_data->key()); | |
172 if (i != by_url_.end() && i->second == url_data) | |
173 by_url_.erase(i); | |
174 } | |
175 | |
176 scoped_refptr<UrlData> UrlIndex::GetByUrl(const GURL& gurl, | |
177 UrlData::CORSMode cors_mode) { | |
178 auto i = by_url_.find(std::make_pair(gurl, cors_mode)); | |
179 if (i != by_url_.end() && i->second->Valid()) { | |
180 return i->second; | |
181 } | |
182 return NewUrlData(gurl, cors_mode); | |
183 } | |
184 | |
185 UrlData* UrlIndex::NewUrlData(const GURL& url, UrlData::CORSMode cors_mode) { | |
186 return new UrlData(url, cors_mode, weak_factory_.GetWeakPtr()); | |
187 } | |
188 | |
189 scoped_refptr<UrlData> UrlIndex::TryInsert( | |
190 const scoped_refptr<UrlData>& urldata) { | |
191 std::map<UrlData::KeyType, scoped_refptr<UrlData>>::iterator i; | |
192 bool urldata_valid = urldata->Valid(); | |
xhwang
2015/11/19 23:34:18
s/urldata/url_data
hubbe
2015/11/20 23:24:23
Done.
| |
193 if (urldata_valid) { | |
194 i = by_url_.insert(std::make_pair(urldata->key(), urldata)).first; | |
195 } else { | |
196 i = by_url_.find(urldata->key()); | |
197 if (i == by_url_.end()) | |
198 return urldata; | |
199 } | |
200 if (i->second == urldata) | |
xhwang
2015/11/19 23:34:18
give i->second a more meaningful by assign it to a
hubbe
2015/11/20 23:24:23
Hmm, I changed it, but I'm not sure it's actually
| |
201 return urldata; | |
202 | |
203 // TODO(hubbe): Support etag validation. | |
204 if (!urldata->last_modified().is_null()) { | |
205 if (i->second->last_modified() != urldata->last_modified()) { | |
206 if (urldata_valid) | |
207 i->second = urldata; | |
208 return urldata; | |
209 } | |
210 } | |
211 // Check if we should replace the in-cache url data with our | |
212 // url data. | |
213 if (urldata_valid) { | |
214 if ((!i->second->Valid() || | |
215 urldata->CachedSize() > i->second->CachedSize())) { | |
216 i->second = urldata; | |
217 } else { | |
218 i->second->MergeFrom(urldata); | |
219 } | |
220 } | |
221 return i->second; | |
222 } | |
223 | |
224 } // namespace media | |
OLD | NEW |