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 #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 | |
liberato (no reviews please)
2015/11/23 19:07:10
since folks will want to create a ResourceMultiBuf
hubbe
2015/11/23 20:17:30
Ignored, as per discussion.
| |
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 UrlData* url_data_; | |
liberato (no reviews please)
2015/11/23 19:07:10
needs a comment that this cannot be accessed safel
hubbe
2015/11/23 20:17:30
Done.
| |
44 }; | |
45 | |
46 class UrlIndex; | |
47 | |
48 // All the data & metadata for a single resource. | |
49 // Data is cached using a MultiBuffer instance. | |
50 class MEDIA_BLINK_EXPORT UrlData : public base::RefCounted<UrlData> { | |
51 public: | |
52 // Keep in sync with WebMediaPlayer::CORSMode. | |
53 enum CORSMode { kUnspecified, kAnonymous, kUseCredentials }; | |
54 typedef std::pair<GURL, CORSMode> KeyType; | |
55 | |
56 // Accessors | |
57 const GURL& url() const { return url_; } | |
58 | |
59 // Cross-origin access mode | |
60 CORSMode cors_mode() const { return cors_mode_; } | |
61 | |
62 // Are HTTP range requests supported? | |
63 bool range_supported() const { return range_supported_; } | |
64 | |
65 // True if we found a reason why this URL won't be stored in the | |
66 // HTTP disk cache. | |
67 bool cacheable() const { return cacheable_; } | |
68 | |
69 // Last used time. | |
70 base::TimeTicks last_used() const { return last_used_; } | |
71 | |
72 // Last modified time. | |
73 base::Time last_modified() const { return last_modified_; } | |
74 | |
75 // Expiration time. | |
76 base::TimeTicks valid_until() const { return valid_until_; } | |
77 | |
78 // The key used by UrlIndex to find this UrlData. | |
79 KeyType key() const; | |
80 | |
81 // Length of data associated with url or |kPositionNotSpecified| | |
82 int64 length() const { return length_; } | |
83 | |
84 // Returns the number of blocks cached for this resource. | |
85 size_t CachedSize(); | |
86 | |
87 // Returns our url_index, or nullptr if it's been destroyed. | |
88 UrlIndex* url_index() const { return url_index_.get(); } | |
89 | |
90 // Notifies the url index that this is currently used. | |
91 // The url <-> URLData mapping will be eventually be invalidated if | |
92 // this is not called regularly. | |
93 void Use(); | |
94 | |
95 // Setters. | |
96 void set_length(int64 length); | |
97 void set_cacheable(bool cacheable); | |
98 void set_valid_until(base::TimeTicks valid_until); | |
99 void set_range_supported(); | |
100 void set_last_modified(base::Time last_modified); | |
101 | |
102 // A redirect has occured (or we've found a better UrlData for the same | |
103 // resource). | |
104 void RedirectTo(const scoped_refptr<UrlData>& to); | |
105 | |
106 // Fail, tell all clients that a failure has occured. | |
107 void Fail(); | |
108 | |
109 // Callback for receving notifications when a redirect occurs. | |
110 typedef base::Callback<void(const scoped_refptr<UrlData>&)> RedirectCB; | |
111 | |
112 // Register a callback to be called when a redirect occurs. | |
113 // Callbacks are cleared when a redirect occurs, so clients must call | |
114 // OnRedirect again if they wish to continue receiving callbacks. | |
115 void OnRedirect(const RedirectCB& cb); | |
116 | |
117 // Returns true it is valid to keep using this to access cached data. | |
118 // A single media player instance may choose to ignore this for resources | |
119 // that have already been opened. | |
120 bool Valid() const; | |
121 | |
122 // Virtual so we can override it for testing. | |
123 virtual ResourceMultiBuffer* multibuffer(); | |
124 | |
125 // Accessor | |
126 blink::WebFrame* frame() const { return frame_; } | |
127 | |
128 protected: | |
129 UrlData(const GURL& url, | |
130 CORSMode cors_mode, | |
131 const base::WeakPtr<UrlIndex>& url_index); | |
132 virtual ~UrlData(); | |
133 | |
134 private: | |
135 friend class ResourceMultiBuffer; | |
136 friend class UrlIndex; | |
137 friend class base::RefCounted<UrlData>; | |
138 | |
139 void OnEmpty(); | |
140 void MergeFrom(const scoped_refptr<UrlData>& other); | |
141 | |
142 // Url we represent, note that there may be multiple UrlData for | |
143 // the same url. | |
144 const GURL url_; | |
145 | |
146 // Cross-origin access mode. | |
147 const CORSMode cors_mode_; | |
148 | |
149 base::WeakPtr<UrlIndex> url_index_; | |
150 | |
151 // Length of resource this url points to. (in bytes) | |
152 int64 length_; | |
153 | |
154 // Does the server support ranges? | |
155 bool range_supported_; | |
156 | |
157 // Set to false if we have reason to beleive the chrome disk cache | |
158 // will not cache this url. | |
159 bool cacheable_; | |
160 | |
161 // Last time some media time used this resource. | |
162 base::TimeTicks last_used_; | |
163 | |
164 // Expiration time according to http headers. | |
165 base::TimeTicks valid_until_; | |
166 | |
167 // Last modification time according to http headers. | |
168 base::Time last_modified_; | |
169 | |
170 ResourceMultiBuffer multibuffer_; | |
171 std::vector<RedirectCB> redirect_callbacks_; | |
172 | |
173 blink::WebFrame* frame_; | |
174 | |
175 base::ThreadChecker thread_checker_; | |
176 DISALLOW_COPY_AND_ASSIGN(UrlData); | |
177 }; | |
178 | |
179 // The UrlIndex lets you look up UrlData instances by url. | |
180 class MEDIA_BLINK_EXPORT UrlIndex { | |
181 public: | |
182 explicit UrlIndex(blink::WebFrame*); | |
183 virtual ~UrlIndex(); | |
184 | |
185 // Look up an UrlData in the index and return it. If none is found, | |
186 // create a new one. Note that newly created UrlData entries are NOT | |
187 // added to the index, instead you must call TryInsert on them after | |
188 // initializing relevant parameters, like whether it support | |
189 // ranges and it's last modified time. | |
190 scoped_refptr<UrlData> GetByUrl(const GURL& gurl, | |
191 UrlData::CORSMode cors_mode); | |
192 | |
193 // Add the given UrlData to the index if possible. If a better UrlData | |
194 // is already present in the index, return it instead. (If not, we just | |
195 // return the given UrlData.) Please make sure to initialize all the data | |
196 // that can be gathered from HTTP headers in |url_data| before calling this. | |
197 // In particular, the following fields are important: | |
198 // o range_supported: Entries which do not support ranges cannot be | |
199 // shared and are not added to the index. | |
200 // o valid_until, last_used: Entries have to be valid to be inserted | |
201 // into the index, this means that they have to have been recently | |
202 // used or have an Expires: header that says when they stop being valid. | |
203 // o last_modified: Expired cache entries can be re-used if last_modified | |
204 // matches. | |
205 // TODO(hubbe): Add etag support. | |
206 scoped_refptr<UrlData> TryInsert(const scoped_refptr<UrlData>& url_data); | |
207 | |
208 blink::WebFrame* frame() const { return frame_; } | |
209 | |
210 private: | |
211 friend class UrlData; | |
212 friend class ResourceMultiBuffer; | |
213 void RemoveUrlData(const UrlData* url_data); | |
214 | |
215 // Virtual so we can override it in tests. | |
216 virtual scoped_refptr<UrlData> NewUrlData(const GURL& url, | |
217 UrlData::CORSMode cors_mode); | |
218 | |
219 std::map<UrlData::KeyType, scoped_refptr<UrlData>> by_url_; | |
220 blink::WebFrame* frame_; | |
221 scoped_refptr<MultiBuffer::GlobalLRU> lru_; | |
222 bool prune_cb_active_; | |
223 | |
224 protected: | |
225 base::WeakPtrFactory<UrlIndex> weak_factory_; | |
226 }; | |
227 | |
228 } // namespace media | |
229 #endif // MEDIA_BLINK_URL_INDEX_H_ | |
OLD | NEW |