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 | |
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::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 explicit UrlIndex(blink::WebFrame*, int block_shift); | |
xhwang
2015/11/25 01:01:44
explicit not needed
hubbe
2015/11/25 01:15:09
Done.
| |
186 virtual ~UrlIndex(); | |
187 | |
188 // Look up an UrlData in the index and return it. If none is found, | |
189 // create a new one. Note that newly created UrlData entries are NOT | |
190 // added to the index, instead you must call TryInsert on them after | |
191 // initializing relevant parameters, like whether it support | |
192 // ranges and it's last modified time. | |
193 scoped_refptr<UrlData> GetByUrl(const GURL& gurl, | |
194 UrlData::CORSMode cors_mode); | |
195 | |
196 // Add the given UrlData to the index if possible. If a better UrlData | |
197 // is already present in the index, return it instead. (If not, we just | |
198 // return the given UrlData.) Please make sure to initialize all the data | |
199 // that can be gathered from HTTP headers in |url_data| before calling this. | |
200 // In particular, the following fields are important: | |
201 // o range_supported: Entries which do not support ranges cannot be | |
202 // shared and are not added to the index. | |
203 // o valid_until, last_used: Entries have to be valid to be inserted | |
204 // into the index, this means that they have to have been recently | |
205 // used or have an Expires: header that says when they stop being valid. | |
206 // o last_modified: Expired cache entries can be re-used if last_modified | |
207 // matches. | |
208 // TODO(hubbe): Add etag support. | |
209 scoped_refptr<UrlData> TryInsert(const scoped_refptr<UrlData>& url_data); | |
210 | |
211 blink::WebFrame* frame() const { return frame_; } | |
212 | |
213 private: | |
214 friend class UrlData; | |
215 friend class ResourceMultiBuffer; | |
216 void RemoveUrlDataIfEmpty(const scoped_refptr<UrlData>& url_data); | |
217 | |
218 // Virtual so we can override it in tests. | |
219 virtual scoped_refptr<UrlData> NewUrlData(const GURL& url, | |
220 UrlData::CORSMode cors_mode); | |
221 | |
222 std::map<UrlData::KeyType, scoped_refptr<UrlData>> by_url_; | |
223 blink::WebFrame* frame_; | |
224 scoped_refptr<MultiBuffer::GlobalLRU> lru_; | |
225 bool prune_cb_active_; | |
226 | |
227 // log2 of block size in multibuffer cache. Defaults to kBlockSizeShift. | |
228 // Currently only changed for testing purposes. | |
229 const int block_shift_; | |
230 | |
231 protected: | |
232 base::WeakPtrFactory<UrlIndex> weak_factory_; | |
233 }; | |
234 | |
235 } // namespace media | |
236 #endif // MEDIA_BLINK_URL_INDEX_H_ | |
OLD | NEW |