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

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: comments addressed Created 5 years, 1 month 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
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 explicit ResourceMultiBuffer(UrlData* url_data_);
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 { kUnspecified, kAnonymous, kUseCredentials };
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::TimeTicks 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::TimeTicks 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::TimeTicks 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 base::TimeTicks last_used_;
165
166 // Expiration time according to http headers.
167 base::TimeTicks valid_until_;
168
169 // Last modification time according to http headers.
170 base::Time last_modified_;
171
172 ResourceMultiBuffer multibuffer_;
173 std::vector<RedirectCB> redirect_callbacks_;
174
175 blink::WebFrame* frame_;
176
177 base::ThreadChecker thread_checker_;
178 DISALLOW_COPY_AND_ASSIGN(UrlData);
179 };
180
181 // The UrlIndex lets you look up UrlData instances by url.
182 class MEDIA_BLINK_EXPORT UrlIndex {
183 public:
184 explicit UrlIndex(blink::WebFrame*);
185 virtual ~UrlIndex();
186
187 // Look up an UrlData in the index and return it. If none is found,
188 // create a new one. Note that newly created UrlData entries are NOT
189 // added to the index, instead you must call TryInsert on them after
190 // initializing relevant parameters, like whether it support
191 // ranges and it's last modified time.
192 scoped_refptr<UrlData> GetByUrl(const GURL& gurl,
193 UrlData::CORSMode cors_mode);
194
195 // Add the given UrlData to the index if possible. If a better UrlData
196 // is already present in the index, return it instead. (If not, we just
197 // return the given UrlData.) Please make sure to initialize all the data
198 // that can be gathered from HTTP headers in |url_data| before calling this.
199 // In particular, the following fields are important:
200 // o range_supported: Entries which do not support ranges cannot be
201 // shared and are not added to the index.
202 // o valid_until, last_used: Entries have to be valid to be inserted
203 // into the index, this means that they have to have been recently
204 // used or have an Expires: header that says when they stop being valid.
205 // o last_modified: Expired cache entries can be re-used if last_modified
206 // matches.
207 // TODO(hubbe): Add etag support.
208 scoped_refptr<UrlData> TryInsert(const scoped_refptr<UrlData>& url_data);
209
210 blink::WebFrame* frame() const { return frame_; }
211
212 private:
213 friend class UrlData;
214 friend class ResourceMultiBuffer;
215 void RemoveUrlDataIfEmpty(const scoped_refptr<UrlData>& url_data);
216
217 // Virtual so we can override it in tests.
218 virtual scoped_refptr<UrlData> NewUrlData(const GURL& url,
219 UrlData::CORSMode cors_mode);
220
221 std::map<UrlData::KeyType, scoped_refptr<UrlData>> by_url_;
222 blink::WebFrame* frame_;
223 scoped_refptr<MultiBuffer::GlobalLRU> lru_;
224 bool prune_cb_active_;
225
226 protected:
227 base::WeakPtrFactory<UrlIndex> weak_factory_;
228 };
229
230 } // namespace media
231 #endif // MEDIA_BLINK_URL_INDEX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698