Index: media/blink/url_index.cc |
diff --git a/media/blink/url_index.cc b/media/blink/url_index.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..dcd9b09c95dbf92b1c0e2e69afd1c5f271923a69 |
--- /dev/null |
+++ b/media/blink/url_index.cc |
@@ -0,0 +1,142 @@ |
+// 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. |
+ |
+#include <set> |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/time/time.h" |
+#include "media/blink/url_index.h" |
+ |
+namespace media { |
+ |
+const int kUrlMappingTimeoutSeconds = 300; |
+ |
+UrlData::UrlData(const GURL& url, |
+ CORSMode cors_mode, |
+ UrlIndex* url_index) : |
+ url_(url), |
+ cors_mode_(cors_mode), |
+ length_(kPositionNotSpecified), |
+ range_supported_(false), |
+ cacheable_(false), |
+ last_used_(), |
+ url_index_(url_index) { |
+} |
+ |
+UrlData::~UrlData() { |
+ if (url_index_) |
+ url_index_->RemoveUrlData(this); |
+} |
+ |
+std::pair<GURL, UrlData::CORSMode> UrlData::key() const { |
+ return std::make_pair(url(), cors_mode()); |
+} |
+ |
+void UrlData::set_redirects_to(const GURL& url) { |
+ redirects_to_ = url; |
+} |
+ |
+void UrlData::set_valid_until(base::TimeTicks t) { |
+ valid_until_ = t; |
+} |
+ |
+void UrlData::set_range_supported() { |
+ range_supported_ = true; |
+} |
+ |
+void UrlData::set_cacheable(bool cacheable) { |
+ cacheable_ = cacheable; |
+} |
+ |
+void UrlData::set_length(int64 length) { |
+ if (length != kPositionNotSpecified) { |
+ length_ = length; |
+ } |
+} |
+ |
+void UrlData::Use() { |
+ last_used_ = base::TimeTicks::Now(); |
+} |
+ |
+bool UrlData::Valid() const { |
+ base::TimeTicks now = base::TimeTicks::Now(); |
+ if (valid_until_ > now) |
+ return true; |
+ if (now - last_used_ < base::TimeDelta::FromSeconds( |
+ kUrlMappingTimeoutSeconds)) |
+ return true; |
+ return false; |
+} |
+ |
+void UrlData::set_last_modified_internal(base::Time last_modified) { |
+ last_modified_ = last_modified; |
+} |
+ |
+scoped_refptr<UrlData> UrlData::set_last_modified(base::Time last_modified) { |
+ if (url_index_) |
+ return url_index_->SetLastModified(this, last_modified); |
+ set_last_modified_internal(last_modified); |
+ return this; |
+} |
+ |
+void UrlData::DisconnectFromIndex() { |
+ url_index_ = nullptr; |
+} |
+ |
+UrlIndex::UrlIndex() {} |
+UrlIndex::~UrlIndex() { |
+ for (auto i : by_url_) |
+ i.second->DisconnectFromIndex(); |
+ for (auto i : with_last_modified_) |
+ i.second->DisconnectFromIndex(); |
+} |
+ |
+void UrlIndex::RemoveUrlData(const UrlData* url_data) { |
+ auto i = by_url_.find(url_data->key()); |
+ if (i != by_url_.end() && i->second == url_data) |
+ by_url_.erase(i); |
+ i = with_last_modified_.find(url_data->key()); |
+ if (i != with_last_modified_.end() && i->second == url_data) |
+ with_last_modified_.erase(i); |
+} |
+ |
+scoped_refptr<UrlData> UrlIndex::GetByUrl(const GURL& gurl, |
+ UrlData::CORSMode cors_mode) { |
+ auto inserted = by_url_.insert( |
+ std::make_pair<UrlData::KeyType,UrlData*>( |
+ std::make_pair(gurl, cors_mode), nullptr)); |
+ if (inserted.second || !inserted.first->second->Valid()) { |
+ inserted.first->second = new UrlData(gurl, cors_mode, this); |
+ to_prune_.push_back(inserted.first->second); |
+ } |
+ DCHECK(inserted.first->second); |
+ return inserted.first->second; |
+} |
+ |
+scoped_refptr<UrlData> UrlIndex::SetLastModified( |
+ scoped_refptr<UrlData> url_data, |
+ base::Time last_modified) { |
+ DCHECK(!last_modified.is_null()); |
+ |
+ if (url_data->last_modified() == last_modified) |
+ return url_data; |
+ |
+ auto i = with_last_modified_.find(url_data->key()); |
+ if (i != with_last_modified_.end()) { |
+ if (i->second->last_modified() == last_modified) { |
+ return by_url_[i->second->key()] = i->second; |
+ } |
+ |
+ if (i->second != url_data && !url_data->last_modified().is_null()) |
+ return url_data; |
+ } |
+ |
+ url_data->set_last_modified_internal(last_modified); |
+ with_last_modified_[url_data->key()] = url_data.get(); |
+ return url_data; |
+} |
+ |
+} // namespace media |