Blink roll 171798:171837
[chromium-blink-merge.git] / webkit / common / appcache / appcache_interfaces.cc
blob154d064750cd4ed90a31b424e522d5416bc2ccad
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.
5 #include "webkit/common/appcache/appcache_interfaces.h"
7 #include <set>
9 #include "base/strings/string_util.h"
10 #include "net/url_request/url_request.h"
11 #include "url/gurl.h"
13 namespace appcache {
15 const char kHttpScheme[] = "http";
16 const char kHttpsScheme[] = "https";
17 const char kDevToolsScheme[] = "chrome-devtools";
18 const char kHttpGETMethod[] = "GET";
19 const char kHttpHEADMethod[] = "HEAD";
21 const char kEnableExecutableHandlers[] = "enable-appcache-executable-handlers";
23 const base::FilePath::CharType kAppCacheDatabaseName[] =
24 FILE_PATH_LITERAL("Index");
26 AppCacheInfo::AppCacheInfo()
27 : cache_id(kNoCacheId),
28 group_id(0),
29 status(UNCACHED),
30 size(0),
31 is_complete(false) {
34 AppCacheInfo::~AppCacheInfo() {
37 AppCacheResourceInfo::AppCacheResourceInfo()
38 : url(),
39 size(0),
40 is_master(false),
41 is_manifest(false),
42 is_intercept(false),
43 is_fallback(false),
44 is_foreign(false),
45 is_explicit(false),
46 response_id(kNoResponseId) {
49 AppCacheResourceInfo::~AppCacheResourceInfo() {
52 ErrorDetails::ErrorDetails()
53 : message(),
54 reason(UNKNOWN_ERROR),
55 url(),
56 status(0),
57 is_cross_origin(false) {}
59 ErrorDetails::ErrorDetails(
60 std::string in_message,
61 ErrorReason in_reason,
62 GURL in_url,
63 int in_status,
64 bool in_is_cross_origin)
65 : message(in_message),
66 reason(in_reason),
67 url(in_url),
68 status(in_status),
69 is_cross_origin(in_is_cross_origin) {}
71 ErrorDetails::~ErrorDetails() {}
73 Namespace::Namespace()
74 : type(FALLBACK_NAMESPACE),
75 is_pattern(false),
76 is_executable(false) {
79 Namespace::Namespace(
80 NamespaceType type, const GURL& url, const GURL& target, bool is_pattern)
81 : type(type),
82 namespace_url(url),
83 target_url(target),
84 is_pattern(is_pattern),
85 is_executable(false) {
88 Namespace::Namespace(
89 NamespaceType type, const GURL& url, const GURL& target,
90 bool is_pattern, bool is_executable)
91 : type(type),
92 namespace_url(url),
93 target_url(target),
94 is_pattern(is_pattern),
95 is_executable(is_executable) {
98 Namespace::~Namespace() {
101 bool Namespace::IsMatch(const GURL& url) const {
102 if (is_pattern) {
103 // We have to escape '?' characters since MatchPattern also treats those
104 // as wildcards which we don't want here, we only do '*'s.
105 std::string pattern = namespace_url.spec();
106 if (namespace_url.has_query())
107 ReplaceSubstringsAfterOffset(&pattern, 0, "?", "\\?");
108 return MatchPattern(url.spec(), pattern);
110 return StartsWithASCII(url.spec(), namespace_url.spec(), true);
113 bool IsSchemeSupported(const GURL& url) {
114 bool supported = url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme) ||
115 url.SchemeIs(kDevToolsScheme);
117 #ifndef NDEBUG
118 // TODO(michaeln): It would be really nice if this could optionally work for
119 // file and filesystem urls too to help web developers experiment and test
120 // their apps, perhaps enabled via a cmd line flag or some other developer
121 // tool setting. Unfortunately file scheme net::URLRequests don't produce the
122 // same signalling (200 response codes, headers) as http URLRequests, so this
123 // doesn't work just yet.
124 // supported |= url.SchemeIsFile();
125 #endif
126 return supported;
129 bool IsMethodSupported(const std::string& method) {
130 return (method == kHttpGETMethod) || (method == kHttpHEADMethod);
133 bool IsSchemeAndMethodSupported(const net::URLRequest* request) {
134 return IsSchemeSupported(request->url()) &&
135 IsMethodSupported(request->method());
138 } // namespace appcache