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/common/appcache/appcache_interfaces.h"
6
7 #include <set>
8
9 #include "base/strings/string_util.h"
10 #include "net/url_request/url_request.h"
11 #include "url/gurl.h"
12
13 namespace appcache {
14
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";
20
21 const char kEnableExecutableHandlers[] = "enable-appcache-executable-handlers";
22
23 const base::FilePath::CharType kAppCacheDatabaseName[] =
24 FILE_PATH_LITERAL("Index");
25
AppCacheInfo()26 AppCacheInfo::AppCacheInfo()
27 : cache_id(kNoCacheId),
28 group_id(0),
29 status(UNCACHED),
30 size(0),
31 is_complete(false) {
32 }
33
~AppCacheInfo()34 AppCacheInfo::~AppCacheInfo() {
35 }
36
AppCacheResourceInfo()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) {
47 }
48
~AppCacheResourceInfo()49 AppCacheResourceInfo::~AppCacheResourceInfo() {
50 }
51
Namespace()52 Namespace::Namespace()
53 : type(FALLBACK_NAMESPACE),
54 is_pattern(false),
55 is_executable(false) {
56 }
57
Namespace(NamespaceType type,const GURL & url,const GURL & target,bool is_pattern)58 Namespace::Namespace(
59 NamespaceType type, const GURL& url, const GURL& target, bool is_pattern)
60 : type(type),
61 namespace_url(url),
62 target_url(target),
63 is_pattern(is_pattern),
64 is_executable(false) {
65 }
66
Namespace(NamespaceType type,const GURL & url,const GURL & target,bool is_pattern,bool is_executable)67 Namespace::Namespace(
68 NamespaceType type, const GURL& url, const GURL& target,
69 bool is_pattern, bool is_executable)
70 : type(type),
71 namespace_url(url),
72 target_url(target),
73 is_pattern(is_pattern),
74 is_executable(is_executable) {
75 }
76
~Namespace()77 Namespace::~Namespace() {
78 }
79
IsMatch(const GURL & url) const80 bool Namespace::IsMatch(const GURL& url) const {
81 if (is_pattern) {
82 // We have to escape '?' characters since MatchPattern also treats those
83 // as wildcards which we don't want here, we only do '*'s.
84 std::string pattern = namespace_url.spec();
85 if (namespace_url.has_query())
86 ReplaceSubstringsAfterOffset(&pattern, 0, "?", "\\?");
87 return MatchPattern(url.spec(), pattern);
88 }
89 return StartsWithASCII(url.spec(), namespace_url.spec(), true);
90 }
91
IsSchemeSupported(const GURL & url)92 bool IsSchemeSupported(const GURL& url) {
93 bool supported = url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme) ||
94 url.SchemeIs(kDevToolsScheme);
95
96 #ifndef NDEBUG
97 // TODO(michaeln): It would be really nice if this could optionally work for
98 // file and filesystem urls too to help web developers experiment and test
99 // their apps, perhaps enabled via a cmd line flag or some other developer
100 // tool setting. Unfortunately file scheme net::URLRequests don't produce the
101 // same signalling (200 response codes, headers) as http URLRequests, so this
102 // doesn't work just yet.
103 // supported |= url.SchemeIsFile();
104 #endif
105 return supported;
106 }
107
IsMethodSupported(const std::string & method)108 bool IsMethodSupported(const std::string& method) {
109 return (method == kHttpGETMethod) || (method == kHttpHEADMethod);
110 }
111
IsSchemeAndMethodSupported(const net::URLRequest * request)112 bool IsSchemeAndMethodSupported(const net::URLRequest* request) {
113 return IsSchemeSupported(request->url()) &&
114 IsMethodSupported(request->method());
115 }
116
117 } // namespace appcache
118