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 #include <set> |
| 6 #include <utility> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/time/time.h" |
| 11 #include "media/blink/url_index.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 const int kUrlMappingTimeoutSeconds = 300; |
| 16 |
| 17 UrlData::UrlData(const GURL& url, |
| 18 CORSMode cors_mode, |
| 19 UrlIndex* url_index) : |
| 20 url_(url), |
| 21 cors_mode_(cors_mode), |
| 22 length_(kPositionNotSpecified), |
| 23 range_supported_(false), |
| 24 cacheable_(false), |
| 25 last_used_(), |
| 26 url_index_(url_index) { |
| 27 } |
| 28 |
| 29 UrlData::~UrlData() { |
| 30 if (url_index_) |
| 31 url_index_->RemoveUrlData(this); |
| 32 } |
| 33 |
| 34 std::pair<GURL, UrlData::CORSMode> UrlData::key() const { |
| 35 return std::make_pair(url(), cors_mode()); |
| 36 } |
| 37 |
| 38 void UrlData::set_redirects_to(const GURL& url) { |
| 39 redirects_to_ = url; |
| 40 } |
| 41 |
| 42 void UrlData::set_valid_until(base::TimeTicks t) { |
| 43 valid_until_ = t; |
| 44 } |
| 45 |
| 46 void UrlData::set_range_supported() { |
| 47 range_supported_ = true; |
| 48 } |
| 49 |
| 50 void UrlData::set_cacheable(bool cacheable) { |
| 51 cacheable_ = cacheable; |
| 52 } |
| 53 |
| 54 void UrlData::set_length(int64 length) { |
| 55 if (length != kPositionNotSpecified) { |
| 56 length_ = length; |
| 57 } |
| 58 } |
| 59 |
| 60 void UrlData::Use() { |
| 61 last_used_ = base::TimeTicks::Now(); |
| 62 } |
| 63 |
| 64 bool UrlData::Valid() const { |
| 65 base::TimeTicks now = base::TimeTicks::Now(); |
| 66 if (valid_until_ > now) |
| 67 return true; |
| 68 if (now - last_used_ < base::TimeDelta::FromSeconds( |
| 69 kUrlMappingTimeoutSeconds)) |
| 70 return true; |
| 71 return false; |
| 72 } |
| 73 |
| 74 void UrlData::set_last_modified_internal(base::Time last_modified) { |
| 75 last_modified_ = last_modified; |
| 76 } |
| 77 |
| 78 scoped_refptr<UrlData> UrlData::set_last_modified(base::Time last_modified) { |
| 79 if (url_index_) |
| 80 return url_index_->SetLastModified(this, last_modified); |
| 81 set_last_modified_internal(last_modified); |
| 82 return this; |
| 83 } |
| 84 |
| 85 void UrlData::DisconnectFromIndex() { |
| 86 url_index_ = nullptr; |
| 87 } |
| 88 |
| 89 UrlIndex::UrlIndex() {} |
| 90 UrlIndex::~UrlIndex() { |
| 91 for (auto i : by_url_) |
| 92 i.second->DisconnectFromIndex(); |
| 93 for (auto i : with_last_modified_) |
| 94 i.second->DisconnectFromIndex(); |
| 95 } |
| 96 |
| 97 void UrlIndex::RemoveUrlData(const UrlData* url_data) { |
| 98 auto i = by_url_.find(url_data->key()); |
| 99 if (i != by_url_.end() && i->second == url_data) |
| 100 by_url_.erase(i); |
| 101 i = with_last_modified_.find(url_data->key()); |
| 102 if (i != with_last_modified_.end() && i->second == url_data) |
| 103 with_last_modified_.erase(i); |
| 104 } |
| 105 |
| 106 scoped_refptr<UrlData> UrlIndex::GetByUrl(const GURL& gurl, |
| 107 UrlData::CORSMode cors_mode) { |
| 108 auto inserted = by_url_.insert( |
| 109 std::make_pair<UrlData::KeyType,UrlData*>( |
| 110 std::make_pair(gurl, cors_mode), nullptr)); |
| 111 if (inserted.second || !inserted.first->second->Valid()) { |
| 112 inserted.first->second = new UrlData(gurl, cors_mode, this); |
| 113 to_prune_.push_back(inserted.first->second); |
| 114 } |
| 115 DCHECK(inserted.first->second); |
| 116 return inserted.first->second; |
| 117 } |
| 118 |
| 119 scoped_refptr<UrlData> UrlIndex::SetLastModified( |
| 120 scoped_refptr<UrlData> url_data, |
| 121 base::Time last_modified) { |
| 122 DCHECK(!last_modified.is_null()); |
| 123 |
| 124 if (url_data->last_modified() == last_modified) |
| 125 return url_data; |
| 126 |
| 127 auto i = with_last_modified_.find(url_data->key()); |
| 128 if (i != with_last_modified_.end()) { |
| 129 if (i->second->last_modified() == last_modified) { |
| 130 return by_url_[i->second->key()] = i->second; |
| 131 } |
| 132 |
| 133 if (i->second != url_data && !url_data->last_modified().is_null()) |
| 134 return url_data; |
| 135 } |
| 136 |
| 137 url_data->set_last_modified_internal(last_modified); |
| 138 with_last_modified_[url_data->key()] = url_data.get(); |
| 139 return url_data; |
| 140 } |
| 141 |
| 142 } // namespace media |
OLD | NEW |