Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / webkit / common / appcache / appcache_interfaces.cc
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
26 AppCacheInfo::AppCacheInfo()
27     : cache_id(kNoCacheId),
28       group_id(0),
29       status(UNCACHED),
30       size(0),
31       is_complete(false) {
32 }
33
34 AppCacheInfo::~AppCacheInfo() {
35 }
36
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
49 AppCacheResourceInfo::~AppCacheResourceInfo() {
50 }
51
52 ErrorDetails::ErrorDetails()
53     : message(),
54       reason(UNKNOWN_ERROR),
55       url(),
56       status(0),
57       is_cross_origin(false) {}
58
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) {}
70
71 ErrorDetails::~ErrorDetails() {}
72
73 Namespace::Namespace()
74     : type(FALLBACK_NAMESPACE),
75       is_pattern(false),
76       is_executable(false) {
77 }
78
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) {
86 }
87
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) {
96 }
97
98 Namespace::~Namespace() {
99 }
100
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);
109   }
110   return StartsWithASCII(url.spec(), namespace_url.spec(), true);
111 }
112
113 bool IsSchemeSupported(const GURL& url) {
114   bool supported = url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme) ||
115       url.SchemeIs(kDevToolsScheme);
116
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;
127 }
128
129 bool IsMethodSupported(const std::string& method) {
130   return (method == kHttpGETMethod) || (method == kHttpHEADMethod);
131 }
132
133 bool IsSchemeAndMethodSupported(const net::URLRequest* request) {
134   return IsSchemeSupported(request->url()) &&
135          IsMethodSupported(request->method());
136 }
137
138 }  // namespace appcache