OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/blink/cache_util.h" | 5 #include "media/blink/cache_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 Time::FromString(response.httpHeaderField("Expires").utf8().data(), | 81 Time::FromString(response.httpHeaderField("Expires").utf8().data(), |
82 &expires) && | 82 &expires) && |
83 date > Time() && expires > Time() && | 83 date > Time() && expires > Time() && |
84 (expires - date) < kMinimumAgeForUsefulness) { | 84 (expires - date) < kMinimumAgeForUsefulness) { |
85 reasons |= kExpiresTooSoon; | 85 reasons |= kExpiresTooSoon; |
86 } | 86 } |
87 | 87 |
88 return reasons; | 88 return reasons; |
89 } | 89 } |
90 | 90 |
| 91 base::TimeDelta GetCacheValidUntil(const WebURLResponse& response) { |
| 92 std::string cache_control_header = |
| 93 base::ToLowerASCII(response.httpHeaderField("cache-control").utf8()); |
| 94 if (cache_control_header.find("no-cache") != std::string::npos) |
| 95 return base::TimeDelta(); |
| 96 if (cache_control_header.find("must-revalidate") != std::string::npos) |
| 97 return base::TimeDelta(); |
| 98 |
| 99 // Max cache timeout ~= 1 month. |
| 100 base::TimeDelta ret = base::TimeDelta::FromDays(30); |
| 101 |
| 102 const char kMaxAgePrefix[] = "max-age="; |
| 103 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; |
| 104 if (cache_control_header.substr(0, kMaxAgePrefixLen) == kMaxAgePrefix) { |
| 105 int64 max_age_seconds; |
| 106 base::StringToInt64( |
| 107 base::StringPiece(cache_control_header.begin() + kMaxAgePrefixLen, |
| 108 cache_control_header.end()), |
| 109 &max_age_seconds); |
| 110 |
| 111 ret = std::min(ret, TimeDelta::FromSeconds(max_age_seconds)); |
| 112 } else { |
| 113 // Note that |date| may be smaller than |expires|, which means we'll |
| 114 // return a timetick some time in the past. |
| 115 Time date; |
| 116 Time expires; |
| 117 if (Time::FromString(response.httpHeaderField("Date").utf8().data(), |
| 118 &date) && |
| 119 Time::FromString(response.httpHeaderField("Expires").utf8().data(), |
| 120 &expires) && |
| 121 date > Time() && expires > Time()) { |
| 122 ret = std::min(ret, expires - date); |
| 123 } |
| 124 } |
| 125 |
| 126 return ret; |
| 127 } |
| 128 |
91 } // namespace media | 129 } // namespace media |
OLD | NEW |