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 explicit ResourceMultiBuffer(UrlData* url_data_); | |
34 ~ResourceMultiBuffer() override; | |
35 | |
36 // MultiBuffer overrides | |
xhwang
2015/11/19 23:34:18
s/overrides/implementation to be consistent with o
hubbe
2015/11/20 23:24:24
Done. (Although technically OnBuffer() is an overr
| |
37 MultiBuffer::DataProvider* CreateWriter(const BlockId& pos) override; | |
38 bool RangeSupported() const override; | |
39 void OnEmpty() override; | |
40 | |
41 // Accessor | |
42 blink::WebFrame* frame() const { return frame_; } | |
xhwang
2015/11/19 23:34:18
See my comment above about passing WebFrame all th
hubbe
2015/11/20 23:24:24
moved to UrlData, better?
| |
43 | |
44 protected: | |
45 UrlData* url_data_; | |
46 blink::WebFrame* frame_; | |
47 }; | |
48 | |
49 class UrlIndex; | |
50 | |
51 // All the data & metadata for a single resource. | |
52 // Data is cached using a MultiBuffer instance. | |
53 class MEDIA_BLINK_EXPORT UrlData : public base::RefCounted<UrlData> { | |
54 public: | |
55 // Keep in sync with WebMediaPlayer::CORSMode. | |
56 enum CORSMode { kUnspecified, kAnonymous, kUseCredentials }; | |
xhwang
2015/11/19 23:34:18
nit: We use UPPER_CASE for enums.
hubbe
2015/11/20 23:24:24
This was copied from buffered_resource_loader.h wh
xhwang
2015/11/23 23:09:21
We don't update old code, but new code should foll
hubbe
2015/11/24 22:55:10
Better?
| |
57 typedef std::pair<GURL, CORSMode> KeyType; | |
58 | |
59 // Accessors | |
60 const GURL& url() const { return url_; } | |
61 | |
62 // Cross-origin access mode | |
63 CORSMode cors_mode() const { return cors_mode_; } | |
64 | |
65 // Are HTTP range requests supported? | |
66 bool range_supported() const { return range_supported_; } | |
67 | |
68 // True if we found a reason why this URL won't be stored in the | |
69 // HTTP disk cache. | |
70 bool cacheable() const { return cacheable_; } | |
71 | |
72 // Last used time. | |
73 base::TimeTicks last_used() const { return last_used_; } | |
74 | |
75 // Last modified time. | |
76 base::Time last_modified() const { return last_modified_; } | |
77 | |
78 // Expiration time. | |
79 base::TimeTicks valid_until() const { return valid_until_; } | |
80 | |
81 // The key used by UrlIndex to find this UrlData. | |
82 KeyType key() const; | |
83 | |
84 // Length of data associated with url or |kPositionNotSpecified| | |
85 int64 length() const { return length_; } | |
86 | |
87 // Returns the number of blocks cached for this resource. | |
88 size_t CachedSize(); | |
89 | |
90 // Returns our url_index, or nullptr if it's been destroyed. | |
91 UrlIndex* url_index() const { return url_index_.get(); } | |
92 | |
93 // Notifies the url index that this is currently used. | |
94 // The url <-> URLData mapping will be eventually be invalidated if | |
95 // this is not called regularly. | |
96 void Use(); | |
97 | |
98 // Setters. | |
99 void set_length(int64 length); | |
100 void set_cacheable(bool cacheable); | |
101 void set_valid_until(base::TimeTicks t); | |
102 void set_range_supported(); | |
103 void set_last_modified(base::Time last_modified); | |
104 | |
105 // A redirect has occured (or we've found a better UrlData for the same | |
106 // resource). | |
107 void RedirectTo(const scoped_refptr<UrlData>& to); | |
108 | |
109 // Fail, tell all clients that a failure has occured. | |
110 void Fail(); | |
111 | |
112 // Callback for receving notifications when a redirect occurs. | |
113 typedef base::Callback<void(const scoped_refptr<UrlData>&)> RedirectCB; | |
114 | |
115 // Register a callback to be called when a redirect occurs. | |
116 // Callbacks are cleared when a redirect occurs, so clients must call | |
117 // OnRedirect again if they wish to continue receiving callbacks. | |
118 void OnRedirect(const RedirectCB& cb); | |
119 | |
120 // Returns true it is valid to keep using this to access cached data. | |
121 // A single media player instance may choose to ignore this for resources | |
122 // that have already been opened. | |
123 bool Valid() const; | |
124 | |
125 // Virtual so we can override it for testing. | |
126 virtual ResourceMultiBuffer* multibuffer(); | |
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 const GURL url_; | |
143 const CORSMode cors_mode_; | |
144 base::WeakPtr<UrlIndex> url_index_; | |
145 | |
146 int64 length_; | |
xhwang
2015/11/19 23:34:18
what length is this? in bytes?
hubbe
2015/11/20 23:24:24
Comment added. (And to several other variables.)
| |
147 bool range_supported_; | |
148 bool cacheable_; | |
149 | |
150 base::TimeTicks last_used_; | |
151 base::TimeTicks valid_until_; | |
152 base::Time last_modified_; | |
153 | |
154 ResourceMultiBuffer multibuffer_; | |
155 std::vector<RedirectCB> redirect_callbacks_; | |
156 | |
157 base::ThreadChecker thread_checker_; | |
158 DISALLOW_COPY_AND_ASSIGN(UrlData); | |
159 }; | |
160 | |
161 // The UrlIndex lets you look up UrlData instances by url. | |
162 class MEDIA_BLINK_EXPORT UrlIndex { | |
163 public: | |
164 explicit UrlIndex(blink::WebFrame*); | |
165 virtual ~UrlIndex(); | |
xhwang
2015/11/19 23:34:18
empty line
hubbe
2015/11/20 23:24:24
Done.
| |
166 // Look up an UrlData in the index and return it. If none is found, | |
167 // create a new one. Note that newly created UrlData entries are NOT | |
168 // added to the index, instead you must call TryInsert on them after | |
169 // after initializing relevant parameters, like weather it support | |
xhwang
2015/11/19 23:34:19
s/after//
s/weather/whether
hubbe
2015/11/20 23:24:24
Done.
| |
170 // ranges and it's last modified time. | |
171 scoped_refptr<UrlData> GetByUrl(const GURL& gurl, | |
172 UrlData::CORSMode cors_mode); | |
173 | |
174 // Add the given urldata to the index if possible. If a better urldata | |
175 // is already present in the index, return it instead. Please make sure to | |
xhwang
2015/11/19 23:34:18
otherwise, what to return?
hubbe
2015/11/20 23:24:24
Done.
| |
176 // initialize all the data that can be gathered from HTTP headers in | |
177 // |urldata| before calling this. In particular, the following fields are | |
178 // important: | |
179 // o range_supported: Entries which do not support ranges cannot be | |
180 // shared and are not added to the index. | |
181 // o valid_until, last_used: Entries have to be valid to be inserted | |
182 // into the index, this means that they have to have been recently | |
183 // used or have an Expires: header that says when they stop being valid. | |
184 // o last_modified: Expired cache entries can be re-used if last_modified | |
185 // matches. | |
186 // TODO(hubbe): Add etag support. | |
187 scoped_refptr<UrlData> TryInsert(const scoped_refptr<UrlData>& urldata); | |
xhwang
2015/11/19 23:34:18
nit: UrlData means Url and Data are two words, s/u
xhwang
2015/11/19 23:34:18
empty line
hubbe
2015/11/20 23:24:24
Done.
hubbe
2015/11/20 23:24:24
Done.
| |
188 blink::WebFrame* frame() const { return frame_; } | |
189 | |
190 private: | |
191 friend class UrlData; | |
192 friend class ResourceMultiBuffer; | |
193 void RemoveUrlData(const UrlData* url_data); | |
194 | |
195 // Virtual so we can override it in tests. | |
196 virtual UrlData* NewUrlData(const GURL& url, UrlData::CORSMode cors_mode); | |
xhwang
2015/11/19 23:34:18
return scoped_refptr<UrlData>? Typically it's a ba
hubbe
2015/11/20 23:24:24
Done.
| |
197 | |
198 std::map<UrlData::KeyType, scoped_refptr<UrlData>> by_url_; | |
199 blink::WebFrame* frame_; | |
200 scoped_refptr<MultiBuffer::GlobalLRU> lru_; | |
201 bool prune_cb_active_; | |
202 | |
203 protected: | |
204 base::WeakPtrFactory<UrlIndex> weak_factory_; | |
205 }; | |
206 | |
207 } // namespace media | |
208 #endif // MEDIA_BLINK_URL_INDEX_H_ | |
OLD | NEW |