Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: media/blink/url_index.h

Issue 1399603003: Tie multibuffers to URLs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@media_cache
Patch Set: formatted Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/blink/resource_multibuffer_data_provider_unittest.cc ('k') | media/blink/url_index.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #ifndef MEDIA_BLINK_URL_INDEX_H_
6 #define MEDIA_BLINK_URL_INDEX_H_
7
8 #include <map>
9 #include <vector>
10
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "media/blink/lru.h"
16 #include "media/blink/media_blink_export.h"
17 #include "media/blink/multibuffer.h"
18 #include "third_party/WebKit/public/web/WebFrame.h"
19 #include "url/gurl.h"
20
21 namespace media {
22
23 const int64 kPositionNotSpecified = -1;
24
25 class UrlData;
26
27 // A multibuffer for loading media resources which knows
28 // how to create MultiBufferDataProviders to load data
29 // into the cache.
30 class MEDIA_BLINK_EXPORT ResourceMultiBuffer
31 : NON_EXPORTED_BASE(public MultiBuffer) {
32 public:
33 ResourceMultiBuffer(UrlData* url_data_, int block_shift);
34 ~ResourceMultiBuffer() override;
35
36 // MultiBuffer implementation.
37 scoped_ptr<MultiBuffer::DataProvider> CreateWriter(
38 const BlockId& pos) override;
39 bool RangeSupported() const override;
40 void OnEmpty() override;
41
42 protected:
43 // Do not access from destructor, it is a pointer to the
44 // object that contains us.
45 UrlData* url_data_;
46 };
47
48 class UrlIndex;
49
50 // All the data & metadata for a single resource.
51 // Data is cached using a MultiBuffer instance.
52 class MEDIA_BLINK_EXPORT UrlData : public base::RefCounted<UrlData> {
53 public:
54 // Keep in sync with WebMediaPlayer::CORSMode.
55 enum CORSMode { CORS_UNSPECIFIED, CORS_ANONYMOUS, CORS_USE_CREDENTIALS };
56 typedef std::pair<GURL, CORSMode> KeyType;
57
58 // Accessors
59 const GURL& url() const { return url_; }
60
61 // Cross-origin access mode
62 CORSMode cors_mode() const { return cors_mode_; }
63
64 // Are HTTP range requests supported?
65 bool range_supported() const { return range_supported_; }
66
67 // True if we found a reason why this URL won't be stored in the
68 // HTTP disk cache.
69 bool cacheable() const { return cacheable_; }
70
71 // Last used time.
72 base::Time last_used() const { return last_used_; }
73
74 // Last modified time.
75 base::Time last_modified() const { return last_modified_; }
76
77 // Expiration time.
78 base::Time valid_until() const { return valid_until_; }
79
80 // The key used by UrlIndex to find this UrlData.
81 KeyType key() const;
82
83 // Length of data associated with url or |kPositionNotSpecified|
84 int64 length() const { return length_; }
85
86 // Returns the number of blocks cached for this resource.
87 size_t CachedSize();
88
89 // Returns our url_index, or nullptr if it's been destroyed.
90 UrlIndex* url_index() const { return url_index_.get(); }
91
92 // Notifies the url index that this is currently used.
93 // The url <-> URLData mapping will be eventually be invalidated if
94 // this is not called regularly.
95 void Use();
96
97 // Setters.
98 void set_length(int64 length);
99 void set_cacheable(bool cacheable);
100 void set_valid_until(base::Time valid_until);
101 void set_range_supported();
102 void set_last_modified(base::Time last_modified);
103
104 // A redirect has occured (or we've found a better UrlData for the same
105 // resource).
106 void RedirectTo(const scoped_refptr<UrlData>& to);
107
108 // Fail, tell all clients that a failure has occured.
109 void Fail();
110
111 // Callback for receving notifications when a redirect occurs.
112 typedef base::Callback<void(const scoped_refptr<UrlData>&)> RedirectCB;
113
114 // Register a callback to be called when a redirect occurs.
115 // Callbacks are cleared when a redirect occurs, so clients must call
116 // OnRedirect again if they wish to continue receiving callbacks.
117 void OnRedirect(const RedirectCB& cb);
118
119 // Returns true it is valid to keep using this to access cached data.
120 // A single media player instance may choose to ignore this for resources
121 // that have already been opened.
122 bool Valid() const;
123
124 // Virtual so we can override it for testing.
125 virtual ResourceMultiBuffer* multibuffer();
126
127 // Accessor
128 blink::WebFrame* frame() const { return frame_; }
129
130 protected:
131 UrlData(const GURL& url,
132 CORSMode cors_mode,
133 const base::WeakPtr<UrlIndex>& url_index);
134 virtual ~UrlData();
135
136 private:
137 friend class ResourceMultiBuffer;
138 friend class UrlIndex;
139 friend class base::RefCounted<UrlData>;
140
141 void OnEmpty();
142 void MergeFrom(const scoped_refptr<UrlData>& other);
143
144 // Url we represent, note that there may be multiple UrlData for
145 // the same url.
146 const GURL url_;
147
148 // Cross-origin access mode.
149 const CORSMode cors_mode_;
150
151 base::WeakPtr<UrlIndex> url_index_;
152
153 // Length of resource this url points to. (in bytes)
154 int64 length_;
155
156 // Does the server support ranges?
157 bool range_supported_;
158
159 // Set to false if we have reason to beleive the chrome disk cache
160 // will not cache this url.
161 bool cacheable_;
162
163 // Last time some media time used this resource.
164 // Note that we use base::Time rather than base::TimeTicks because
165 // TimeTicks will stop advancing when a machine goes to sleep.
166 // base::Time can go backwards, jump hours at a time and be generally
167 // unpredictable, but it doesn't stop, which is preferable here.
168 // (False negatives are better than false positivies.)
169 base::Time last_used_;
170
171 // Expiration time according to http headers.
172 base::Time valid_until_;
173
174 // Last modification time according to http headers.
175 base::Time last_modified_;
176
177 ResourceMultiBuffer multibuffer_;
178 std::vector<RedirectCB> redirect_callbacks_;
179
180 blink::WebFrame* frame_;
181
182 base::ThreadChecker thread_checker_;
183 DISALLOW_COPY_AND_ASSIGN(UrlData);
184 };
185
186 // The UrlIndex lets you look up UrlData instances by url.
187 class MEDIA_BLINK_EXPORT UrlIndex {
188 public:
189 explicit UrlIndex(blink::WebFrame*);
190 UrlIndex(blink::WebFrame*, int block_shift);
191 virtual ~UrlIndex();
192
193 // Look up an UrlData in the index and return it. If none is found,
194 // create a new one. Note that newly created UrlData entries are NOT
195 // added to the index, instead you must call TryInsert on them after
196 // initializing relevant parameters, like whether it support
197 // ranges and it's last modified time.
198 scoped_refptr<UrlData> GetByUrl(const GURL& gurl,
199 UrlData::CORSMode cors_mode);
200
201 // Add the given UrlData to the index if possible. If a better UrlData
202 // is already present in the index, return it instead. (If not, we just
203 // return the given UrlData.) Please make sure to initialize all the data
204 // that can be gathered from HTTP headers in |url_data| before calling this.
205 // In particular, the following fields are important:
206 // o range_supported: Entries which do not support ranges cannot be
207 // shared and are not added to the index.
208 // o valid_until, last_used: Entries have to be valid to be inserted
209 // into the index, this means that they have to have been recently
210 // used or have an Expires: header that says when they stop being valid.
211 // o last_modified: Expired cache entries can be re-used if last_modified
212 // matches.
213 // TODO(hubbe): Add etag support.
214 scoped_refptr<UrlData> TryInsert(const scoped_refptr<UrlData>& url_data);
215
216 blink::WebFrame* frame() const { return frame_; }
217
218 private:
219 friend class UrlData;
220 friend class ResourceMultiBuffer;
221 void RemoveUrlDataIfEmpty(const scoped_refptr<UrlData>& url_data);
222
223 // Virtual so we can override it in tests.
224 virtual scoped_refptr<UrlData> NewUrlData(const GURL& url,
225 UrlData::CORSMode cors_mode);
226
227 std::map<UrlData::KeyType, scoped_refptr<UrlData>> by_url_;
228 blink::WebFrame* frame_;
229 scoped_refptr<MultiBuffer::GlobalLRU> lru_;
230 bool prune_cb_active_;
231
232 // log2 of block size in multibuffer cache. Defaults to kBlockSizeShift.
233 // Currently only changed for testing purposes.
234 const int block_shift_;
235
236 protected:
237 base::WeakPtrFactory<UrlIndex> weak_factory_;
238 };
239
240 } // namespace media
241 #endif // MEDIA_BLINK_URL_INDEX_H_
OLDNEW
« no previous file with comments | « media/blink/resource_multibuffer_data_provider_unittest.cc ('k') | media/blink/url_index.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698