Chromium Code Reviews| Index: media/blink/url_index.h |
| diff --git a/media/blink/url_index.h b/media/blink/url_index.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0ef77c2af0143d64f9e66031e15cc2397a3e8a6f |
| --- /dev/null |
| +++ b/media/blink/url_index.h |
| @@ -0,0 +1,208 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MEDIA_BLINK_URL_INDEX_H_ |
| +#define MEDIA_BLINK_URL_INDEX_H_ |
| + |
| +#include <map> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "media/blink/lru.h" |
| +#include "media/blink/media_blink_export.h" |
| +#include "media/blink/multibuffer.h" |
| +#include "third_party/WebKit/public/web/WebFrame.h" |
| +#include "url/gurl.h" |
| + |
| +namespace media { |
| + |
| +const int64 kPositionNotSpecified = -1; |
| + |
| +class UrlData; |
| + |
| +// A multibuffer for loading media resources which knows |
| +// how to create MultiBufferDataProviders to load data |
| +// into the cache. |
| +class MEDIA_BLINK_EXPORT ResourceMultiBuffer |
| + : NON_EXPORTED_BASE(public MultiBuffer) { |
| + public: |
| + explicit ResourceMultiBuffer(UrlData* url_data_); |
| + ~ResourceMultiBuffer() override; |
| + |
| + // 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
|
| + MultiBuffer::DataProvider* CreateWriter(const BlockId& pos) override; |
| + bool RangeSupported() const override; |
| + void OnEmpty() override; |
| + |
| + // Accessor |
| + 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?
|
| + |
| + protected: |
| + UrlData* url_data_; |
| + blink::WebFrame* frame_; |
| +}; |
| + |
| +class UrlIndex; |
| + |
| +// All the data & metadata for a single resource. |
| +// Data is cached using a MultiBuffer instance. |
| +class MEDIA_BLINK_EXPORT UrlData : public base::RefCounted<UrlData> { |
| + public: |
| + // Keep in sync with WebMediaPlayer::CORSMode. |
| + 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?
|
| + typedef std::pair<GURL, CORSMode> KeyType; |
| + |
| + // Accessors |
| + const GURL& url() const { return url_; } |
| + |
| + // Cross-origin access mode |
| + CORSMode cors_mode() const { return cors_mode_; } |
| + |
| + // Are HTTP range requests supported? |
| + bool range_supported() const { return range_supported_; } |
| + |
| + // True if we found a reason why this URL won't be stored in the |
| + // HTTP disk cache. |
| + bool cacheable() const { return cacheable_; } |
| + |
| + // Last used time. |
| + base::TimeTicks last_used() const { return last_used_; } |
| + |
| + // Last modified time. |
| + base::Time last_modified() const { return last_modified_; } |
| + |
| + // Expiration time. |
| + base::TimeTicks valid_until() const { return valid_until_; } |
| + |
| + // The key used by UrlIndex to find this UrlData. |
| + KeyType key() const; |
| + |
| + // Length of data associated with url or |kPositionNotSpecified| |
| + int64 length() const { return length_; } |
| + |
| + // Returns the number of blocks cached for this resource. |
| + size_t CachedSize(); |
| + |
| + // Returns our url_index, or nullptr if it's been destroyed. |
| + UrlIndex* url_index() const { return url_index_.get(); } |
| + |
| + // Notifies the url index that this is currently used. |
| + // The url <-> URLData mapping will be eventually be invalidated if |
| + // this is not called regularly. |
| + void Use(); |
| + |
| + // Setters. |
| + void set_length(int64 length); |
| + void set_cacheable(bool cacheable); |
| + void set_valid_until(base::TimeTicks t); |
| + void set_range_supported(); |
| + void set_last_modified(base::Time last_modified); |
| + |
| + // A redirect has occured (or we've found a better UrlData for the same |
| + // resource). |
| + void RedirectTo(const scoped_refptr<UrlData>& to); |
| + |
| + // Fail, tell all clients that a failure has occured. |
| + void Fail(); |
| + |
| + // Callback for receving notifications when a redirect occurs. |
| + typedef base::Callback<void(const scoped_refptr<UrlData>&)> RedirectCB; |
| + |
| + // Register a callback to be called when a redirect occurs. |
| + // Callbacks are cleared when a redirect occurs, so clients must call |
| + // OnRedirect again if they wish to continue receiving callbacks. |
| + void OnRedirect(const RedirectCB& cb); |
| + |
| + // Returns true it is valid to keep using this to access cached data. |
| + // A single media player instance may choose to ignore this for resources |
| + // that have already been opened. |
| + bool Valid() const; |
| + |
| + // Virtual so we can override it for testing. |
| + virtual ResourceMultiBuffer* multibuffer(); |
| + |
| + protected: |
| + UrlData(const GURL& url, |
| + CORSMode cors_mode, |
| + const base::WeakPtr<UrlIndex>& url_index); |
| + virtual ~UrlData(); |
| + |
| + private: |
| + friend class ResourceMultiBuffer; |
| + friend class UrlIndex; |
| + friend class base::RefCounted<UrlData>; |
| + |
| + void OnEmpty(); |
| + void MergeFrom(const scoped_refptr<UrlData>& other); |
| + |
| + const GURL url_; |
| + const CORSMode cors_mode_; |
| + base::WeakPtr<UrlIndex> url_index_; |
| + |
| + 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.)
|
| + bool range_supported_; |
| + bool cacheable_; |
| + |
| + base::TimeTicks last_used_; |
| + base::TimeTicks valid_until_; |
| + base::Time last_modified_; |
| + |
| + ResourceMultiBuffer multibuffer_; |
| + std::vector<RedirectCB> redirect_callbacks_; |
| + |
| + base::ThreadChecker thread_checker_; |
| + DISALLOW_COPY_AND_ASSIGN(UrlData); |
| +}; |
| + |
| +// The UrlIndex lets you look up UrlData instances by url. |
| +class MEDIA_BLINK_EXPORT UrlIndex { |
| + public: |
| + explicit UrlIndex(blink::WebFrame*); |
| + virtual ~UrlIndex(); |
|
xhwang
2015/11/19 23:34:18
empty line
hubbe
2015/11/20 23:24:24
Done.
|
| + // Look up an UrlData in the index and return it. If none is found, |
| + // create a new one. Note that newly created UrlData entries are NOT |
| + // added to the index, instead you must call TryInsert on them after |
| + // 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.
|
| + // ranges and it's last modified time. |
| + scoped_refptr<UrlData> GetByUrl(const GURL& gurl, |
| + UrlData::CORSMode cors_mode); |
| + |
| + // Add the given urldata to the index if possible. If a better urldata |
| + // 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.
|
| + // initialize all the data that can be gathered from HTTP headers in |
| + // |urldata| before calling this. In particular, the following fields are |
| + // important: |
| + // o range_supported: Entries which do not support ranges cannot be |
| + // shared and are not added to the index. |
| + // o valid_until, last_used: Entries have to be valid to be inserted |
| + // into the index, this means that they have to have been recently |
| + // used or have an Expires: header that says when they stop being valid. |
| + // o last_modified: Expired cache entries can be re-used if last_modified |
| + // matches. |
| + // TODO(hubbe): Add etag support. |
| + 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.
|
| + blink::WebFrame* frame() const { return frame_; } |
| + |
| + private: |
| + friend class UrlData; |
| + friend class ResourceMultiBuffer; |
| + void RemoveUrlData(const UrlData* url_data); |
| + |
| + // Virtual so we can override it in tests. |
| + 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.
|
| + |
| + std::map<UrlData::KeyType, scoped_refptr<UrlData>> by_url_; |
| + blink::WebFrame* frame_; |
| + scoped_refptr<MultiBuffer::GlobalLRU> lru_; |
| + bool prune_cb_active_; |
| + |
| + protected: |
| + base::WeakPtrFactory<UrlIndex> weak_factory_; |
| +}; |
| + |
| +} // namespace media |
| +#endif // MEDIA_BLINK_URL_INDEX_H_ |