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/appcache/appcache_interfaces.h"
9 #include "base/lazy_instance.h"
10 #include "base/string_util.h"
11 #include "googleurl/src/gurl.h"
12 #include "net/url_request/url_request.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebApplicationCacheHost.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h"
16 using WebKit::WebApplicationCacheHost
;
17 using WebKit::WebConsoleMessage
;
21 base::LazyInstance
<std::set
<std::string
> >::Leaky g_supported_schemes
=
22 LAZY_INSTANCE_INITIALIZER
;
28 const char kHttpScheme
[] = "http";
29 const char kHttpsScheme
[] = "https";
30 const char kHttpGETMethod
[] = "GET";
31 const char kHttpHEADMethod
[] = "HEAD";
33 const char kEnableExecutableHandlers
[] = "enable-appcache-executable-handlers";
35 const base::FilePath::CharType kAppCacheDatabaseName
[] =
36 FILE_PATH_LITERAL("Index");
38 AppCacheInfo::AppCacheInfo()
39 : cache_id(kNoCacheId
),
46 AppCacheInfo::~AppCacheInfo() {
49 AppCacheResourceInfo::AppCacheResourceInfo()
58 response_id(kNoResponseId
) {
61 AppCacheResourceInfo::~AppCacheResourceInfo() {
64 Namespace::Namespace()
65 : type(FALLBACK_NAMESPACE
),
67 is_executable(false) {
71 NamespaceType type
, const GURL
& url
, const GURL
& target
, bool is_pattern
)
75 is_pattern(is_pattern
),
76 is_executable(false) {
80 NamespaceType type
, const GURL
& url
, const GURL
& target
,
81 bool is_pattern
, bool is_executable
)
85 is_pattern(is_pattern
),
86 is_executable(is_executable
) {
89 Namespace::~Namespace() {
92 bool Namespace::IsMatch(const GURL
& url
) const {
94 // We have to escape '?' characters since MatchPattern also treats those
95 // as wildcards which we don't want here, we only do '*'s.
96 std::string pattern
= namespace_url
.spec();
97 if (namespace_url
.has_query())
98 ReplaceSubstringsAfterOffset(&pattern
, 0, "?", "\\?");
99 return MatchPattern(url
.spec(), pattern
);
101 return StartsWithASCII(url
.spec(), namespace_url
.spec(), true);
104 void AddSupportedScheme(const char* scheme
) {
105 g_supported_schemes
.Get().insert(scheme
);
108 bool IsSchemeSupported(const GURL
& url
) {
109 bool supported
= url
.SchemeIs(kHttpScheme
) || url
.SchemeIs(kHttpsScheme
) ||
110 (!(g_supported_schemes
== NULL
) &&
111 g_supported_schemes
.Get().find(url
.scheme()) !=
112 g_supported_schemes
.Get().end());
115 // TODO(michaeln): It would be really nice if this could optionally work for
116 // file and filesystem urls too to help web developers experiment and test
117 // their apps, perhaps enabled via a cmd line flag or some other developer
118 // tool setting. Unfortunately file scheme net::URLRequests don't produce the
119 // same signalling (200 response codes, headers) as http URLRequests, so this
120 // doesn't work just yet.
121 // supported |= url.SchemeIsFile();
126 bool IsMethodSupported(const std::string
& method
) {
127 return (method
== kHttpGETMethod
) || (method
== kHttpHEADMethod
);
130 bool IsSchemeAndMethodSupported(const net::URLRequest
* request
) {
131 return IsSchemeSupported(request
->url()) &&
132 IsMethodSupported(request
->method());
135 // Ensure that enum values never get out of sync with the
136 // ones declared for use within the WebKit api
137 COMPILE_ASSERT((int)WebApplicationCacheHost::Uncached
==
138 (int)UNCACHED
, Uncached
);
139 COMPILE_ASSERT((int)WebApplicationCacheHost::Idle
==
141 COMPILE_ASSERT((int)WebApplicationCacheHost::Checking
==
142 (int)CHECKING
, Checking
);
143 COMPILE_ASSERT((int)WebApplicationCacheHost::Downloading
==
144 (int)DOWNLOADING
, Downloading
);
145 COMPILE_ASSERT((int)WebApplicationCacheHost::UpdateReady
==
146 (int)UPDATE_READY
, UpdateReady
);
147 COMPILE_ASSERT((int)WebApplicationCacheHost::Obsolete
==
148 (int)OBSOLETE
, Obsolete
);
149 COMPILE_ASSERT((int)WebApplicationCacheHost::CheckingEvent
==
150 (int)CHECKING_EVENT
, CheckingEvent
);
151 COMPILE_ASSERT((int)WebApplicationCacheHost::ErrorEvent
==
152 (int)ERROR_EVENT
, ErrorEvent
);
153 COMPILE_ASSERT((int)WebApplicationCacheHost::NoUpdateEvent
==
154 (int)NO_UPDATE_EVENT
, NoUpdateEvent
);
155 COMPILE_ASSERT((int)WebApplicationCacheHost::DownloadingEvent
==
156 (int)DOWNLOADING_EVENT
, DownloadingEvent
);
157 COMPILE_ASSERT((int)WebApplicationCacheHost::ProgressEvent
==
158 (int)PROGRESS_EVENT
, ProgressEvent
);
159 COMPILE_ASSERT((int)WebApplicationCacheHost::UpdateReadyEvent
==
160 (int)UPDATE_READY_EVENT
, UpdateReadyEvent
);
161 COMPILE_ASSERT((int)WebApplicationCacheHost::CachedEvent
==
162 (int)CACHED_EVENT
, CachedEvent
);
163 COMPILE_ASSERT((int)WebApplicationCacheHost::ObsoleteEvent
==
164 (int)OBSOLETE_EVENT
, ObsoleteEvent
);
165 COMPILE_ASSERT((int)WebConsoleMessage::LevelDebug
==
166 (int)LOG_DEBUG
, LevelDebug
);
167 COMPILE_ASSERT((int)WebConsoleMessage::LevelLog
==
168 (int)LOG_INFO
, LevelLog
);
169 COMPILE_ASSERT((int)WebConsoleMessage::LevelWarning
==
170 (int)LOG_WARNING
, LevelWarning
);
171 COMPILE_ASSERT((int)WebConsoleMessage::LevelError
==
172 (int)LOG_ERROR
, LevelError
);
174 } // namespace appcache