| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "webkit/media/cache_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" |
| 11 #include "base/time.h" |
| 12 #include "net/http/http_util.h" |
| 13 #include "net/http/http_version.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon
se.h" |
| 17 |
| 18 using base::Time; |
| 19 using base::TimeDelta; |
| 20 using net::HttpVersion; |
| 21 using WebKit::WebURLResponse; |
| 22 |
| 23 namespace webkit_media { |
| 24 |
| 25 enum { kHttpOK = 200, kHttpPartialContent = 206 }; |
| 26 |
| 27 std::vector<UncacheableReason> GetReasonsForUncacheability( |
| 28 const WebURLResponse& response) { |
| 29 std::vector<UncacheableReason> reasons; |
| 30 const int code = response.httpStatusCode(); |
| 31 const int version = response.httpVersion(); |
| 32 const HttpVersion http_version = |
| 33 version == WebURLResponse::HTTP_1_1 ? HttpVersion(1, 1) : |
| 34 version == WebURLResponse::HTTP_1_0 ? HttpVersion(1, 0) : |
| 35 version == WebURLResponse::HTTP_0_9 ? HttpVersion(0, 9) : |
| 36 HttpVersion(); |
| 37 if (code != kHttpOK && code != kHttpPartialContent) |
| 38 reasons.push_back(kNoData); |
| 39 if (http_version < HttpVersion(1, 1) && code == kHttpPartialContent) |
| 40 reasons.push_back(kPre11PartialResponse); |
| 41 if (code == kHttpPartialContent && |
| 42 !net::HttpUtil::HasStrongValidators( |
| 43 http_version, |
| 44 response.httpHeaderField("etag").utf8(), |
| 45 response.httpHeaderField("Last-Modified").utf8(), |
| 46 response.httpHeaderField("Date").utf8())) { |
| 47 reasons.push_back(kNoStrongValidatorOnPartialResponse); |
| 48 } |
| 49 |
| 50 std::string cache_control_header = |
| 51 response.httpHeaderField("cache-control").utf8(); |
| 52 StringToLowerASCII(&cache_control_header); |
| 53 if (cache_control_header.find("no-cache") != std::string::npos) |
| 54 reasons.push_back(kNoCache); |
| 55 if (cache_control_header.find("no-store") != std::string::npos) |
| 56 reasons.push_back(kNoStore); |
| 57 if (cache_control_header.find("must-revalidate") != std::string::npos) |
| 58 reasons.push_back(kHasMustRevalidate); |
| 59 |
| 60 const TimeDelta kMinimumAgeForUsefulness = |
| 61 TimeDelta::FromSeconds(3600); // Arbitrary value. |
| 62 |
| 63 const char kMaxAgePrefix[] = "max-age="; |
| 64 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; |
| 65 if (LowerCaseEqualsASCII(cache_control_header.begin(), |
| 66 cache_control_header.begin() + kMaxAgePrefixLen, |
| 67 kMaxAgePrefix)) { |
| 68 int64 max_age_seconds; |
| 69 base::StringToInt64( |
| 70 base::StringPiece(cache_control_header.begin() + kMaxAgePrefixLen, |
| 71 cache_control_header.end()), |
| 72 &max_age_seconds); |
| 73 if (TimeDelta::FromSeconds(max_age_seconds) < kMinimumAgeForUsefulness) |
| 74 reasons.push_back(kShortMaxAge); |
| 75 } |
| 76 |
| 77 Time date; |
| 78 Time expires; |
| 79 if (Time::FromString(response.httpHeaderField("Date").utf8().data(), &date) && |
| 80 Time::FromString(response.httpHeaderField("Expires").utf8().data(), |
| 81 &expires) && |
| 82 date > Time() && expires > Time() && |
| 83 (expires - date) < kMinimumAgeForUsefulness) { |
| 84 reasons.push_back(kExpiresTooSoon); |
| 85 } |
| 86 |
| 87 return reasons; |
| 88 } |
| 89 |
| 90 } // namespace webkit_media |
| OLD | NEW |