Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: media/blink/url_index.cc

Issue 1165903002: Multi reader/writer cache/buffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more tests, comments adressed, minor refactoring + a little code cleanup Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Should be higher than what chrome normally allows.
17 const int kMaxRedirects = 50;
18
19 UrlData::UrlData(const GURL& url,
20 CORSMode cors_mode,
21 UrlIndex* url_index) :
22 url_(url),
23 cors_mode_(cors_mode),
24 length_(kPositionNotSpecified),
25 range_supported_(false),
26 cacheable_(false),
27 last_used_(),
28 url_index_(url_index) {
29 }
30
31 UrlData::~UrlData() {
32 if (url_index_)
33 url_index_->RemoveUrlData(this);
34 }
35
36 std::pair<GURL, UrlData::CORSMode> UrlData::key() const {
37 return std::make_pair(url(), cors_mode());
38 }
39
40 void UrlData::set_redirects_to(const GURL& url) {
41 redirects_to_ = url;
42 }
43
44 void UrlData::set_valid_until(base::TimeTicks t) {
45 valid_until_ = t;
46 }
47
48 void UrlData::set_range_supported() {
49 range_supported_ = true;
50 }
51
52 void UrlData::set_cacheable(bool cacheable) {
53 cacheable_ = cacheable;
54 }
55
56 void UrlData::set_length(int64 length) {
57 if (length != kPositionNotSpecified) {
58 length_ = length;
59 }
60 }
61
62 void UrlData::Use() {
63 last_used_ = base::TimeTicks::Now();
64 }
65
66 bool UrlData::Valid() const {
67 base::TimeTicks now = base::TimeTicks::Now();
68 if (valid_until_ > now)
69 return true;
70 if (now - last_used_ < base::TimeDelta::FromSeconds(
71 kUrlMappingTimeoutSeconds))
72 return true;
73 return false;
74 }
75
76 void UrlData::set_last_modified_internal(base::Time last_modified) {
77 last_modified_ = last_modified;
78 }
79
80 scoped_refptr<UrlData> UrlData::set_last_modified(base::Time last_modified) {
81 if (url_index_)
82 return url_index_->SetLastModified(this, last_modified);
83 set_last_modified_internal(last_modified);
84 return this;
85 }
86
87 void UrlData::DisconnectFromIndex() {
88 url_index_ = nullptr;
89 }
90
91 UrlIndex::UrlIndex() {}
92 UrlIndex::~UrlIndex() {
93 for (auto i : by_url_)
94 i.second->DisconnectFromIndex();
95 for (auto i : with_last_modified_)
96 i.second->DisconnectFromIndex();
97 }
98
99 void UrlIndex::RemoveUrlData(const UrlData* url_data) {
100 auto i = by_url_.find(url_data->key());
101 if (i != by_url_.end() && i->second == url_data)
102 by_url_.erase(i);
103 i = with_last_modified_.find(url_data->key());
104 if (i != with_last_modified_.end() && i->second == url_data)
105 with_last_modified_.erase(i);
106 }
107
108 scoped_refptr<UrlData> UrlIndex::GetByUrl(const GURL& gurl,
109 UrlData::CORSMode cors_mode) {
110 auto inserted = by_url_.insert(
111 std::make_pair<UrlData::KeyType,UrlData*>(
112 std::make_pair(gurl, cors_mode), nullptr));
113 if (inserted.second || !inserted.first->second->Valid()) {
114 inserted.first->second = new UrlData(gurl, cors_mode, this);
115 to_prune_.push_back(inserted.first->second);
116 }
117 DCHECK(inserted.first->second);
118 return inserted.first->second;
119 }
120
121 scoped_refptr<UrlData> UrlIndex::SetLastModified(
122 scoped_refptr<UrlData> url_data,
123 base::Time last_modified) {
124 DCHECK(!last_modified.is_null());
125
126 if (url_data->last_modified() == last_modified)
127 return url_data;
128
129 auto i = with_last_modified_.find(url_data->key());
130 if (i != with_last_modified_.end()) {
131 if (i->second->last_modified() == last_modified) {
132 return by_url_[i->second->key()] = i->second;
133 }
134
135 if (i->second != url_data && !url_data->last_modified().is_null())
136 return url_data;
137 }
138
139 url_data->set_last_modified_internal(last_modified);
140 with_last_modified_[url_data->key()] = url_data.get();
141 return url_data;
142 }
143
144 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698