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 "content/common/appcache_interfaces.h"
9 #include "base/strings/pattern.h"
10 #include "base/strings/string_util.h"
11 #include "content/public/common/url_constants.h"
12 #include "net/url_request/url_request.h"
14 #include "url/url_constants.h"
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(kAppCacheNoCacheId
),
29 status(APPCACHE_STATUS_UNCACHED
),
34 AppCacheInfo::~AppCacheInfo() {
37 AppCacheResourceInfo::AppCacheResourceInfo()
46 response_id(kAppCacheNoResponseId
) {
49 AppCacheResourceInfo::~AppCacheResourceInfo() {
52 AppCacheErrorDetails::AppCacheErrorDetails()
54 reason(APPCACHE_UNKNOWN_ERROR
),
57 is_cross_origin(false) {}
59 AppCacheErrorDetails::AppCacheErrorDetails(
60 std::string in_message
,
61 AppCacheErrorReason in_reason
,
64 bool in_is_cross_origin
)
65 : message(in_message
),
69 is_cross_origin(in_is_cross_origin
) {}
71 AppCacheErrorDetails::~AppCacheErrorDetails() {}
73 AppCacheNamespace::AppCacheNamespace()
74 : type(APPCACHE_FALLBACK_NAMESPACE
),
76 is_executable(false) {
79 AppCacheNamespace::AppCacheNamespace(
80 AppCacheNamespaceType type
, const GURL
& url
, const GURL
& target
,
85 is_pattern(is_pattern
),
86 is_executable(false) {
89 AppCacheNamespace::AppCacheNamespace(
90 AppCacheNamespaceType type
, const GURL
& url
, const GURL
& target
,
91 bool is_pattern
, bool is_executable
)
95 is_pattern(is_pattern
),
96 is_executable(is_executable
) {
99 AppCacheNamespace::~AppCacheNamespace() {
102 bool AppCacheNamespace::IsMatch(const GURL
& url
) const {
104 // We have to escape '?' characters since MatchPattern also treats those
105 // as wildcards which we don't want here, we only do '*'s.
106 std::string pattern
= namespace_url
.spec();
107 if (namespace_url
.has_query())
108 base::ReplaceSubstringsAfterOffset(&pattern
, 0, "?", "\\?");
109 return base::MatchPattern(url
.spec(), pattern
);
111 return base::StartsWith(url
.spec(), namespace_url
.spec(),
112 base::CompareCase::SENSITIVE
);
115 bool IsSchemeSupportedForAppCache(const GURL
& url
) {
116 bool supported
= url
.SchemeIs(url::kHttpScheme
) ||
117 url
.SchemeIs(url::kHttpsScheme
) ||
118 url
.SchemeIs(kChromeDevToolsScheme
);
121 // TODO(michaeln): It would be really nice if this could optionally work for
122 // file and filesystem urls too to help web developers experiment and test
123 // their apps, perhaps enabled via a cmd line flag or some other developer
124 // tool setting. Unfortunately file scheme net::URLRequests don't produce the
125 // same signalling (200 response codes, headers) as http URLRequests, so this
126 // doesn't work just yet.
127 // supported |= url.SchemeIsFile();
132 bool IsMethodSupportedForAppCache(const std::string
& method
) {
133 return (method
== kHttpGETMethod
) || (method
== kHttpHEADMethod
);
136 bool IsSchemeAndMethodSupportedForAppCache(const net::URLRequest
* request
) {
137 return IsSchemeSupportedForAppCache(request
->url()) &&
138 IsMethodSupportedForAppCache(request
->method());
141 } // namespace content