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"
9 #include "base/lazy_instance.h"
10 #include "base/strings/string_util.h"
11 #include "net/url_request/url_request.h"
16 base::LazyInstance
<std::set
<std::string
> >::Leaky g_supported_schemes
=
17 LAZY_INSTANCE_INITIALIZER
;
23 const char kHttpScheme
[] = "http";
24 const char kHttpsScheme
[] = "https";
25 const char kHttpGETMethod
[] = "GET";
26 const char kHttpHEADMethod
[] = "HEAD";
28 const char kEnableExecutableHandlers
[] = "enable-appcache-executable-handlers";
30 const base::FilePath::CharType kAppCacheDatabaseName
[] =
31 FILE_PATH_LITERAL("Index");
33 AppCacheInfo::AppCacheInfo()
34 : cache_id(kNoCacheId
),
41 AppCacheInfo::~AppCacheInfo() {
44 AppCacheResourceInfo::AppCacheResourceInfo()
53 response_id(kNoResponseId
) {
56 AppCacheResourceInfo::~AppCacheResourceInfo() {
59 Namespace::Namespace()
60 : type(FALLBACK_NAMESPACE
),
62 is_executable(false) {
66 NamespaceType type
, const GURL
& url
, const GURL
& target
, bool is_pattern
)
70 is_pattern(is_pattern
),
71 is_executable(false) {
75 NamespaceType type
, const GURL
& url
, const GURL
& target
,
76 bool is_pattern
, bool is_executable
)
80 is_pattern(is_pattern
),
81 is_executable(is_executable
) {
84 Namespace::~Namespace() {
87 bool Namespace::IsMatch(const GURL
& url
) const {
89 // We have to escape '?' characters since MatchPattern also treats those
90 // as wildcards which we don't want here, we only do '*'s.
91 std::string pattern
= namespace_url
.spec();
92 if (namespace_url
.has_query())
93 ReplaceSubstringsAfterOffset(&pattern
, 0, "?", "\\?");
94 return MatchPattern(url
.spec(), pattern
);
96 return StartsWithASCII(url
.spec(), namespace_url
.spec(), true);
99 void AddSupportedScheme(const char* scheme
) {
100 g_supported_schemes
.Get().insert(scheme
);
103 bool IsSchemeSupported(const GURL
& url
) {
104 bool supported
= url
.SchemeIs(kHttpScheme
) || url
.SchemeIs(kHttpsScheme
) ||
105 (!(g_supported_schemes
== NULL
) &&
106 g_supported_schemes
.Get().find(url
.scheme()) !=
107 g_supported_schemes
.Get().end());
110 // TODO(michaeln): It would be really nice if this could optionally work for
111 // file and filesystem urls too to help web developers experiment and test
112 // their apps, perhaps enabled via a cmd line flag or some other developer
113 // tool setting. Unfortunately file scheme net::URLRequests don't produce the
114 // same signalling (200 response codes, headers) as http URLRequests, so this
115 // doesn't work just yet.
116 // supported |= url.SchemeIsFile();
121 bool IsMethodSupported(const std::string
& method
) {
122 return (method
== kHttpGETMethod
) || (method
== kHttpHEADMethod
);
125 bool IsSchemeAndMethodSupported(const net::URLRequest
* request
) {
126 return IsSchemeSupported(request
->url()) &&
127 IsMethodSupported(request
->method());
130 } // namespace appcache